Withdraw.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. namespace app\admin\controller\withdraw;
  3. use app\common\controller\Backend;
  4. use app\common\model\Withdraw as WithdrawModel;
  5. use app\common\model\WithdrawLog as WithdrawLogModel;
  6. use app\common\model\User;
  7. use app\admin\model\Admin as AdminModel;
  8. use app\common\Service\Withdraw as WithdrawService;
  9. use app\common\Enum\WithdrawEnum;
  10. use app\common\library\Operator;
  11. use think\Db;
  12. use think\exception\HttpResponseException;
  13. use app\common\exception\BusinessException;
  14. /**
  15. * 提现管理
  16. *
  17. * @icon fa fa-money
  18. */
  19. class Withdraw extends Backend
  20. {
  21. /**
  22. * Withdraw模型对象
  23. * @var \app\common\model\Withdraw
  24. */
  25. protected $model = null;
  26. protected $logModel = null;
  27. protected $noNeedRight = ['checkWechatTransferResult'];
  28. public function _initialize()
  29. {
  30. parent::_initialize();
  31. $this->model = new WithdrawModel;
  32. $this->logModel = new WithdrawLogModel;
  33. // 只显示需要的状态
  34. $statusList = [
  35. '0' => '待审核',
  36. '1' => '处理中',
  37. '2' => '已处理',
  38. '-1' => '已拒绝'
  39. ];
  40. $this->view->assign("statusList", $statusList);
  41. $this->view->assign("typeList", WithdrawEnum::getTypeList());
  42. $this->view->assign("wechatTransferStateList", WithdrawEnum::getWechatTransferStateList());
  43. }
  44. /**
  45. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法
  46. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  47. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  48. */
  49. /**
  50. * 查看
  51. */
  52. public function index()
  53. {
  54. //当前是否为关联查询
  55. $this->relationSearch = true;
  56. //设置过滤方法
  57. $this->request->filter(['strip_tags', 'trim']);
  58. if ($this->request->isAjax()) {
  59. //如果发送的来源是Selectpage,则转发到Selectpage
  60. if ($this->request->request('keyField')) {
  61. return $this->selectpage();
  62. }
  63. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  64. $total = $this->model
  65. ->with(['user'])
  66. ->where($where)
  67. ->order($sort, $order)
  68. ->count();
  69. $list = $this->model
  70. ->with(['user'])
  71. ->where($where)
  72. ->order($sort, $order)
  73. ->limit($offset, $limit)
  74. ->select();
  75. // 处理数据显示
  76. foreach ($list as $row) {
  77. $row->status_text = WithdrawEnum::getStatusName($row->status);
  78. // $row->withdraw_type_text = WithdrawEnum::getTypeName($row->withdraw_type);
  79. $row->wechat_transfer_state_text = WithdrawEnum::getWechatTransferStateName($row->wechat_transfer_state);
  80. // 安全处理手续费率格式化
  81. $chargeRate = $row->charge_rate;
  82. if (is_numeric($chargeRate) && $chargeRate !== null && $chargeRate !== '') {
  83. $row->charge_rate_format = bcmul((string)$chargeRate, '100', 1) . '%';
  84. } else {
  85. $row->charge_rate_format = '0%';
  86. }
  87. // 处理用户信息
  88. if ($row->user) {
  89. $row->user->nickname = $row->user->nickname ?: '未知用户';
  90. }
  91. }
  92. $result = array("total" => $total, "rows" => $list);
  93. return json($result);
  94. }
  95. return $this->view->fetch();
  96. }
  97. /**
  98. * 处理提现申请
  99. */
  100. public function handle($ids = null)
  101. {
  102. if ($this->request->isPost()) {
  103. $params = $this->request->param();
  104. $action = $params['action'] ?? null;
  105. if (!in_array($action, ['agree', 'withdraw', 'agree_and_withdraw', 'refuse', 'refund_commission', 'cancel'])) {
  106. $this->error('您的操作有误');
  107. }
  108. $refuse_msg = $params['refuse_msg'] ?? '';
  109. if ($action === 'refuse' && !$refuse_msg) {
  110. $this->error('请输入拒绝原因');
  111. }
  112. $ids = is_array($ids) ? $ids : explode(',', $ids);
  113. foreach ($ids as $id) {
  114. Db::startTrans();
  115. try {
  116. $withdraw = $this->model->lock(true)->where('id', $id)->find();
  117. if (!$withdraw) {
  118. throw new BusinessException(__('No Results were found'));
  119. }
  120. $withdrawService = new WithdrawService($withdraw->user_id);
  121. switch ($action) {
  122. case 'agree':
  123. $withdraw = $withdrawService->handleAgree($withdraw);
  124. break;
  125. case 'withdraw':
  126. $withdraw = $withdrawService->handleWithdraw($withdraw);
  127. break;
  128. case 'agree_and_withdraw':
  129. $withdraw = $withdrawService->handleAgree($withdraw);
  130. $withdraw = $withdrawService->handleWithdraw($withdraw);
  131. break;
  132. case 'refuse':
  133. $withdraw = $withdrawService->handleRefuse($withdraw, $refuse_msg);
  134. break;
  135. case 'refund_commission':
  136. $withdraw = $withdrawService->handleFail($withdraw, '管理员退回佣金', -3);
  137. break;
  138. case 'cancel':
  139. $withdraw = $withdrawService->handleFail($withdraw, '管理员撤销提现', -3);
  140. break;
  141. }
  142. Db::commit();
  143. } catch (BusinessException $e) {
  144. // 不回滚,记录错误日志
  145. Db::commit();
  146. $this->error($e->getMessage());
  147. } catch (HttpResponseException $e) {
  148. $data = $e->getResponse()->getData();
  149. $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
  150. $this->error($message);
  151. } catch (\Exception $e) {
  152. Db::rollback();
  153. $this->error($e->getMessage());
  154. }
  155. }
  156. $this->success('处理成功');
  157. }
  158. // GET请求 - 显示处理表单(用于拒绝操作)
  159. $withdraw = $this->model->get($ids);
  160. if (!$withdraw) {
  161. $this->error(__('No Results were found'));
  162. }
  163. $this->view->assign("withdraw", $withdraw);
  164. return $this->view->fetch();
  165. }
  166. /**
  167. * 查看提现日志
  168. */
  169. public function log($ids = null)
  170. {
  171. $withdraw = $this->model->get($ids);
  172. if (!$withdraw) {
  173. $this->error(__('No Results were found'));
  174. }
  175. // 直接获取日志数据,不使用 Ajax
  176. $logs = $this->logModel->where('withdraw_id', $ids)->order('id desc')->select();
  177. // 处理操作人信息
  178. foreach ($logs as $log) {
  179. switch ($log->oper_type) {
  180. case 'user':
  181. $oper = User::get($log->oper_id);
  182. $log->oper_info = [
  183. 'type' => 'user',
  184. 'id' => $log->oper_id,
  185. 'name' => $oper ? $oper->nickname : '未知用户',
  186. 'avatar' => $oper ? $oper->avatar : ''
  187. ];
  188. break;
  189. case 'admin':
  190. case 'system':
  191. $oper = AdminModel::get($log->oper_id);
  192. $log->oper_info = [
  193. 'type' => $log->oper_type,
  194. 'id' => $log->oper_id,
  195. 'name' => $oper ? $oper->username : '系统',
  196. 'avatar' => $oper ? $oper->avatar : ''
  197. ];
  198. break;
  199. default:
  200. $log->oper_info = [
  201. 'type' => 'system',
  202. 'id' => 0,
  203. 'name' => '系统',
  204. 'avatar' => ''
  205. ];
  206. break;
  207. }
  208. }
  209. $this->view->assign("withdraw", $withdraw);
  210. $this->view->assign("logs", $logs);
  211. return $this->view->fetch();
  212. }
  213. /**
  214. * 检查微信转账结果
  215. */
  216. public function checkWechatTransferResult($id)
  217. {
  218. $withdraw = $this->model->lock(true)->where('id', $id)->find();
  219. if (!$withdraw) {
  220. $this->error(__('No Results were found'));
  221. }
  222. try {
  223. $withdrawService = new WithdrawService($withdraw->user_id);
  224. $withdrawService->checkWechatTransferResult($withdraw);
  225. $this->success('检查成功');
  226. } catch (\Exception $e) {
  227. $this->error($e->getMessage());
  228. }
  229. }
  230. /**
  231. * 批量删除
  232. */
  233. public function del($ids = "")
  234. {
  235. $this->error('提现记录不允许删除');
  236. }
  237. /**
  238. * 添加
  239. */
  240. public function add()
  241. {
  242. $this->error('提现记录不允许手动添加');
  243. }
  244. /**
  245. * 编辑
  246. */
  247. public function edit($ids = null)
  248. {
  249. $this->error('提现记录不允许编辑');
  250. }
  251. }