允许流转到的目标状态 * reject/done 为终态,不允许再变更 * @var array */ protected $statusFlow = [ 'wait' => ['pass', 'reject'], 'pass' => ['done', 'reject'], ]; public function _initialize() { parent::_initialize(); $this->model = new \app\admin\model\Withdraw; $this->view->assign("channelList", $this->model->getChannelList()); $this->view->assign("statusList", $this->model->getStatusList()); } /** * 提现列表:关联师傅信息,展示联系方式与处理时间 */ public function index() { $this->request->filter(['strip_tags', 'trim']); if (false === $this->request->isAjax()) { return $this->view->fetch(); } if ($this->request->request('keyField')) { return $this->selectpage(); } [$where, $sort, $order, $offset, $limit] = $this->buildparams(); $list = $this->model ->with(['master']) ->where($where) ->order($sort, $order) ->paginate($limit); $result = ['total' => $list->total(), 'rows' => $list->items()]; return json($result); } /** * 财务审核:申请信息只读,仅允许合法状态流转并做资金联动 * - 驳回:退回师傅余额并解冻,写入钱包流水,通知师傅 * - 通过:仅标记审核通过,等待财务打款 * - 打款完成:划出冻结资金,写入钱包流水,通知师傅 */ public function edit($ids = null) { $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } if (false === $this->request->isPost()) { // 展示师傅当前钱包情况,辅助财务判断 $master = Db::name('master')->where('id', (int)$row['master_id'])->field('id,realname,mobile,user_id,money,frozen_money')->find(); $this->view->assign('row', $row); $this->view->assign('master', $master ?: []); $this->view->assign('flowOptions', $this->statusFlow[$row['status']] ?? []); return $this->view->fetch(); } $status = (string)$this->request->post('row.status'); $auditMsg = trim((string)$this->request->post('row.audit_msg')); // 状态机校验:终态禁止操作,流转目标必须合法 if (!isset($this->statusFlow[$row['status']])) { $this->error('该提现单已处于终态(已驳回/已打款),不允许再次审核'); } if (!in_array($status, $this->statusFlow[$row['status']], true)) { $this->error('状态流转不合法,请刷新后重试'); } if ($status === 'reject' && $auditMsg === '') { $this->error('驳回时必须填写驳回原因'); } $masterId = (int)$row['master_id']; $amount = round((float)$row['amount'], 2); Db::startTrans(); try { // 乐观锁更新状态,防止并发重复审核 $affected = Db::name('withdraw') ->where('id', $row['id']) ->where('status', $row['status']) ->update([ 'status' => $status, 'audit_msg' => $auditMsg, 'updatetime' => time(), ]); if (!$affected) { throw new \Exception('提现单状态已被其他人变更,请刷新后重试'); } $master = Db::name('master')->where('id', $masterId)->lock(true)->find(); if (!$master) { throw new \Exception('提现师傅不存在'); } if ($status === 'reject') { // 驳回:解冻并退回余额,写入钱包流水 $newMoney = round((float)$master['money'] + $amount, 2); Db::name('master')->where('id', $masterId)->update([ 'money' => $newMoney, 'frozen_money' => round((float)$master['frozen_money'] - $amount, 2), 'updatetime' => time(), ]); Db::name('wallet_log')->insert([ 'user_type' => 'master', 'user_id' => $masterId, 'type' => 'refund', 'amount' => $amount, 'balance' => $newMoney, 'order_id' => 0, 'remark' => '提现驳回,申请金额退回钱包余额' . ($auditMsg !== '' ? '(原因:' . $auditMsg . ')' : ''), 'createtime' => time(), ]); $this->sendMasterMessage((int)$master['user_id'], '提现申请已驳回', '您的提现申请 ¥' . $amount . ' 未通过财务审核:' . ($auditMsg !== '' ? $auditMsg : '无') . ',申请金额已退回钱包余额'); } elseif ($status === 'pass') { // 审核通过:仅标记,资金保持冻结,等待财务打款 $this->sendMasterMessage((int)$master['user_id'], '提现审核通过', '您的提现申请 ¥' . $amount . ' 已通过审核,财务将尽快安排打款到 ' . ($row['channel'] === 'bank' ? '银行卡' : '微信钱包')); } elseif ($status === 'done') { // 打款完成:冻结资金划出 Db::name('master')->where('id', $masterId)->update([ 'frozen_money' => round((float)$master['frozen_money'] - $amount, 2), 'updatetime' => time(), ]); Db::name('wallet_log')->insert([ 'user_type' => 'master', 'user_id' => $masterId, 'type' => 'withdraw', 'amount' => 0, 'balance' => (float)$master['money'], 'order_id' => 0, 'remark' => '提现打款完成,冻结资金 ¥' . $amount . ' 划出(' . ($row['channel'] === 'bank' ? '银行储蓄卡' : '微信钱包') . ' ' . $row['account'] . ')', 'createtime' => time(), ]); $this->sendMasterMessage((int)$master['user_id'], '提现打款完成', '您的提现 ¥' . $amount . ' 已打款成功,请留意到账通知'); } Db::commit(); } catch (PDOException|\Exception $e) { Db::rollback(); $this->error($e->getMessage()); } $this->success(); } /** * 提现单仅由师傅端发起,禁止后台手工添加 */ public function add() { $this->error('提现申请仅支持师傅端发起,后台不可手动添加'); } /** * 提现流水记录禁止删除,保障资金对账完整 */ public function del($ids = null) { $this->error('提现申请记录禁止删除'); } /** * 禁止批量修改状态,状态变更必须走审核流程以触发资金联动 */ public function multi($ids = null) { $this->error('提现审核不支持批量操作,请逐单审核处理'); } /** * 向师傅所属用户发送站内消息 * @param int $userId 用户ID * @param string $title 消息标题 * @param string $content 消息内容 */ protected function sendMasterMessage($userId, $title, $content) { if (!$userId) { return; } Db::name('message')->insert([ 'receive_type' => 'user', 'receive_id' => $userId, 'type' => 'system', 'title' => mb_substr((string)$title, 0, 100), 'content' => mb_substr((string)$content, 0, 500), 'order_id' => 0, 'is_read' => 0, 'createtime' => time(), ]); } }