fa_master) */ class MasterBase extends Api { protected $noNeedLogin = []; protected $noNeedRight = '*'; /** @var MasterAuth */ protected $masterAuth = null; /** @var array|null 当前登录师傅行记录 */ protected $master = null; protected function _initialize() { check_cors_request(); check_ip_allowed(); $this->request->filter('trim,strip_tags,htmlspecialchars'); $this->masterAuth = MasterAuth::instance(); $controllername = Loader::parseName($this->request->controller()); $actionname = strtolower($this->request->action()); $token = $this->request->server('HTTP_TOKEN', $this->request->request('token', \think\Cookie::get('master_token'))); $needLogin = true; $arr = array_map('strtolower', (array)$this->noNeedLogin); if (in_array('*', $arr, true) || in_array($actionname, $arr, true)) { $needLogin = false; } if ($needLogin) { $this->masterAuth->init($token); if (!$this->masterAuth->isLogin()) { $this->error(__('Please login first'), null, 401); } } elseif ($token) { $this->masterAuth->init($token); } $this->master = $this->masterAuth->getMaster(); $upload = \app\common\model\Config::upload(); \think\Hook::listen('upload_config_init', $upload); Config::set('upload', array_merge(Config::get('upload'), $upload)); $this->loadlang($controllername); } /** * 当前登录师傅 ID */ protected function masterId() { return (int)$this->masterAuth->id(); } /** * 获取当前登录师傅完整档案 */ protected function masterInfo($reload = false) { if ($this->masterId() <= 0) { return false; } if ($this->master === null || $reload) { $this->master = $this->masterAuth->reload(); } return $this->master ?: false; } /** * 要求师傅已提交过实名入驻资料(非 draft 草稿态) */ protected function requireMasterProfile() { $master = $this->masterInfo(); if (!$master || ($master['verify_status'] ?? 'draft') === 'draft') { $this->error('请先完成师傅入驻', ['need_apply' => 1], 2001); } return $master; } /** * 要求师傅已通过实名认证,供接单/报价/提现等敏感操作使用 */ protected function requireMaster() { $master = $this->requireMasterProfile(); if ($master['verify_status'] === 'pending') { $this->error('师傅资质审核中,请耐心等待', ['verify_status' => 'pending'], 2002); } if ($master['verify_status'] === 'reject') { $this->error('资质未通过:' . $master['audit_msg'], ['verify_status' => 'reject'], 2003); } if ($master['verify_status'] !== 'pass') { $this->error('师傅资质尚未审核通过,无法接单'); } return $master; } /** * 校验支付密码(用于提现) */ protected function requirePaymentPassword() { try { MasterSecurity::verify($this->masterId(), 'payment', (string)$this->request->post('payment_password', '')); } catch (\Throwable $e) { $this->error($e->getMessage()); } } /** * 验证新头像属于当前师傅账号,且符合大小与格式要求 */ protected function validateAvatar($url) { $attachment = Db::name('attachment') ->where('master_id', $this->masterId()) ->where('category', 'avatar') ->where('url', $url) ->find(); if ( !$attachment || (int)$attachment['filesize'] > 10 * 1024 * 1024 || !in_array(strtolower((string)$attachment['mimetype']), ['image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/bmp'], true) ) { $this->error('头像无效,请重新上传不超过10MB的图片'); } } /** * 取本师傅订单(越权拦截) */ protected function ownOrder($orderId) { $orderId = (int)$orderId; if ($orderId <= 0) { $this->error('订单参数错误'); } $order = Db::name('order')->where('id', $orderId)->find(); if (!$order) { $this->error('订单不存在'); } $master = $this->requireMaster(); if ((int)$order['master_id'] !== (int)$master['id']) { $this->error('无权操作该订单', null, 403); } return $order; } /** * 订单状态流转:校验 -> 乐观锁更新 -> 写流水 */ protected function transit(array $order, $to, $remark = '', $extra = [], $operatorType = 'master', $operatorId = 0) { $from = (int)$order['status']; if (!OrderStatus::can($from, $to)) { $this->error('订单状态「' . OrderStatus::label($from) . '」不允许变更为「' . OrderStatus::label($to) . '」'); } $data = array_merge($extra, [ 'status' => (int)$to, 'updatetime' => time(), ]); $updated = Db::name('order')->where('id', (int)$order['id'])->where('status', $from)->update($data); if (!$updated) { $this->error('订单状态已变更,请刷新后重试'); } $this->addLog((int)$order['id'], (int)$to, $remark, $operatorType, $operatorId ?: $this->masterId()); return array_merge($order, $data); } protected function addLog($orderId, $status, $remark = '', $operatorType = 'master', $operatorId = 0) { Db::name('order_log')->insert([ 'order_id' => (int)$orderId, 'status' => (int)$status, 'remark' => mb_substr((string)$remark, 0, 255), 'operator_type' => $operatorType, 'operator_id' => (int)$operatorId, 'createtime' => time(), ]); } protected function notify($receiveType, $receiveId, $title, $content, $type = 'order', $orderId = 0) { if (!$receiveId) { return; } Db::name('message')->insert([ 'receive_type' => $receiveType, 'receive_id' => (int)$receiveId, 'type' => $type, 'title' => mb_substr((string)$title, 0, 100), 'content' => mb_substr((string)$content, 0, 500), 'order_id' => (int)$orderId, 'is_read' => 0, 'createtime' => time(), ]); } }