boot.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. const api = window.yaoyaoDesktop
  2. const $ = id => document.getElementById(id)
  3. let registering = false
  4. let state, initialized = false, pending = false, serverDirty = false, generation = 0, actionError = ''
  5. function display(next) {
  6. state = next
  7. const restoring = state.restoring === true || state.phase === 'complete'
  8. $('onboarding').hidden = restoring
  9. $('session-loading').hidden = !restoring
  10. document.title = restoring ? '夭夭' : '开始使用夭夭'
  11. if (restoring) return
  12. if (!initialized) {
  13. $('server').value = state.serverURL || ''
  14. $('remember').checked = state.remember
  15. initialized = true
  16. }
  17. const busy = pending || state.busy
  18. const local = state.mode === 'local'
  19. const ready = ['ready', 'authenticating', 'entering'].includes(state.phase) && !serverDirty
  20. const setup = ready && state.setupRequired
  21. const confirm = setup || registering
  22. const accountTitle = setup ? '创建管理员' : registering ? '注册子账号' : '登录账号'
  23. $('register-toggle').hidden = !ready || setup || state.authenticated
  24. $('register-toggle').disabled = busy
  25. $('register-toggle').textContent = registering ? '已有账号?返回登录' : '没有账号?注册子账号'
  26. $('registration-notice').textContent = state.registrationNotice || ''
  27. $('registration-notice').hidden = !ready || !state.registrationNotice
  28. $('mode-options').disabled = busy
  29. for (const input of document.querySelectorAll('[name=mode]')) input.checked = input.value === state.mode
  30. $('remember').disabled = busy
  31. $('prepare-section').classList.toggle('pending', !state.mode)
  32. $('prepare-section').classList.toggle('local-ready', local && ready)
  33. $('prepare-heading').textContent = local ? '准备本机服务' : state.mode ? '连接服务器' : '准备服务'
  34. $('choose-hint').hidden = !!state.mode
  35. $('local-panel').hidden = !local
  36. $('connection-form').hidden = state.mode !== 'remote'
  37. $('server').disabled = busy
  38. $('detect').disabled = busy || !$('server').value.trim()
  39. $('detect').textContent = busy && !ready ? '正在检测…' : ready ? '重新检测' : '检测连接'
  40. $('prepare-local').hidden = !local || state.phase !== 'idle'
  41. $('prepare-local').disabled = busy
  42. $('actions').hidden = !local || state.phase !== 'error'
  43. $('retry').disabled = busy
  44. $('force-sync').hidden = !state.canForceSync
  45. $('force-sync').disabled = busy
  46. const preparationError = !ready && !serverDirty && (actionError || state.error)
  47. $('status').textContent = preparationError || (serverDirty ? '服务器地址已更改,请重新检测。' : state.message)
  48. $('status').setAttribute('role', preparationError ? 'alert' : 'status')
  49. $('status').classList.toggle('success', ready && !preparationError)
  50. $('prepare-section').setAttribute('aria-busy', String(busy && !ready))
  51. $('account-heading').textContent = accountTitle
  52. $('account-step-title').textContent = accountTitle
  53. $('account-section').classList.toggle('pending', !ready)
  54. $('account-section').setAttribute('aria-busy', String(busy && ready))
  55. $('account-hint').textContent = !ready ? '服务准备完成后,在此创建账号或登录。'
  56. : state.authenticated ? `已登录${state.username ? `:${state.username}` : ''},可以继续进入夭夭。`
  57. : setup ? '首次使用,请设置管理员账号和密码。密码至少 8 个字符。' : registering ? '子账号仅可使用 Bot 模式,注册后需管理员分配机器人并开通。' : '登录所选服务器的夭夭账号。'
  58. $('login-form').hidden = !ready
  59. $('credentials').hidden = state.authenticated
  60. $('confirmation-field').hidden = !confirm
  61. $('username').disabled = busy || state.authenticated || !ready
  62. $('password').disabled = busy || state.authenticated || !ready
  63. $('confirmation').disabled = busy || !confirm || state.authenticated
  64. $('confirmation').required = confirm && !state.authenticated
  65. $('password').minLength = confirm ? 8 : 1
  66. $('password').autocomplete = confirm ? 'new-password' : 'current-password'
  67. $('toggle-password').disabled = busy
  68. $('submit').disabled = busy
  69. $('submit').querySelector('span').textContent = state.phase === 'authenticating' ? (confirm ? '正在提交…' : '正在登录…')
  70. : state.phase === 'entering' ? '正在进入…' : state.authenticated ? '进入夭夭' : setup ? '创建并进入夭夭' : registering ? '注册子账号' : '登录并进入夭夭'
  71. $('login-error').textContent = ready ? actionError || state.error : ''
  72. $('login-error').hidden = !$('login-error').textContent
  73. $('waiting').hidden = !busy || ready
  74. $('waiting').textContent = local ? '正在准备服务,请稍候' : '正在检测连接,请稍候'
  75. const activeStep = ready ? 2 : state.mode ? 1 : 0
  76. for (const [index, id] of ['step-choice', 'step-prepare', 'step-account'].entries()) {
  77. $(id).classList.toggle('complete', index < activeStep)
  78. if (index === activeStep) $(id).setAttribute('aria-current', 'step')
  79. else $(id).removeAttribute('aria-current')
  80. }
  81. const stages = ['checking', 'installing', 'starting']
  82. const current = stages.indexOf(state.stage)
  83. for (const [index, row] of [...document.querySelectorAll('[data-stage]')].entries()) {
  84. const complete = ready || (state.phase !== 'idle' && index < current)
  85. const active = !complete && state.phase === 'preparing' && index === current
  86. row.classList.toggle('complete', complete)
  87. row.classList.toggle('active', active)
  88. row.querySelector('use').setAttribute('href', `boot-icons.svg#${complete ? 'check' : active ? 'loader' : 'circle'}`)
  89. row.querySelector('small').textContent = complete ? '已完成' : active ? '进行中' : state.phase === 'error' && index === current ? '未完成' : '等待中'
  90. }
  91. $('install-progress').hidden = !local || state.phase !== 'preparing'
  92. document.querySelector('.install-steps').hidden = local && ready
  93. const history = state.history?.join('\n') || '准备开始'
  94. if ($('install-log').textContent !== history) $('install-log').textContent = history
  95. }
  96. async function action(run, { resetServer = false, focusAccount = false } = {}) {
  97. if (pending) return
  98. pending = true; actionError = ''; generation++
  99. if (state) display(state)
  100. try {
  101. const next = await run()
  102. if (resetServer) {
  103. if (next.phase === 'ready') $('server').value = next.serverURL || ''
  104. serverDirty = false
  105. }
  106. display(next)
  107. } catch (error) {
  108. actionError = error.message || '操作未完成,请重试。'
  109. } finally {
  110. pending = false
  111. if (state) display(state)
  112. if (focusAccount && state?.phase === 'ready' && !state.authenticated) $('username').focus()
  113. }
  114. }
  115. for (const input of document.querySelectorAll('[name=mode]')) input.addEventListener('change', () => {
  116. $('login-form').reset(); $('password').type = 'password'
  117. $('toggle-password').setAttribute('aria-pressed', 'false'); $('toggle-password').setAttribute('aria-label', '显示密码')
  118. $('password').removeAttribute('aria-invalid'); $('confirmation').removeAttribute('aria-invalid')
  119. serverDirty = false; registering = false
  120. void action(() => api.selectServer(input.value))
  121. })
  122. $('server').addEventListener('input', () => {
  123. serverDirty = true; registering = false; actionError = ''
  124. // Credentials belong to the checked server, never carry them to a new address.
  125. $('login-form').reset(); $('password').type = 'password'
  126. $('toggle-password').setAttribute('aria-pressed', 'false'); $('toggle-password').setAttribute('aria-label', '显示密码')
  127. if (state) display(state)
  128. })
  129. $('connection-form').addEventListener('submit', event => {
  130. event.preventDefault()
  131. void action(() => api.prepareServer({ serverURL: $('server').value, remember: $('remember').checked }), { resetServer: true, focusAccount: true })
  132. })
  133. $('prepare-local').onclick = () => action(() => api.prepareServer({ remember: $('remember').checked }), { focusAccount: true })
  134. $('retry').onclick = () => action(() => api.prepareServer({ remember: $('remember').checked }), { focusAccount: true })
  135. $('force-sync').onclick = () => action(() => api.forceSync(), { focusAccount: true })
  136. $('logs').onclick = async () => { try { await api.logs() } catch (error) { actionError = error.message; display(state) } }
  137. $('toggle-password').onclick = () => {
  138. const show = $('password').type === 'password'
  139. $('password').type = show ? 'text' : 'password'
  140. $('toggle-password').setAttribute('aria-label', show ? '隐藏密码' : '显示密码')
  141. $('toggle-password').setAttribute('aria-pressed', String(show))
  142. }
  143. $('confirmation').addEventListener('blur', () => {
  144. const mismatch = $('confirmation').value && $('confirmation').value !== $('password').value
  145. $('confirmation').setAttribute('aria-invalid', String(!!mismatch))
  146. actionError = mismatch ? '两次输入的密码不一致' : ''
  147. display(state)
  148. })
  149. $('login-form').addEventListener('submit', event => {
  150. event.preventDefault()
  151. if (serverDirty || pending) return
  152. if ((state.setupRequired || registering) && !state.authenticated && $('confirmation').value !== $('password').value) {
  153. actionError = '两次输入的密码不一致'; $('confirmation').setAttribute('aria-invalid', 'true'); display(state); $('confirmation').focus(); return
  154. }
  155. void action(async () => {
  156. const next = await api.login({ username: $('username').value, password: $('password').value,
  157. confirmation: $('confirmation').value, register: registering, remember: $('remember').checked })
  158. $('password').value = ''; $('confirmation').value = ''
  159. if (registering && !next.error) registering = false
  160. return next
  161. })
  162. })
  163. $('register-toggle').onclick = () => {
  164. if (!state.registrationAvailable) { actionError = '当前服务器尚不支持子账号注册,请联系管理员升级。'; display(state); return }
  165. registering = !registering; actionError = ''; state.error = ''; state.registrationNotice = ''
  166. $('password').value = ''; $('confirmation').value = ''; display(state); $('username').focus()
  167. }
  168. $('help-button').onclick = () => {
  169. $('help').hidden = !$('help').hidden
  170. $('help-button').setAttribute('aria-expanded', String(!$('help').hidden))
  171. }
  172. async function render() {
  173. const request = generation
  174. try { const next = await window.yaoyaoDesktop.status(); if (request === generation) display(next) }
  175. catch {
  176. if (!state) {
  177. $('session-loading-message').textContent = '无法读取启动状态,请重新打开应用。'
  178. $('session-loading').setAttribute('role', 'alert')
  179. $('session-loading').setAttribute('aria-busy', 'false')
  180. }
  181. }
  182. }
  183. void render()
  184. const poll = setInterval(() => { void render() }, 500)
  185. window.addEventListener('pagehide', () => clearInterval(poll), { once: true })