123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- <?php
- namespace app\api\controller;
- use think\Db;
- use think\exception\HttpResponseException;
- use app\common\model\Withdraw as WithdrawModel;
- use app\common\Service\Withdraw as WithdrawService;
- class Withdraw extends Base
- {
- protected $noNeedLogin = [];
- protected $noNeedRight = ['*'];
- public function index()
- {
- $params = $this->request->param();
-
- // 验证分页参数
- $this->validate($params, 'Withdraw.index');
-
- $user = auth_user();
- $withdraws = WithdrawModel::where(['user_id' => $user->id])
- ->order('id desc')
- ->paginate($this->request->param('page_size', 10))->each(function ($withdraw) {
- $withdraw->hidden(['withdraw_info']);
- });
- $this->success('获取成功', $withdraws);
- }
- // 提现规则
- public function rules()
- {
- $user = auth_user();
- $config = (new WithdrawService($user))->config;
- $this->success('提现规则', $config);
- }
- // 发起提现请求
- public function apply()
- {
- $user = auth_user();
- $params = $this->request->param();
- $this->validate($params, 'Withdraw.apply');
- $withdrawService = new WithdrawService($user);
- // 申请提现
- Db::startTrans();
- try {
- $withdraw = $withdrawService->apply($params);
- Db::commit();
- } catch (HttpResponseException $e) {
- $data = $e->getResponse()->getData();
- $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
- $this->error($message);
- } catch (\Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- // 微信新版本商家转账
- if ($withdraw->withdraw_type === 'wechat') {
- try {
- $transferData = $withdrawService->handleWechatTransfer($withdraw);
- } catch (HttpResponseException $e) {
- $data = $e->getResponse()->getData();
- $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
- $this->error($message);
- } catch (\Exception $e) {
- $this->error($e->getMessage());
- }
- }
- // 支付宝提现+自动打款
- if ($withdraw->withdraw_type === 'alipay' && $withdrawService->config['auto_arrival']) {
- try {
- // 记录提现日志
- $withdrawService->handleAlipayWithdraw($withdraw);
- } catch (HttpResponseException $e) {
- $data = $e->getResponse()->getData();
- $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
- $this->error($message);
- } catch (\Exception $e) {
- $this->error($e->getMessage());
- }
- }
- $this->success('申请成功', [
- 'id' => $withdraw->id,
- 'type' => $withdraw->withdraw_type,
- 'withdraw_sn' => $withdraw->withdraw_sn,
- 'transfer_data' => $transferData ?? null,
- ]);
- }
- // 继续提现(仅支持微信商家转账)
- public function transfer()
- {
- $user = auth_user();
- $params = $this->request->param();
- $this->validate($params, 'Withdraw.transfer');
- $withdrawService = new WithdrawService($user);
- // 如果 微信提现, result 为 package_info,其他的为 withdrawModel 对象
- $result = $withdrawService->retry($params);
- $this->success('操作成功', $result);
- }
- // 取消提现(仅支持微信商家转账)
- public function cancel()
- {
- $user = auth_user();
- $params = $this->request->param();
- $this->validate($params, 'Withdraw.cancel');
- $withdrawService = new WithdrawService($user);
- try {
- $result = $withdrawService->cancel($params);
- } catch (\Exception $e) {
- $this->error($e->getMessage());
- }
- $this->success('操作成功', $result);
- }
- // 取消提现(仅支持微信商家转账)
- public function retry()
- {
- $user = auth_user();
- $params = $this->request->param();
- $this->validate($params, 'Withdraw.retry');
- $withdrawService = new WithdrawService($user);
- try {
- $withdraw = $withdrawService->retry($params);
- $transferData = $withdrawService->handleWechatTransfer($withdraw);
- } catch (\Exception $e) {
- $this->error($e->getMessage());
- }
- $this->success('申请成功', [
- 'id' => $withdraw->id,
- 'type' => $withdraw->withdraw_type,
- 'withdraw_sn' => $withdraw->withdraw_sn,
- 'transfer_data' => $transferData ?? null,
- ]);
- }
- }
|