123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- <?php
- namespace app\service;
- use app\service\ConfServiceFacade;
- use app\service\api\UserServiceFacade;
- use app\service\BillServiceFacade;
- use laytp\traits\Error;
- use think\facade\Cache;
- use think\facade\Config;
- use think\facade\Request;
- use EasyWeChat\Factory;
- use think\facade\Db;
- class Member{
- use Error;
- /**
- * 消费
- * @param $token
- * @return bool
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function check($loginUserInfo,$type,$coin,$conf)
- {
- global $_GPC;
- $modelVipinfo =new \app\model\Vipinfo();
- $vipinfo = $modelVipinfo->where('uid',$loginUserInfo['id'])->find();
- // print_r($vipinfo);
- if(!empty($loginUserInfo) && !empty($vipinfo)) {
- $whereToday = [['uid','=',$loginUserInfo['id']],['type','=',$type],['create_time','>',date('Y-m-d 0:0:0')],['uniacid','=',$_GPC['uniacid'],['is_time','=',1]] ];
- $whereTotal = [['uid','=',$loginUserInfo['id']],['type','=',$type],['create_time','>',$vipinfo['update_time']],['uniacid','=',$_GPC['uniacid'],['is_time','=',1]] ];
- }
- $numberAlias = !empty($conf['number_alias'])?$conf['number_alias']:'点数';
- if(!empty($conf['time_member']) && $conf['time_member'] ==1 && !empty($loginUserInfo['vip_time']) && ($loginUserInfo['vip_time'] >= time() || $loginUserInfo['vip_time'] ==9999) && !empty($vipinfo)){
- if($vipinfo[$type.'_times'] == 0){
- if($loginUserInfo['coin'] <$coin){
- return ['status'=>false,'msg'=>'会员套餐内使用量已用完,且'.$numberAlias.'不足'];
- }
- }else{
- // 开了会员,并此用户是有效会员
- // 会员内有数量限制
- if($vipinfo[$type.'_times'] > 0 ){
- if($vipinfo[$type.'_type'] == 'time'){
- $used = \app\model\Bill::where($whereToday)->count();
- }else{
- $used = \app\model\Bill::where($whereTotal)->count();
- }
- // print_r($used);
- if($used > $vipinfo[$type.'_times']){
- if($loginUserInfo['coin'] <$coin){
- return ['status'=>false,'msg'=>'套餐内使用次数不足'];
- }
- }
- }
- }
- }else{
- if($loginUserInfo['coin'] <$coin){
- return ['status'=>false,'msg'=>'套餐内使用次数不足'];
- }
- }
- }
- /**
- * 消费
- * @param $token
- * @return bool
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function cash($loginUserInfo,$type,$coin,$conf){
- global $_GPC;
- $modelVipinfo =new \app\model\Vipinfo();
- $vipinfo = $modelVipinfo->where('uid',$loginUserInfo['id'])->find();
- $numberAlias = !empty($conf['number_alias'])?$conf['number_alias']:'点数';
- if(!empty($loginUserInfo) && !empty($vipinfo)){
- $whereToday = [['uid','=',$loginUserInfo['id']],['type','=',$type],['create_time','>',date('Y-m-d 0:0:0')],['uniacid','=',$_GPC['uniacid']] ];
- $whereTotal = [['uid','=',$loginUserInfo['id']],['type','=',$type],['create_time','>',$vipinfo['update_time']],['uniacid','=',$_GPC['uniacid']] ];
- }
- $title='';
- switch($type){
- case 'gpt35':
- $title = !empty($conf['gpt35_alias']?$conf['gpt35_alias']:'GPT-3.5');
- break;
- case 'gpt4':
- $title = !empty($conf['gpt4_alias']?$conf['gpt4_alias']:'GPT-4');
- break;
- case 'sd':
- $title = !empty($conf['aipainting_alias']?$conf['aipainting_alias']:'StableDiffusion绘画');
- break;
- case 'mj':
- $title = !empty($conf['aipainting_alias']?$conf['aipainting_alias']:'Midjourney绘画');
- break;
- }
- if(!empty($conf['time_member']) && $conf['time_member'] ==1 && !empty($loginUserInfo['vip_time']) && ($loginUserInfo['vip_time'] >= time() || $loginUserInfo['vip_time'] ==9999) && !empty($vipinfo) ){
- if($vipinfo[$type.'_times'] == 0){
- if($loginUserInfo['coin'] <$coin){
- return ['status'=>false,'msg'=>'会员套餐内使用量已用完,且'.$numberAlias.'不足'];
- }else{
- \app\model\User::where(['id'=>$loginUserInfo['id']])->dec('coin',$coin)->update();
- BillServiceFacade::record(0,$coin,$type,$title.'消耗'.$numberAlias,$loginUserInfo['id'],$_GPC['uniacid']);
- }
- }else{
- if($vipinfo[$type.'_times']>0){
- if($vipinfo[$type.'_type'] == 'time'){
- $used = \app\model\Bill::where($whereToday)->count();
- }else{
- $used = \app\model\Bill::where($whereTotal)->count();
- }
- if($used > $vipinfo[$type.'_times']){
- if($loginUserInfo['coin'] <$coin){
- return ['status'=>false,'msg'=>$numberAlias.'不足1'];
- }else{
- \app\model\User::where(['id'=>$loginUserInfo['id']])->dec('coin',$coin)->update();
- BillServiceFacade::record(0,$coin,$type,$title.'消耗'.$numberAlias,$loginUserInfo['id'],$_GPC['uniacid']);
- }
- }else{
- $res = BillServiceFacade::record(0,1,$type,'消耗会员套餐内'.$type.'一次',$loginUserInfo['id'],$_GPC['uniacid'],true,$loginUserInfo['coin'],1);
- // if(isset($member['status']) && !$member['status']){
- // return return ['status'=>false,'msg'=>$member['msg']];
- // }
- }
- }
- }
- }else{
- if($loginUserInfo['coin'] <$coin){
- return ['status'=>false,'msg'=>$numberAlias.'不足'];
- }else{
- \app\model\User::where(['id'=>$loginUserInfo['id']])->dec('coin',$coin)->update();
- BillServiceFacade::record(0,$coin,$type,$title.'消耗'.$numberAlias,$loginUserInfo['id'],$_GPC['uniacid']);
- }
- }
- return ['status'=>true,'msg'=>''];
- }
- /**
- * 积分
- * @param $token
- * @return bool
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function coin(){
- \app\model\User::where(['id'=>$loginUserInfo['id']])->dec('coin',$coin)->update();
- BillServiceFacade::record(0,$coin,$type,$title.'消耗'.$numberAlias,$loginUserInfo['id'],$_GPC['uniacid']);
- }
- /**
- * 会员
- * @param $token
- * @return bool
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function vip($loginUserInfo,$type,$coin,$conf)
- {
- global $_GPC;
- $modelVipinfo =new \app\model\Vipinfo();
- $vipinfo = $modelVipinfo->where('uid',$loginUserInfo['id'])->find();
- $where = [['uid','=',$loginUserInfo['id']],['type','=',$type],['is_time','=','1'],['create_time','>',date('Y-m-d 0:0:0')],['uniacid','=',$_GPC['uniacid'],['is_time','=',1]] ];
- $numberAlias = !empty($conf['number_alias'])?$conf['number_alias']:'点数';
- if(!empty($vipinfo)){
- if($vipinfo[$type.'_type'] == 'time'){
- $used = \app\model\Bill::where([['uid','=',$loginUserInfo['id']],['type','=',$type],['is_time','=','1'],['create_time','>',date('Y-m-d 0:0:0')],['uniacid','=',$_GPC['uniacid'],['is_time','=',1]] ])->count();
- if($used < $vipinfo[$type.'_times']){
- $res = BillServiceFacade::record(0,$coin,$type,'问答消耗会员套餐内'.$type,$loginUserInfo['id'],$_GPC['uniacid'],true,$loginUserInfo['coin'],1);
- }else{
- if($loginUserInfo['coin'] <$coin){
- return ['status'=>false,'msg'=>$numberAlias.'不足'];
- }else{
- \app\model\User::where(['id'=>$loginUserInfo['id']])->dec('coin',$coin)->update();
- $res = BillServiceFacade::record(0,$coin,$type,'问答消耗'.$numberAlias,$loginUserInfo['id'],$_GPC['uniacid']);
- }
- }
- }else{
- $used = \app\model\Bill::where([['uid','=',$loginUserInfo['id']],['type','=',$type],['is_time','=','1'],['create_time','>',$vipinfo['update_time'] ],['uniacid','=',$_GPC['uniacid']] ])->count();
- if($used < $vipinfo[$type.'_times']){
- \app\model\User::where(['id'=>$loginUserInfo['id']])->dec('coin',$coin)->update();
- $res = BillServiceFacade::record(0,$coin,$type,'问答消耗'.$numberAlias,$loginUserInfo['id'],$_GPC['uniacid']);
- }else{
- return ['status'=>false,'msg'=>'会员套餐内'.$type.'剩余次数不足'];
- }
- }
- }
- }
- }
|