LoginPage.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. <script setup>
  2. import { computed, onBeforeUnmount, onMounted, reactive, ref } from 'vue';
  3. import { useI18n } from 'vue-i18n';
  4. import { UserOutlined, LockOutlined, KeyOutlined, SettingOutlined } from '@ant-design/icons-vue';
  5. import { HttpUtil, LanguageManager } from '@/utils';
  6. import {
  7. antdThemeConfig,
  8. currentTheme,
  9. theme as themeState,
  10. toggleTheme,
  11. toggleUltra,
  12. pauseAnimationsUntilLeave,
  13. } from '@/composables/useTheme.js';
  14. const { t } = useI18n();
  15. const fetched = ref(false);
  16. const submitting = ref(false);
  17. const twoFactorEnable = ref(false);
  18. const user = reactive({
  19. username: '',
  20. password: '',
  21. twoFactorCode: '',
  22. });
  23. const basePath = window.X_UI_BASE_PATH || '';
  24. const headlineWords = computed(() => [t('pages.login.hello'), t('pages.login.title')]);
  25. const HEADLINE_INTERVAL_MS = 2000;
  26. const headlineIndex = ref(0);
  27. let headlineTimer = null;
  28. onMounted(() => {
  29. headlineTimer = window.setInterval(() => {
  30. headlineIndex.value = (headlineIndex.value + 1) % headlineWords.value.length;
  31. }, HEADLINE_INTERVAL_MS);
  32. });
  33. onBeforeUnmount(() => {
  34. if (headlineTimer != null) window.clearInterval(headlineTimer);
  35. });
  36. onMounted(async () => {
  37. const msg = await HttpUtil.post('/getTwoFactorEnable');
  38. if (msg.success) twoFactorEnable.value = !!msg.obj;
  39. fetched.value = true;
  40. });
  41. async function login() {
  42. submitting.value = true;
  43. try {
  44. const msg = await HttpUtil.post('/login', user);
  45. if (msg.success) window.location.href = basePath + 'panel/';
  46. } finally {
  47. submitting.value = false;
  48. }
  49. }
  50. const lang = ref(LanguageManager.getLanguage());
  51. function onLangChange(next) {
  52. LanguageManager.setLanguage(next);
  53. }
  54. /* Same Light -> Dark -> Ultra Dark -> Light cycle the sidebar's brand
  55. * button uses, so the login chrome offers a one-click theme toggle
  56. * without the popover ceremony. */
  57. function cycleTheme() {
  58. pauseAnimationsUntilLeave('login-theme-cycle');
  59. if (!themeState.isDark) {
  60. toggleTheme();
  61. if (themeState.isUltra) toggleUltra();
  62. } else if (!themeState.isUltra) {
  63. toggleUltra();
  64. } else {
  65. toggleUltra();
  66. toggleTheme();
  67. }
  68. }
  69. </script>
  70. <template>
  71. <a-config-provider :theme="antdThemeConfig">
  72. <a-layout class="login-app" :class="{ 'is-dark': themeState.isDark, 'is-ultra': themeState.isUltra }">
  73. <a-layout-content class="login-content">
  74. <!-- Floating chrome at top-right: theme cycle (Light/Dark/Ultra)
  75. plus a language picker hidden behind the gear popover. -->
  76. <div class="login-toolbar">
  77. <button type="button" class="theme-cycle" :aria-label="t('menu.theme')" :title="t('menu.theme')"
  78. @click="cycleTheme">
  79. <svg v-if="!themeState.isDark" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"
  80. stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
  81. <circle cx="12" cy="12" r="4" />
  82. <path
  83. d="M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M6.34 17.66l-1.41 1.41M19.07 4.93l-1.41 1.41" />
  84. </svg>
  85. <svg v-else-if="!themeState.isUltra" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"
  86. stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
  87. <path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z" />
  88. </svg>
  89. <svg v-else viewBox="0 0 24 24" fill="currentColor" stroke="currentColor" stroke-width="1.5"
  90. stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
  91. <path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z" />
  92. <path fill="none" d="M19 3l0.7 1.4 1.4 0.7-1.4 0.7L19 7.2l-0.7-1.4-1.4-0.7 1.4-0.7z" />
  93. </svg>
  94. </button>
  95. <a-popover :overlay-class-name="currentTheme" :title="t('pages.settings.language')" placement="bottomRight"
  96. trigger="click">
  97. <template #content>
  98. <a-space direction="vertical" :size="10" class="settings-popover">
  99. <a-select v-model:value="lang" class="lang-select" @change="onLangChange">
  100. <a-select-option v-for="l in LanguageManager.supportedLanguages" :key="l.value" :value="l.value">
  101. <span :aria-label="l.name">{{ l.icon }}</span>
  102. &nbsp;&nbsp;<span>{{ l.name }}</span>
  103. </a-select-option>
  104. </a-select>
  105. </a-space>
  106. </template>
  107. <a-button shape="circle" class="toolbar-btn" :aria-label="t('menu.settings')">
  108. <template #icon>
  109. <SettingOutlined />
  110. </template>
  111. </a-button>
  112. </a-popover>
  113. </div>
  114. <div class="login-wrapper">
  115. <div v-if="!fetched" class="login-loading">
  116. <a-spin size="large" />
  117. </div>
  118. <div v-else class="login-card">
  119. <div class="brand">
  120. <span class="brand-name">3X-UI</span>
  121. <span class="brand-accent" aria-hidden="true"></span>
  122. </div>
  123. <h2 class="welcome">
  124. <Transition name="headline" mode="out-in">
  125. <b :key="headlineIndex">{{ headlineWords[headlineIndex] }}</b>
  126. </Transition>
  127. </h2>
  128. <a-form layout="vertical" class="login-form" @submit.prevent="login">
  129. <a-form-item :label="t('username')">
  130. <a-input v-model:value="user.username" autocomplete="username" name="username" size="large"
  131. :placeholder="t('username')" autofocus required>
  132. <template #prefix>
  133. <UserOutlined />
  134. </template>
  135. </a-input>
  136. </a-form-item>
  137. <a-form-item :label="t('password')">
  138. <a-input-password v-model:value="user.password" autocomplete="current-password" name="password"
  139. size="large" :placeholder="t('password')" required>
  140. <template #prefix>
  141. <LockOutlined />
  142. </template>
  143. </a-input-password>
  144. </a-form-item>
  145. <a-form-item v-if="twoFactorEnable" :label="t('twoFactorCode')">
  146. <a-input v-model:value="user.twoFactorCode" autocomplete="one-time-code" name="twoFactorCode"
  147. size="large" :placeholder="t('twoFactorCode')" required>
  148. <template #prefix>
  149. <KeyOutlined />
  150. </template>
  151. </a-input>
  152. </a-form-item>
  153. <a-form-item class="submit-row">
  154. <a-button type="primary" html-type="submit" :loading="submitting" size="large" block>
  155. {{ submitting ? '' : t('login') }}
  156. </a-button>
  157. </a-form-item>
  158. </a-form>
  159. </div>
  160. </div>
  161. </a-layout-content>
  162. </a-layout>
  163. </a-config-provider>
  164. </template>
  165. <style scoped>
  166. .login-app {
  167. --bg-page: #f5f7fa;
  168. --bg-card: #ffffff;
  169. --color-text: rgba(0, 0, 0, 0.88);
  170. --color-text-subtle: rgba(0, 0, 0, 0.55);
  171. --color-accent: #1677ff;
  172. --color-border: rgba(0, 0, 0, 0.08);
  173. --shadow-card: 0 1px 3px rgba(0, 0, 0, 0.04), 0 8px 24px rgba(0, 0, 0, 0.06);
  174. --blob-1: rgba(99, 102, 241, 0.45);
  175. --blob-2: rgba(236, 72, 153, 0.38);
  176. --blob-3: rgba(20, 184, 166, 0.32);
  177. position: relative;
  178. min-height: 100vh;
  179. overflow: hidden;
  180. background: var(--bg-page);
  181. }
  182. .login-app.is-dark {
  183. --bg-page: #1e1e1e;
  184. --bg-card: #252526;
  185. --color-text: rgba(255, 255, 255, 0.92);
  186. --color-text-subtle: rgba(255, 255, 255, 0.55);
  187. --color-accent: #4096ff;
  188. --color-border: rgba(255, 255, 255, 0.08);
  189. --shadow-card: 0 1px 3px rgba(0, 0, 0, 0.3), 0 8px 32px rgba(0, 0, 0, 0.4);
  190. --blob-1: rgba(64, 150, 255, 0.40);
  191. --blob-2: rgba(168, 85, 247, 0.34);
  192. --blob-3: rgba(34, 211, 238, 0.22);
  193. }
  194. .login-app.is-dark.is-ultra {
  195. --bg-page: #000;
  196. --bg-card: #141414;
  197. --color-border: rgba(255, 255, 255, 0.06);
  198. --blob-1: rgba(64, 150, 255, 0.22);
  199. --blob-2: rgba(168, 85, 247, 0.18);
  200. --blob-3: rgba(34, 211, 238, 0.12);
  201. }
  202. /* Three blurred blobs slowly drift across the page; ::before and
  203. * ::after carry two of them, the third lives on .login-content::before
  204. * so we can animate it independently. */
  205. .login-app::before,
  206. .login-app::after {
  207. content: '';
  208. position: absolute;
  209. width: 70vw;
  210. height: 70vw;
  211. max-width: 900px;
  212. max-height: 900px;
  213. border-radius: 50%;
  214. filter: blur(90px);
  215. pointer-events: none;
  216. z-index: 0;
  217. will-change: transform;
  218. }
  219. .login-app::before {
  220. top: -25vw;
  221. left: -20vw;
  222. background: radial-gradient(circle, var(--blob-1) 0%, transparent 65%);
  223. animation: blob-drift-a 24s ease-in-out infinite alternate;
  224. }
  225. .login-app::after {
  226. bottom: -25vw;
  227. right: -20vw;
  228. background: radial-gradient(circle, var(--blob-2) 0%, transparent 65%);
  229. animation: blob-drift-b 30s ease-in-out infinite alternate;
  230. }
  231. .login-content::before {
  232. content: '';
  233. position: absolute;
  234. top: 30%;
  235. left: 50%;
  236. width: 50vw;
  237. height: 50vw;
  238. max-width: 700px;
  239. max-height: 700px;
  240. border-radius: 50%;
  241. background: radial-gradient(circle, var(--blob-3) 0%, transparent 65%);
  242. filter: blur(90px);
  243. pointer-events: none;
  244. z-index: 0;
  245. will-change: transform;
  246. animation: blob-drift-c 36s ease-in-out infinite alternate;
  247. }
  248. @keyframes blob-drift-a {
  249. 0% {
  250. transform: translate(0, 0) scale(1);
  251. }
  252. 50% {
  253. transform: translate(18vw, 10vh) scale(1.15);
  254. }
  255. 100% {
  256. transform: translate(34vw, 22vh) scale(1.25);
  257. }
  258. }
  259. @keyframes blob-drift-b {
  260. 0% {
  261. transform: translate(0, 0) scale(1);
  262. }
  263. 50% {
  264. transform: translate(-16vw, -10vh) scale(1.12);
  265. }
  266. 100% {
  267. transform: translate(-30vw, -22vh) scale(1.2);
  268. }
  269. }
  270. @keyframes blob-drift-c {
  271. 0% {
  272. transform: translate(-50%, -50%) scale(1);
  273. }
  274. 50% {
  275. transform: translate(-20%, -20%) scale(1.1);
  276. }
  277. 100% {
  278. transform: translate(-80%, -10%) scale(1.05);
  279. }
  280. }
  281. @media (prefers-reduced-motion: reduce) {
  282. .login-app::before,
  283. .login-app::after,
  284. .login-content::before {
  285. animation: none;
  286. }
  287. }
  288. .login-app :deep(.ant-layout-content) {
  289. background: transparent;
  290. }
  291. .login-content {
  292. position: relative;
  293. }
  294. .login-content>* {
  295. position: relative;
  296. z-index: 1;
  297. }
  298. .login-toolbar {
  299. position: fixed;
  300. top: 16px;
  301. right: 16px;
  302. z-index: 10;
  303. display: inline-flex;
  304. align-items: center;
  305. gap: 8px;
  306. }
  307. .toolbar-btn {
  308. width: 40px;
  309. height: 40px;
  310. }
  311. .theme-cycle {
  312. width: 40px;
  313. height: 40px;
  314. border-radius: 50%;
  315. border: 1px solid var(--color-border);
  316. background: var(--bg-card);
  317. color: var(--color-text);
  318. display: inline-flex;
  319. align-items: center;
  320. justify-content: center;
  321. cursor: pointer;
  322. padding: 0;
  323. transition: background-color 0.2s, transform 0.15s, color 0.2s;
  324. }
  325. .theme-cycle:hover,
  326. .theme-cycle:focus-visible {
  327. background-color: rgba(64, 150, 255, 0.1);
  328. color: #4096ff;
  329. transform: scale(1.05);
  330. outline: none;
  331. }
  332. .theme-cycle svg {
  333. width: 18px;
  334. height: 18px;
  335. }
  336. .login-wrapper {
  337. min-height: 100vh;
  338. display: flex;
  339. align-items: center;
  340. justify-content: center;
  341. padding: 24px 16px;
  342. }
  343. .login-loading {
  344. text-align: center;
  345. }
  346. .login-card {
  347. width: 100%;
  348. max-width: 400px;
  349. background: var(--bg-card);
  350. border: 1px solid var(--color-border);
  351. border-radius: 12px;
  352. padding: 40px 32px 28px;
  353. box-shadow: var(--shadow-card);
  354. }
  355. @media (max-width: 480px) {
  356. .login-card {
  357. padding: 32px 20px 24px;
  358. }
  359. }
  360. .brand {
  361. display: flex;
  362. flex-direction: column;
  363. align-items: center;
  364. gap: 10px;
  365. margin-bottom: 8px;
  366. }
  367. .brand-name {
  368. font-size: 28px;
  369. font-weight: 700;
  370. letter-spacing: 1.5px;
  371. color: var(--color-text);
  372. }
  373. .brand-accent {
  374. display: block;
  375. width: 40px;
  376. height: 3px;
  377. border-radius: 2px;
  378. background: var(--color-accent);
  379. }
  380. .welcome {
  381. text-align: center;
  382. color: var(--color-text);
  383. font-size: 32px;
  384. font-weight: 700;
  385. line-height: 1.2;
  386. min-height: 42px;
  387. margin: 12px 0 28px;
  388. letter-spacing: 0.3px;
  389. }
  390. .welcome b {
  391. display: inline-block;
  392. font-weight: inherit;
  393. }
  394. .headline-enter-active,
  395. .headline-leave-active {
  396. transition: opacity 280ms ease, transform 280ms ease;
  397. }
  398. .headline-enter-from {
  399. opacity: 0;
  400. transform: translateY(6px);
  401. }
  402. .headline-leave-to {
  403. opacity: 0;
  404. transform: translateY(-6px);
  405. }
  406. .login-form :deep(.ant-form-item-label > label) {
  407. color: var(--color-text);
  408. font-weight: 500;
  409. }
  410. .login-form :deep(input.ant-input:-webkit-autofill) {
  411. -webkit-text-fill-color: var(--color-text) !important;
  412. -webkit-box-shadow: 0 0 0 1000px var(--bg-card) inset !important;
  413. box-shadow: 0 0 0 1000px var(--bg-card) inset !important;
  414. transition: background-color 9999s ease-in-out 0s, color 9999s ease-in-out 0s;
  415. }
  416. .submit-row {
  417. margin-bottom: 0;
  418. }
  419. .settings-popover {
  420. min-width: 220px;
  421. }
  422. .lang-select {
  423. width: 100%;
  424. }
  425. </style>