文件
yjyf/server/application/api/controller/MasterAuth.php
MiAn b259c8ec30 feat(master): 后台增加师傅密码重置功能并优化师傅端账号安全页面
- 后端后台管理:在师傅管理模块新增 resetpwd 功能,支持重置登录密码、支付密码或全部重置,并联动清理锁定状态与在线令牌
- 视图与脚本:新增 resetpwd.html 弹窗视图并在 master.js 与 index.html 中注册操作按钮与表单联动逻辑
- 师傅端接口:MasterAuth::setPaymentPassword 简化流程,依托已有登录态直接设置6位数字支付密码,并在修改登录密码时自动重置锁定状态
- 师傅端小程序:security.vue 全面升级账号安全中心界面视觉与交互,展示账号信息、认证状态并提供标准弹窗;清理 wxlogin.js 中冗余代码
2026-11-24 18:02:35 +08:00

296 行
11 KiB
PHP

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

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

<?php
namespace app\api\controller;
use app\common\library\MasterSecurity;
use app\common\library\MasterToken;
use app\common\library\WxLogin;
use think\Db;
/**
* 师傅端独立登录 / 安全凭证 / 个人信息接口(完全独立于 fa_user)
*/
class MasterAuth extends MasterBase
{
protected $noNeedLogin = ['login', 'wxPhoneLogin'];
/**
* 师傅手机号 + 密码登录(H5调试/账号密码通道)
* @ApiMethod (POST)
*/
public function login()
{
$mobile = trim((string)$this->request->post('mobile', $this->request->post('account', '')));
$mobile = preg_replace('/[^\d]/', '', $mobile);
$password = (string)$this->request->post('password', '');
$code = trim((string)$this->request->post('code', ''));
if ($password === '' && $code !== '') {
$this->error('短信验证码通道已移除,请使用手机号密码或微信一键登录', null, 2010);
}
if ($mobile === '') {
$this->error('请填写手机号');
}
if (!preg_match('/^1[3-9]\d{9}$/', $mobile)) {
$this->error('手机号格式不正确');
}
if ($password === '') {
$this->error('请填写登录密码');
}
$master = Db::name('master')->where('mobile', $mobile)->find();
if (!$master) {
if (!preg_match('/^\S{6,30}$/', $password)) {
$this->error('登录密码须为6至30位非空字符');
}
if (!$this->masterAuth->register($mobile, $password)) {
$this->error($this->masterAuth->getError() ?: '注册登录失败');
}
} else {
if (isset($master['status']) && $master['status'] !== 'normal') {
$this->error('师傅账号已被禁用');
}
if (empty($master['password'])) {
$this->error('该师傅账号尚未设置登录密码,请使用微信一键登录后在账号安全中设置密码');
}
try {
MasterSecurity::verify((int)$master['id'], 'login', $password);
} catch (\Throwable $e) {
$this->error($e->getMessage() === '密码不正确' ? '手机号或密码不正确' : $e->getMessage());
}
if (!$this->masterAuth->direct((int)$master['id'])) {
$this->error($this->masterAuth->getError() ?: '登录失败');
}
}
$this->success('登录成功', [
'token' => $this->masterAuth->getToken(),
'expires' => 2592000,
'masterinfo' => $this->profileData(),
]);
}
/**
* 师傅端微信小程序 OpenID 静默登录(已下线,统一走微信手机号授权登录)
* @ApiMethod (POST)
*/
public function wxlogin()
{
$this->error('师傅端已取消静默登录,请使用微信手机号授权登录');
}
/**
* 师傅端微信手机号授权快捷登录
* @ApiMethod (POST)
*/
public function wxPhoneLogin()
{
$phoneCode = trim((string)$this->request->post('phone_code'));
$loginCode = trim((string)$this->request->post('login_code'));
if ($phoneCode === '') {
$this->error('缺少微信手机号授权凭证');
}
$phoneInfo = WxLogin::getPhoneNumber($phoneCode, 'master');
if (isset($phoneInfo['error'])) {
$this->error('获取微信手机号失败:' . $phoneInfo['error'], ['wx_errcode' => isset($phoneInfo['errcode']) ? (int)$phoneInfo['errcode'] : 0]);
}
$mobile = isset($phoneInfo['purePhoneNumber']) ? (string)$phoneInfo['purePhoneNumber'] : (string)($phoneInfo['phoneNumber'] ?? '');
if ($mobile === '') {
$this->error('解析手机号失败');
}
$openid = '';
$sessionKey = '';
$unionid = '';
if ($loginCode !== '') {
$wx = WxLogin::jscode2session($loginCode, 'master');
if (isset($wx['error']) || empty($wx['openid'])) {
$errMsg = isset($wx['error']) && $wx['error'] !== '' ? (string)$wx['error'] : '微信登录凭证换取 OpenID 失败,请重进小程序再试';
\think\Log::error('[MasterAuth::wxPhoneLogin] jscode2session 失败: ' . $errMsg);
$this->error($errMsg, ['wx_errcode' => isset($wx['errcode']) ? (int)$wx['errcode'] : 0]);
}
$openid = $wx['openid'];
$sessionKey = isset($wx['session_key']) ? $wx['session_key'] : '';
$unionid = isset($wx['unionid']) ? $wx['unionid'] : '';
}
$ret = WxLogin::provisionMasterWithMobile($mobile, $openid, $unionid, $sessionKey);
$masterRow = Db::name('master')->field('status')->where('id', $ret['master_id'])->find();
if ($masterRow && isset($masterRow['status']) && $masterRow['status'] !== 'normal') {
$this->error('师傅账号已被禁用');
}
if (!$this->masterAuth->direct($ret['master_id'])) {
$this->error($this->masterAuth->getError() ?: '登录失败');
}
$this->success('登录成功', [
'token' => $this->masterAuth->getToken(),
'expires' => 2592000,
'masterinfo' => $this->profileData(),
'is_new' => $ret['is_new'],
'mobile' => $mobile,
]);
}
/**
* 当前登录师傅基础信息
*/
public function profile()
{
$this->success('', $this->profileData());
}
/**
* 退出登录
* @ApiMethod (POST)
*/
public function logout()
{
$this->masterAuth->logout();
$this->success('已退出登录');
}
/**
* 安全状态查询(登录密码 / 支付密码 / 绑定手机号 / 实名状态)
*/
public function securityStatus()
{
$mid = $this->masterId();
$master = Db::name('master')->field('password,mobile,verify_status')->where('id', $mid)->find();
$security = Db::name('master_security')->where('master_id', $mid)->find();
$rawMobile = trim((string)($master['mobile'] ?? ''));
$maskedMobile = strlen($rawMobile) === 11
? (substr($rawMobile, 0, 3) . '****' . substr($rawMobile, -4))
: $rawMobile;
$this->success('', [
'has_login_password' => !empty($master['password']),
'has_payment_password' => $security && $security['payment_password_hash'] !== '',
'mobile' => $maskedMobile,
'verify_status' => (string)($master['verify_status'] ?? 'draft'),
]);
}
/**
* 设置或修改师傅登录密码(未设置时直接设置,已设置时需校验原密码)
* @ApiMethod (POST)
*/
public function changeLoginPassword()
{
$mid = $this->masterId();
$new = (string)$this->request->post('new_password', '');
$old = (string)$this->request->post('old_password', '');
if (!preg_match('/^\S{6,30}$/', $new)) {
$this->error('登录密码须为6至30位非空字符');
}
$master = Db::name('master')->field('password')->where('id', $mid)->find();
if (!empty($master['password'])) {
try {
MasterSecurity::verify($mid, 'login', $old);
} catch (\Throwable $e) {
$this->error($e->getMessage());
}
$ignoreOld = false;
} else {
$ignoreOld = true;
}
if (!$this->masterAuth->changepwd($new, $old, $ignoreOld)) {
$this->error($this->masterAuth->getError() ?: '设置失败');
}
MasterSecurity::ensure($mid);
Db::name('master_security')->where('master_id', $mid)->update([
'failed_attempts' => 0,
'locked_until' => 0,
]);
MasterToken::clear($mid);
$this->success('登录密码已更新,请重新登录');
}
/**
* 首次设置师傅支付密码
* @ApiMethod (POST)
*/
public function setPaymentPassword()
{
$mid = $this->masterId();
$new = (string)$this->request->post('new_password', '');
if (!preg_match('/^\d{6}$/', $new)) {
$this->error('支付密码须为6位数字');
}
MasterSecurity::ensure($mid);
$updated = Db::name('master_security')
->where('master_id', $mid)
->where('payment_password_hash', '')
->update(['payment_password_hash' => password_hash($new, PASSWORD_DEFAULT)]);
if (!$updated) {
$this->error('支付密码已设置,请使用修改功能');
}
$this->success('支付密码设置成功');
}
/**
* 修改师傅支付密码(需校验原支付密码)
* @ApiMethod (POST)
*/
public function changePaymentPassword()
{
$mid = $this->masterId();
$new = (string)$this->request->post('new_password', '');
if (!preg_match('/^\d{6}$/', $new)) {
$this->error('支付密码须为6位数字');
}
$oldHash = (string)Db::name('master_security')->where('master_id', $mid)->value('payment_password_hash');
try {
MasterSecurity::verify($mid, 'payment', (string)$this->request->post('old_password', ''));
} catch (\Throwable $e) {
$this->error($e->getMessage());
}
$updated = Db::name('master_security')
->where('master_id', $mid)
->where('payment_password_hash', $oldHash)
->update(['payment_password_hash' => password_hash($new, PASSWORD_DEFAULT)]);
if (!$updated) {
$this->error('支付密码已变更,请重新操作');
}
$this->success('支付密码修改成功');
}
protected function profileData()
{
$mid = $this->masterId();
$master = Db::name('master')->where('id', $mid)->find();
if (!$master) {
return null;
}
$unread = (int)Db::name('message')
->where('receive_type', 'master')
->where('receive_id', $mid)
->where('is_read', 0)
->count();
return [
'id' => (int)$master['id'],
'nickname' => !empty($master['nickname']) ? (string)$master['nickname'] : (!empty($master['realname']) ? (string)$master['realname'] : ('师傅' . substr((string)$master['mobile'], -4))),
'realname' => (string)$master['realname'],
'mobile' => (string)$master['mobile'],
'avatar' => !empty($master['avatar']) ? \app\common\library\MasterAvatar::url($master['avatar']) : '',
'city' => (string)$master['city'],
'verify_status' => (string)$master['verify_status'],
'audit_msg' => (string)$master['audit_msg'],
'work_status' => (string)$master['work_status'],
'level' => (int)$master['level'],
'score' => (float)$master['score'],
'order_count' => (int)$master['order_count'],
'money' => (float)$master['money'],
'frozen_money' => (float)$master['frozen_money'],
'can_take' => $master['verify_status'] === 'pass',
'needs_apply' => ($master['verify_status'] === 'draft'),
'unread_count' => $unread,
];
}
}