Withdraw.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace app\store\model\dealer;
  3. use app\store\model\User as UserModel;
  4. use app\store\model\Wxapp as WxappModel;
  5. use app\common\model\dealer\User as dealerUserModel;
  6. use app\common\model\dealer\Withdraw as WithdrawModel;
  7. use app\common\service\Order as OrderService;
  8. use app\common\service\Message as MessageService;
  9. use app\common\enum\dealer\withdraw\PayType as PayTypeEnum;
  10. use app\common\enum\dealer\withdraw\ApplyStatus as ApplyStatusEnum;
  11. use app\common\library\wechat\WxPay;
  12. /**
  13. * 分销商提现明细模型
  14. * Class Withdraw
  15. * @package app\store\model\dealer
  16. */
  17. class Withdraw extends WithdrawModel
  18. {
  19. /**
  20. * 获取器:申请时间
  21. * @param $value
  22. * @return false|string
  23. */
  24. public function getAuditTimeAttr($value)
  25. {
  26. return $value > 0 ? date('Y-m-d H:i:s', $value) : 0;
  27. }
  28. /**
  29. * 获取器:打款方式
  30. * @param $value
  31. * @return mixed
  32. */
  33. public function getPayTypeAttr($value)
  34. {
  35. return ['text' => PayTypeEnum::data()[$value]['name'], 'value' => $value];
  36. }
  37. /**
  38. * 获取分销商提现列表
  39. * @param null $userId
  40. * @param int $apply_status
  41. * @param int $pay_type
  42. * @param string $search
  43. * @return \think\Paginator
  44. * @throws \think\exception\DbException
  45. */
  46. public function getList($userId = null, $apply_status = -1, $pay_type = -1, $search = '')
  47. {
  48. // 构建查询规则
  49. $this->alias('withdraw')
  50. ->with(['user'])
  51. ->field('withdraw.*, dealer.real_name, dealer.mobile, user.nickName, user.avatarUrl')
  52. ->join('user', 'user.user_id = withdraw.user_id')
  53. ->join('dealer_user dealer', 'dealer.user_id = withdraw.user_id')
  54. ->order(['withdraw.create_time' => 'desc']);
  55. // 查询条件
  56. $userId > 0 && $this->where('withdraw.user_id', '=', $userId);
  57. !empty($search) && $this->where('dealer.real_name|dealer.mobile', 'like', "%$search%");
  58. $apply_status > 0 && $this->where('withdraw.apply_status', '=', $apply_status);
  59. $pay_type > 0 && $this->where('withdraw.pay_type', '=', $pay_type);
  60. // 获取列表数据
  61. return $this->paginate(15, false, [
  62. 'query' => \request()->request()
  63. ]);
  64. }
  65. /**
  66. * 分销商提现审核
  67. * @param $data
  68. * @return bool
  69. */
  70. public function submit($data)
  71. {
  72. if (
  73. $data['apply_status'] == ApplyStatusEnum::AUDIT_REJECT
  74. && empty($data['reject_reason'])
  75. ) {
  76. $this->error = '请填写驳回原因';
  77. return false;
  78. }
  79. $this->transaction(function () use ($data) {
  80. // 更新申请记录
  81. $data['audit_time'] = time();
  82. $this->allowField(true)->save($data);
  83. // 提现驳回:解冻分销商资金
  84. if ($data['apply_status'] == ApplyStatusEnum::AUDIT_REJECT) {
  85. User::backFreezeMoney($this['user_id'], $this['money']);
  86. }
  87. // 发送消息通知
  88. MessageService::send('dealer.withdraw', [
  89. 'withdraw' => $this,
  90. 'user' => UserModel::detail($this['user_id']),
  91. ]);
  92. });
  93. return true;
  94. }
  95. /**
  96. * 确认已打款
  97. * @param bool $verifyUserFreezeMoney 验证已冻结佣金是否合法
  98. * @return bool|mixed
  99. * @throws \think\exception\DbException
  100. */
  101. public function money($verifyUserFreezeMoney = true)
  102. {
  103. // 验证已冻结佣金是否合法
  104. if ($verifyUserFreezeMoney && !$this->verifyUserFreezeMoney($this['user_id'], $this['money'])) {
  105. return false;
  106. }
  107. return $this->transaction(function () {
  108. // 更新申请状态
  109. $this->allowField(true)->save([
  110. 'apply_status' => 40,
  111. 'audit_time' => time(),
  112. ]);
  113. // 更新分销商累积提现佣金
  114. User::totalMoney($this['user_id'], $this['money']);
  115. // 记录分销商资金明细
  116. Capital::add([
  117. 'user_id' => $this['user_id'],
  118. 'flow_type' => 20,
  119. 'money' => -$this['money'],
  120. 'describe' => '申请提现',
  121. ]);
  122. return true;
  123. });
  124. }
  125. /**
  126. * 分销商提现:微信支付企业付款
  127. * @return bool
  128. * @throws \app\common\exception\BaseException
  129. * @throws \think\Exception
  130. * @throws \think\exception\DbException
  131. */
  132. public function wechatPay()
  133. {
  134. // 验证已冻结佣金是否合法
  135. if (!$this->verifyUserFreezeMoney($this['user_id'], $this['money'])) {
  136. return false;
  137. }
  138. // 微信用户信息
  139. $user = $this['user']['user'];
  140. // 生成付款订单号
  141. $orderNO = OrderService::createOrderNo();
  142. // 付款描述
  143. $desc = '分销商提现付款';
  144. // 微信支付api:企业付款到零钱
  145. $wxConfig = WxappModel::getWxappCache();
  146. $WxPay = new WxPay($wxConfig);
  147. // 请求付款api
  148. if ($WxPay->transfers($orderNO, $user['open_id'], $this['money'], $desc)) {
  149. // 确认已打款
  150. $this->money(false);
  151. return true;
  152. }
  153. return false;
  154. }
  155. /**
  156. * 验证已冻结佣金是否合法
  157. * @param $userId
  158. * @param $money
  159. * @return bool
  160. * @throws \think\exception\DbException
  161. */
  162. public function verifyUserFreezeMoney($userId, $money)
  163. {
  164. $dealerUserInfo = dealerUserModel::detail($userId);
  165. if ($dealerUserInfo['freeze_money'] < $money) {
  166. $this->error = '数据错误:已冻结的佣金不能小于提现的金额';
  167. return false;
  168. }
  169. return true;
  170. }
  171. }