device-center 0.2.0-beta.2 → 0.2.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/login.js CHANGED
@@ -1,3 +1,6 @@
1
+ import { initLanguage, getLocale, t } from './i18n.js';
2
+ initLanguage();
3
+ function setMessage(element, key) { if (key) element.dataset.i18n = key; else delete element.dataset.i18n; element.textContent = key ? t(key) : ''; }
1
4
  const form = document.querySelector('#login');
2
5
  const loading = document.querySelector('#loading');
3
6
  let checking = false, watchingUntil = 0, timer, options;
@@ -8,19 +11,19 @@ async function continueUnified(manual = false) {
8
11
  button.disabled = true;
9
12
  try {
10
13
  const response = await fetch('/api/auth/continue', { method: 'POST', headers: { 'content-type': 'application/json' }, body: '{}', signal: AbortSignal.timeout(10_000) });
11
- if (response.ok) { clearInterval(timer); location.replace('/'); return; }
12
- if (response.status === 401) message.textContent = manual ? '请先在统一账号页面完成邮箱或 Google 登录,再回到这里。' : '';
14
+ if (response.ok) { clearInterval(timer); location.replace('/?lang=' + getLocale()); return; }
15
+ if (response.status === 401) setMessage(message, manual ? 'login.first' : null);
13
16
  else {
14
17
  clearInterval(timer);
15
- message.textContent = response.status === 403 ? '当前账号没有此个人面板的访问权限,请使用面板所有者的账号。' : response.status === 429 ? '尝试过于频繁,请一分钟后重试。' : '统一账号服务暂不可用,请稍后重试。';
18
+ setMessage(message, response.status === 403 ? 'login.owner' : response.status === 429 ? 'login.rate' : 'login.unavailable');
16
19
  }
17
- } catch { message.textContent = '连接账号服务超时,请重试。'; clearInterval(timer); }
20
+ } catch { setMessage(message, 'login.timeout'); clearInterval(timer); }
18
21
  finally { checking = false; button.disabled = false; }
19
22
  }
20
23
  document.querySelector('#continue-login').addEventListener('click', () => continueUnified(true));
21
24
  document.querySelector('#unified-login').addEventListener('click', () => {
22
25
  watchingUntil = Date.now() + 5 * 60_000;
23
- document.querySelector('#unified-message').textContent = '完成统一账号登录后,回到此页即可。';
26
+ setMessage(document.querySelector('#unified-message'), 'login.return');
24
27
  clearInterval(timer);
25
28
  timer = setInterval(() => { if (Date.now() > watchingUntil) clearInterval(timer); else void continueUnified(); }, 4000);
26
29
  });
@@ -30,7 +33,7 @@ try {
30
33
  const response = await fetch('/api/auth/options', { signal: AbortSignal.timeout(5000) });
31
34
  if (!response.ok) throw new Error();
32
35
  options = await response.json();
33
- document.querySelector('#access-notes').textContent = options.accountMode === 'multi-user' ? '账号设备隔离 · HTTPS 保护' : '个人访问 · HTTPS 保护';
36
+ setMessage(document.querySelector('#access-notes'), options.accountMode === 'multi-user' ? 'login.multi' : 'login.personal');
34
37
  loading.hidden = true;
35
38
  if (options.provider === 'app-services') {
36
39
  const target = new URL(options.accountLoginUrl);
@@ -40,17 +43,17 @@ try {
40
43
  if (options.automaticContinue) void continueUnified();
41
44
  } else if (options.provider === 'password') { form.hidden = false; form.elements.username.focus(); }
42
45
  else throw new Error();
43
- } catch { loading.hidden = false; loading.textContent = '无法读取登录方式,请刷新重试。'; }
46
+ } catch { loading.hidden = false; setMessage(loading, 'login.optionsError'); }
44
47
  form.addEventListener('submit', async (event) => {
45
48
  event.preventDefault();
46
49
  const button = form.querySelector('button');
47
50
  const message = document.querySelector('#message');
48
- button.disabled = true; message.textContent = '';
51
+ button.disabled = true; setMessage(message, null);
49
52
  try {
50
53
  const data = Object.fromEntries(new FormData(form));
51
54
  const response = await fetch('/api/auth/login', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(data), signal: AbortSignal.timeout(10_000) });
52
- if (!response.ok) throw new Error(response.status === 429 ? '尝试过于频繁,请一分钟后重试。' : '用户名或密码不正确。');
53
- form.reset(); location.replace('/');
54
- } catch (cause) { message.textContent = cause.name === 'TimeoutError' ? '连接超时,请重试。' : cause.message; }
55
+ if (!response.ok) throw new Error(response.status === 429 ? 'login.rate' : 'login.credentials');
56
+ form.reset(); location.replace('/?lang=' + getLocale());
57
+ } catch (cause) { setMessage(message, cause.name === 'TimeoutError' ? 'login.timeout' : cause.message.startsWith('login.') ? cause.message : 'login.unavailable'); }
55
58
  finally { button.disabled = false; }
56
59
  });