- 安全校验提示:优化 AccountSecurity 与 MasterSecurity 密码错误提示,区分登录密码与支付密码,并增加剩余可重试次数提示 - 接口响应对齐:优化 Auth、MasterAuth、Security、Sms 控制器在短信验证码发送与密码校验时的异常处理与返回格式 - 腾讯云短信SDK:优化 SmsSingleSender::sendWithParam 方法入参默认值 - 双端小程序:优化用户端与师傅端登录页及账号安全中心的密码修改与找回交互逻辑
227 行
11 KiB
PHP
227 行
11 KiB
PHP
<?php
|
|
namespace app\api\controller;
|
|
|
|
use app\common\library\AccountSecurity;
|
|
use app\common\library\SmsService;
|
|
use app\common\library\WxLogin;
|
|
use app\common\library\TencentVerifyService;
|
|
use app\common\library\EnterpriseLicenseImage;
|
|
use think\Db;
|
|
|
|
class Security extends Base
|
|
{
|
|
public function status()
|
|
{
|
|
$user = Db::name('user')->field('password,mobile')->where('id', $this->uid())->find();
|
|
$security = Db::name('user_security')->where('user_id', $this->uid())->find();
|
|
$cert = Db::name('user_enterprise_cert')->where('user_id', $this->uid())->find();
|
|
$rawMobile = trim((string)($user['mobile'] ?? ''));
|
|
$maskedMobile = strlen($rawMobile) === 11
|
|
? (substr($rawMobile, 0, 3) . '****' . substr($rawMobile, -4))
|
|
: $rawMobile;
|
|
$this->success('', [
|
|
'has_login_password' => !empty($user['password']),
|
|
'has_payment_password' => $security && $security['payment_password_hash'] !== '',
|
|
'mobile' => $maskedMobile,
|
|
'enterprise_cert' => $cert ?: null,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 发送修改登录密码短信验证码(给当前登录账号绑定的手机号发送)
|
|
* @ApiMethod (POST)
|
|
*/
|
|
public function sendPasswordSms()
|
|
{
|
|
$uid = $this->uid();
|
|
$rawMobile = (string)Db::name('user')->where('id', $uid)->value('mobile');
|
|
try {
|
|
$mobile = SmsService::normalizeMobile($rawMobile);
|
|
} catch (\InvalidArgumentException $e) {
|
|
$this->error('当前账号尚未绑定有效手机号,无法发送短信验证码');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$res = SmsService::sendVerifyCode($mobile, 'user_changepwd', $this->request->ip());
|
|
} catch (\Throwable $e) {
|
|
$this->error($e->getMessage());
|
|
return;
|
|
}
|
|
$this->success('验证码已发送至绑定手机', array_merge($res, [
|
|
'mobile' => substr_replace($mobile, '****', 3, 4),
|
|
]));
|
|
}
|
|
|
|
public function changeLoginPassword()
|
|
{
|
|
$uid = $this->uid();
|
|
$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位非空字符');
|
|
$user = Db::name('user')->field('password,mobile')->where('id', $uid)->find();
|
|
if (!empty($user['password'])) {
|
|
if ($verifyMode === 'sms') {
|
|
try {
|
|
SmsService::verifyCode((string)($user['mobile'] ?? ''), $smsCode, 'user_changepwd', true);
|
|
} catch (\Throwable $e) {
|
|
$this->error($e->getMessage());
|
|
}
|
|
$ignoreOld = true;
|
|
} else {
|
|
try {
|
|
AccountSecurity::verify($uid, 'login', $old, '原登录密码');
|
|
} catch (\Throwable $e) {
|
|
$msg = $e->getMessage();
|
|
if (strpos($msg, '15分钟') !== false) {
|
|
$msg .= ',或点击下方改用短信验证码';
|
|
}
|
|
$this->error($msg);
|
|
}
|
|
$ignoreOld = false;
|
|
}
|
|
} else {
|
|
$ignoreOld = true;
|
|
}
|
|
if (!$this->auth->changepwd($new, $old, $ignoreOld)) $this->error($this->auth->getError() ?: '设置失败');
|
|
AccountSecurity::ensure($uid);
|
|
Db::name('user_security')->where('user_id', $uid)->update(['failed_attempts' => 0, 'locked_until' => 0]);
|
|
\app\common\library\Token::clear($uid);
|
|
$this->success('登录密码已更新,请重新登录');
|
|
}
|
|
|
|
public function setPaymentPassword()
|
|
{
|
|
$uid = $this->uid();
|
|
$new = (string)$this->request->post('new_password', '');
|
|
if (!preg_match('/^\d{6}$/', $new)) $this->error('支付密码须为6位数字');
|
|
AccountSecurity::ensure($uid);
|
|
$updated = Db::name('user_security')->where('user_id', $uid)->where('payment_password_hash', '')->update(['payment_password_hash' => password_hash($new, PASSWORD_DEFAULT)]);
|
|
if (!$updated) $this->error('支付密码已设置,请使用修改功能');
|
|
$this->success('支付密码设置成功');
|
|
}
|
|
|
|
/**
|
|
* 发送修改/重置支付密码短信验证码(给当前登录账号绑定的手机号发送)
|
|
* @ApiMethod (POST)
|
|
*/
|
|
public function sendPaymentPasswordSms()
|
|
{
|
|
$uid = $this->uid();
|
|
$rawMobile = (string)Db::name('user')->where('id', $uid)->value('mobile');
|
|
try {
|
|
$mobile = SmsService::normalizeMobile($rawMobile);
|
|
} catch (\InvalidArgumentException $e) {
|
|
$this->error('当前账号尚未绑定有效手机号,无法发送短信验证码');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$res = SmsService::sendVerifyCode($mobile, 'user_paypwd', $this->request->ip());
|
|
} catch (\Throwable $e) {
|
|
$this->error($e->getMessage());
|
|
return;
|
|
}
|
|
$this->success('验证码已发送至绑定手机', array_merge($res, [
|
|
'mobile' => substr_replace($mobile, '****', 3, 4),
|
|
]));
|
|
}
|
|
|
|
public function changePaymentPassword()
|
|
{
|
|
$uid = $this->uid();
|
|
$new = (string)$this->request->post('new_password', '');
|
|
if (!preg_match('/^\d{6}$/', $new)) $this->error('支付密码须为6位数字');
|
|
AccountSecurity::ensure($uid);
|
|
$oldHash = (string)Db::name('user_security')->where('user_id', $uid)->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('user')->where('id', $uid)->value('mobile');
|
|
try {
|
|
SmsService::verifyCode($mobile, $smsCode, 'user_paypwd', true);
|
|
} catch (\Throwable $e) {
|
|
$this->error($e->getMessage());
|
|
}
|
|
} else {
|
|
try {
|
|
AccountSecurity::verify($uid, '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('user_security')->where('user_id', $uid)->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('支付密码修改成功');
|
|
}
|
|
|
|
public function submitEnterpriseCert()
|
|
{
|
|
$uid = $this->uid();
|
|
$name = trim((string)$this->request->post('company_name', ''));
|
|
$license = strtoupper(trim((string)$this->request->post('license_no', '')));
|
|
$image = trim((string)$this->request->post('license_image', ''));
|
|
$person = trim((string)$this->request->post('legal_person', ''));
|
|
if ($name === '' || mb_strlen($name) > 255) $this->error('请填写企业名称');
|
|
if (!preg_match('/^[A-Z0-9]{15,18}$/', $license)) $this->error('请填写正确的营业执照号码');
|
|
if (mb_strlen($person) > 100) $this->error('法人姓名过长');
|
|
$attachment = Db::name('attachment')->where('user_id', $uid)->where('url', $image)->where('category', 'cert')->find();
|
|
if (!$attachment || (int)$attachment['filesize'] > 10 * 1024 * 1024 || !in_array(strtolower((string)$attachment['mimetype']), ['image/jpeg', 'image/png', 'image/webp'], true)) {
|
|
$this->error('请上传本人营业执照图片');
|
|
}
|
|
$existing = Db::name('user_enterprise_cert')->where('user_id', $uid)->find();
|
|
$data = ['company_name' => $name, 'license_no' => $license, 'license_image' => $image,
|
|
'legal_person' => $person, 'verify_status' => 'pending', 'audit_msg' => '', 'updatetime' => time()];
|
|
if ($existing) {
|
|
if ($existing['verify_status'] !== 'reject') $this->error('已有企业资质申请,审核中或已通过');
|
|
$updated = Db::name('user_enterprise_cert')->where('id', (int)$existing['id'])->where('verify_status', 'reject')->update($data);
|
|
if (!$updated) $this->error('申请状态已变更,请刷新后重试');
|
|
} else {
|
|
$data['user_id'] = $uid;
|
|
$data['createtime'] = time();
|
|
try { Db::name('user_enterprise_cert')->insert($data); }
|
|
catch (\Throwable $e) { $this->error('提交失败,请刷新后重试'); }
|
|
}
|
|
$this->success('企业资质已提交,请等待审核');
|
|
}
|
|
|
|
public function recognizeEnterpriseLicense()
|
|
{
|
|
$image = trim((string)$this->request->post('license_image', ''));
|
|
$attachment = Db::name('attachment')->where('user_id', $this->uid())->where('url', $image)->where('category', 'cert')->find();
|
|
if (!$attachment || (int)$attachment['filesize'] > 10 * 1024 * 1024 || !in_array(strtolower((string)$attachment['mimetype']), ['image/jpeg', 'image/png', 'image/webp'], true)) {
|
|
$this->error('请上传本人营业执照图片');
|
|
}
|
|
try {
|
|
$imageBytes = EnterpriseLicenseImage::read($attachment);
|
|
} catch (\Throwable $e) {
|
|
$this->error('图片读取失败,请重新上传或手动填写');
|
|
}
|
|
try {
|
|
$reply = TencentVerifyService::request('ocr', '2018-11-19', 'BizLicenseOCR', ['ImageBase64' => base64_encode($imageBytes)]);
|
|
$data = $reply['Response'] ?? [];
|
|
if (!empty($data['Error']) || empty($data['Name']) || empty($data['RegNum'])) {
|
|
$this->error('未能识别营业执照,请重新上传或手动填写');
|
|
}
|
|
$this->success('识别成功,请核对信息', ['company_name' => (string)$data['Name'], 'license_no' => (string)$data['RegNum'], 'legal_person' => (string)($data['Person'] ?? '')]);
|
|
} catch (\think\exception\HttpResponseException $e) {
|
|
throw $e;
|
|
} catch (\Throwable $e) {
|
|
$this->error('营业执照识别暂不可用,请手动填写');
|
|
}
|
|
}
|
|
}
|