Withdraw.php 4.7 KB

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