TaxiUserWithdraw.php 9.4 KB

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