WalletManage.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\GoodsOrderItem;
  4. use app\common\model\UserBank;
  5. use app\common\model\UserIntegralLog;
  6. use app\common\model\UserLevel;
  7. use app\common\model\UserMoneyInfo;
  8. use app\common\model\UserWallet;
  9. use app\common\model\UserWithdrawLog;
  10. use think\Db;
  11. use app\common\model\User;
  12. /**
  13. * @title 会员钱包管理(佣金提现,积分,经验...)
  14. * @controller WalletManage
  15. * @group base
  16. */
  17. class WalletManage extends Base
  18. {
  19. public function initialize()
  20. {
  21. parent::initialize();
  22. parent::checkLogin();
  23. }
  24. /**
  25. * @title 获取钱包详情
  26. * @desc 获取钱包详情
  27. * @author qc
  28. * @url /api/Wallet_manage/walletInfo
  29. * @method GET
  30. * @tag 个人信息
  31. * @header name:Authorization require:1 desc:Token
  32. * @return name:detail type:array default:-- desc:钱包
  33. * @return name:detail.integral type:int default:-- desc:积分
  34. * @return name:detail.growth type:int default:-- desc:经验值(分销等级成长值)
  35. * @return name:detail.money type:float default:-- desc:佣金
  36. * @return name:detail.level_exp type:int default:-- desc:等级经验(会员等级经验值)
  37. * @return name:detail.withdraw type:float default:-- desc:累积提现
  38. * @return name:openid type:string default:-- desc:绑定的微信openid
  39. * @return name:ali_account type:array default:-- desc:支付宝账号
  40. * @return name:ali_account.real_name type:string default:-- desc:支付宝姓名
  41. * @return name:ali_account.card_no type:string default:-- desc:支付宝账号
  42. * @return name:bank_list type:array default:-- desc:绑定银行卡列表
  43. * @return name:bank_list.real_name type:string require:1 default:-- desc:真实姓名
  44. * @return name:bank_list.card_no type:string require:1 default:-- desc:账号
  45. * @return name:bank_list.bank_name type:string require:1 default:-- desc:所属银行
  46. *
  47. */
  48. public function walletInfo()
  49. {
  50. $wallet = UserWallet::where('user_id',$this->user_id)->find();
  51. if(!$wallet) $wallet = UserWallet::create(['user_id'=>$this->user_id]);
  52. $user_info = User::field('id,name,openid,headimg')->where('id',$this->user_id)->find();
  53. $ali_account = UserBank::field('id,real_name,card_no')->where(['user_id'=>$this->user_id,'type'=>1,'is_deleted'=>0])->find();
  54. $bank_list = UserBank::field('id,real_name,card_no,bank_name')->where(['user_id'=>$this->user_id,'type'=>2,'is_deleted'=>0])->order('id desc')->select()->toArray();
  55. $this->success('获取成功',['detail'=> $wallet->toArray(),'user_info'=>$user_info,'ali_account'=>$ali_account,'bank_list'=>$bank_list]);
  56. }
  57. /**
  58. * @title 获取个人钱包信息
  59. * @desc 获取个人钱包信息
  60. * @author qc
  61. * @url /api/Wallet_manage/getUserWallet
  62. * @method GET
  63. * @tag 获取个人钱包信息
  64. * @header name:Authorization require:1 desc:Token
  65. * @return name:phone type:int default:-- desc:手机号
  66. * @return name:name type:string default:-- desc:昵称
  67. * @return name:headimg type:string default:-- desc:头像地址
  68. * @return name:card_front type:string default:-- desc:身份证正面
  69. * @return name:card_back type:string default:-- desc:身份证背面
  70. * @return name:level_name type:string default:-- desc:等级名称
  71. * @return name:partner_lev type:int default:-- desc:分销商等级id(0不是分销商)
  72. * @return name:is_agency type:int default:-- desc:是否是区域代理(0不是)
  73. * @return name:wallet type:array default:-- desc:钱包
  74. * @return name:wallet.integral type:int default:-- desc:积分
  75. * @return name:wallet.growth type:int default:-- desc:经验值(分销等级成长值)
  76. * @return name:wallet.money type:float default:-- desc:佣金
  77. * @return name:wallet.level_exp type:int default:-- desc:经验值(会员等级)
  78. *
  79. */
  80. public function getUserWallet()
  81. {
  82. $field = 'id,name,level_id,headimg,true_name,birthday,is_agency,partner_lev';
  83. $user_info = User::with('wallet')->field($field)->where('id',$this->user_id)->find()->toArray();
  84. $user_info['level_name'] = UserLevel::where('id',$user_info['level_id'])->value('name');
  85. if(empty($user_info)) $this->error('用户信息不正确');
  86. $this->success('获取成功',['user_info'=>$user_info]);
  87. }
  88. /**
  89. * @title 佣金提现
  90. * @desc 佣金提现
  91. * @author qc
  92. * @url /api/Wallet_manage/withdraw
  93. * @method POST
  94. * @tag 佣金提现
  95. * @header name:Authorization require:1 desc:Token
  96. * @param name:money type:float default:-- desc:提现金额
  97. * @param name:type type:int default:-- desc:到账类型(1支付宝,2银行卡,3微信)
  98. * @param name:pay_password type:string default:-- desc:支付密码
  99. * @param name:bank_id type:int default:-- desc:账号id(type=1或2必传)
  100. */
  101. public function withdraw()
  102. {
  103. $rate = 0.03;// 提现手续费
  104. $min_money = 100 ;// 最低提现金额
  105. $max_money = 1000;// 最大提现金额
  106. $money = input('post.money',0);
  107. $type = input('post.type',0);
  108. $bank_id = input('post.bank_id',0);
  109. $pay_password = input('post.pay_password',0);
  110. if($money < $min_money) $this->error('最低提现金额:'.$min_money);
  111. if($money > $max_money) $this->error('最大提现金额:'.$max_money);
  112. $user_info = User::where('id',$this->user_id)->find()->toArray();
  113. if(!$user_info['pay_password']) $this->error('请设置提现密码');
  114. if(!check_password($pay_password,$user_info['pay_password'])) $this->error('密码错误');
  115. if($type == 3 && !$user_info['openid']) $this->error('请先绑定微信!');
  116. if(in_array($type,[1,2])) {
  117. $acc_info = UserBank::where(['id'=>$bank_id,'user_id'=>$this->user_id])->find();
  118. if(!$acc_info) $this->error('账号信息有误');
  119. }
  120. Db::startTrans();
  121. try {
  122. $wallet = UserWallet::where(['user_id'=>$this->user_id])->find();
  123. if($wallet->money < $money) $this->exception('佣金余额不足');
  124. $service_fee = bcmul($money,$rate,2);
  125. $account = bcsub($money,$service_fee,2);
  126. $ins_data = [
  127. 'user_id' => $this->user_id,
  128. 'money' => $money,// 提现金额
  129. 'rate' => $rate,// 手续费率
  130. 'service_fee' => $service_fee,// 手续费
  131. 'account' => $account,// 实际到账金额
  132. 'type' => $type,
  133. 'user_name' => $type == 3 ? $user_info['name'] : $acc_info->real_name,
  134. 'card_no' => $type == 3 ? $user_info['openid'] : $acc_info->card_no,
  135. 'bank_name' => $type == 3 ? '微信账户' : $acc_info->bank_name,
  136. ];
  137. $ins_res = UserWithdrawLog::create($ins_data);// 提现日志
  138. UserWallet::userMoneyChange($this->user_id,$money,'提现申请',3,-1,$ins_res->id);
  139. $this->ret_msg = '申请成功';
  140. }catch (\Exception $e){
  141. $this->is_commit = false;
  142. $this->ret_msg = $e->getMessage();
  143. }
  144. $this->transReturn();
  145. }
  146. /**
  147. * @title 获取用户提现日志
  148. * @desc 获取用户提现日志
  149. * @author qc
  150. * @url /api/Wallet_manage/getWithdrawLog
  151. * @method GET
  152. * @header name:Authorization require:1 desc:Token
  153. * @param name:status type:int default:-1 desc:状态(0待处理,1已审核 2已到账 3已拒绝 4异常)
  154. * @param name:type type:int default:-- desc:类型
  155. * @param name:page type:int : default:1 desc:页数
  156. * @param name:page_num type:int : default:20 desc:每页数
  157. * @return name:money type:float default:-- desc:提现金额
  158. * @return name:type type:int default:-- desc:到账类型(1支付宝,2银行卡,3微信)
  159. * @return name:money type:float default:-- desc:申请提现金额
  160. * @return name:rate type:float default:-- desc:手续费率
  161. * @return name:service_fee type:float default:-- desc:手续费
  162. * @return name:desc type:float default:-- desc:描述
  163. * @return name:status type:int default:-- desc:状态(0待处理,1已审核 2已到账 3已拒绝 4异常)
  164. * @return name:user_name type:string default:-- desc:收款人
  165. * @return name:card_no type:string default:-- desc:收款账号
  166. * @return name:bank_name type:string default:-- desc:名称
  167. */
  168. public function getWithdrawLog()
  169. {
  170. $status = input('get.status',-1);
  171. $where = [];
  172. $where['is_deleted'] = 0;
  173. $where['user_id'] = $this->user_id;
  174. if($status) $where['status'] = $status;
  175. $list = UserWithdrawLog::where($where)->limit($this->off_set,$this->page_num)
  176. ->order('id desc')->select();
  177. $this->success('ok',['list'=>$list ? $list->toArray():[]]);
  178. }
  179. /**
  180. * @title 获取用户佣金变更记录
  181. * @desc 获取用户佣金变更记录
  182. * @author qc
  183. * @url /api/Wallet_manage/getUserMoneyInfo
  184. * @method GET
  185. * @header name:Authorization require:1 desc:Token
  186. * @param name:page type:int : default:1 desc:页数
  187. * @param name:page_num type:int : default:20 desc:每页数
  188. * @return name:money type:float default:-- desc:金额
  189. * @return name:create_at type:string default:-- desc:时间
  190. * @return name:desc type:float default:-- desc:描述
  191. */
  192. public function getUserMoneyInfo()
  193. {
  194. $where = [];
  195. $where['user_id'] = $this->user_id;
  196. $list = UserMoneyInfo::where($where)->limit($this->off_set,$this->page_num)->order('id desc')->select();
  197. $this->success('ok',['list'=>$list ? $list->toArray():[]]);
  198. }
  199. /**
  200. * @title 获取用户积分变更记录
  201. * @desc 获取用户积分变更记录
  202. * @author qc
  203. * @url /api/Wallet_manage/getUserIntegralLog
  204. * @method GET
  205. * @header name:Authorization require:1 desc:Token
  206. * @param name:page type:int : default:1 desc:页数
  207. * @param name:page_num type:int : default:20 desc:每页数
  208. * @param name:type type:int : default:0 desc:类型(0全部,1增加,2减少)
  209. * @return name:integral type:int default:-- desc:变更数量
  210. * @return name:create_at type:string default:-- desc:时间
  211. * @return name:desc type:float default:-- desc:描述
  212. */
  213. public function getUserIntegralLog()
  214. {
  215. $where = [];
  216. $where[] = ['user_id','=',$this->user_id];
  217. $where[] = ['is_deleted','=',0];
  218. if(input('type') == 1) $where[] = ['integral','>','0'];
  219. if(input('type') == 2) $where[] = ['integral','<','0'];
  220. $list = UserIntegralLog::where($where)->limit($this->off_set,$this->page_num)->order('id desc')->select();
  221. $this->success('ok',['list'=>$list ? $list->toArray():[]]);
  222. }
  223. /**
  224. * @title 获取用户分销佣金记录
  225. * @desc 获取用户分销佣金记录
  226. * @author qc
  227. * @url /api/Wallet_manage/getDistributionMoney
  228. * @method GET
  229. * @header name:Authorization require:1 desc:Token
  230. * @param name:page type:int : default:1 desc:页数
  231. * @param name:page_num type:int : default:20 desc:每页数
  232. * @return name:money type:float default:-- desc:金额
  233. * @return name:create_at type:string default:-- desc:时间
  234. * @return name:desc type:float default:-- desc:描述
  235. * @return name:type type:float default:-- desc:类型(7一级分销,8二级分销)
  236. * @return name:user_info type:array default:-- desc:会员信息
  237. * @return name:user_info.name type:string default:-- desc:会员名
  238. * @return name:user_info.headimg type:string default:-- desc:会员头像
  239. * @return name:goods_list type:array default:-- desc:商品列表
  240. * @return name:goods_list.cover type:array default:-- desc:商品图
  241. * @return name:goods_list.goods_name type:string default:-- desc:商品名
  242. * @return name:goods_list.goods_spec type:string default:-- desc:商品规格
  243. * @return name:goods_list.sell_money type:float default:-- desc:商品价格
  244. * @return name:goods_list.is_dist type:int default:-- desc:是否是分销商品(0否,1是)
  245. */
  246. public function getDistributionMoney()
  247. {
  248. $where = [];
  249. $where[] = ['user_id','=',$this->user_id];
  250. $where[] = ['type','in','7,8'];
  251. $list = UserMoneyInfo::where($where)->limit($this->off_set,$this->page_num)->order('id desc')->select();
  252. array_walk($list,function ($val){
  253. $val['user_info'] = User::where('id',$val['user_id'])->field('id,name,headimg')->find()->toArray();
  254. $goods_list = GoodsOrderItem::where('i.order_id',$val['id'])
  255. ->alias('i')
  256. ->leftJoin('StoreGoods g','g.id = i.goods_id')
  257. ->field('i.cover,i.sell_money,i.num,g.is_dist,i.goods_name,i.goods_spec')
  258. ->select()->toArray();
  259. $val['goods_list'] = $goods_list;
  260. });
  261. $this->success('ok',['list'=>$list ? $list->toArray():'']);
  262. }
  263. }