From d5a3183895a00f0009d1d83554133b2d156866ef Mon Sep 17 00:00:00 2001 From: root Date: Wed, 21 Oct 2026 15:04:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BB=A3=E7=90=86=E7=99=BB=E5=BD=95/?= =?UTF-8?q?=E6=B3=A8=E5=86=8C=E6=94=B9=E5=9B=BE=E5=BD=A2=E6=95=B0=E5=AD=97?= =?UTF-8?q?=E9=AA=8C=E8=AF=81=E7=A0=81=EF=BC=88SVG=E6=97=A0GD=E4=BE=9D?= =?UTF-8?q?=E8=B5=96=E3=80=81=E4=B8=80=E6=AC=A1=E6=80=A7=E6=A0=A1=E9=AA=8C?= =?UTF-8?q?=EF=BC=89=EF=BC=9B=E5=90=8E=E5=8F=B0=E5=86=85=E5=AE=B9=E5=8C=BA?= =?UTF-8?q?=E5=85=A8=E5=B1=8F=E9=93=BA=E6=BB=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/Controllers/AuthController.php | 25 ++-- backend/app/Controllers/PublicController.php | 16 +++ backend/app/Services/CaptchaService.php | 132 +++++++++++++++++++ backend/routes/api.php | 3 + frontend/src/components/ImageCaptcha.vue | 69 ++++++++++ frontend/src/stores/user.js | 7 +- frontend/src/styles/common.css | 9 +- frontend/src/views/Login.vue | 2 +- frontend/src/views/agent/AgentLogin.vue | 39 ++---- frontend/src/views/agent/AgentRegister.vue | 41 ++---- 10 files changed, 272 insertions(+), 71 deletions(-) create mode 100644 backend/app/Services/CaptchaService.php create mode 100644 frontend/src/components/ImageCaptcha.vue diff --git a/backend/app/Controllers/AuthController.php b/backend/app/Controllers/AuthController.php index b4a8e30..d35ea34 100644 --- a/backend/app/Controllers/AuthController.php +++ b/backend/app/Controllers/AuthController.php @@ -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('图形验证码错误或已过期,请刷新后重试'); } // 手机号格式验证 diff --git a/backend/app/Controllers/PublicController.php b/backend/app/Controllers/PublicController.php index f563f24..ff02839 100644 --- a/backend/app/Controllers/PublicController.php +++ b/backend/app/Controllers/PublicController.php @@ -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 diff --git a/backend/app/Services/CaptchaService.php b/backend/app/Services/CaptchaService.php new file mode 100644 index 0000000..0cd1f7e --- /dev/null +++ b/backend/app/Services/CaptchaService.php @@ -0,0 +1,132 @@ + 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( + '', + $w, $h, $w, $h + ); + $svg[] = sprintf('', $w, $h, $bg); + + // 干扰线 + for ($i = 0; $i < 4; $i++) { + $svg[] = sprintf( + '', + random_int(-8, $w), random_int(0, $h), + random_int(-8, $w), random_int(0, $h), + $line + ); + } + + // 干扰曲线 + $svg[] = sprintf( + '', + 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( + '', + 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( + '%s', + $x, $y, $size, htmlspecialchars($code[$i], ENT_QUOTES), + $rotate, $cx, $cy, htmlspecialchars($code[$i], ENT_QUOTES) + ); + } + + $svg[] = ''; + + return implode('', $svg); + } +} diff --git a/backend/routes/api.php b/backend/routes/api.php index e26117d..0ae36c8 100644 --- a/backend/routes/api.php +++ b/backend/routes/api.php @@ -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']); diff --git a/frontend/src/components/ImageCaptcha.vue b/frontend/src/components/ImageCaptcha.vue new file mode 100644 index 0000000..6707dd4 --- /dev/null +++ b/frontend/src/components/ImageCaptcha.vue @@ -0,0 +1,69 @@ + + + + + diff --git a/frontend/src/stores/user.js b/frontend/src/stores/user.js index fd89126..a0844a9 100644 --- a/frontend/src/stores/user.js +++ b/frontend/src/stores/user.js @@ -11,12 +11,11 @@ export const useUserStore = defineStore('user', () => { * @param {string} account 用户名或邮箱 * @param {string} password 密码 * @param {'admin'|'agent'} portal 登录入口 - * @param {string} turnstileToken Turnstile 人机验证 token + * @param {object} extra 附加参数(admin: {turnstile_token} / agent: {captcha_code}) */ - async function login(account, password, portal = 'admin', turnstileToken = '') { + async function login(account, password, portal = 'admin', extra = {}) { const endpoint = portal === 'agent' ? '/auth/agent-login' : '/auth/admin-login' - const body = { username: account, password } - if (turnstileToken) body.turnstile_token = turnstileToken + const body = { username: account, password, ...(extra || {}) } const res = await request.post(endpoint, body) user.value = res.data.user token.value = res.data.token.access_token diff --git a/frontend/src/styles/common.css b/frontend/src/styles/common.css index 600f421..a4bfd5a 100644 --- a/frontend/src/styles/common.css +++ b/frontend/src/styles/common.css @@ -69,9 +69,12 @@ /* 全局公共样式 - 列表页统一显示方式 */ -/* 页面容器 */ -[class$="-page"] { - max-width: 1600px; +/* 页面容器:后台内容全屏铺满右侧内容区 + (去掉历史 1600px 限宽;!important 用于覆盖各页面 scoped 样式中的限宽) */ +[class$="-page"], +.page-container { + width: 100%; + max-width: none !important; margin: 0 auto; } diff --git a/frontend/src/views/Login.vue b/frontend/src/views/Login.vue index 8b8144a..ec76ed4 100644 --- a/frontend/src/views/Login.vue +++ b/frontend/src/views/Login.vue @@ -91,7 +91,7 @@ async function handleLogin() { } loading.value = true try { - await userStore.login(form.username, form.password, 'admin', turnstileToken.value) + await userStore.login(form.username, form.password, 'admin', { turnstile_token: turnstileToken.value }) ElMessage.success('登录成功') router.push('/dashboard') } catch (e) { diff --git a/frontend/src/views/agent/AgentLogin.vue b/frontend/src/views/agent/AgentLogin.vue index 3a7f241..27c59f8 100644 --- a/frontend/src/views/agent/AgentLogin.vue +++ b/frontend/src/views/agent/AgentLogin.vue @@ -43,8 +43,8 @@ 忘记密码? - - + + @@ -69,60 +69,47 @@ import { useRouter } from 'vue-router' import { ElMessage } from 'element-plus' import { User, Lock, Avatar } from '@element-plus/icons-vue' import { useUserStore } from '@/stores/user' -import TurnstileWidget from '@/components/TurnstileWidget.vue' -import { TURNSTILE_SITE_KEY } from '@/config/turnstile' -import request from '@/api' +import ImageCaptcha from '@/components/ImageCaptcha.vue' const router = useRouter() const userStore = useUserStore() const loading = ref(false) const formRef = ref(null) -const turnstileRef = ref(null) -const turnstileToken = ref('') -const tsEnabled = ref(false) - -function onToken(t) { - turnstileToken.value = t || '' -} +const captchaRef = ref(null) const form = reactive({ account: '', password: '', + captcha: '', remember: false, }) const rules = { account: [{ required: true, message: '请输入用户名或邮箱', trigger: 'blur' }], password: [{ required: true, message: '请输入密码', trigger: 'blur' }], + captcha: [{ required: true, message: '请输入图形验证码', trigger: 'blur' }], } -onMounted(async () => { +onMounted(() => { const saved = localStorage.getItem('agent_remember_account') if (saved) { form.account = saved form.remember = true } - try { - const res = await request.get('/public/turnstile-config') - const d = res.data || {} - tsEnabled.value = !!d.enabled && Array.isArray(d.endpoints) && d.endpoints.includes('agent_login') - } catch { - tsEnabled.value = false - } }) async function handleLogin() { if (!formRef.value) return await formRef.value.validate(async (valid) => { if (!valid) return - if (tsEnabled.value && !turnstileToken.value) { - ElMessage.warning('请先完成人机验证') - turnstileRef.value?.reset?.() + if (!form.captcha.trim()) { + ElMessage.warning('请输入图形验证码') + captchaRef.value?.refresh?.() return } loading.value = true try { - await userStore.login(form.account, form.password, 'agent', turnstileToken.value) + await userStore.login(form.account, form.password, 'agent', { captcha_code: form.captcha.trim() }) if (form.remember) { localStorage.setItem('agent_remember_account', form.account) } else { @@ -131,8 +118,8 @@ async function handleLogin() { ElMessage.success('登录成功') router.push('/dashboard') } catch (e) { - turnstileRef.value?.reset?.() - turnstileToken.value = '' + // 登录失败:验证码已失效,刷新图片 + captchaRef.value?.refresh?.() } finally { loading.value = false } diff --git a/frontend/src/views/agent/AgentRegister.vue b/frontend/src/views/agent/AgentRegister.vue index a046612..3f0c858 100644 --- a/frontend/src/views/agent/AgentRegister.vue +++ b/frontend/src/views/agent/AgentRegister.vue @@ -73,8 +73,8 @@ - - + + @@ -94,34 +94,17 @@