Withdraw.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace app\admin\controller\store;
  3. use app\common\controller\AdminController;
  4. use app\http\IResponse;
  5. use EasyWeChat\Factory;
  6. use GuzzleHttp\Exception\GuzzleException;
  7. use think\Db;
  8. class Withdraw extends AdminController
  9. {
  10. /**
  11. * 提现列表
  12. *
  13. * @author 许祖兴 < zuxing.xu@lettered.cn>
  14. * @date 2020/7/9 9:39
  15. *
  16. * @return mixed
  17. * @throws \think\exception\DbException
  18. */
  19. public function index()
  20. {
  21. $where = [];
  22. //组合搜索
  23. !empty(input('name')) && $where[]
  24. = ['name', 'like', '%' . input('name') . '%'];
  25. !empty(input('mobile')) && $where[]
  26. = ['mobile', 'eq', input('mobile')];
  27. $withdraw = model('common/UsersWithdraw');
  28. return IResponse::paginate($withdraw->where($where)->order(['created_at'=>'desc'])->with(['user'])
  29. ->paginate(input('limit'),false));
  30. }
  31. /**
  32. * 更新数据
  33. *
  34. * @author 许祖兴 < zuxing.xu@lettered.cn>
  35. * @date 2020/6/11 14:20
  36. *
  37. * @param $id
  38. * @return \think\response\Json
  39. */
  40. public function update($id)
  41. {
  42. // 接收数据
  43. $params = $this->request->param();
  44. // 查询用户
  45. $withdraw = model('common/UsersWithdraw')->findBy($id);
  46. // 状态操作
  47. $valid = $this->validate($params, [
  48. 'status|配置参数' => 'require|integer'
  49. ]);
  50. // 错误返回
  51. (true !== $valid) && IResponse::failure($valid);
  52. // 更新信息
  53. $withdraw->updateBy($id, $params);
  54. Db::startTrans();
  55. try {
  56. if ($params['status'] == 2) {
  57. // 查用户
  58. $user = model('common/Users')->getBy($withdraw['user_id']);
  59. // 更新资金
  60. $user->where(['id' => $withdraw['user_id']])->setDec('balance', $withdraw['amount']);
  61. // 写入资金记录
  62. model('common/UsersBalanceRecord')->storeBy([
  63. 'user_id' => $withdraw['user_id'],
  64. 'dec_amount' => $withdraw['amount'],
  65. 'aft_amount' => round($user['balance'] - $withdraw['amount']),
  66. 'remark' => '主动提现,¥' . $withdraw['amount'] . ',单号;' . $withdraw['draw_no']
  67. ]);
  68. // 加载配置
  69. $wechat = sys_config('', 'wechat');
  70. $config = [
  71. // 前面的appid什么的也得保留哦
  72. 'app_id' => $wechat['mini_appid'],
  73. 'mch_id' => $wechat['pay_mch_id'],
  74. 'key' => $wechat['pay_secret_key'],
  75. // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
  76. 'cert_path' => $wechat['cert_path'], // XXX: 绝对路径!!!!
  77. 'key_path' => $wechat['key_path'], // XXX: 绝对路径!!!!
  78. // 'notify_url' => 'https://api.gxrrj.cn/api/v1/wechat/notify',
  79. // 'notify_url' => 'http://rrj.gxnwsoft.com/api/v1/wechat/refundNotify',
  80. // 'sandbox' => true
  81. ];
  82. // 创建应用实例
  83. $app = Factory::payment($config);
  84. $result = $app->transfer->toBalance([
  85. 'partner_trade_no' => $withdraw['draw_no'], // 商户订单号,需保持唯一性(只能是字母或者数字,不能包含有符号)
  86. 'openid' => $user['open_id'],
  87. 'check_name' => 'FORCE_CHECK', // NO_CHECK:不校验真实姓名, FORCE_CHECK:强校验真实姓名
  88. 're_user_name' => $withdraw['realname'], // 如果 check_name 设置为FORCE_CHECK,则必填用户真实姓名
  89. 'amount' => $withdraw['amount'] * 100, // 企业付款金额,单位为分
  90. 'desc' => '用户提现', // 企业付款操作说明信息。必填
  91. ]);
  92. app()->log(json_encode($result));
  93. }
  94. Db::commit();
  95. } catch (\Exception | GuzzleException $e) {
  96. Db::rollback();
  97. }
  98. return IResponse::success('成功');
  99. }
  100. /**
  101. * 删除
  102. *
  103. * @author 许祖兴 < zuxing.xu@lettered.cn>
  104. * @date 2020/6/11 14:26
  105. *
  106. * @param $id
  107. * @return \think\response\Json
  108. */
  109. public function delete($id)
  110. {
  111. model('common/UsersWithdraw')->deleteBy($id);
  112. return IResponse::success([],'删除提现申请单成功');
  113. }
  114. /**
  115. * 用户批量操作
  116. *
  117. * @author 许祖兴 < zuxing.xu@lettered.cn>
  118. * @date 2020/6/11 14:34
  119. *
  120. * @return mixed
  121. */
  122. public function plectron(){
  123. // 收参数
  124. $params = $this->request->param();
  125. foreach (str2arr($params['ids']) as $id){
  126. $withdraw = model('common/UsersWithdraw')->getBy($id);
  127. if ($this->request->isDelete()){
  128. $withdraw->deleteBy($id);
  129. }else
  130. // 等待审核状态下才做更新
  131. if ($withdraw['status'] == 1){
  132. $withdraw->allowField(true)->updateBy($id, $params);
  133. Db::startTrans();
  134. try {
  135. if ($params['status'] == 2){
  136. // 查用户
  137. $user = model('common/Users')->getBy($withdraw['user_id']);
  138. // 更新资金
  139. $user->where(['id' => $withdraw['user_id']])->setDec('balance', $withdraw['amount']);
  140. // 写入资金记录
  141. model('common/UsersBalanceRecord')::create([
  142. 'user_id' => $withdraw['user_id'],
  143. 'dec_amount' => $withdraw['amount'],
  144. 'aft_amount' => round($user['balance'] - $withdraw['amount']),
  145. 'remark' => '主动提现,¥' . $withdraw['amount'] . ',单号;' . $withdraw['draw_no']
  146. ], true);
  147. }
  148. Db::commit();
  149. }catch (\Exception $e){
  150. Db::rollback();
  151. }
  152. }
  153. }
  154. return IResponse::success([],'操作成功');
  155. }
  156. }