PasswordChangeView.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <script setup lang="ts">
  2. import { computed, ref } from 'vue'
  3. import BrandMark from '@/components/common/BrandMark.vue'
  4. import { useAuthStore } from '@/stores/auth'
  5. const auth = useAuthStore()
  6. const username = ref(auth.user?.username || '')
  7. const currentPassword = ref('')
  8. const newPassword = ref('')
  9. const confirmation = ref('')
  10. const busy = ref(false)
  11. const error = ref('')
  12. const canSubmit = computed(() => currentPassword.value && newPassword.value.length >= 8
  13. && newPassword.value === confirmation.value && username.value.trim())
  14. async function submit() {
  15. if (!canSubmit.value) return
  16. busy.value = true
  17. error.value = ''
  18. try {
  19. await auth.changeCredentials({
  20. currentPassword: currentPassword.value,
  21. newPassword: newPassword.value,
  22. username: auth.user?.role === 'admin' ? username.value.trim() : undefined,
  23. })
  24. } catch (cause) {
  25. error.value = cause instanceof Error ? cause.message : '修改密码失败'
  26. } finally {
  27. busy.value = false
  28. }
  29. }
  30. </script>
  31. <template>
  32. <main class="password-change">
  33. <section>
  34. <BrandMark :size="68" />
  35. <div><h1>先修改初始密码</h1><p>默认密码或管理员重置的临时密码不能继续用于工作区。</p></div>
  36. <form @submit.prevent="submit">
  37. <label v-if="auth.user?.role === 'admin'"><span>管理员用户名</span><input v-model="username" autocomplete="username" /></label>
  38. <label><span>当前密码</span><input v-model="currentPassword" type="password" autocomplete="current-password" /></label>
  39. <label><span>新密码</span><input v-model="newPassword" type="password" autocomplete="new-password" placeholder="至少 8 个字符" /></label>
  40. <label><span>确认新密码</span><input v-model="confirmation" type="password" autocomplete="new-password" /></label>
  41. <p v-if="newPassword && confirmation && newPassword !== confirmation" class="error">两次输入的新密码不一致</p>
  42. <p v-else-if="error" class="error">{{ error }}</p>
  43. <button class="solid-button" type="submit" :disabled="busy || !canSubmit">{{ busy ? '正在保存…' : '保存并进入夭夭' }}</button>
  44. </form>
  45. </section>
  46. </main>
  47. </template>
  48. <style scoped>
  49. .password-change { display: grid; min-height: 100%; place-items: center; padding: 24px; background: var(--canvas); }.password-change > section { width: min(380px, 100%); }.password-change :deep(.brand-mark) { display: flex; justify-content: center; }.password-change > section > div { margin: 28px 0 22px; text-align: center; }h1 { margin: 0 0 8px; font-size: 19px; }p { margin: 0; color: var(--text-secondary); font-size: 11px; line-height: 1.6; }form { display: grid; gap: 13px; }label { display: grid; gap: 6px; color: var(--text-secondary); font-size: 10px; }input { height: 43px; padding: 0 11px; border: 1px solid var(--line); border-radius: 11px; outline: none; background: var(--surface-raised); color: var(--text-primary); }input:focus { border-color: var(--line-strong); box-shadow: 0 0 0 3px var(--focus-ring); }.error { color: var(--danger); }.solid-button { min-height: 43px; margin-top: 3px; }
  50. </style>