123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- <?php
- namespace app\admin\controller\withdraw;
- use app\common\controller\Backend;
- use app\common\model\Withdraw as WithdrawModel;
- use app\common\model\WithdrawLog as WithdrawLogModel;
- use app\common\model\User;
- use app\admin\model\Admin as AdminModel;
- use app\common\Service\Withdraw as WithdrawService;
- use app\common\Enum\WithdrawEnum;
- use app\common\library\Operator;
- use think\Db;
- use think\exception\HttpResponseException;
- use app\common\exception\BusinessException;
- /**
- * 提现管理
- *
- * @icon fa fa-money
- */
- class Withdraw extends Backend
- {
- /**
- * Withdraw模型对象
- * @var \app\common\model\Withdraw
- */
- protected $model = null;
- protected $logModel = null;
- protected $noNeedRight = ['checkWechatTransferResult'];
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new WithdrawModel;
- $this->logModel = new WithdrawLogModel;
-
- // 只显示需要的状态
- $statusList = [
- '0' => '待审核',
- '1' => '处理中',
- '2' => '已处理',
- '-1' => '已拒绝'
- ];
- $this->view->assign("statusList", $statusList);
- $this->view->assign("typeList", WithdrawEnum::getTypeList());
- $this->view->assign("wechatTransferStateList", WithdrawEnum::getWechatTransferStateList());
- }
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法
- * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
- /**
- * 查看
- */
- public function index()
- {
- //当前是否为关联查询
- $this->relationSearch = true;
- //设置过滤方法
- $this->request->filter(['strip_tags', 'trim']);
- if ($this->request->isAjax()) {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
-
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
-
- $total = $this->model
- ->with(['user'])
- ->where($where)
- ->order($sort, $order)
- ->count();
- $list = $this->model
- ->with(['user'])
- ->where($where)
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
- // 处理数据显示
- foreach ($list as $row) {
- $row->status_text = WithdrawEnum::getStatusName($row->status);
- // $row->withdraw_type_text = WithdrawEnum::getTypeName($row->withdraw_type);
- $row->wechat_transfer_state_text = WithdrawEnum::getWechatTransferStateName($row->wechat_transfer_state);
- // 安全处理手续费率格式化
- $chargeRate = $row->charge_rate;
- if (is_numeric($chargeRate) && $chargeRate !== null && $chargeRate !== '') {
- $row->charge_rate_format = bcmul((string)$chargeRate, '100', 1) . '%';
- } else {
- $row->charge_rate_format = '0%';
- }
-
- // 处理用户信息
- if ($row->user) {
- $row->user->nickname = $row->user->nickname ?: '未知用户';
- }
- }
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- return $this->view->fetch();
- }
- /**
- * 处理提现申请
- */
- public function handle($ids = null)
- {
- if ($this->request->isPost()) {
- $params = $this->request->param();
- $action = $params['action'] ?? null;
-
- if (!in_array($action, ['agree', 'withdraw', 'agree_and_withdraw', 'refuse', 'refund_commission', 'cancel'])) {
- $this->error('您的操作有误');
- }
- $refuse_msg = $params['refuse_msg'] ?? '';
- if ($action === 'refuse' && !$refuse_msg) {
- $this->error('请输入拒绝原因');
- }
- $ids = is_array($ids) ? $ids : explode(',', $ids);
-
- foreach ($ids as $id) {
- Db::startTrans();
- try {
- $withdraw = $this->model->lock(true)->where('id', $id)->find();
- if (!$withdraw) {
- throw new BusinessException(__('No Results were found'));
- }
-
- $withdrawService = new WithdrawService($withdraw->user_id);
- switch ($action) {
- case 'agree':
- $withdraw = $withdrawService->handleAgree($withdraw);
- break;
- case 'withdraw':
- $withdraw = $withdrawService->handleWithdraw($withdraw);
- break;
- case 'agree_and_withdraw':
- $withdraw = $withdrawService->handleAgree($withdraw);
- $withdraw = $withdrawService->handleWithdraw($withdraw);
- break;
- case 'refuse':
- $withdraw = $withdrawService->handleRefuse($withdraw, $refuse_msg);
- break;
- case 'refund_commission':
- $withdraw = $withdrawService->handleFail($withdraw, '管理员退回佣金', -3);
- break;
- case 'cancel':
- $withdraw = $withdrawService->handleFail($withdraw, '管理员撤销提现', -3);
- break;
- }
- Db::commit();
- } catch (BusinessException $e) {
- // 不回滚,记录错误日志
- Db::commit();
- $this->error($e->getMessage());
- } 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());
- }
- }
- $this->success('处理成功');
- }
-
- // GET请求 - 显示处理表单(用于拒绝操作)
- $withdraw = $this->model->get($ids);
- if (!$withdraw) {
- $this->error(__('No Results were found'));
- }
-
- $this->view->assign("withdraw", $withdraw);
- return $this->view->fetch();
- }
- /**
- * 查看提现日志
- */
- public function log($ids = null)
- {
- $withdraw = $this->model->get($ids);
- if (!$withdraw) {
- $this->error(__('No Results were found'));
- }
- // 直接获取日志数据,不使用 Ajax
- $logs = $this->logModel->where('withdraw_id', $ids)->order('id desc')->select();
-
- // 处理操作人信息
- foreach ($logs as $log) {
- switch ($log->oper_type) {
- case 'user':
- $oper = User::get($log->oper_id);
- $log->oper_info = [
- 'type' => 'user',
- 'id' => $log->oper_id,
- 'name' => $oper ? $oper->nickname : '未知用户',
- 'avatar' => $oper ? $oper->avatar : ''
- ];
- break;
- case 'admin':
- case 'system':
- $oper = AdminModel::get($log->oper_id);
- $log->oper_info = [
- 'type' => $log->oper_type,
- 'id' => $log->oper_id,
- 'name' => $oper ? $oper->username : '系统',
- 'avatar' => $oper ? $oper->avatar : ''
- ];
- break;
- default:
- $log->oper_info = [
- 'type' => 'system',
- 'id' => 0,
- 'name' => '系统',
- 'avatar' => ''
- ];
- break;
- }
- }
-
- $this->view->assign("withdraw", $withdraw);
- $this->view->assign("logs", $logs);
- return $this->view->fetch();
- }
- /**
- * 检查微信转账结果
- */
- public function checkWechatTransferResult($id)
- {
- $withdraw = $this->model->lock(true)->where('id', $id)->find();
- if (!$withdraw) {
- $this->error(__('No Results were found'));
- }
- try {
- $withdrawService = new WithdrawService($withdraw->user_id);
- $withdrawService->checkWechatTransferResult($withdraw);
- $this->success('检查成功');
- } catch (\Exception $e) {
- $this->error($e->getMessage());
- }
- }
- /**
- * 批量删除
- */
- public function del($ids = "")
- {
- $this->error('提现记录不允许删除');
- }
- /**
- * 添加
- */
- public function add()
- {
- $this->error('提现记录不允许手动添加');
- }
- /**
- * 编辑
- */
- public function edit($ids = null)
- {
- $this->error('提现记录不允许编辑');
- }
- }
|