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
父节点 d3cd6ba342
当前提交 14533eadb7
共有 270 个文件被更改,包括 9757 次插入 和 1392 次删除

查看文件

@@ -0,0 +1,267 @@
<?php
namespace app\common\library;
use fast\Random;
use think\Db;
use think\Exception;
/**
* 师傅端独立认证服务(基于 fa_master 与 fa_master_token,完全不依赖 fa_user)
*/
class MasterAuth
{
protected static $instance = null;
protected $_error = '';
protected $_logined = false;
protected $_master = null;
protected $_token = '';
protected $keeptime = 2592000;
public static function instance()
{
if (is_null(self::$instance)) {
self::$instance = new static();
}
return self::$instance;
}
public function id()
{
return $this->_master ? (int)$this->_master['id'] : 0;
}
public function getMaster()
{
return $this->_master;
}
public function isLogin()
{
return $this->_logined;
}
public function getToken()
{
return $this->_token;
}
public function getError()
{
return $this->_error;
}
public function setError($error)
{
$this->_error = (string)$error;
return $this;
}
public function getEncryptPassword($password, $salt = '')
{
return md5(md5((string)$password) . (string)$salt);
}
/**
* 根据 Token 初始化师傅登录态
*/
public function init($token)
{
if ($this->_logined) {
return true;
}
if ($this->_error) {
return false;
}
$data = MasterToken::get($token);
if (!$data) {
return false;
}
$masterId = (int)$data['master_id'];
if ($masterId <= 0) {
$this->setError('请先登录师傅账号');
return false;
}
$master = Db::name('master')->where('id', $masterId)->find();
if (!$master) {
$this->setError('师傅账号不存在');
return false;
}
if (isset($master['status']) && $master['status'] !== 'normal') {
$this->setError('师傅账号已被禁用');
return false;
}
$this->_master = $master;
$this->_logined = true;
$this->_token = $token;
return true;
}
/**
* 直接登录指定师傅账号(签发独立师傅 Token)
*/
public function direct($masterId)
{
$master = Db::name('master')->where('id', (int)$masterId)->find();
if (!$master) {
$this->setError('师傅账号不存在');
return false;
}
if (isset($master['status']) && $master['status'] !== 'normal') {
$this->setError('师傅账号已被禁用');
return false;
}
Db::startTrans();
try {
$now = time();
$ip = request()->ip();
$update = [
'prevtime' => $master['logintime'] ?: $now,
'logintime' => $now,
'loginip' => $ip,
'loginfailure' => 0,
'updatetime' => $now,
];
Db::name('master')->where('id', (int)$master['id'])->update($update);
$master = array_merge($master, $update);
$this->_master = $master;
$this->_token = $this->generateToken((int)$master['id']);
$this->_logined = true;
MasterSecurity::ensure((int)$master['id']);
Db::commit();
} catch (\Throwable $e) {
Db::rollback();
$this->setError($e->getMessage());
return false;
}
return true;
}
/**
* 手机号 + 密码注册并登录独立师傅账号(默认 verify_status=draft)
*/
public function register($mobile, $password)
{
$mobile = trim((string)$mobile);
if (Db::name('master')->where('mobile', $mobile)->find()) {
$this->setError('该手机号已注册师傅账号');
return false;
}
$now = time();
$ip = request()->ip();
$salt = Random::alnum();
$encrypt = $this->getEncryptPassword($password, $salt);
Db::startTrans();
try {
$masterId = (int)Db::name('master')->insertGetId([
'mobile' => $mobile,
'nickname' => '师傅' . substr($mobile, -4),
'realname' => '',
'password' => $encrypt,
'salt' => $salt,
'verify_status' => 'draft',
'work_status' => 'off',
'status' => 'normal',
'level' => 1,
'score' => 5.00,
'accept_rate' => 100.00,
'ontime_rate' => 100.00,
'serve_radius' => 10,
'money' => 0.00,
'frozen_money' => 0.00,
'prevtime' => $now,
'logintime' => $now,
'loginip' => $ip,
'createtime' => $now,
'updatetime' => $now,
]);
MasterSecurity::ensure($masterId);
$this->_master = Db::name('master')->where('id', $masterId)->find();
$this->_token = $this->generateToken($masterId);
$this->_logined = true;
Db::commit();
} catch (\Throwable $e) {
Db::rollback();
$this->setError($e->getMessage());
return false;
}
return true;
}
/**
* 退出登录
*/
public function logout()
{
if (!$this->_logined) {
$this->setError('尚未登录');
return false;
}
$this->_logined = false;
MasterToken::delete($this->_token);
$this->_master = null;
$this->_token = '';
return true;
}
/**
* 修改登录密码
*/
public function changepwd($newpassword, $oldpassword = '', $ignoreOld = false)
{
if (!$this->_logined || !$this->_master) {
$this->setError('请先登录');
return false;
}
$masterId = (int)$this->_master['id'];
if (!$ignoreOld) {
if ($this->_master['password'] !== $this->getEncryptPassword($oldpassword, $this->_master['salt'])) {
$this->setError('原登录密码不正确');
return false;
}
}
Db::startTrans();
try {
$salt = Random::alnum();
$encrypted = $this->getEncryptPassword($newpassword, $salt);
Db::name('master')->where('id', $masterId)->update([
'loginfailure' => 0,
'password' => $encrypted,
'salt' => $salt,
'updatetime' => time(),
]);
MasterToken::clear($masterId);
Db::commit();
} catch (\Throwable $e) {
Db::rollback();
$this->setError($e->getMessage());
return false;
}
return true;
}
/**
* 刷新当前内存缓存的师傅行
*/
public function reload()
{
if ($this->id() > 0) {
$this->_master = Db::name('master')->where('id', $this->id())->find();
}
return $this->_master;
}
private function generateToken($masterId)
{
$maxAttempts = 5;
for ($i = 0; $i < $maxAttempts; $i++) {
$token = Random::uuid();
if (MasterToken::set($token, $masterId, $this->keeptime)) {
return $token;
}
}
throw new Exception('生成师傅登录令牌失败');
}
}