TaxiUserWithdraw.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace app\admin\controller\store;
  3. use app\common\controller\AdminController;
  4. use app\common\model\Users;
  5. use app\http\IResponse;
  6. use EasyWeChat\Factory;
  7. use GuzzleHttp\Exception\GuzzleException;
  8. use think\Db;
  9. class TaxiUserWithdraw extends AdminController
  10. {
  11. /**
  12. * 提现列表
  13. *
  14. * @return mixed
  15. * @throws \think\exception\DbException
  16. */
  17. public function index()
  18. {
  19. $where = [];
  20. //组合搜索
  21. !empty(input('name')) && $where[]
  22. = ['name', 'like', '%' . input('name') . '%'];
  23. !empty(input('keyword')) && $where[]
  24. = ['mobile', 'like', '%' . input('keyword') . '%'];
  25. $withdraw = model('common/TaxiUsersWithdraw');
  26. $list = $withdraw->where($where)->order(['created_at' => 'desc'])->with(['user', 'taxiUser'])
  27. ->paginate(input('limit'), false);
  28. //return IResponse::paginate();
  29. return IResponse::success([
  30. 'total' => $list->total(),
  31. 'totalRow' => [
  32. 'wait' => $withdraw->where($where)->where(['status' => 10])->order(['created_at' => 'desc'])->sum('amount'),
  33. 'success' => $withdraw->where($where)->where(['status' => 20])->order(['created_at' => 'desc'])->sum('amount')
  34. ],
  35. 'list' => $list->getCollection(),
  36. ]);
  37. }
  38. /**
  39. * 更新数据
  40. *
  41. * @param $id
  42. * @return \think\response\Json
  43. */
  44. public function update($id)
  45. {
  46. // 接收数据
  47. $params = $this->request->param();
  48. // 查询用户
  49. $withdraw = model('common/TaxiUsersWithdraw')->findBy($id);
  50. // 状态操作
  51. $valid = $this->validate($params, [
  52. 'status|配置参数' => 'require|integer'
  53. ]);
  54. // 错误返回
  55. (true !== $valid) && IResponse::failure($valid);
  56. // 更新信息
  57. $withdraw->updateBy($id, $params);
  58. Db::startTrans();
  59. // 通过
  60. $withdraw->status = $params['status'];
  61. $withdraw->remittance_time = $params['status'] == 20 ? time() : 0;
  62. $withdraw->arrival_amount = $params['status'] == 20 ? $withdraw['amount'] : 0;
  63. if (!$withdraw->save()) {
  64. Db::rollback();
  65. IResponse::failure('提现审核失败');
  66. }
  67. // 拒绝退款
  68. if ($params['status'] == 30) {
  69. // 查用户
  70. $user = model('common/Users')->getBy($withdraw['user_id']);
  71. if (empty($user)) {
  72. Db::rollback();
  73. IResponse::failure('司机账户错误,请联系管理员处理');
  74. }
  75. // 写入资金记录
  76. $Users = new Users();
  77. $Users->changePartnership($user['id'], $withdraw['amount'], '资产余额提现失败退还', 20, true);
  78. } // 打款到账
  79. else if ($params['status'] == 20) {
  80. $wechat = sys_config('', 'wechat');
  81. $config = [
  82. // 前面的appid什么的也得保留哦
  83. 'app_id' => $wechat['mini_appid'],
  84. 'mch_id' => $wechat['pay_mch_id'],
  85. 'key' => $wechat['pay_secret_key'],
  86. // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
  87. 'cert_path' => $wechat['cert_path'], // XXX: 绝对路径!!!!
  88. 'key_path' => $wechat['key_path'], // XXX: 绝对路径!!!!
  89. ];
  90. $user = model('common/Users')->getBy($withdraw['user_id']);
  91. // 创建应用实例
  92. $app = Factory::payment($config);
  93. $result = $app->transfer->toBalance([
  94. 'partner_trade_no' => $withdraw['order_no'], // 商户订单号,需保持唯一性(只能是字母或者数字,不能包含有符号)
  95. 'openid' => $user['open_id'],
  96. 'check_name' => 'FORCE_CHECK', // NO_CHECK:不校验真实姓名, FORCE_CHECK:强校验真实姓名
  97. 're_user_name' => $withdraw['real_name'], // 如果 check_name 设置为FORCE_CHECK,则必填用户真实姓名
  98. 'amount' => $withdraw['amount'] * 100, // 企业付款金额,单位为分
  99. 'desc' => '司机用户提现', // 企业付款操作说明信息。必填
  100. ]);
  101. $status = $result['return_code'] == 'SUCCESS' && $result['result_code'] != 'FAIL';
  102. if (!$status) {
  103. Db::rollback();
  104. var_dump($result);
  105. IResponse::failure('打款失败,请联系管理员或核对账户');
  106. }
  107. }
  108. Db::commit();
  109. return IResponse::success('审核成功');
  110. }
  111. /**
  112. * 删除
  113. *
  114. * @param $id
  115. * @return \think\response\Json
  116. * @author 许祖兴 < zuxing.xu@lettered.cn>
  117. * @date 2020/6/11 14:26
  118. *
  119. */
  120. public function delete($id)
  121. {
  122. model('common/UsersWithdraw')->deleteBy($id);
  123. return IResponse::success([], '删除提现申请单成功');
  124. }
  125. /**
  126. * 用户批量操作
  127. *
  128. * @return mixed
  129. * @author 许祖兴 < zuxing.xu@lettered.cn>
  130. * @date 2020/6/11 14:34
  131. *
  132. */
  133. public function plectron()
  134. {
  135. // 收参数
  136. $params = $this->request->param();
  137. foreach (str2arr($params['ids']) as $id) {
  138. $withdraw = model('common/UsersWithdraw')->getBy($id);
  139. if ($this->request->isDelete()) {
  140. $withdraw->deleteBy($id);
  141. } else {
  142. // 等待审核状态下才做更新
  143. if ($withdraw['status'] == 10) {
  144. Db::startTrans();
  145. try {
  146. if (!$withdraw->allowField(true)->updateBy($id, $params)) {
  147. Db::rollback();
  148. continue;
  149. }
  150. // 拒绝退款
  151. if ($params['status'] == 30) {
  152. // 查用户
  153. $user = model('common/Users')->getBy($withdraw['user_id']);
  154. if (empty($user)) {
  155. Db::rollback();
  156. continue;
  157. }
  158. // 写入资金记录
  159. $Users = new Users();
  160. $Users->changePartnership($user['id'], $withdraw['amount'], '资产余额提现失败退还', 20, true);
  161. } // 打款到账
  162. else if ($params['status'] == 20) {
  163. $wechat = sys_config('', 'wechat');
  164. $config = [
  165. // 前面的appid什么的也得保留哦
  166. 'app_id' => $wechat['mini_appid'],
  167. 'mch_id' => $wechat['pay_mch_id'],
  168. 'key' => $wechat['pay_secret_key'],
  169. // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
  170. 'cert_path' => $wechat['cert_path'], // XXX: 绝对路径!!!!
  171. 'key_path' => $wechat['key_path'], // XXX: 绝对路径!!!!
  172. ];
  173. $user = model('common/Users')->getBy($withdraw['user_id']);
  174. // 创建应用实例
  175. $app = Factory::payment($config);
  176. $result = $app->transfer->toBalance([
  177. 'partner_trade_no' => $withdraw['order_no'], // 商户订单号,需保持唯一性(只能是字母或者数字,不能包含有符号)
  178. 'openid' => $user['open_id'],
  179. 'check_name' => 'FORCE_CHECK', // NO_CHECK:不校验真实姓名, FORCE_CHECK:强校验真实姓名
  180. 're_user_name' => $withdraw['real_name'], // 如果 check_name 设置为FORCE_CHECK,则必填用户真实姓名
  181. 'amount' => $withdraw['amount'] * 100, // 企业付款金额,单位为分
  182. 'desc' => '司机用户提现', // 企业付款操作说明信息。必填
  183. ]);
  184. $result = $result['return_code'] == 'SUCCESS' && $result['result_code'] != 'FAIL';
  185. if (!$result) {
  186. Db::rollback();
  187. continue;
  188. }
  189. }
  190. } catch (\Exception $exception) {
  191. Db::rollback();
  192. }
  193. }
  194. }
  195. }
  196. return IResponse::success([], '操作成功');
  197. }
  198. }