文件
yjyf/server/application/api/controller/MasterBase.php
MiAn 14533eadb7 feat: 用户端与师傅端双小程序架构解耦,新增师傅独立认证及超时自动关单机制
1. 账号体系与数据库解耦:
   - 师傅表 fa_master 解除对 fa_user.id 的外键依赖,升级为独立认证账号主表;
   - 新增师傅端独立支撑表:fa_master_security、fa_master_token、fa_master_wechat、fa_master_agreement_read;
   - 新增工单超时未接单自动关闭与原路退款配置(order_unaccepted_close_days)。

2. 服务端(Server):
   - 新增师傅端独立 API 控制器与认证库(MasterAuth, MasterBase, MasterMessage, MasterSecurity, MasterToken);
   - WxLogin 支持双小程序端(user/master)独立换取 session_key、openid 与 access_token;
   - 新增控制台调度命令 php think order:autoclose,支持订单超时自动关闭与售后退款闭环;
   - 修复 PHP 8.5 兼容性问题(Request.php 的 (bool) 强制转换);
   - 系统业务配置全面收敛至数据库 fa_config 及 site.php。

3. 小程序端:
   - yjyf-app(用户端):彻底剥离师傅端路由、组件及静态资源,增加隐私授权弹窗与评价模块;
   - yjyf-master-app(师傅端):全新独立微信小程序工程(wx140d90ceda786107),实现工作台、接单大厅、资质认证、工单协同与钱包提现完整闭环。
2026-11-23 15:10:30 +08:00

217 行
6.9 KiB
PHP

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<?php
namespace app\api\controller;
use app\common\controller\Api;
use app\common\library\MasterAuth;
use app\common\library\MasterSecurity;
use app\common\library\OrderStatus;
use think\Config;
use think\Db;
use think\Loader;
/**
* 师傅端 API 基类(完全独立于 fa_user,仅校验 fa_master_token -> 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(),
]);
}
}