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 次删除

查看文件

@@ -18,13 +18,14 @@ class Master extends Backend
* @var \app\admin\model\Master
*/
protected $model = null;
protected $relationSearch = true;
protected $searchFields = 'id,realname,mobile,city';
protected $relationSearch = false;
protected $searchFields = 'id,realname,nickname,mobile,city';
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\Master;
$this->view->assign("statusList", $this->model->getStatusList());
$this->view->assign("workStatusList", $this->model->getWorkStatusList());
$this->view->assign("verifyStatusList", $this->model->getVerifyStatusList());
}
@@ -40,7 +41,6 @@ class Master extends Backend
}
[$where, $sort, $order, $offset, $limit] = $this->buildparams();
$list = $this->model
->with(['user'])
->where($where)
->order($sort, $order)
->paginate($limit);
@@ -48,6 +48,8 @@ class Master extends Backend
$items = $list->items();
$masterIds = array_column($items, 'id');
$skillsGroup = [];
$wechatMap = [];
$securityMap = [];
if (!empty($masterIds)) {
$allSkills = Db::name('master_skill')
->where('master_id', 'in', $masterIds)
@@ -59,9 +61,37 @@ class Master extends Backend
$skillsGroup[$sk['master_id']][] = $name;
}
}
$wechatRows = Db::name('master_wechat')
->where('master_id', 'in', $masterIds)
->field('master_id,openid,createtime')
->select();
foreach ($wechatRows as $wc) {
$wechatMap[(int)$wc['master_id']] = $wc;
}
$secRows = Db::name('master_security')
->where('master_id', 'in', $masterIds)
->field('master_id,payment_password_hash,failed_attempts,locked_until,payment_failed_attempts,payment_locked_until')
->select();
foreach ($secRows as $sr) {
$securityMap[(int)$sr['master_id']] = $sr;
}
}
$now = time();
foreach ($items as &$it) {
$it['skills_list'] = $skillsGroup[$it['id']] ?? [];
$mid = (int)$it['id'];
unset($it['password'], $it['salt']);
$it['skills_list'] = $skillsGroup[$mid] ?? [];
$wc = $wechatMap[$mid] ?? null;
$it['wechat_bound'] = !empty($wc['openid']);
$it['wechat_openid_mask'] = !empty($wc['openid'])
? (mb_substr($wc['openid'], 0, 6) . '****' . mb_substr($wc['openid'], -4))
: '';
$sec = $securityMap[$mid] ?? null;
$it['has_pay_password'] = !empty($sec['payment_password_hash']);
$it['is_locked'] = ((int)($sec['locked_until'] ?? 0) > $now)
|| ((int)($sec['payment_locked_until'] ?? 0) > $now);
}
unset($it);
@@ -69,7 +99,7 @@ class Master extends Backend
}
/**
* 新增认证师傅档案(校验用户唯一绑定,强制初始化资金与履约统计为 0)
* 新增独立师傅账号与档案(不再绑定 fa_user)
*/
public function add()
{
@@ -81,13 +111,38 @@ class Master extends Backend
$this->error(__('Parameter %s can not be empty', ''));
}
$params = $this->preExcludeFields($params);
$uid = (int)($params['user_id'] ?? 0);
if ($uid <= 0) {
$this->error('请选择关联的平台注册用户账号');
unset($params['user_id']);
$mobile = trim((string)($params['mobile'] ?? ''));
if (!preg_match('/^1[3-9]\d{9}$/', $mobile)) {
$this->error('请输入有效的11位手机号码');
}
if (Db::name('master')->where('user_id', $uid)->count() > 0) {
$this->error('该用户已绑定过师傅档案,请勿重复创建');
if (Db::name('master')->where('mobile', $mobile)->count() > 0) {
$this->error('该手机号已注册过师傅端账号,请勿重复创建');
}
$realname = trim((string)($params['realname'] ?? ''));
if ($realname === '') {
$this->error('请填写师傅真实姓名');
}
$plainPassword = trim((string)($params['password'] ?? ''));
if ($plainPassword !== '' && mb_strlen($plainPassword) < 6) {
$this->error('登录密码长度不能少于6位');
}
$salt = \fast\Random::alnum();
if ($plainPassword === '') {
// 若管理员未指定初始密码,生成高强度随机盐与不可逆占位密码,师傅可通过微信或手机验证码登录
$plainPassword = \fast\Random::alnum(16);
}
$params['salt'] = $salt;
$params['password'] = \app\common\library\MasterAuth::instance()->getEncryptPassword($plainPassword, $salt);
if (empty($params['nickname'])) {
$params['nickname'] = $realname;
}
$params['mobile'] = $mobile;
$params['status'] = in_array($params['status'] ?? 'normal', ['normal', 'disabled'], true) ? $params['status'] : 'normal';
$params['money'] = 0.00;
$params['frozen_money'] = 0.00;
$params['order_count'] = 0;
@@ -95,6 +150,9 @@ class Master extends Backend
$params['score'] = 5.00;
$params['accept_rate'] = 100.00;
$params['ontime_rate'] = 100.00;
if ($params['status'] !== 'normal') {
$params['work_status'] = 'off';
}
$result = $this->model->allowField(true)->save($params);
if ($result !== false) {
@@ -104,7 +162,7 @@ class Master extends Backend
}
/**
* 删除保护:已产生承接工单、钱包余额或结算流水的师傅禁止物理删除
* 删除保护:已产生承接工单、提现、申诉、评价、钱包余额或结算流水的师傅禁止物理删除
*/
public function del($ids = null)
{
@@ -119,12 +177,19 @@ class Master extends Backend
$hasOrder = Db::name('order')->where('master_id', 'in', $idArr)->count();
$hasFund = Db::name('master')->where('id', 'in', $idArr)->where('money > 0 OR frozen_money > 0')->count();
$hasSettle = Db::name('settle')->where('master_id', 'in', $idArr)->count();
if ($hasOrder > 0 || $hasFund > 0 || $hasSettle > 0) {
$this->error('所选师傅已关联历史工单、结算记录或存在钱包资金,禁止物理删除;如需停用请关闭听单状态');
$hasWithdraw = Db::name('withdraw')->where('master_id', 'in', $idArr)->count();
$hasAppeal = Db::name('appeal')->where('master_id', 'in', $idArr)->count();
if ($hasOrder > 0 || $hasFund > 0 || $hasSettle > 0 || $hasWithdraw > 0 || $hasAppeal > 0) {
$this->error('所选师傅已关联历史工单、提现、申诉、结算记录或存在钱包资金,禁止物理删除;如需停用请将账号状态设为已停用');
}
Db::startTrans();
try {
Db::name('master_skill')->where('master_id', 'in', $idArr)->delete();
Db::name('master_cert')->where('master_id', 'in', $idArr)->delete();
Db::name('master_token')->where('master_id', 'in', $idArr)->delete();
Db::name('master_wechat')->where('master_id', 'in', $idArr)->delete();
Db::name('master_security')->where('master_id', 'in', $idArr)->delete();
Db::name('master_agreement_read')->where('master_id', 'in', $idArr)->delete();
$deleted = Db::name('master')->where('id', 'in', $idArr)->delete();
Db::commit();
} catch (Exception $e) {
@@ -138,7 +203,7 @@ class Master extends Backend
}
/**
* 编辑师傅资料与入驻资质审核(状态变更自动触发通知)
* 编辑师傅资料、独立账号状态与入驻资质审核
*/
public function edit($ids = null)
{
@@ -153,7 +218,45 @@ class Master extends Backend
if ($params) {
$params = $this->preExcludeFields($params);
// 禁止在师傅档案编辑中直接篡改钱包可用余额、冻结资金与累计完单数
unset($params['money'], $params['frozen_money'], $params['order_count']);
unset($params['user_id'], $params['money'], $params['frozen_money'], $params['order_count'], $params['salt']);
if (isset($params['mobile'])) {
$mobile = trim((string)$params['mobile']);
if (!preg_match('/^1[3-9]\d{9}$/', $mobile)) {
$this->error('请输入有效的11位手机号码');
}
$dup = Db::name('master')->where('mobile', $mobile)->where('id', '<>', (int)$row['id'])->count();
if ($dup > 0) {
$this->error('该手机号已被其他师傅账号占用');
}
$params['mobile'] = $mobile;
}
// 登录密码留空表示不修改,填写则重置盐值与加密密码并清理登录会话
$resetPassword = false;
if (array_key_exists('password', $params)) {
$plainPassword = trim((string)$params['password']);
if ($plainPassword === '') {
unset($params['password']);
} else {
if (mb_strlen($plainPassword) < 6) {
$this->error('重置的登录密码长度不能少于6位');
}
$salt = \fast\Random::alnum();
$params['salt'] = $salt;
$params['password'] = \app\common\library\MasterAuth::instance()->getEncryptPassword($plainPassword, $salt);
$resetPassword = true;
}
}
$kickToken = $resetPassword;
if (isset($params['status'])) {
$params['status'] = in_array($params['status'], ['normal', 'disabled'], true) ? $params['status'] : $row['status'];
if ($params['status'] === 'disabled') {
$params['work_status'] = 'off';
$kickToken = true;
}
}
// 若提交的身份证号包含脱敏星号 *,保留原数据库记录不覆盖;若为新填写明文则做合法性校验
if (isset($params['idcard_no'])) {
@@ -184,6 +287,9 @@ class Master extends Backend
Db::startTrans();
try {
$result = $row->allowField(true)->save($params);
if ($kickToken) {
\app\common\library\MasterToken::clear((int)$row['id']);
}
$newVerify = isset($params['verify_status']) ? (string)$params['verify_status'] : $oldVerify;
if ($newVerify !== $oldVerify && in_array($newVerify, ['pass', 'reject'], true)) {
$now = time();
@@ -201,18 +307,6 @@ class Master extends Backend
'is_read' => 0,
'createtime' => $now,
]);
if ((int)$row['user_id'] > 0) {
Db::name('message')->insert([
'receive_type' => 'user',
'receive_id' => (int)$row['user_id'],
'type' => 'audit',
'title' => $title,
'content' => $content,
'order_id' => 0,
'is_read' => 0,
'createtime' => $now,
]);
}
}
Db::commit();
} catch (Exception $e) {
@@ -370,7 +464,7 @@ class Master extends Backend
*/
public function detail($ids = null)
{
$row = $this->model->with(['user'])->where('master.id', (int)$ids)->find();
$row = $this->model->where('id', (int)$ids)->find();
if (!$row) {
$this->error(__('No Results were found'));
}
@@ -448,8 +542,26 @@ class Master extends Backend
$row['idcard_back_watermark'] = \app\common\library\TencentVerifyService::getWatermarkUrl($row['idcard_back']);
}
// 8. 独立账号安全、微信绑定与活跃会话状态
$wechatBind = Db::name('master_wechat')->where('master_id', $row['id'])->find();
$securityInfo = Db::name('master_security')->where('master_id', $row['id'])->find();
$activeTokens = Db::name('master_token')
->where('master_id', $row['id'])
->where(function ($q) {
$q->where('expiretime', 0)->whereOr('expiretime', '>', time());
})
->count();
$now = time();
$isLoginLocked = (int)($securityInfo['locked_until'] ?? 0) > $now;
$isPayLocked = (int)($securityInfo['payment_locked_until'] ?? 0) > $now;
$this->view->assign([
'row' => $row,
'wechatBind' => $wechatBind ?: [],
'securityInfo' => $securityInfo ?: [],
'activeTokens' => (int)$activeTokens,
'isLoginLocked' => $isLoginLocked,
'isPayLocked' => $isPayLocked,
'recentOrders' => $recentOrders,
'recentWithdraws' => $recentWithdraws,
'recentSettles' => $recentSettles,
@@ -468,7 +580,7 @@ class Master extends Backend
public function audit($ids = null)
{
$id = (int)($ids ?: $this->request->param('ids'));
$row = $this->model->with(['user'])->where('master.id', $id)->find();
$row = $this->model->where('id', $id)->find();
if (!$row) {
$this->error(__('No Results were found'));
}
@@ -489,6 +601,8 @@ class Master extends Backend
->order('id desc')
->select();
$wechatBind = Db::name('master_wechat')->where('master_id', $row['id'])->find();
// 证件防伪水印处理
if (!empty($row['idcard_front'])) {
$row['idcard_front_watermark'] = \app\common\library\TencentVerifyService::getWatermarkUrl($row['idcard_front']);
@@ -498,9 +612,10 @@ class Master extends Backend
}
$this->view->assign([
'row' => $row,
'skills' => $skills,
'certs' => $certs,
'row' => $row,
'wechatBind' => $wechatBind ?: [],
'skills' => $skills,
'certs' => $certs,
]);
return $this->view->fetch('audit');
}
@@ -543,7 +658,7 @@ class Master extends Backend
try {
Db::name('master')->where('id', $row['id'])->update($updateData);
// 发送审核结果通知给师傅和用户
// 发送审核结果通知给独立师傅端
$title = $verifyStatus === 'pass' ? '师傅入驻审核通过' : '师傅入驻审核未通过';
$content = $verifyStatus === 'pass'
? '恭喜您!您的师傅入驻与实名认证已审核通过,已获得接单资格,现在可开启听单承接同城订单。'
@@ -560,19 +675,6 @@ class Master extends Backend
'createtime' => $now,
]);
if ((int)$row['user_id'] > 0) {
Db::name('message')->insert([
'receive_type' => 'user',
'receive_id' => (int)$row['user_id'],
'type' => 'audit',
'title' => $title,
'content' => $content,
'order_id' => 0,
'is_read' => 0,
'createtime' => $now,
]);
}
Db::commit();
} catch (Exception $e) {
Db::rollback();
@@ -621,7 +723,6 @@ class Master extends Backend
foreach ($pendingMasters as $m) {
$mId = (int)$m['id'];
$uId = (int)$m['user_id'];
$title = '师傅入驻审核通过';
$content = '恭喜您!您的师傅入驻与实名认证已审核通过,已获得接单资格,现在可开启听单承接同城订单。';
@@ -635,19 +736,6 @@ class Master extends Backend
'is_read' => 0,
'createtime' => $now,
]);
if ($uId > 0) {
Db::name('message')->insert([
'receive_type' => 'user',
'receive_id' => $uId,
'type' => 'audit',
'title' => $title,
'content' => $content,
'order_id' => 0,
'is_read' => 0,
'createtime' => $now,
]);
}
}
Db::commit();
@@ -658,4 +746,99 @@ class Master extends Backend
$this->success('已成功批量审核通过 ' . count($pendingMasters) . ' 位师傅');
}
/**
* 解绑师傅端微信账号(清空 fa_master_wechat 并踢掉会话)
*/
public function unbind_wechat($ids = null)
{
if (false === $this->request->isPost()) {
$this->error(__('Invalid parameters'));
}
$masterId = (int)($ids ?: $this->request->post('ids'));
$row = $this->model->find($masterId);
if (!$row) {
$this->error(__('No Results were found'));
}
Db::startTrans();
try {
Db::name('master_wechat')->where('master_id', $masterId)->delete();
\app\common\library\MasterToken::clear($masterId);
Db::commit();
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
$this->success('已解绑该师傅的微信授权绑定');
}
/**
* 解除师傅登录/支付密码防爆破锁定
*/
public function unlock_security($ids = null)
{
if (false === $this->request->isPost()) {
$this->error(__('Invalid parameters'));
}
$masterId = (int)($ids ?: $this->request->post('ids'));
$row = $this->model->find($masterId);
if (!$row) {
$this->error(__('No Results were found'));
}
$now = time();
Db::startTrans();
try {
Db::name('master')->where('id', $masterId)->update([
'loginfailure' => 0,
'loginfailuretime' => 0,
'updatetime' => $now,
]);
Db::name('master_security')->where('master_id', $masterId)->update([
'failed_attempts' => 0,
'locked_until' => 0,
'payment_failed_attempts' => 0,
'payment_locked_until' => 0,
]);
Db::commit();
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
$this->success('已解除登录与支付防爆破锁定');
}
/**
* 停用 / 启用独立师傅账号(停用自动关闭听单并下线会话)
*/
public function toggle_status($ids = null)
{
if (false === $this->request->isPost()) {
$this->error(__('Invalid parameters'));
}
$masterId = (int)($ids ?: $this->request->post('ids'));
$row = $this->model->find($masterId);
if (!$row) {
$this->error(__('No Results were found'));
}
$target = ($row['status'] ?? 'normal') === 'normal' ? 'disabled' : 'normal';
$update = [
'status' => $target,
'updatetime' => time(),
];
if ($target === 'disabled') {
$update['work_status'] = 'off';
}
Db::startTrans();
try {
Db::name('master')->where('id', $masterId)->update($update);
if ($target === 'disabled') {
\app\common\library\MasterToken::clear($masterId);
}
Db::commit();
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
$this->success($target === 'disabled' ? '已停用该师傅账号并强制下线' : '已恢复启用该师傅账号');
}
}