TaxiUserWithdraw.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. $result = $result['return_code'] == 'SUCCESS' && $result['result_code'] != 'FAIL';
  102. if (!$result) {
  103. Db::rollback();
  104. IResponse::failure('打款失败,请联系管理员或核对账户');
  105. }
  106. }
  107. Db::commit();
  108. return IResponse::success('审核成功');
  109. }
  110. /**
  111. * 删除
  112. *
  113. * @param $id
  114. * @return \think\response\Json
  115. * @author 许祖兴 < zuxing.xu@lettered.cn>
  116. * @date 2020/6/11 14:26
  117. *
  118. */
  119. public function delete($id)
  120. {
  121. model('common/UsersWithdraw')->deleteBy($id);
  122. return IResponse::success([], '删除提现申请单成功');
  123. }
  124. /**
  125. * 用户批量操作
  126. *
  127. * @return mixed
  128. * @author 许祖兴 < zuxing.xu@lettered.cn>
  129. * @date 2020/6/11 14:34
  130. *
  131. */
  132. public function plectron()
  133. {
  134. // 收参数
  135. $params = $this->request->param();
  136. foreach (str2arr($params['ids']) as $id) {
  137. $withdraw = model('common/UsersWithdraw')->getBy($id);
  138. if ($this->request->isDelete()) {
  139. $withdraw->deleteBy($id);
  140. } else {
  141. // 等待审核状态下才做更新
  142. if ($withdraw['status'] == 10) {
  143. Db::startTrans();
  144. try {
  145. if (!$withdraw->allowField(true)->updateBy($id, $params)) {
  146. Db::rollback();
  147. continue;
  148. }
  149. // 拒绝退款
  150. if ($params['status'] == 30) {
  151. // 查用户
  152. $user = model('common/Users')->getBy($withdraw['user_id']);
  153. if (empty($user)) {
  154. Db::rollback();
  155. continue;
  156. }
  157. // 写入资金记录
  158. $Users = new Users();
  159. $Users->changePartnership($user['id'], $withdraw['amount'], '资产余额提现失败退还', 20, true);
  160. } // 打款到账
  161. else if ($params['status'] == 20) {
  162. $wechat = sys_config('', 'wechat');
  163. $config = [
  164. // 前面的appid什么的也得保留哦
  165. 'app_id' => $wechat['mini_appid'],
  166. 'mch_id' => $wechat['pay_mch_id'],
  167. 'key' => $wechat['pay_secret_key'],
  168. // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
  169. 'cert_path' => $wechat['cert_path'], // XXX: 绝对路径!!!!
  170. 'key_path' => $wechat['key_path'], // XXX: 绝对路径!!!!
  171. ];
  172. $user = model('common/Users')->getBy($withdraw['user_id']);
  173. // 创建应用实例
  174. $app = Factory::payment($config);
  175. $result = $app->transfer->toBalance([
  176. 'partner_trade_no' => $withdraw['order_no'], // 商户订单号,需保持唯一性(只能是字母或者数字,不能包含有符号)
  177. 'openid' => $user['open_id'],
  178. 'check_name' => 'FORCE_CHECK', // NO_CHECK:不校验真实姓名, FORCE_CHECK:强校验真实姓名
  179. 're_user_name' => $withdraw['real_name'], // 如果 check_name 设置为FORCE_CHECK,则必填用户真实姓名
  180. 'amount' => $withdraw['amount'] * 100, // 企业付款金额,单位为分
  181. 'desc' => '司机用户提现', // 企业付款操作说明信息。必填
  182. ]);
  183. $result = $result['return_code'] == 'SUCCESS' && $result['result_code'] != 'FAIL';
  184. if (!$result) {
  185. Db::rollback();
  186. continue;
  187. }
  188. }
  189. } catch (\Exception $exception) {
  190. Db::rollback();
  191. }
  192. }
  193. }
  194. }
  195. return IResponse::success([], '操作成功');
  196. }
  197. }