index.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <template>
  2. <div class="p-password">
  3. <div v-if="action === 'change'" class="step ptc-inner">
  4. <div class="ptc-form">
  5. <h3 class="title">Change Password</h3>
  6. <div class="ptc-form-item">
  7. <input
  8. v-model="values.old_password"
  9. class="ptc-input"
  10. type="password"
  11. placeholder="Enter old password"
  12. />
  13. </div>
  14. <div class="ptc-form-item">
  15. <input
  16. v-model="values.new_password"
  17. class="ptc-input"
  18. type="password"
  19. placeholder="Enter new password"
  20. />
  21. </div>
  22. <div class="ptc-form-item">
  23. <input
  24. v-model="values.new_password_confirmation"
  25. class="ptc-input"
  26. type="password"
  27. placeholder="Repeat new password"
  28. />
  29. </div>
  30. <div class="ptc-form-item">
  31. <button class="ptc-button" @click="handleSubmit(applyChange)">
  32. SUBMIT
  33. </button>
  34. </div>
  35. </div>
  36. </div>
  37. <div v-else-if="step === 0" class="step ptc-inner">
  38. <h3 class="title">Recover password</h3>
  39. <div class="desc">
  40. We will send you an email for password reset. Please check it
  41. </div>
  42. <div class="ptc-form">
  43. <div class="ptc-form-item">
  44. <input
  45. v-model="resetForm.email"
  46. class="ptc-input"
  47. placeholder="email address"
  48. />
  49. </div>
  50. <div class="ptc-form-item">
  51. <button class="ptc-button" @click="sendEmail">NEXT</button>
  52. </div>
  53. <div class="ptc-form-item">
  54. <button class="ptc-button ptc-button--stroke" @click="$router.back()">
  55. BACK
  56. </button>
  57. </div>
  58. </div>
  59. </div>
  60. <div v-else-if="step === 1" class="step ptc-inner">
  61. <h3 class="title">Recover password</h3>
  62. <div class="desc">
  63. We will send you an email( {{ resetForm.email }} )The password reset
  64. email has been sent. Please go to check it
  65. </div>
  66. </div>
  67. <div v-else-if="step === 2" class="step ptc-inner">
  68. <template v-if="resetStatus === 1">
  69. <h3 class="title">Recover password</h3>
  70. <div class="desc">
  71. Please set a new password for {{ resetForm.email }}. It is recommended
  72. to use a combination of numbers, letters, and characters to improve
  73. the password security level
  74. </div>
  75. <div class="ptc-form">
  76. <div class="ptc-form-item">
  77. <input
  78. v-model="resetForm.password"
  79. class="ptc-input"
  80. placeholder="6-20 digits password, case sensitive"
  81. />
  82. </div>
  83. <div class="ptc-form-item">
  84. <button class="ptc-button" @click="applyReset">SUBMIT</button>
  85. </div>
  86. </div>
  87. </template>
  88. <template v-if="resetStatus === -1">
  89. <i class="icon-fail"></i>
  90. <h3 class="title tac">The Link For Reset Your Password Has Expired</h3>
  91. <p class="desc tac">Please Press Below Button To Retrieve New Link</p>
  92. <button class="ptc-button mgt96" @click="refresh">
  93. RESET PASSWORD
  94. </button>
  95. </template>
  96. </div>
  97. <div v-else-if="step === 3" class="step ptc-inner">
  98. <i class="icon-success"></i>
  99. <h3 class="title tac">Password reset successfully</h3>
  100. <button class="ptc-button mgt96" @click="$router.push('/login')">
  101. TO LOG IN
  102. </button>
  103. </div>
  104. </div>
  105. </template>
  106. <script setup lang="ts">
  107. import { reactive, ref, watch, onMounted } from 'vue'
  108. import { useRoute, useRouter } from 'vue-router'
  109. import { state } from '@/store'
  110. import {
  111. sendPasswordEmail,
  112. resetPassword,
  113. resetCheck,
  114. changePassword,
  115. } from '@/service/user'
  116. import { string } from 'yup'
  117. import { debounce } from 'lodash-es'
  118. import useForm from '@/hooks/useForm'
  119. import Toast from '@/components/toast'
  120. const props = defineProps<{ action: 'change' | 'reset' }>()
  121. const router = useRouter()
  122. const { query } = useRoute() as any
  123. const fromPath = history.state.back
  124. const step = ref(query.token ? 2 : 0)
  125. const resetStatus = ref(0)
  126. const { values, handleSubmit } = useForm<ApiUser.PasswordChange.Request>({
  127. schema: {
  128. old_password: string().required('Please enter old password'),
  129. new_password: string().required('Please enter new password'),
  130. new_password_confirmation: string().required('Please repeat new password'),
  131. },
  132. })
  133. const resetForm = reactive<ApiUser.PasswordReset.Request>({
  134. email: step.value === 2 ? query.email : '',
  135. token: query.token || '',
  136. password: '',
  137. password_confirmation: '',
  138. })
  139. state.bgWhite = true
  140. onMounted(() => {
  141. if (step.value === 2) {
  142. const { email, token } = resetForm
  143. resetCheck({ email, token })
  144. .then(() => (resetStatus.value = 1))
  145. .catch(() => (resetStatus.value = -1))
  146. }
  147. })
  148. watch(step, () => window.scrollTo(0, 0))
  149. async function applyChange() {
  150. const { message } = await changePassword(values as any)
  151. Toast(message)
  152. state.userInfo = null
  153. fromPath.startsWith('/login') ? router.back() : router.push('/login')
  154. }
  155. async function sendEmail() {
  156. try {
  157. await string()
  158. .email('invalid email')
  159. .required('email is required')
  160. .validate(resetForm.email)
  161. await sendPasswordEmail(resetForm.email)
  162. step.value++
  163. } catch (err) {
  164. Toast(err.message)
  165. }
  166. }
  167. async function applyReset() {
  168. try {
  169. await string().required('password is required').validate(resetForm.password)
  170. resetForm.password_confirmation = resetForm.password
  171. await resetPassword(resetForm)
  172. step.value++
  173. } catch (err) {
  174. Toast(err.message)
  175. }
  176. }
  177. function refresh() {
  178. location.href = 'reset'
  179. }
  180. </script>
  181. <style lang="scss">
  182. .p-password {
  183. .step {
  184. margin-top: 98px;
  185. padding: 0 76px;
  186. @include media-breakpoint-up(md) {
  187. padding: 0;
  188. min-height: 1084px - 98px;
  189. }
  190. }
  191. .title {
  192. line-height: 56px;
  193. font-size: 40px;
  194. font-weight: 500;
  195. color: #333;
  196. }
  197. .desc {
  198. margin: 36px 0 64px;
  199. line-height: 44px;
  200. font-size: 32px;
  201. color: #333;
  202. }
  203. .icon-fail,
  204. .icon-success {
  205. display: block;
  206. margin: 0 auto 32px;
  207. }
  208. .icon-fail {
  209. @include icon('@img/warn.png', 96px);
  210. }
  211. .icon-success {
  212. @include icon('@img/success.png', 96px);
  213. }
  214. .mgt96 {
  215. margin-top: 96px;
  216. }
  217. }
  218. </style>