Withdraw.php 4.6 KB

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