Withdraw.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace app\admin\controller\shopro;
  3. use think\Db;
  4. use think\exception\HttpResponseException;
  5. use addons\shopro\exception\ShoproException;
  6. use app\admin\model\shopro\Withdraw as WithdrawModel;
  7. use app\admin\model\shopro\WithdrawLog as WithdrawLogModel;
  8. use app\admin\model\shopro\user\User as UserModel;
  9. use app\admin\model\Admin as AdminModel;
  10. use addons\shopro\service\Withdraw as WithdrawLibrary;
  11. use addons\shopro\library\Operator;
  12. /**
  13. * 提现
  14. */
  15. class Withdraw extends Common
  16. {
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new WithdrawModel;
  21. $this->logModel = new WithdrawLogModel;
  22. }
  23. /**
  24. * 提现列表
  25. */
  26. public function index()
  27. {
  28. if (!$this->request->isAjax()) {
  29. return $this->view->fetch();
  30. }
  31. $withdraws = $this->model->sheepFilter()->with(['user'])->paginate($this->request->param('list_rows', 10));
  32. $this->success('获取成功', null, $withdraws);
  33. }
  34. /**
  35. * 提现日志
  36. */
  37. public function log($id)
  38. {
  39. if (!$this->request->isAjax()) {
  40. return $this->view->fetch();
  41. }
  42. $logs = $this->logModel->where('withdraw_id', $id)->order('id desc')->select();
  43. $morphs = [
  44. 'user' => UserModel::class,
  45. 'admin' => AdminModel::class,
  46. 'system' => AdminModel::class
  47. ];
  48. $logs = morph_to($logs, $morphs, ['oper_type', 'oper_id']);
  49. $logs = $logs->toArray();
  50. // 解析操作人信息
  51. foreach ($logs as &$log) {
  52. $log['oper'] = Operator::info($log['oper_type'], $log['oper'] ?? null);
  53. }
  54. $this->success('获取成功', null, $logs);
  55. }
  56. public function handle($id)
  57. {
  58. if (!$this->request->isAjax()) {
  59. return $this->view->fetch();
  60. }
  61. $params = $this->request->param();
  62. $action = $params['action'] ?? null;
  63. $refuse_msg = $params['refuse_msg'] ?? '';
  64. if ($action == 'refuse' && !$refuse_msg) {
  65. $this->error('请输入拒绝原因');
  66. }
  67. $ids = is_array($id) ? $id : explode(',', $id);
  68. foreach ($ids as $key => $id) {
  69. Db::startTrans();
  70. try {
  71. $withdraw = $this->model->lock(true)->where('id', $id)->find();
  72. if (!$withdraw) {
  73. $this->error(__('No Results were found'));
  74. }
  75. $withdrawLib = new WithdrawLibrary($withdraw->user_id);
  76. switch ($action) {
  77. case 'agree':
  78. $withdraw = $withdrawLib->handleAgree($withdraw);
  79. break;
  80. case 'agree&withdraw':
  81. $withdraw = $withdrawLib->handleAgree($withdraw);
  82. $withdraw = $withdrawLib->handleWithdraw($withdraw);
  83. break;
  84. case 'withdraw':
  85. $withdraw = $withdrawLib->handleWithdraw($withdraw);
  86. break;
  87. case 'refuse':
  88. $withdraw = $withdrawLib->handleRefuse($withdraw, $refuse_msg);
  89. break;
  90. }
  91. Db::commit();
  92. } catch (ShoproException $e) {
  93. Db::commit(); // 不回滚,记录错误日志
  94. $this->error($e->getMessage());
  95. } catch (HttpResponseException $e) {
  96. $data = $e->getResponse()->getData();
  97. $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
  98. $this->error($message);
  99. } catch (\Exception $e) {
  100. Db::rollback();
  101. $this->error($e->getMessage());
  102. }
  103. }
  104. $this->success('处理成功');
  105. }
  106. }