LoginPage.tsx 17 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
import { useCallback, useEffect, useRef, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { App as AntdApp, Button, Form, Input, Spin } from 'antd';
import type { FormInstance } from 'antd';
import { LockOutlined, SafetyOutlined, UserOutlined } from '@ant-design/icons';
import { BrandMark } from '@hm/ui';
import { useAuthStore } from '@/store/useAuthStore';
import { fetchCaptcha, type CaptchaImage } from './captcha';
import '@/theme/design.css';
import './login.css';

interface LoginForm {
  username: string;
  password: string;
  captcha: string;
}

/**
 * 服务端验证码是**一次性**的:只要提交过一次(无论密码对错),这张图就在服务端被消耗或
 * 记了一次错,必须换新的。所以这个集合决定「哪些失败要立刻换图」。
 */
const CAPTCHA_CONSUMING_ERRORS = new Set([
  'captcha_invalid',
  'captcha_required',
  'invalid_credentials',
  'too_many_attempts',
]);

/** 验证码位数,与服务端 captcha.ts 的 CODE_LENGTH 对应 */
const CAPTCHA_LENGTH = 4;

/**
 * 登录页:一张夜色底图 + 一台「落点仪器」。
 *
 * 左侧不是装饰画:经纬网、等值线、散点都是地图语言;一条从地址标签出发的虚线
 * 落到一个带坐标的点位上,把「地址 → 坐标 → 点位」这件事本身讲清楚。
 * 右侧登录卡沿用全站的毛玻璃语言,只是换成深色底图上的浅色玻璃。
 */
export function LoginPage() {
  const navigate = useNavigate();
  const { message } = AntdApp.useApp();
  const login = useAuthStore((s) => s.login);
  const [submitting, setSubmitting] = useState(false);
  const [captcha, setCaptcha] = useState<CaptchaImage | null>(null);
  const [captchaLoading, setCaptchaLoading] = useState(true);
  const [captchaError, setCaptchaError] = useState<string | null>(null);
  const formRef = useRef<FormInstance<LoginForm>>(null);
  /** 请求序号:狂点「换一张」时只认最后一次回来的那张,避免旧图把新图盖掉 */
  const captchaSeq = useRef(0);

  const refreshCaptcha = useCallback(async () => {
    const seq = (captchaSeq.current += 1);
    setCaptchaLoading(true);
    setCaptchaError(null);
    try {
      const next = await fetchCaptcha();
      if (seq !== captchaSeq.current) return;
      setCaptcha(next);
    } catch (err) {
      if (seq !== captchaSeq.current) return;
      setCaptcha(null);
      setCaptchaError(err instanceof Error ? err.message : '验证码加载失败');
    } finally {
      if (seq === captchaSeq.current) setCaptchaLoading(false);
    }
  }, []);

  useEffect(() => {
    void refreshCaptcha();
  }, [refreshCaptcha]);

  const onFinish = async (values: LoginForm) => {
    if (!captcha) {
      message.error(captchaError ?? '验证码还没加载出来,请点击图片重试');
      return;
    }
    setSubmitting(true);
    const ok = await login(values.username, values.password, {
      id: captcha.id,
      code: values.captcha,
    });
    setSubmitting(false);
    if (ok) {
      message.success('登录成功');
      navigate('/', { replace: true });
      return;
    }

    const { error, errorCode } = useAuthStore.getState();
    message.error(error ?? '登录失败');

    if (errorCode && CAPTCHA_CONSUMING_ERRORS.has(errorCode)) {
      // 这张图已经被服务端消费掉了,留着只会让人一直收到「验证码错误」
      formRef.current?.setFieldValue('captcha', '');
      void refreshCaptcha();
    }
  };

  return (
    <div className="hm-login">
      <div className="hm-login__scene" aria-hidden="true">
        <svg
          className="hm-login__chart"
          viewBox="0 0 1600 900"
          preserveAspectRatio="xMidYMid slice"
          focusable="false"
        >
          <defs>
            <radialGradient id="hmLoginHeatHot">
              <stop offset="0%" stopColor="#ff2a2a" stopOpacity="0.5" />
              <stop offset="42%" stopColor="#ff7600" stopOpacity="0.26" />
              <stop offset="100%" stopColor="#ff7600" stopOpacity="0" />
            </radialGradient>
            <radialGradient id="hmLoginHeatWarm">
              <stop offset="0%" stopColor="#ffce00" stopOpacity="0.34" />
              <stop offset="55%" stopColor="#7ade3c" stopOpacity="0.14" />
              <stop offset="100%" stopColor="#7ade3c" stopOpacity="0" />
            </radialGradient>
            <radialGradient id="hmLoginHeatCool">
              <stop offset="0%" stopColor="#18a8ff" stopOpacity="0.3" />
              <stop offset="100%" stopColor="#18a8ff" stopOpacity="0" />
            </radialGradient>
            <filter id="hmLoginBlur" x="-60%" y="-60%" width="220%" height="220%">
              <feGaussianBlur stdDeviation="34" />
            </filter>
            <pattern id="hmLoginGrid" width="100" height="100" patternUnits="userSpaceOnUse">
              <path
                d="M100 0H0V100"
                fill="none"
                stroke="#bcd0ff"
                strokeOpacity="0.07"
                strokeWidth="1"
              />
            </pattern>
            <pattern id="hmLoginGridMajor" width="400" height="400" patternUnits="userSpaceOnUse">
              <path
                d="M400 0H0V400"
                fill="none"
                stroke="#bcd0ff"
                strokeOpacity="0.12"
                strokeWidth="1"
              />
            </pattern>
          </defs>

          <rect width="1600" height="900" fill="url(#hmLoginGrid)" />
          <rect width="1600" height="900" fill="url(#hmLoginGridMajor)" />

          {/* 行政区界与海岸线(示意) */}
          <g
            fill="none"
            stroke="#9fb6ff"
            strokeOpacity="0.22"
            strokeWidth="1.1"
            strokeLinecap="round"
          >
            <path d="M-20 206C154 178 246 268 398 246C536 226 588 152 754 166C922 180 968 268 1132 262C1290 256 1402 202 1620 236" />
            <path d="M-20 314C120 300 208 356 344 342C474 328 544 250 700 262" />
            <path d="M-20 864C140 826 254 786 388 828C520 870 604 786 764 796C902 804 986 866 1104 842C1246 814 1390 758 1620 802" />
          </g>

          {/* 热力底色 */}
          <g filter="url(#hmLoginBlur)" className="hm-login__heat">
            <ellipse cx="900" cy="430" rx="250" ry="190" fill="url(#hmLoginHeatHot)" />
            <ellipse cx="1190" cy="760" rx="300" ry="210" fill="url(#hmLoginHeatWarm)" />
            <ellipse cx="322" cy="184" rx="260" ry="190" fill="url(#hmLoginHeatCool)" />
            <ellipse cx="690" cy="600" rx="200" ry="160" fill="url(#hmLoginHeatWarm)" opacity="0.55" />
          </g>

          {/* 等值线 */}
          <g fill="none" strokeLinecap="round">
            <path
              d="M900 314C996 314 1076 366 1076 430C1076 494 996 546 900 546C804 546 724 494 724 430C724 366 804 314 900 314Z"
              stroke="#ffce00"
              strokeOpacity="0.34"
              strokeWidth="1.2"
            />
            <path
              d="M900 268C1028 268 1130 340 1130 430C1130 520 1028 592 900 592C772 592 670 520 670 430C670 340 772 268 900 268Z"
              stroke="#ffce00"
              strokeOpacity="0.18"
              strokeWidth="1"
              strokeDasharray="5 7"
            />
            <path
              d="M900 222C1060 222 1184 314 1184 430C1184 546 1060 638 900 638C740 638 616 546 616 430C616 314 740 222 900 222Z"
              stroke="#ff7600"
              strokeOpacity="0.12"
              strokeWidth="1"
              strokeDasharray="2 9"
            />
          </g>

          {/* 散落点位 */}
          <g fill="#e8eeff">
            <circle cx="852" cy="404" r="2.2" opacity="0.6" />
            <circle cx="938" cy="458" r="1.8" opacity="0.5" />
            <circle cx="886" cy="486" r="2.6" opacity="0.7" />
            <circle cx="830" cy="450" r="1.6" opacity="0.4" />
            <circle cx="962" cy="402" r="2" opacity="0.55" />
            <circle cx="1002" cy="492" r="1.7" opacity="0.42" />
            <circle cx="762" cy="482" r="1.5" opacity="0.34" />
            <circle cx="1150" cy="716" r="2.4" opacity="0.5" />
            <circle cx="1226" cy="782" r="2" opacity="0.44" />
            <circle cx="1118" cy="812" r="1.8" opacity="0.38" />
            <circle cx="1288" cy="734" r="1.6" opacity="0.34" />
            <circle cx="1064" cy="742" r="1.5" opacity="0.3" />
            <circle cx="406" cy="238" r="1.8" opacity="0.32" />
            <circle cx="498" cy="284" r="1.5" opacity="0.26" />
            <circle cx="1348" cy="332" r="1.7" opacity="0.3" />
            <circle cx="764" cy="230" r="1.5" opacity="0.28" />
          </g>

          {/* 地址 → 坐标 → 落点 */}
          <g className="hm-login__map-note">
            <rect
              x="470"
              y="290"
              width="238"
              height="40"
              rx="9"
              fill="rgba(12,18,44,0.6)"
              stroke="rgba(214,226,255,0.26)"
            />
            <circle cx="492" cy="310" r="3.2" fill="#fcc929" />
            <text x="508" y="315" fill="rgba(238,242,255,0.9)">
              南山区 · 粤海街道
            </text>
            <path
              className="hm-login__route"
              d="M714 310C774 310 812 428 884 430"
              fill="none"
              stroke="#fcc929"
              strokeOpacity="0.55"
              strokeWidth="1.3"
              strokeDasharray="3 7"
            />
            <text x="932" y="404" fill="rgba(214,226,255,0.62)">
              113.94°E
            </text>
            <text x="932" y="422" fill="rgba(214,226,255,0.42)">
              22.54°N
            </text>
          </g>

          {/* 落点脉冲与十字标 */}
          <g className="hm-login__pin">
            <circle className="hm-login__pin-ring" cx="900" cy="430" r="16" />
            <circle
              className="hm-login__pin-ring hm-login__pin-ring--late"
              cx="900"
              cy="430"
              r="16"
            />
            <g stroke="#ffce00" strokeOpacity="0.7" strokeWidth="1">
              <line x1="856" y1="430" x2="874" y2="430" />
              <line x1="926" y1="430" x2="944" y2="430" />
              <line x1="900" y1="386" x2="900" y2="404" />
              <line x1="900" y1="456" x2="900" y2="474" />
            </g>
            <circle cx="900" cy="430" r="6" fill="#ffce00" />
            <circle cx="900" cy="430" r="2" fill="#151b3c" />
          </g>
        </svg>
        <div className="hm-login__grain" />
        <div className="hm-login__vignette" />
      </div>

      <header className="hm-login__top">
        <BrandMark size={38} />
        <div className="hm-login__geo">
          <span className="hm-login__geo-rule" />
          <span className="hm-login__geo-coord">22°32′N · 114°03′E</span>
          <span className="hm-login__geo-place">深圳市</span>
        </div>
      </header>

      <main className="hm-login__main">
        <section className="hm-login__intro">
          <p className="hm-login__eyebrow">
            <span className="hm-login__cross" />
            智能网联汽车监管
          </p>
          <h1 className="hm-login__display">
            出险热力图
            <span className="hm-login__display-dot" />
          </h1>
          <p className="hm-login__lede">每一起出险,都落回它发生的那条街。</p>

          <div className="hm-login__trace" aria-hidden="true">
            <span className="hm-login__trace-node">地址</span>
            <span className="hm-login__trace-line">
              <i />
            </span>
            <span className="hm-login__trace-node">坐标</span>
            <span className="hm-login__trace-line">
              <i style={{ animationDelay: '1.8s' }} />
            </span>
            <span className="hm-login__trace-node hm-login__trace-node--pin">点位</span>
          </div>

          <div className="hm-login__legend" aria-hidden="true">
            <div className="hm-login__legend-head">
              <span>出险密度</span>
              <span>低 / 高</span>
            </div>
            <div className="hm-login__legend-ramp" />
          </div>
        </section>
      </main>

      <aside className="hm-login__panel">
        <span className="hm-login__spine" aria-hidden="true">
          113.94°E · 22.54°N
        </span>
        <div className="hm-login__card">
          <div className="hm-login__card-head">
            <p className="hm-login__card-kicker">Access 01</p>
            <span className="hm-login__card-mark" aria-hidden="true" />
          </div>
          <h2 className="hm-login__card-title">进入监管平台</h2>
          <p className="hm-login__card-sub">使用管理员分配的账号与密码</p>

          <Form<LoginForm>
            ref={formRef}
            className="hm-login__form"
            layout="vertical"
            requiredMark={false}
            onFinish={onFinish}
            disabled={submitting}
          >
            <Form.Item
              name="username"
              label={
                <span className="hm-login__label">
                  <span className="hm-login__label-no" aria-hidden="true">
                    01
                  </span>
                  用户名
                </span>
              }
              rules={[{ required: true, message: '请输入用户名' }]}
            >
              <Input
                prefix={<UserOutlined />}
                placeholder="请输入用户名"
                autoComplete="username"
                autoFocus
              />
            </Form.Item>
            <Form.Item
              name="password"
              label={
                <span className="hm-login__label">
                  <span className="hm-login__label-no" aria-hidden="true">
                    02
                  </span>
                  密码
                </span>
              }
              rules={[{ required: true, message: '请输入密码' }]}
            >
              <Input.Password
                prefix={<LockOutlined />}
                placeholder="请输入密码"
                autoComplete="current-password"
              />
            </Form.Item>
            <Form.Item
              name="captcha"
              label={
                <span className="hm-login__label">
                  <span className="hm-login__label-no" aria-hidden="true">
                    03
                  </span>
                  验证码
                  <button
                    type="button"
                    className="hm-login__label-action"
                    onClick={() => void refreshCaptcha()}
                    disabled={submitting || captchaLoading}
                  >
                    换一张
                  </button>
                </span>
              }
              rules={[
                { required: true, message: '请输入验证码' },
                { len: CAPTCHA_LENGTH, message: `验证码为 ${CAPTCHA_LENGTH} 位` },
              ]}
              // 视觉上把「看不清就点图」说清楚:这张图是唯一的提示入口
              extra={captchaError ? <span className="hm-login__captcha-error">{captchaError}</span> : null}
            >
              <Input
                prefix={<SafetyOutlined />}
                placeholder="4 位验证码"
                // 验证码是一次性口令,不能让浏览器/密码管理器记住或自动填
                autoComplete="off"
                spellCheck={false}
                autoCapitalize="characters"
                maxLength={CAPTCHA_LENGTH}
                className="hm-login__captcha-input"
                suffix={
                  <button
                    type="button"
                    className="hm-login__captcha-btn"
                    onClick={() => void refreshCaptcha()}
                    disabled={submitting}
                    title="看不清?点击换一张"
                    aria-label="刷新验证码"
                  >
                    {captcha && !captchaError ? (
                      <img src={captcha.image} alt="图形验证码" draggable={false} />
                    ) : (
                      <span className="hm-login__captcha-fallback">
                        {captchaLoading ? <Spin size="small" /> : '点击重试'}
                      </span>
                    )}
                    {captchaLoading && captcha ? (
                      <span className="hm-login__captcha-mask" aria-hidden="true" />
                    ) : null}
                  </button>
                }
              />
            </Form.Item>
            <Form.Item className="hm-login__submit-item">
              <Button type="primary" htmlType="submit" block loading={submitting}>
                <span>登录</span>
                <span className="hm-login__submit-arrow" aria-hidden="true">
                  →
                </span>
              </Button>
            </Form.Item>
          </Form>

          <div className="hm-login__card-rule" />
          <div className="hm-login__card-serial">
            <span>SZ · Supervision</span>
            <span>22°32′N 114°03′E</span>
          </div>
        </div>
      </aside>
    </div>
  );
}