Commission.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace app\controller\api;
  3. use app\service\api\UserServiceFacade;
  4. use app\service\MoneyServiceFacade;
  5. use app\service\ConfServiceFacade;
  6. use laytp\controller\Api;
  7. use laytp\library\Random;
  8. use laytp\traits\Error;
  9. use think\facade\Db;
  10. /**
  11. * 会员相关
  12. * @ApiWeigh (90)
  13. */
  14. class Commission extends Api
  15. {
  16. use Error;
  17. protected function _initialize()
  18. {
  19. $this->model = new \app\model\commission\Team();
  20. }
  21. // 检测
  22. public function auth()
  23. {
  24. $loginUserInfo = UserServiceFacade::getUserInfo();
  25. $modelUser = new \app\model\commission\User();
  26. $res = $modelUser::where('uid','=',$loginUserInfo['id'])->find();
  27. if(!$res){
  28. return $this->error('不是分销商',2);
  29. }
  30. $res['money_total'] = \app\model\commission\Income::where(['uid'=>$loginUserInfo['id']])->sum('money');
  31. $res['on_cashout_money'] =\app\model\commission\Cashout::where(['uid'=>$loginUserInfo['id'],'status'=>0])->sum('extract_price');
  32. return $this->success('数据获取成功', $res);
  33. }
  34. // 申请分销商
  35. public function apply()
  36. {
  37. global $_GPC;
  38. $modelUser = new \app\model\commission\User();
  39. $post = $this->request->post();
  40. $realName = $this->request->post('real_name','');
  41. $telnum = $this->request->post('telnum','');
  42. if(!$realName || !$telnum){
  43. return $this->error('请上传姓名或手机号');
  44. }
  45. $loginUserInfo = UserServiceFacade::getUserInfo();
  46. $conf = ConfServiceFacade::groupGet('system.commission', 0);
  47. $res = $modelUser::where('uid','=',$loginUserInfo['id'])->find();
  48. $status =0;
  49. if(!empty($conf['is_examine']) && $conf['is_examine'] == 2){
  50. $status = 1;
  51. }
  52. $post['uid'] = $loginUserInfo['id'];
  53. $post['uniacid'] = $_GPC['uniacid'];
  54. $post['status'] = $status;
  55. $post['paid'] = 0;
  56. if(!$res){
  57. $a = $modelUser->save($post);
  58. return $this->success('数据获取成功', $a);
  59. }
  60. return $this->success('数据获取成功', $res);
  61. }
  62. // 完善提现信息
  63. public function update()
  64. {
  65. global $_GPC;
  66. $modelUser = new \app\model\commission\User();
  67. $post = $this->request->post();
  68. if(!($post['qrcode_wechat'])){
  69. return $this->error('修改失败,请上传二维码');
  70. }
  71. $loginUserInfo = UserServiceFacade::getUserInfo();
  72. $updateRes = $modelUser->where('uid', '=', $loginUserInfo['id'])->update($post);
  73. if (!$updateRes) return $this->error('数据更新失败');
  74. return $this->success('获取结果', $post);
  75. }
  76. public function teamList()
  77. {
  78. global $_GPC;
  79. $loginUserInfo = UserServiceFacade::getUserInfo();
  80. $where = ['pid'=>$loginUserInfo['id'],'uniacid'=>$_GPC['uniacid']];
  81. $order = ['create_time' => 'desc'];
  82. $param = $this->request->param();
  83. $limit = $this->request->param('limit', 10);
  84. $level = $this->request->param('level', 1);
  85. if($level == 1){
  86. $data = $this->model->where($where)->with(['userinfo'])->order($order)->paginate($limit)->toArray();
  87. }elseif($level == 2){
  88. $data = $this->model->where(['gid'=>$loginUserInfo['id'],'uniacid'=>$_GPC['uniacid']])->with(['userinfo'])->paginate($limit)->toArray();
  89. }elseif($level == 3){
  90. $data = $this->model->where(['gfid'=>$loginUserInfo['id'],'uniacid'=>$_GPC['uniacid']])->with(['userinfo'])->paginate($limit)->toArray();
  91. }
  92. if(!$data){
  93. return $this->error('数据获取失败');
  94. }
  95. return $this->success('数据获取成功', $data);
  96. }
  97. // 加入团队
  98. public function addTeam()
  99. {
  100. global $_GPC;
  101. $loginUserInfo = UserServiceFacade::getUserInfo();
  102. $spm = $this->request->post('spm',0);
  103. if(!$spm){
  104. return $this->error('数据获取失败,请上传spm',2);
  105. }
  106. $arr = explode('.',$spm);
  107. $shareuid = $arr[0];
  108. if($shareuid == 0){
  109. return $this->error('分销商用户id不能为0',2);
  110. }
  111. if($shareuid == $loginUserInfo['id']){
  112. return $this->error('不能邀请自己',2);
  113. }
  114. $sharecheck = \app\model\commission\User::where(['uid'=>$shareuid])->find();
  115. if(empty($sharecheck) && $sharecheck['status']!=1){
  116. return $this->error('分享者不是分销商',2);
  117. }
  118. $sharecheckUserInfo = \app\model\User::where(['id'=>$shareuid])->find();
  119. if($loginUserInfo['create_time_int'] < $sharecheckUserInfo['create_time_int']){
  120. return $this->error('被绑定用户注册时间大于分销商用户注册时间',2);
  121. }
  122. $check = $this->model->where(['uid'=>$loginUserInfo['id']])->find();
  123. if($check){
  124. return $this->error('已经绑定,无需重复绑定',2);
  125. }
  126. // 说明没被绑定过
  127. $gid = 0;
  128. $gfid = 0;
  129. // 查shareuid的信息
  130. $pinfo = $this->model->where(['uid'=>$shareuid])->find();
  131. if($pinfo){
  132. if($loginUserInfo['id'] == $pinfo['pid']){
  133. return $this->error('绑定关系错误,不支持逆向绑定',2);
  134. }
  135. $gid = $pinfo['pid'];
  136. if(!empty($pinfo['gid'])){
  137. if($loginUserInfo['id'] == $pinfo['gid']){
  138. return $this->error('绑定关系错误,不支持逆向绑定',2);
  139. }
  140. $gfid = $pinfo['gid'];
  141. }
  142. }
  143. $a = $this->model->save(['uid'=>$loginUserInfo['id'],'pid'=>$shareuid,'gid'=>$gid,'gfid'=>$gfid,'uniacid'=>$_GPC['uniacid'] ]);
  144. if(!$a){
  145. return $this->error('数据获取失败');
  146. }
  147. return $this->success('数据获取成功', $a);
  148. }
  149. // 申请提现
  150. public function extract()
  151. {
  152. global $_GPC;
  153. $loginUserInfo = UserServiceFacade::getUserInfo();
  154. $post = $this->request->post();
  155. $extractPrice = $this->request->post('extract_price',0);
  156. $modelUser = new \app\model\commission\User();
  157. $modelCashout = new \app\model\commission\Cashout();
  158. $commissionUser = $modelUser::where('uid','=',$loginUserInfo['id'])->find();
  159. if(!$commissionUser){
  160. return $this->error('未找到您的分销信息');
  161. }
  162. if($commissionUser['money'] < $extractPrice){
  163. return $this->error('金额不足');
  164. }
  165. if($extractPrice <= 0){
  166. return $this->error('提现金额不能小于或等于0元');
  167. }
  168. $mark = '';
  169. $platform = $this->request->post('platform','wxOfficialAccount');
  170. $post['uid'] = $loginUserInfo['id'];
  171. $post['uniacid'] = $_GPC['uniacid'];
  172. // if(!$post['real_name']) return $this->error('姓名输入有误');
  173. if (!preg_match('/^[0-9]+(.[0-9]{1,2})?$/', $post['extract_price'])) return $this->error('提现金额输入有误');
  174. $mark = '使用微信提现' . $post['extract_price'] . '元';
  175. if($platform =='wxOfficialAccount' ){
  176. $post['extract_type'] = 'wechat';
  177. }else{
  178. $post['extract_type'] = 'miniapp';
  179. }
  180. $balance = bcsub($commissionUser['money'],$extractPrice,2) ?? 0;
  181. $post['balance'] = $balance;//用户余额
  182. $post['mark'] = $mark;
  183. $saveRes = $modelCashout->save($post);
  184. if (!$saveRes) return $this->error('保存基础信息失败');
  185. $balanceRes = \app\model\commission\User::where(['uid'=>$loginUserInfo['id']])->update(['money' => $balance]);
  186. return $this->success('数据获取成功', $saveRes);
  187. }
  188. // 收入记录
  189. public function incomeLog()
  190. {
  191. global $_GPC;
  192. $order = ['id' => 'desc'];
  193. $modelIncome = new \app\model\commission\Income();
  194. $loginUserInfo = UserServiceFacade::getUserInfo();
  195. $where = ['uid'=>$loginUserInfo['id'],'uniacid'=>$_GPC['uniacid']];
  196. $post = $this->request->param();
  197. $limit = $this->request->param('limit', 10);
  198. $data = $modelIncome->order($order)->with(['userinfo','buy_userinfo'])->where($where)->paginate($limit)->toArray();
  199. if(!$data){
  200. return $this->error('数据获取失败');
  201. }
  202. return $this->success('数据获取成功', $data);
  203. }
  204. // 提现记录
  205. public function cashoutLog()
  206. {
  207. global $_GPC;
  208. $order = ['id' => 'desc'];
  209. $modelCashout = new \app\model\commission\Cashout();
  210. $loginUserInfo = UserServiceFacade::getUserInfo();
  211. $where = ['uid'=>$loginUserInfo['id'],'uniacid'=>$_GPC['uniacid']];
  212. $post = $this->request->param();
  213. $limit = $this->request->param('limit', 10);
  214. $data = $modelCashout->order($order)->where($where)->paginate($limit)->toArray();
  215. if(!$data){
  216. return $this->error('数据获取失败');
  217. }
  218. return $this->success('数据获取成功', $data);
  219. }
  220. // 排行榜
  221. public function ranking()
  222. {
  223. global $_GPC;
  224. $where = ['uniacid'=>$_GPC['uniacid']];
  225. $model = new \app\model\commission\User();
  226. $post = $this->request->param();
  227. // $where[] = ['pm' ,'=', 1];
  228. $order = ['income_sum' => 'desc'];
  229. $limit = $this->request->param('limit', 10);
  230. $data = $model->order($order)->where($where)->with('userinfo')->withSum('income','money')->paginate($limit)->toArray();
  231. if(!$data){
  232. return $this->error('数据获取失败');
  233. }
  234. return $this->success('数据获取成功', $data);
  235. }
  236. }