ForgetPasswordForm.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <template>
  2. <el-form
  3. v-show="getShow"
  4. ref="formSmsResetPassword"
  5. :model="resetPasswordData"
  6. :rules="rules"
  7. class="login-form"
  8. label-position="top"
  9. label-width="120px"
  10. size="large"
  11. >
  12. <el-row style="margin-right: -10px; margin-left: -10px">
  13. <!-- 租户名 -->
  14. <el-col :span="24" style="padding-right: 10px; padding-left: 10px">
  15. <el-form-item>
  16. <LoginFormTitle style="width: 100%" />
  17. </el-form-item>
  18. </el-col>
  19. <el-col :span="24" style="padding-right: 10px; padding-left: 10px">
  20. <el-form-item v-if="resetPasswordData.tenantEnable === 'true'" prop="tenantName">
  21. <el-input
  22. v-model="resetPasswordData.tenantName"
  23. :placeholder="t('login.tenantNamePlaceholder')"
  24. :prefix-icon="iconHouse"
  25. type="primary"
  26. link
  27. />
  28. </el-form-item>
  29. </el-col>
  30. <!-- 手机号 -->
  31. <el-col :span="24" style="padding-right: 10px; padding-left: 10px">
  32. <el-form-item prop="mobile">
  33. <el-input
  34. v-model="resetPasswordData.mobile"
  35. :placeholder="t('login.mobileNumberPlaceholder')"
  36. :prefix-icon="iconCellphone"
  37. />
  38. </el-form-item>
  39. </el-col>
  40. <Verify
  41. ref="verify"
  42. :captchaType="captchaType"
  43. :imgSize="{ width: '400px', height: '200px' }"
  44. mode="pop"
  45. @success="getSmsCode"
  46. />
  47. <!-- 验证码 -->
  48. <el-col :span="24" style="padding-right: 10px; padding-left: 10px">
  49. <el-form-item prop="code">
  50. <el-row :gutter="5" justify="space-between" style="width: 100%">
  51. <el-col :span="24">
  52. <el-input
  53. v-model="resetPasswordData.code"
  54. :placeholder="t('login.codePlaceholder')"
  55. :prefix-icon="iconCircleCheck"
  56. >
  57. <template #append>
  58. <span
  59. v-if="mobileCodeTimer <= 0"
  60. class="getMobileCode"
  61. style="cursor: pointer"
  62. @click="getCode"
  63. >
  64. {{ t('login.getSmsCode') }}
  65. </span>
  66. <span v-if="mobileCodeTimer > 0" class="getMobileCode" style="cursor: pointer">
  67. {{ mobileCodeTimer }}秒后可重新获取
  68. </span>
  69. </template>
  70. </el-input>
  71. <!-- </el-button> -->
  72. </el-col>
  73. </el-row>
  74. </el-form-item>
  75. </el-col>
  76. <el-col :span="24" style="padding-right: 10px; padding-left: 10px">
  77. <el-form-item prop="password">
  78. <InputPassword
  79. v-model="resetPasswordData.password"
  80. :placeholder="t('login.passwordPlaceholder')"
  81. style="width: 100%"
  82. strength="true"
  83. />
  84. </el-form-item>
  85. </el-col>
  86. <el-col :span="24" style="padding-right: 10px; padding-left: 10px">
  87. <el-form-item prop="check_password">
  88. <InputPassword
  89. v-model="resetPasswordData.check_password"
  90. :placeholder="t('login.checkPassword')"
  91. style="width: 100%"
  92. strength="true"
  93. />
  94. </el-form-item>
  95. </el-col>
  96. <!-- 登录按钮 / 返回按钮 -->
  97. <el-col :span="24" style="padding-right: 10px; padding-left: 10px">
  98. <el-form-item>
  99. <XButton
  100. :loading="loginLoading"
  101. :title="t('login.resetPassword')"
  102. class="w-[100%]"
  103. type="primary"
  104. @click="resetPassword()"
  105. />
  106. </el-form-item>
  107. </el-col>
  108. <el-col :span="24" style="padding-right: 10px; padding-left: 10px">
  109. <el-form-item>
  110. <XButton
  111. :loading="loginLoading"
  112. :title="t('login.backLogin')"
  113. class="w-[100%]"
  114. @click="handleBackLogin()"
  115. />
  116. </el-form-item>
  117. </el-col>
  118. </el-row>
  119. </el-form>
  120. </template>
  121. <script lang="ts" setup>
  122. import type { RouteLocationNormalizedLoaded } from 'vue-router'
  123. import { useIcon } from '@/hooks/web/useIcon'
  124. import { sendSmsCode, smsResetPassword } from '@/api/login'
  125. import LoginFormTitle from './LoginFormTitle.vue'
  126. import { LoginStateEnum, useFormValid, useLoginState } from './useLogin'
  127. import { ElLoading } from 'element-plus'
  128. import * as authUtil from '@/utils/auth'
  129. import * as LoginApi from '@/api/login'
  130. defineOptions({ name: 'ForgetPasswordForm' })
  131. const verify = ref()
  132. const { t } = useI18n()
  133. const message = useMessage()
  134. const { currentRoute, push } = useRouter()
  135. const formSmsResetPassword = ref()
  136. const loginLoading = ref(false)
  137. const iconHouse = useIcon({ icon: 'ep:house' })
  138. const iconCellphone = useIcon({ icon: 'ep:cellphone' })
  139. const iconCircleCheck = useIcon({ icon: 'ep:circle-check' })
  140. const { validForm } = useFormValid(formSmsResetPassword)
  141. const { handleBackLogin, getLoginState, setLoginState } = useLoginState()
  142. const getShow = computed(() => unref(getLoginState) === LoginStateEnum.RESET_PASSWORD)
  143. const captchaType = ref('blockPuzzle') // blockPuzzle 滑块 clickWord 点击文字
  144. const validatePass2 = (rule, value, callback) => {
  145. if (value === '') {
  146. callback(new Error('请再次输入密码'))
  147. } else if (value !== resetPasswordData.password) {
  148. callback(new Error('两次输入密码不一致!'))
  149. } else {
  150. callback()
  151. }
  152. }
  153. const rules = {
  154. tenantName: [{ required: true, min: 2, max: 20, trigger: 'blur', message: '长度为4到16位' }],
  155. mobile: [{ required: true, min: 11, max: 11, trigger: 'blur', message: '手机号长度为11位' }],
  156. password: [
  157. {
  158. required: true,
  159. min: 4,
  160. max: 16,
  161. validator: validatePass2,
  162. trigger: 'blur',
  163. message: '密码长度为4到16位'
  164. }
  165. ],
  166. check_password: [{ required: true, validator: validatePass2, trigger: 'blur' }],
  167. code: [required]
  168. }
  169. const resetPasswordData = reactive({
  170. captchaEnable: import.meta.env.VITE_APP_CAPTCHA_ENABLE,
  171. tenantEnable: import.meta.env.VITE_APP_TENANT_ENABLE,
  172. tenantName: '',
  173. username: '',
  174. password: '',
  175. check_password: '',
  176. mobile: '',
  177. code: ''
  178. })
  179. const smsVO = reactive({
  180. tenantName: '',
  181. mobile: '',
  182. captchaVerification: '',
  183. scene: 23
  184. })
  185. const mobileCodeTimer = ref(0)
  186. const redirect = ref<string>('')
  187. // 获取验证码
  188. const getCode = async () => {
  189. // 情况一,未开启:则直接发送验证码
  190. if (resetPasswordData.captchaEnable === 'false') {
  191. await getSmsCode({})
  192. } else {
  193. // 情况二,已开启:则展示验证码;只有完成验证码的情况,才进行发送验证码
  194. // 弹出验证码
  195. verify.value.show()
  196. }
  197. }
  198. const getSmsCode = async (params) => {
  199. if (resetPasswordData.tenantEnable === 'true') {
  200. await getTenantId()
  201. }
  202. smsVO.captchaVerification = params.captchaVerification
  203. smsVO.mobile = resetPasswordData.mobile
  204. await sendSmsCode(smsVO).then(async () => {
  205. message.success(t('login.SmsSendMsg'))
  206. // 设置倒计时
  207. mobileCodeTimer.value = 60
  208. let msgTimer = setInterval(() => {
  209. mobileCodeTimer.value = mobileCodeTimer.value - 1
  210. if (mobileCodeTimer.value <= 0) {
  211. clearInterval(msgTimer)
  212. }
  213. }, 1000)
  214. })
  215. }
  216. watch(
  217. () => currentRoute.value,
  218. (route: RouteLocationNormalizedLoaded) => {
  219. redirect.value = route?.query?.redirect as string
  220. },
  221. {
  222. immediate: true
  223. }
  224. )
  225. const getTenantId = async () => {
  226. if (resetPasswordData.tenantEnable === 'true') {
  227. const res = await LoginApi.getTenantIdByName(resetPasswordData.tenantName)
  228. if (res == null) {
  229. message.error(t('login.invalidTenantName'))
  230. throw t('login.invalidTenantName')
  231. }
  232. authUtil.setTenantId(res)
  233. }
  234. }
  235. // 重置密码
  236. const resetPassword = async () => {
  237. const data = await validForm()
  238. if (!data) return
  239. await getTenantId()
  240. loginLoading.value = true
  241. await smsResetPassword(resetPasswordData)
  242. .then(async () => {
  243. message.success(t('login.resetPasswordSuccess'))
  244. setLoginState(LoginStateEnum.LOGIN)
  245. })
  246. .catch(() => {})
  247. .finally(() => {
  248. loginLoading.value = false
  249. setTimeout(() => {
  250. const loadingInstance = ElLoading.service()
  251. loadingInstance.close()
  252. }, 400)
  253. })
  254. }
  255. </script>
  256. <style lang="scss" scoped>
  257. :deep(.anticon) {
  258. &:hover {
  259. color: var(--el-color-primary) !important;
  260. }
  261. }
  262. .smsbtn {
  263. margin-top: 33px;
  264. }
  265. </style>