ForgotPassword.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <template>
  2. <div class="forgot-password-container">
  3. <el-card class="forgot-password-card">
  4. <div class="forgot-password-header">
  5. <h2>重置密码</h2>
  6. </div>
  7. <el-form
  8. ref="forgotPasswordFormRef"
  9. :model="forgotPasswordForm"
  10. :rules="rules"
  11. label-width="0"
  12. class="forgot-password-form"
  13. >
  14. <el-form-item prop="phone">
  15. <el-input
  16. v-model="forgotPasswordForm.phone"
  17. placeholder="请输入手机号"
  18. prefix-icon="Phone"
  19. />
  20. </el-form-item>
  21. <el-form-item prop="verificationCode">
  22. <div class="verification-code-input">
  23. <el-input
  24. v-model="forgotPasswordForm.verificationCode"
  25. placeholder="请输入验证码"
  26. prefix-icon="Key"
  27. />
  28. <el-button
  29. type="primary"
  30. :disabled="countdown > 0"
  31. @click="handleSendCode"
  32. >
  33. {{ countdown > 0 ? `${countdown}s后重试` : '获取验证码' }}
  34. </el-button>
  35. </div>
  36. </el-form-item>
  37. <el-form-item prop="newPassword">
  38. <el-input
  39. v-model="forgotPasswordForm.newPassword"
  40. type="password"
  41. placeholder="请输入新密码"
  42. prefix-icon="Lock"
  43. show-password
  44. />
  45. </el-form-item>
  46. <el-form-item prop="confirmPassword">
  47. <el-input
  48. v-model="forgotPasswordForm.confirmPassword"
  49. type="password"
  50. placeholder="请确认新密码"
  51. prefix-icon="Lock"
  52. show-password
  53. />
  54. </el-form-item>
  55. <el-form-item>
  56. <el-button
  57. type="primary"
  58. :loading="loading"
  59. class="reset-button"
  60. @click="handleResetPassword"
  61. >
  62. 重置密码
  63. </el-button>
  64. </el-form-item>
  65. <div class="forgot-password-footer">
  66. <span>想起密码了?</span>
  67. <el-link type="primary" @click="goToLogin">立即登录</el-link>
  68. </div>
  69. </el-form>
  70. </el-card>
  71. </div>
  72. </template>
  73. <script setup>
  74. import { ref, reactive } from 'vue'
  75. import { ElMessage } from 'element-plus'
  76. import { Phone, Key, Lock } from '@element-plus/icons-vue'
  77. import { useRouter } from 'vue-router'
  78. const router = useRouter()
  79. const forgotPasswordForm = reactive({
  80. phone: '',
  81. verificationCode: '',
  82. newPassword: '',
  83. confirmPassword: ''
  84. })
  85. const loading = ref(false)
  86. const countdown = ref(0)
  87. const forgotPasswordFormRef = ref(null)
  88. const validatePass = (rule, value, callback) => {
  89. if (value === '') {
  90. callback(new Error('请再次输入密码'))
  91. } else if (value !== forgotPasswordForm.newPassword) {
  92. callback(new Error('两次输入密码不一致!'))
  93. } else {
  94. callback()
  95. }
  96. }
  97. const rules = {
  98. phone: [
  99. { required: true, message: '请输入手机号', trigger: 'blur' },
  100. { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  101. ],
  102. verificationCode: [
  103. { required: true, message: '请输入验证码', trigger: 'blur' },
  104. { len: 6, message: '验证码长度应为6位', trigger: 'blur' }
  105. ],
  106. newPassword: [
  107. { required: true, message: '请输入新密码', trigger: 'blur' },
  108. { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  109. ],
  110. confirmPassword: [
  111. { required: true, message: '请再次输入密码', trigger: 'blur' },
  112. { validator: validatePass, trigger: 'blur' }
  113. ]
  114. }
  115. const startCountdown = () => {
  116. countdown.value = 60
  117. const timer = setInterval(() => {
  118. countdown.value--
  119. if (countdown.value <= 0) {
  120. clearInterval(timer)
  121. }
  122. }, 1000)
  123. }
  124. const handleSendCode = async () => {
  125. try {
  126. await forgotPasswordFormRef.value.validateField('phone')
  127. // TODO: 调用发送验证码的API
  128. ElMessage.success('验证码已发送')
  129. startCountdown()
  130. } catch (error) {
  131. console.error('手机号验证失败:', error)
  132. }
  133. }
  134. const handleResetPassword = async () => {
  135. if (!forgotPasswordFormRef.value) return
  136. try {
  137. await forgotPasswordFormRef.value.validate()
  138. loading.value = true
  139. // TODO: 实现重置密码逻辑
  140. setTimeout(() => {
  141. ElMessage.success('密码重置成功')
  142. loading.value = false
  143. router.push('/login')
  144. }, 1000)
  145. } catch (error) {
  146. console.error('表单验证失败:', error)
  147. }
  148. }
  149. const goToLogin = () => {
  150. router.push('/login')
  151. }
  152. </script>
  153. <style scoped>
  154. .forgot-password-container {
  155. height: 100vh;
  156. display: flex;
  157. justify-content: center;
  158. align-items: center;
  159. background: linear-gradient(135deg, #e0f2f1 0%, #e3f2fd 100%);
  160. }
  161. .forgot-password-card {
  162. width: 400px;
  163. padding: 20px;
  164. border-radius: 8px;
  165. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  166. }
  167. .forgot-password-header {
  168. text-align: center;
  169. margin-bottom: 30px;
  170. }
  171. .forgot-password-header h2 {
  172. color: #409EFF;
  173. margin: 0;
  174. }
  175. .forgot-password-form {
  176. margin-top: 20px;
  177. }
  178. .verification-code-input {
  179. display: flex;
  180. gap: 10px;
  181. }
  182. .verification-code-input .el-input {
  183. flex: 1;
  184. }
  185. .verification-code-input .el-button {
  186. width: 120px;
  187. white-space: nowrap;
  188. }
  189. .reset-button {
  190. width: 100%;
  191. }
  192. .forgot-password-footer {
  193. display: flex;
  194. justify-content: center;
  195. align-items: center;
  196. margin-top: 20px;
  197. font-size: 14px;
  198. gap: 8px;
  199. }
  200. :deep(.el-input__wrapper) {
  201. box-shadow: 0 0 0 1px #dcdfe6 inset;
  202. }
  203. :deep(.el-input__wrapper:hover) {
  204. box-shadow: 0 0 0 1px #409EFF inset;
  205. }
  206. </style>