TaxiUserWithdraw.php 9.3 KB

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