feat: 新增账号安全模块及资质校验功能
- 新增账号安全页面及支付密码验证组件 - 新增用户类型选择流程及商家资质管理 - 新增订单资质校验逻辑,师傅需满足订单证书要求 - 新增企业资质认证相关数据库表及接口 - 优化用户列表展示,区分个人与商家用户类型
这个提交包含在:
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
namespace app\common\library;
|
||||
|
||||
use think\Db;
|
||||
|
||||
/** Shared account credential verification and per-account throttling. */
|
||||
class AccountSecurity
|
||||
{
|
||||
public static function ensure($uid)
|
||||
{
|
||||
$prefix = config('database.prefix');
|
||||
if (!preg_match('/^[A-Za-z0-9_]+$/', $prefix)) {
|
||||
throw new \RuntimeException('数据库配置错误');
|
||||
}
|
||||
Db::execute("INSERT IGNORE INTO `{$prefix}user_security` (`user_id`) VALUES (?)", [(int)$uid]);
|
||||
}
|
||||
|
||||
public static function verify($uid, $kind, $password)
|
||||
{
|
||||
if (!in_array($kind, ['login', 'payment'], true)) {
|
||||
throw new \InvalidArgumentException('验证类型错误');
|
||||
}
|
||||
$attemptsField = $kind === 'payment' ? 'payment_failed_attempts' : 'failed_attempts';
|
||||
$lockedField = $kind === 'payment' ? 'payment_locked_until' : 'locked_until';
|
||||
self::ensure($uid);
|
||||
Db::startTrans();
|
||||
try {
|
||||
$row = Db::name('user_security')->where('user_id', (int)$uid)->lock(true)->find();
|
||||
if ((int)$row[$lockedField] > time()) {
|
||||
throw new \RuntimeException('密码错误次数过多,请15分钟后重试');
|
||||
}
|
||||
if ($kind === 'login') {
|
||||
$user = Db::name('user')->field('password,salt')->where('id', (int)$uid)->find();
|
||||
$correct = $user && $user['password'] !== '' && hash_equals((string)$user['password'], md5(md5($password) . $user['salt']));
|
||||
} elseif ($kind === 'payment') {
|
||||
if ($row['payment_password_hash'] === '') {
|
||||
throw new \RuntimeException('请先在账号安全中设置支付密码');
|
||||
}
|
||||
$correct = password_verify($password, $row['payment_password_hash']);
|
||||
}
|
||||
$failures = $correct ? 0 : (int)$row[$attemptsField] + 1;
|
||||
Db::name('user_security')->where('user_id', (int)$uid)->update([
|
||||
$attemptsField => $failures >= 5 ? 0 : $failures,
|
||||
$lockedField => $failures >= 5 ? time() + 900 : 0,
|
||||
]);
|
||||
Db::commit();
|
||||
} catch (\Throwable $e) {
|
||||
Db::rollback();
|
||||
throw $e;
|
||||
}
|
||||
if (!$correct) {
|
||||
throw new \RuntimeException($failures >= 5 ? '密码错误次数过多,请15分钟后重试' : '密码不正确');
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -221,14 +221,10 @@ class Auth
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($user->loginfailure >= 10 && time() - $user->loginfailuretime < 86400) {
|
||||
$this->setError('Please try again after 1 day');
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($user->password != $this->getEncryptPassword($password, $user->salt)) {
|
||||
$user->save(['loginfailure' => $user->loginfailure + 1, 'loginfailuretime' => time()]);
|
||||
$this->setError('Password is incorrect');
|
||||
try {
|
||||
AccountSecurity::verify((int)$user->id, 'login', $password);
|
||||
} catch (\RuntimeException $e) {
|
||||
$this->setError($e->getMessage() === '密码不正确' ? 'Password is incorrect' : $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
namespace app\common\library;
|
||||
|
||||
use think\Loader;
|
||||
|
||||
/** Reads an already-authorized attachment without accepting a caller-supplied remote URL. */
|
||||
class EnterpriseLicenseImage
|
||||
{
|
||||
public static function read(array $attachment)
|
||||
{
|
||||
$url = (string)($attachment['url'] ?? '');
|
||||
$size = (int)($attachment['filesize'] ?? 0);
|
||||
if ($url === '' || $url[0] !== '/' || $size <= 0 || $size > 10 * 1024 * 1024) {
|
||||
throw new \RuntimeException('营业执照图片无效');
|
||||
}
|
||||
if (($attachment['storage'] ?? '') === 'cos') {
|
||||
$config = get_addon_config('cos');
|
||||
if (!$config || empty($config['bucket']) || empty($config['region']) || empty($config['secretId']) || empty($config['secretKey'])) {
|
||||
throw new \RuntimeException('云存储未配置');
|
||||
}
|
||||
Loader::addNamespace('Qcloud\\Cos', ROOT_PATH . 'addons/cos/library/Qcloud/Cos/');
|
||||
$client = new \Qcloud\Cos\Client([
|
||||
'region' => $config['region'], 'schema' => 'https',
|
||||
'credentials' => ['secretId' => $config['secretId'], 'secretKey' => $config['secretKey']],
|
||||
]);
|
||||
$result = $client->getObject(['Bucket' => $config['bucket'], 'Key' => ltrim($url, '/'), 'Range' => 'bytes=0-10485760']);
|
||||
$bytes = (string)$result['Body'];
|
||||
} elseif (($attachment['storage'] ?? '') === 'local') {
|
||||
$root = realpath(ROOT_PATH . 'public');
|
||||
$file = realpath(ROOT_PATH . 'public' . $url);
|
||||
if (!$root || !$file || strpos($file, $root . DIRECTORY_SEPARATOR) !== 0 || !is_file($file) || !is_readable($file)) {
|
||||
throw new \RuntimeException('营业执照图片不存在');
|
||||
}
|
||||
if (filesize($file) > 10 * 1024 * 1024) throw new \RuntimeException('营业执照图片过大');
|
||||
$bytes = file_get_contents($file);
|
||||
} else {
|
||||
throw new \RuntimeException('不支持的图片存储类型');
|
||||
}
|
||||
if (!is_string($bytes) || $bytes === '' || strlen($bytes) > 10 * 1024 * 1024) {
|
||||
throw new \RuntimeException('营业执照图片读取失败');
|
||||
}
|
||||
return $bytes;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
namespace app\common\library;
|
||||
|
||||
class MasterOrderCredentials
|
||||
{
|
||||
public static function project(array $masterIds, array $skills, array $certificates, $today)
|
||||
{
|
||||
$result = [];
|
||||
foreach ($masterIds as $id) {
|
||||
if ((int)$id > 0) $result[(int)$id] = ['skills' => [], 'certificates' => []];
|
||||
}
|
||||
foreach ($skills as $row) {
|
||||
$id = (int)$row['master_id'];
|
||||
$name = trim((string)$row['skill_name']);
|
||||
if (isset($result[$id]) && $row['status'] === 'normal' && $name !== '' && !in_array($name, $result[$id]['skills'], true)) {
|
||||
$result[$id]['skills'][] = $name;
|
||||
}
|
||||
}
|
||||
foreach ($certificates as $row) {
|
||||
$id = (int)$row['master_id'];
|
||||
$name = trim((string)$row['cert_name']);
|
||||
$valid = (int)$row['is_long_term'] === 1 || (!empty($row['expire_date']) && $row['expire_date'] >= $today);
|
||||
if (isset($result[$id]) && $row['status'] === 'pass' && $valid && $name !== '' && !in_array($name, $result[$id]['certificates'], true)) {
|
||||
$result[$id]['certificates'][] = $name;
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
namespace app\common\library;
|
||||
|
||||
use think\Db;
|
||||
|
||||
class OrderQualification
|
||||
{
|
||||
public static function certificates($masterId)
|
||||
{
|
||||
return Db::name('master_cert')->field('cert_name,cert_code,is_custom,status,is_long_term,expire_date')
|
||||
->where('master_id', (int)$masterId)->where('status', 'pass')->select();
|
||||
}
|
||||
|
||||
public static function allows($requirement, array $certificates, $today)
|
||||
{
|
||||
$requirement = trim((string)$requirement);
|
||||
if ($requirement === '') return true;
|
||||
foreach ($certificates as $cert) {
|
||||
if ((string)$cert['status'] !== 'pass' || (int)$cert['is_custom'] !== 0 || (string)$cert['cert_code'] === 'other') continue;
|
||||
if (trim((string)$cert['cert_name']) !== $requirement) continue;
|
||||
if ((int)$cert['is_long_term'] === 1 || (!empty($cert['expire_date']) && $cert['expire_date'] >= $today)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function forMaster($requirement, $masterId)
|
||||
{
|
||||
if (trim((string)$requirement) === '') return true;
|
||||
return self::allows($requirement, self::certificates($masterId), date('Y-m-d'));
|
||||
}
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户