feat: 代理登录/注册改图形数字验证码(SVG无GD依赖、一次性校验);后台内容区全屏铺满
This commit is contained in:
1 parent
423ac0d3b5
commit
d5a3183895
10 files changed
+272
-71
No files matched your search
@@ -7,6 +7,7 @@ use App\Core\Database;
|
||||
use App\Core\Validator;
|
||||
use App\Services\JwtService;
|
||||
use App\Services\TurnstileService;
|
||||
use App\Services\CaptchaService;
|
||||
|
||||
/**
|
||||
* 认证控制器
|
||||
@@ -53,11 +54,17 @@ class AuthController extends Controller
|
||||
$this->error('请输入用户名或邮箱');
|
||||
}
|
||||
|
||||
// Cloudflare Turnstile(按系统配置开关与入口白名单)
|
||||
$portalKey = $portal === 'agent' ? 'agent_login' : 'admin_login';
|
||||
$turnstileToken = trim((string) ($input['turnstile_token'] ?? ''));
|
||||
if (!TurnstileService::verifyFor($portalKey, $turnstileToken !== '' ? $turnstileToken : null, clientIp())) {
|
||||
$this->error('人机验证失败,请完成验证后重试');
|
||||
// 人机验证:代理入口用图形数字验证码,管理员入口用 Cloudflare Turnstile(按配置开关)
|
||||
if ($portal === 'agent') {
|
||||
$captchaCode = trim((string) ($input['captcha_code'] ?? ''));
|
||||
if (!CaptchaService::verify($captchaCode)) {
|
||||
$this->error('图形验证码错误或已过期,请刷新后重试');
|
||||
}
|
||||
} else {
|
||||
$turnstileToken = trim((string) ($input['turnstile_token'] ?? ''));
|
||||
if (!TurnstileService::verifyFor('admin_login', $turnstileToken !== '' ? $turnstileToken : null, clientIp())) {
|
||||
$this->error('人机验证失败,请完成验证后重试');
|
||||
}
|
||||
}
|
||||
|
||||
$db = Database::getInstance();
|
||||
@@ -133,10 +140,10 @@ class AuthController extends Controller
|
||||
$this->error('两次输入的密码不一致');
|
||||
}
|
||||
|
||||
// Cloudflare Turnstile(按系统配置开关与入口白名单)
|
||||
$turnstileToken = trim((string) ($input['turnstile_token'] ?? ''));
|
||||
if (!TurnstileService::verifyFor('agent_register', $turnstileToken !== '' ? $turnstileToken : null, clientIp())) {
|
||||
$this->error('人机验证失败,请完成验证后重试');
|
||||
// 图形数字验证码(代理注册)
|
||||
$captchaCode = trim((string) ($input['captcha_code'] ?? ''));
|
||||
if (!CaptchaService::verify($captchaCode)) {
|
||||
$this->error('图形验证码错误或已过期,请刷新后重试');
|
||||
}
|
||||
|
||||
// 手机号格式验证
|
||||
|
||||
@@ -832,6 +832,22 @@ class PublicController extends Controller
|
||||
], 'ok');
|
||||
}
|
||||
|
||||
/**
|
||||
* 图形数字验证码(SVG,代理登录/注册用)
|
||||
* GET /api/public/captcha
|
||||
*/
|
||||
public function captcha(): void
|
||||
{
|
||||
[, $svg] = \App\Services\CaptchaService::generate();
|
||||
|
||||
header('Content-Type: image/svg+xml; charset=UTF-8');
|
||||
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
|
||||
header('Pragma: no-cache');
|
||||
header('Expires: 0');
|
||||
echo $svg;
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取公告列表(公开/客户端)
|
||||
* GET /api/public/announcements?target=client&project_id=1&limit=10
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
namespace App\Services;
|
||||
|
||||
/**
|
||||
* 图形数字验证码(SVG 输出,无 GD 扩展依赖)
|
||||
* 一次性使用:生成即写入 Session,校验后立即失效
|
||||
*/
|
||||
class CaptchaService
|
||||
{
|
||||
/** 验证码位数 */
|
||||
public const LENGTH = 4;
|
||||
|
||||
/** 有效期(秒) */
|
||||
public const TTL = 300;
|
||||
|
||||
/**
|
||||
* 生成验证码:写入 Session 并返回 [code, svg]
|
||||
* @return array{0:string,1:string}
|
||||
*/
|
||||
public static function generate(): array
|
||||
{
|
||||
$code = '';
|
||||
for ($i = 0; $i < self::LENGTH; $i++) {
|
||||
$code .= (string) random_int(0, 9);
|
||||
}
|
||||
|
||||
if (session_status() === PHP_SESSION_ACTIVE) {
|
||||
$_SESSION['captcha_code'] = $code;
|
||||
$_SESSION['captcha_time'] = time();
|
||||
}
|
||||
|
||||
return [$code, self::render($code)];
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验验证码(比对后立即失效)
|
||||
*/
|
||||
public static function verify(?string $input): bool
|
||||
{
|
||||
$code = (string) ($_SESSION['captcha_code'] ?? '');
|
||||
$time = (int) ($_SESSION['captcha_time'] ?? 0);
|
||||
unset($_SESSION['captcha_code'], $_SESSION['captcha_time']);
|
||||
|
||||
if ($code === '' || $time <= 0) {
|
||||
return false;
|
||||
}
|
||||
if (time() - $time > self::TTL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$input = trim((string) $input);
|
||||
if ($input === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return hash_equals($code, $input);
|
||||
}
|
||||
|
||||
/**
|
||||
* 渲染 SVG 图形
|
||||
*/
|
||||
private static function render(string $code): string
|
||||
{
|
||||
$w = 132;
|
||||
$h = 40;
|
||||
|
||||
$textColors = ['#1f2937', '#374151', '#1e3a8a', '#3f3f46', '#4c1d95', '#7c2d12', '#065f46'];
|
||||
$bgColors = ['#f8fafc', '#f1f5f9', '#fdf4ff', '#ecfeff', '#f7fee7'];
|
||||
$lineColors = ['#c7d2fe', '#fbcfe8', '#a5f3fc', '#bbf7d0'];
|
||||
|
||||
$bg = $bgColors[random_int(0, count($bgColors) - 1)];
|
||||
$line = $lineColors[random_int(0, count($lineColors) - 1)];
|
||||
|
||||
$svg = [];
|
||||
$svg[] = sprintf(
|
||||
'<svg xmlns="http://www.w3.org/2000/svg" width="%d" height="%d" viewBox="0 0 %d %d">',
|
||||
$w, $h, $w, $h
|
||||
);
|
||||
$svg[] = sprintf('<rect width="%d" height="%d" fill="%s"/>', $w, $h, $bg);
|
||||
|
||||
// 干扰线
|
||||
for ($i = 0; $i < 4; $i++) {
|
||||
$svg[] = sprintf(
|
||||
'<line x1="%d" y1="%d" x2="%d" y2="%d" stroke="%s" stroke-width="1.6" stroke-opacity="0.75"/>',
|
||||
random_int(-8, $w), random_int(0, $h),
|
||||
random_int(-8, $w), random_int(0, $h),
|
||||
$line
|
||||
);
|
||||
}
|
||||
|
||||
// 干扰曲线
|
||||
$svg[] = sprintf(
|
||||
'<path d="M0 %d Q %d %d %d %d T %d %d" fill="none" stroke="%s" stroke-width="1.6" stroke-opacity="0.6"/>',
|
||||
random_int(8, 40),
|
||||
(int) ($w / 4), random_int(0, $h),
|
||||
(int) ($w / 2), random_int(0, $h),
|
||||
$w, random_int(8, 40),
|
||||
$line
|
||||
);
|
||||
|
||||
// 噪点
|
||||
for ($i = 0; $i < 26; $i++) {
|
||||
$svg[] = sprintf(
|
||||
'<circle cx="%d" cy="%d" r="%d" fill="#64748b" fill-opacity="0.3"/>',
|
||||
random_int(0, $w), random_int(0, $h), random_int(1, 2)
|
||||
);
|
||||
}
|
||||
|
||||
// 数字(逐位随机大小/旋转/位置)
|
||||
$len = strlen($code);
|
||||
$cell = (int) (($w - 24) / $len);
|
||||
for ($i = 0; $i < $len; $i++) {
|
||||
$x = 16 + $i * $cell + random_int(-3, 3);
|
||||
$y = random_int(28, 33);
|
||||
$size = random_int(22, 27);
|
||||
$rotate = random_int(-22, 22);
|
||||
$cx = $x + 8;
|
||||
$cy = $y - 7;
|
||||
$color = $textColors[random_int(0, count($textColors) - 1)];
|
||||
|
||||
$svg[] = sprintf(
|
||||
'<text x="%d" y="%d" font-size="%d" font-family="Consolas, Monaco, \'Courier New\', monospace" font-weight="bold" fill="%s" transform="rotate(%d %d %d)">%s</text>',
|
||||
$x, $y, $size, htmlspecialchars($code[$i], ENT_QUOTES),
|
||||
$rotate, $cx, $cy, htmlspecialchars($code[$i], ENT_QUOTES)
|
||||
);
|
||||
}
|
||||
|
||||
$svg[] = '</svg>';
|
||||
|
||||
return implode('', $svg);
|
||||
}
|
||||
}
|
||||
@@ -50,6 +50,9 @@ $router->group('api/public', function ($router) {
|
||||
// 人机验证公开配置(登录页用;客户端激活/验证接口从不校验)
|
||||
$router->get('turnstile-config', [PublicController::class, 'turnstileConfig']);
|
||||
|
||||
// 图形数字验证码(代理登录/注册用)
|
||||
$router->get('captcha', [PublicController::class, 'captcha']);
|
||||
|
||||
// 支付方式(前台展示用)
|
||||
$router->get('payment/options', [PublicController::class, 'paymentOptions']);
|
||||
|
||||
|
||||
Reference in new issue
Block a user