- 安全校验提示:优化 AccountSecurity 与 MasterSecurity 密码错误提示,区分登录密码与支付密码,并增加剩余可重试次数提示 - 接口响应对齐:优化 Auth、MasterAuth、Security、Sms 控制器在短信验证码发送与密码校验时的异常处理与返回格式 - 腾讯云短信SDK:优化 SmsSingleSender::sendWithParam 方法入参默认值 - 双端小程序:优化用户端与师傅端登录页及账号安全中心的密码修改与找回交互逻辑
490 行
18 KiB
PHP
490 行
18 KiB
PHP
<?php
|
||
|
||
namespace app\api\controller;
|
||
|
||
use app\common\library\MasterSecurity;
|
||
use app\common\library\MasterToken;
|
||
use app\common\library\SmsService;
|
||
use app\common\library\WxLogin;
|
||
use fast\Random;
|
||
use think\Db;
|
||
|
||
/**
|
||
* 师傅端独立登录 / 安全凭证 / 个人信息接口(完全独立于 fa_user)
|
||
*/
|
||
class MasterAuth extends MasterBase
|
||
{
|
||
protected $noNeedLogin = ['login', 'wxPhoneLogin', 'sendResetPasswordSms', 'resetPasswordBySms'];
|
||
|
||
/**
|
||
* 师傅手机号 + 密码登录(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) {
|
||
$msg = $e->getMessage();
|
||
if (strpos($msg, '不正确') !== false) {
|
||
$msg = str_replace('登录密码不正确', '手机号或密码不正确', $msg);
|
||
}
|
||
$this->error($msg);
|
||
}
|
||
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 sendResetPasswordSms()
|
||
{
|
||
try {
|
||
$mobile = SmsService::normalizeMobile((string)$this->request->post('mobile', ''));
|
||
} catch (\InvalidArgumentException $e) {
|
||
$this->error($e->getMessage());
|
||
return;
|
||
}
|
||
|
||
$master = Db::name('master')->field('id,status')->where('mobile', $mobile)->find();
|
||
if (!$master) {
|
||
$this->error('该手机号尚未注册师傅账号');
|
||
return;
|
||
}
|
||
if (isset($master['status']) && $master['status'] !== 'normal') {
|
||
$this->error('师傅账号已被禁用');
|
||
return;
|
||
}
|
||
|
||
try {
|
||
$res = SmsService::sendVerifyCode($mobile, 'master_resetpwd', $this->request->ip());
|
||
} catch (\Throwable $e) {
|
||
$this->error($e->getMessage());
|
||
return;
|
||
}
|
||
$this->success('验证码已发送,请注意查收', array_merge($res, [
|
||
'mobile' => substr_replace($mobile, '****', 3, 4),
|
||
]));
|
||
}
|
||
|
||
/**
|
||
* 师傅端通过短信验证码重置登录密码(忘记密码)
|
||
* @ApiMethod (POST)
|
||
*/
|
||
public function resetPasswordBySms()
|
||
{
|
||
try {
|
||
$mobile = SmsService::normalizeMobile((string)$this->request->post('mobile', ''));
|
||
} catch (\InvalidArgumentException $e) {
|
||
$this->error($e->getMessage());
|
||
return;
|
||
}
|
||
$smsCode = trim((string)$this->request->post('sms_code', $this->request->post('captcha', '')));
|
||
$newPassword = (string)$this->request->post('new_password', '');
|
||
if (!preg_match('/^\S{6,30}$/', $newPassword)) {
|
||
$this->error('登录密码须为6至30位非空字符');
|
||
return;
|
||
}
|
||
|
||
$master = Db::name('master')->field('id,status')->where('mobile', $mobile)->find();
|
||
if (!$master) {
|
||
$this->error('该手机号尚未注册师傅账号');
|
||
return;
|
||
}
|
||
if (isset($master['status']) && $master['status'] !== 'normal') {
|
||
$this->error('师傅账号已被禁用');
|
||
return;
|
||
}
|
||
|
||
try {
|
||
SmsService::verifyCode($mobile, $smsCode, 'master_resetpwd', true);
|
||
} catch (\Throwable $e) {
|
||
$this->error($e->getMessage());
|
||
return;
|
||
}
|
||
|
||
$mid = (int)$master['id'];
|
||
$salt = Random::alnum();
|
||
$encrypted = $this->masterAuth->getEncryptPassword($newPassword, $salt);
|
||
|
||
Db::startTrans();
|
||
try {
|
||
Db::name('master')->where('id', $mid)->update([
|
||
'loginfailure' => 0,
|
||
'password' => $encrypted,
|
||
'salt' => $salt,
|
||
'updatetime' => time(),
|
||
]);
|
||
MasterSecurity::ensure($mid);
|
||
Db::name('master_security')->where('master_id', $mid)->update([
|
||
'failed_attempts' => 0,
|
||
'locked_until' => 0,
|
||
]);
|
||
MasterToken::clear($mid);
|
||
Db::commit();
|
||
} catch (\Throwable $e) {
|
||
Db::rollback();
|
||
$this->error('密码重置失败,请稍后重试');
|
||
return;
|
||
}
|
||
|
||
$this->success('登录密码重置成功,请使用新密码登录');
|
||
}
|
||
|
||
/**
|
||
* 发送修改师傅登录密码短信验证码(给当前绑定手机号发送)
|
||
* @ApiMethod (POST)
|
||
*/
|
||
public function sendPasswordSms()
|
||
{
|
||
$mid = $this->masterId();
|
||
$rawMobile = (string)Db::name('master')->where('id', $mid)->value('mobile');
|
||
try {
|
||
$mobile = SmsService::normalizeMobile($rawMobile);
|
||
} catch (\InvalidArgumentException $e) {
|
||
$this->error('当前师傅账号尚未绑定有效手机号,无法发送短信验证码');
|
||
return;
|
||
}
|
||
|
||
try {
|
||
$res = SmsService::sendVerifyCode($mobile, 'master_changepwd', $this->request->ip());
|
||
} catch (\Throwable $e) {
|
||
$this->error($e->getMessage());
|
||
return;
|
||
}
|
||
$this->success('验证码已发送至绑定手机', array_merge($res, [
|
||
'mobile' => substr_replace($mobile, '****', 3, 4),
|
||
]));
|
||
}
|
||
|
||
/**
|
||
* 设置或修改师傅登录密码(支持原密码校验与绑定手机短信验证码校验)
|
||
* @ApiMethod (POST)
|
||
*/
|
||
public function changeLoginPassword()
|
||
{
|
||
$mid = $this->masterId();
|
||
$new = (string)$this->request->post('new_password', '');
|
||
$old = (string)$this->request->post('old_password', '');
|
||
$smsCode = trim((string)$this->request->post('sms_code', ''));
|
||
$verifyMode = trim((string)$this->request->post('verify_mode', $smsCode !== '' ? 'sms' : 'old_password'));
|
||
if (!preg_match('/^\S{6,30}$/', $new)) {
|
||
$this->error('登录密码须为6至30位非空字符');
|
||
}
|
||
|
||
$master = Db::name('master')->field('password,mobile')->where('id', $mid)->find();
|
||
if (!empty($master['password'])) {
|
||
if ($verifyMode === 'sms') {
|
||
try {
|
||
SmsService::verifyCode((string)($master['mobile'] ?? ''), $smsCode, 'master_changepwd', true);
|
||
} catch (\Throwable $e) {
|
||
$this->error($e->getMessage());
|
||
}
|
||
$ignoreOld = true;
|
||
} else {
|
||
try {
|
||
MasterSecurity::verify($mid, 'login', $old, '原登录密码');
|
||
} catch (\Throwable $e) {
|
||
$msg = $e->getMessage();
|
||
if (strpos($msg, '15分钟') !== false) {
|
||
$msg .= ',或点击下方改用短信验证码';
|
||
}
|
||
$this->error($msg);
|
||
}
|
||
$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 sendPaymentPasswordSms()
|
||
{
|
||
$mid = $this->masterId();
|
||
$rawMobile = (string)Db::name('master')->where('id', $mid)->value('mobile');
|
||
try {
|
||
$mobile = SmsService::normalizeMobile($rawMobile);
|
||
} catch (\InvalidArgumentException $e) {
|
||
$this->error('当前师傅账号尚未绑定有效手机号,无法发送短信验证码');
|
||
return;
|
||
}
|
||
|
||
try {
|
||
$res = SmsService::sendVerifyCode($mobile, 'master_paypwd', $this->request->ip());
|
||
} catch (\Throwable $e) {
|
||
$this->error($e->getMessage());
|
||
return;
|
||
}
|
||
$this->success('验证码已发送至绑定手机', array_merge($res, [
|
||
'mobile' => substr_replace($mobile, '****', 3, 4),
|
||
]));
|
||
}
|
||
|
||
/**
|
||
* 修改师傅支付密码(支持原支付密码校验与绑定手机短信验证码校验)
|
||
* @ApiMethod (POST)
|
||
*/
|
||
public function changePaymentPassword()
|
||
{
|
||
$mid = $this->masterId();
|
||
$new = (string)$this->request->post('new_password', '');
|
||
if (!preg_match('/^\d{6}$/', $new)) {
|
||
$this->error('支付密码须为6位数字');
|
||
}
|
||
MasterSecurity::ensure($mid);
|
||
$oldHash = (string)Db::name('master_security')->where('master_id', $mid)->value('payment_password_hash');
|
||
if ($oldHash === '') {
|
||
$this->error('尚未设置支付密码,请先设置支付密码');
|
||
}
|
||
$smsCode = trim((string)$this->request->post('sms_code', ''));
|
||
$verifyMode = trim((string)$this->request->post('verify_mode', $smsCode !== '' ? 'sms' : 'old_password'));
|
||
if ($verifyMode === 'sms') {
|
||
$mobile = (string)Db::name('master')->where('id', $mid)->value('mobile');
|
||
try {
|
||
SmsService::verifyCode($mobile, $smsCode, 'master_paypwd', true);
|
||
} catch (\Throwable $e) {
|
||
$this->error($e->getMessage());
|
||
}
|
||
} else {
|
||
try {
|
||
MasterSecurity::verify($mid, 'payment', (string)$this->request->post('old_password', ''), '原支付密码');
|
||
} catch (\Throwable $e) {
|
||
$msg = $e->getMessage();
|
||
if (strpos($msg, '15分钟') !== false) {
|
||
$msg .= ',或点击下方改用短信验证码';
|
||
}
|
||
$this->error($msg);
|
||
}
|
||
}
|
||
$updated = Db::name('master_security')
|
||
->where('master_id', $mid)
|
||
->where('payment_password_hash', $oldHash)
|
||
->update([
|
||
'payment_password_hash' => password_hash($new, PASSWORD_DEFAULT),
|
||
'payment_failed_attempts' => 0,
|
||
'payment_locked_until' => 0,
|
||
]);
|
||
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,
|
||
];
|
||
}
|
||
}
|