Cash.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace app\shop\model\plus\agent;
  3. use app\common\library\easywechat\AppWx;
  4. use app\common\library\easywechat\AppMp;
  5. use app\common\service\message\MessageService;
  6. use app\common\service\order\OrderService;
  7. use app\common\library\easywechat\WxPay;
  8. use app\common\model\plus\agent\Cash as CashModel;
  9. use app\shop\model\user\User as UserModel;
  10. /**
  11. * 分销商提现明细模型
  12. */
  13. class Cash extends CashModel
  14. {
  15. /**
  16. * 获取器:申请时间
  17. */
  18. public function getAuditTimeAttr($value)
  19. {
  20. return $value > 0 ? date('Y-m-d H:i:s', $value) : 0;
  21. }
  22. /**
  23. * 获取器:打款方式
  24. */
  25. public function getPayTypeAttr($value)
  26. {
  27. return ['text' => $this->payType[$value], 'value' => $value];
  28. }
  29. /**
  30. * 获取分销商提现列表
  31. */
  32. public function getList($user_id = null, $apply_status = -1, $pay_type = -1, $search = '')
  33. {
  34. $model = $this;
  35. // 构建查询规则
  36. $model = $model->alias('cash')
  37. ->with(['user'])
  38. ->field('cash.*, agent.real_name, agent.mobile, user.nickName, user.avatarUrl')
  39. ->join('user', 'user.user_id = cash.user_id')
  40. ->join('agent_user agent', 'agent.user_id = cash.user_id')
  41. ->order(['cash.create_time' => 'desc']);
  42. // 查询条件
  43. if ($user_id > 0) {
  44. $model = $model->where('cash.user_id', '=', $user_id);
  45. }
  46. if (!empty($search)) {
  47. $model = $model->where('agent.real_name|agent.mobile', 'like', '%' . $search . '%');
  48. }
  49. if ($apply_status > 0) {
  50. $model = $model->where('cash.apply_status', '=', $apply_status);
  51. }
  52. if ($pay_type > 0) {
  53. $model = $model->where('cash.pay_type', '=', $pay_type);
  54. }
  55. // 获取列表数据
  56. return $model->paginate(15);
  57. }
  58. /**
  59. * 分销商提现审核
  60. */
  61. public function submit($param)
  62. {
  63. $data = ['apply_status' => $param['apply_status']];
  64. if ($param['apply_status'] == 30) {
  65. $data['reject_reason'] = $param['reject_reason'];
  66. }
  67. // 更新申请记录
  68. $data['audit_time'] = time();
  69. self::update($data, ['id' => $param['id']]);
  70. // 提现驳回:解冻分销商资金
  71. if ($param['apply_status'] == 30) {
  72. User::backFreezeMoney($param['user_id'], $param['money']);
  73. }
  74. // 发送模板消息
  75. (new MessageService)->cash($this);
  76. return true;
  77. }
  78. /**
  79. * 确认已打款
  80. */
  81. public function money()
  82. {
  83. $this->startTrans();
  84. try {
  85. // 更新申请状态
  86. $data = ['apply_status' => 40, 'audit_time' => time()];
  87. self::update($data, ['id' => $this['id']]);
  88. // 更新分销商累积提现佣金
  89. User::totalMoney($this['user_id'], $this['money']);
  90. // 记录分销商资金明细
  91. Capital::add([
  92. 'user_id' => $this['user_id'],
  93. 'flow_type' => 20,
  94. 'money' => -$this['money'],
  95. 'describe' => '申请提现',
  96. ]);
  97. // 发送模板消息
  98. //(new Message)->withdraw($this);
  99. // 事务提交
  100. $this->commit();
  101. return true;
  102. } catch (\Exception $e) {
  103. $this->error = $e->getMessage();
  104. $this->rollback();
  105. return false;
  106. }
  107. }
  108. /**
  109. * 分销商提现:微信支付企业付款
  110. */
  111. public function wechatPay()
  112. {
  113. // 微信用户信息
  114. $user = UserModel::detail($this['user_id']);
  115. // 生成付款订单号
  116. $orderNO = OrderService::createOrderNo();
  117. // 付款描述
  118. $desc = '分销商提现付款';
  119. // 微信支付api:企业付款到零钱
  120. $open_id = '';
  121. $app = [];
  122. if($user['reg_source'] == 'mp'){
  123. $app = AppMp::getWxPayApp($user['app_id']);
  124. $open_id = $user['mpopen_id'];
  125. }else if($user['reg_source'] == 'wx'){
  126. $app = AppWx::getWxPayApp($user['app_id']);
  127. $open_id = $user['open_id'];
  128. }
  129. if($open_id == ''){
  130. $this->error = '未找到用户open_id';
  131. return false;
  132. }
  133. $WxPay = new WxPay($app);
  134. // 请求付款api
  135. if ($WxPay->transfers($orderNO, $open_id, $this['money'], $desc)) {
  136. // 确认已打款
  137. $this->money();
  138. return true;
  139. }
  140. return false;
  141. }
  142. /*
  143. *统计提现总数量
  144. */
  145. public function getAgentOrderTotal()
  146. {
  147. return $this->count('id');
  148. }
  149. /*
  150. * 统计提现待审核总数量
  151. */
  152. public function getAgentApplyTotal($apply_status)
  153. {
  154. return $this->where('apply_status', '=', $apply_status)->count();
  155. }
  156. }