Member.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace app\service;
  3. use app\service\ConfServiceFacade;
  4. use app\service\api\UserServiceFacade;
  5. use app\service\BillServiceFacade;
  6. use laytp\traits\Error;
  7. use think\facade\Cache;
  8. use think\facade\Config;
  9. use think\facade\Request;
  10. use EasyWeChat\Factory;
  11. use think\facade\Db;
  12. class Member{
  13. use Error;
  14. /**
  15. * 消费
  16. * @param $token
  17. * @return bool
  18. * @throws \think\db\exception\DataNotFoundException
  19. * @throws \think\db\exception\DbException
  20. * @throws \think\db\exception\ModelNotFoundException
  21. */
  22. public function check($loginUserInfo,$type,$coin,$conf)
  23. {
  24. global $_GPC;
  25. $modelVipinfo =new \app\model\Vipinfo();
  26. $vipinfo = $modelVipinfo->where('uid',$loginUserInfo['id'])->find();
  27. // print_r($vipinfo);
  28. if(!empty($loginUserInfo) && !empty($vipinfo)) {
  29. $whereToday = [['uid','=',$loginUserInfo['id']],['type','=',$type],['create_time','>',date('Y-m-d 0:0:0')],['uniacid','=',$_GPC['uniacid'],['is_time','=',1]] ];
  30. $whereTotal = [['uid','=',$loginUserInfo['id']],['type','=',$type],['create_time','>',$vipinfo['update_time']],['uniacid','=',$_GPC['uniacid'],['is_time','=',1]] ];
  31. }
  32. $numberAlias = !empty($conf['number_alias'])?$conf['number_alias']:'点数';
  33. if(!empty($conf['time_member']) && $conf['time_member'] ==1 && !empty($loginUserInfo['vip_time']) && ($loginUserInfo['vip_time'] >= time() || $loginUserInfo['vip_time'] ==9999) && !empty($vipinfo)){
  34. if($vipinfo[$type.'_times'] == 0){
  35. if($loginUserInfo['coin'] <$coin){
  36. return ['status'=>false,'msg'=>'会员套餐内使用量已用完,且'.$numberAlias.'不足'];
  37. }
  38. }else{
  39. // 开了会员,并此用户是有效会员
  40. // 会员内有数量限制
  41. if($vipinfo[$type.'_times'] > 0 ){
  42. if($vipinfo[$type.'_type'] == 'time'){
  43. $used = \app\model\Bill::where($whereToday)->count();
  44. }else{
  45. $used = \app\model\Bill::where($whereTotal)->count();
  46. }
  47. // print_r($used);
  48. if($used > $vipinfo[$type.'_times']){
  49. if($loginUserInfo['coin'] <$coin){
  50. return ['status'=>false,'msg'=>'套餐内使用次数不足'];
  51. }
  52. }
  53. }
  54. }
  55. }else{
  56. if($loginUserInfo['coin'] <$coin){
  57. return ['status'=>false,'msg'=>'套餐内使用次数不足'];
  58. }
  59. }
  60. }
  61. /**
  62. * 消费
  63. * @param $token
  64. * @return bool
  65. * @throws \think\db\exception\DataNotFoundException
  66. * @throws \think\db\exception\DbException
  67. * @throws \think\db\exception\ModelNotFoundException
  68. */
  69. public function cash($loginUserInfo,$type,$coin,$conf){
  70. global $_GPC;
  71. $modelVipinfo =new \app\model\Vipinfo();
  72. $vipinfo = $modelVipinfo->where('uid',$loginUserInfo['id'])->find();
  73. $numberAlias = !empty($conf['number_alias'])?$conf['number_alias']:'点数';
  74. if(!empty($loginUserInfo) && !empty($vipinfo)){
  75. $whereToday = [['uid','=',$loginUserInfo['id']],['type','=',$type],['create_time','>',date('Y-m-d 0:0:0')],['uniacid','=',$_GPC['uniacid']] ];
  76. $whereTotal = [['uid','=',$loginUserInfo['id']],['type','=',$type],['create_time','>',$vipinfo['update_time']],['uniacid','=',$_GPC['uniacid']] ];
  77. }
  78. $title='';
  79. switch($type){
  80. case 'gpt35':
  81. $title = !empty($conf['gpt35_alias']?$conf['gpt35_alias']:'GPT-3.5');
  82. break;
  83. case 'gpt4':
  84. $title = !empty($conf['gpt4_alias']?$conf['gpt4_alias']:'GPT-4');
  85. break;
  86. case 'sd':
  87. $title = !empty($conf['aipainting_alias']?$conf['aipainting_alias']:'StableDiffusion绘画');
  88. break;
  89. case 'mj':
  90. $title = !empty($conf['aipainting_alias']?$conf['aipainting_alias']:'Midjourney绘画');
  91. break;
  92. }
  93. if(!empty($conf['time_member']) && $conf['time_member'] ==1 && !empty($loginUserInfo['vip_time']) && ($loginUserInfo['vip_time'] >= time() || $loginUserInfo['vip_time'] ==9999) && !empty($vipinfo) ){
  94. if($vipinfo[$type.'_times'] == 0){
  95. if($loginUserInfo['coin'] <$coin){
  96. return ['status'=>false,'msg'=>'会员套餐内使用量已用完,且'.$numberAlias.'不足'];
  97. }else{
  98. \app\model\User::where(['id'=>$loginUserInfo['id']])->dec('coin',$coin)->update();
  99. BillServiceFacade::record(0,$coin,$type,$title.'消耗'.$numberAlias,$loginUserInfo['id'],$_GPC['uniacid']);
  100. }
  101. }else{
  102. if($vipinfo[$type.'_times']>0){
  103. if($vipinfo[$type.'_type'] == 'time'){
  104. $used = \app\model\Bill::where($whereToday)->count();
  105. }else{
  106. $used = \app\model\Bill::where($whereTotal)->count();
  107. }
  108. if($used > $vipinfo[$type.'_times']){
  109. if($loginUserInfo['coin'] <$coin){
  110. return ['status'=>false,'msg'=>$numberAlias.'不足1'];
  111. }else{
  112. \app\model\User::where(['id'=>$loginUserInfo['id']])->dec('coin',$coin)->update();
  113. BillServiceFacade::record(0,$coin,$type,$title.'消耗'.$numberAlias,$loginUserInfo['id'],$_GPC['uniacid']);
  114. }
  115. }else{
  116. $res = BillServiceFacade::record(0,1,$type,'消耗会员套餐内'.$type.'一次',$loginUserInfo['id'],$_GPC['uniacid'],true,$loginUserInfo['coin'],1);
  117. // if(isset($member['status']) && !$member['status']){
  118. // return return ['status'=>false,'msg'=>$member['msg']];
  119. // }
  120. }
  121. }
  122. }
  123. }else{
  124. if($loginUserInfo['coin'] <$coin){
  125. return ['status'=>false,'msg'=>$numberAlias.'不足'];
  126. }else{
  127. \app\model\User::where(['id'=>$loginUserInfo['id']])->dec('coin',$coin)->update();
  128. BillServiceFacade::record(0,$coin,$type,$title.'消耗'.$numberAlias,$loginUserInfo['id'],$_GPC['uniacid']);
  129. }
  130. }
  131. return ['status'=>true,'msg'=>''];
  132. }
  133. /**
  134. * 积分
  135. * @param $token
  136. * @return bool
  137. * @throws \think\db\exception\DataNotFoundException
  138. * @throws \think\db\exception\DbException
  139. * @throws \think\db\exception\ModelNotFoundException
  140. */
  141. public function coin(){
  142. \app\model\User::where(['id'=>$loginUserInfo['id']])->dec('coin',$coin)->update();
  143. BillServiceFacade::record(0,$coin,$type,$title.'消耗'.$numberAlias,$loginUserInfo['id'],$_GPC['uniacid']);
  144. }
  145. /**
  146. * 会员
  147. * @param $token
  148. * @return bool
  149. * @throws \think\db\exception\DataNotFoundException
  150. * @throws \think\db\exception\DbException
  151. * @throws \think\db\exception\ModelNotFoundException
  152. */
  153. public function vip($loginUserInfo,$type,$coin,$conf)
  154. {
  155. global $_GPC;
  156. $modelVipinfo =new \app\model\Vipinfo();
  157. $vipinfo = $modelVipinfo->where('uid',$loginUserInfo['id'])->find();
  158. $where = [['uid','=',$loginUserInfo['id']],['type','=',$type],['is_time','=','1'],['create_time','>',date('Y-m-d 0:0:0')],['uniacid','=',$_GPC['uniacid'],['is_time','=',1]] ];
  159. $numberAlias = !empty($conf['number_alias'])?$conf['number_alias']:'点数';
  160. if(!empty($vipinfo)){
  161. if($vipinfo[$type.'_type'] == 'time'){
  162. $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();
  163. if($used < $vipinfo[$type.'_times']){
  164. $res = BillServiceFacade::record(0,$coin,$type,'问答消耗会员套餐内'.$type,$loginUserInfo['id'],$_GPC['uniacid'],true,$loginUserInfo['coin'],1);
  165. }else{
  166. if($loginUserInfo['coin'] <$coin){
  167. return ['status'=>false,'msg'=>$numberAlias.'不足'];
  168. }else{
  169. \app\model\User::where(['id'=>$loginUserInfo['id']])->dec('coin',$coin)->update();
  170. $res = BillServiceFacade::record(0,$coin,$type,'问答消耗'.$numberAlias,$loginUserInfo['id'],$_GPC['uniacid']);
  171. }
  172. }
  173. }else{
  174. $used = \app\model\Bill::where([['uid','=',$loginUserInfo['id']],['type','=',$type],['is_time','=','1'],['create_time','>',$vipinfo['update_time'] ],['uniacid','=',$_GPC['uniacid']] ])->count();
  175. if($used < $vipinfo[$type.'_times']){
  176. \app\model\User::where(['id'=>$loginUserInfo['id']])->dec('coin',$coin)->update();
  177. $res = BillServiceFacade::record(0,$coin,$type,'问答消耗'.$numberAlias,$loginUserInfo['id'],$_GPC['uniacid']);
  178. }else{
  179. return ['status'=>false,'msg'=>'会员套餐内'.$type.'剩余次数不足'];
  180. }
  181. }
  182. }
  183. }
  184. }