文件
yjyf/server/application/api/controller/Security.php
MiAn b71980dd6b feat: 新增账号安全模块及资质校验功能
- 新增账号安全页面及支付密码验证组件
- 新增用户类型选择流程及商家资质管理
- 新增订单资质校验逻辑,师傅需满足订单证书要求
- 新增企业资质认证相关数据库表及接口
- 优化用户列表展示,区分个人与商家用户类型
2026-11-21 15:37:01 +08:00

134 行
7.3 KiB
PHP

<?php
namespace app\api\controller;
use app\common\library\AccountSecurity;
use app\common\library\WxLogin;
use app\common\library\TencentVerifyService;
use app\common\library\EnterpriseLicenseImage;
use think\Db;
class Security extends Base
{
private function verifyBoundPhone($code, $mobile)
{
if (trim((string)$code) === '') $this->error('请先验证绑定的微信手机号');
$phone = WxLogin::getPhoneNumber((string)$code);
$verified = isset($phone['purePhoneNumber']) ? (string)$phone['purePhoneNumber'] : (string)($phone['phoneNumber'] ?? '');
if (isset($phone['error']) || $verified === '' || !hash_equals((string)$mobile, $verified)) {
$this->error('手机号验证失败,请使用绑定手机号重新验证');
}
}
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();
$this->success('', [
'has_login_password' => !empty($user['password']),
'has_payment_password' => $security && $security['payment_password_hash'] !== '',
'enterprise_cert' => $cert ?: null,
]);
}
public function changeLoginPassword()
{
$uid = $this->uid();
$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位非空字符');
$user = Db::name('user')->field('password,mobile')->where('id', $uid)->find();
if (!empty($user['password'])) {
try { AccountSecurity::verify($uid, 'login', $old); } catch (\Throwable $e) { $this->error($e->getMessage()); }
$ignoreOld = false;
} else {
$this->verifyBoundPhone($this->request->post('phone_code', ''), $user['mobile']);
$ignoreOld = true;
}
if (!$this->auth->changepwd($new, $old, $ignoreOld)) $this->error($this->auth->getError() ?: '设置失败');
\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位数字');
try { AccountSecurity::verify($uid, 'login', (string)$this->request->post('login_password', '')); }
catch (\Throwable $e) { $this->error($e->getMessage()); }
$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('支付密码设置成功');
}
public function changePaymentPassword()
{
$uid = $this->uid();
$new = (string)$this->request->post('new_password', '');
if (!preg_match('/^\d{6}$/', $new)) $this->error('支付密码须为6位数字');
$oldHash = (string)Db::name('user_security')->where('user_id', $uid)->value('payment_password_hash');
try { AccountSecurity::verify($uid, 'payment', (string)$this->request->post('old_password', '')); }
catch (\Throwable $e) { $this->error($e->getMessage()); }
$updated = Db::name('user_security')->where('user_id', $uid)->where('payment_password_hash', $oldHash)->update(['payment_password_hash' => password_hash($new, PASSWORD_DEFAULT)]);
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('营业执照识别暂不可用,请手动填写');
}
}
}