forgotPassword.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import { defineComponent, reactive, ref } from 'vue';
  2. import styles from '../index.module.less';
  3. import openEye from '../images/openEye.png';
  4. import closeEye from '../images/closeEye.png';
  5. import {
  6. useMessage,
  7. NForm,
  8. NFormItem,
  9. NInput,
  10. NButton,
  11. NInputGroup,
  12. NSpace
  13. } from 'naive-ui';
  14. import { useRoute, useRouter } from 'vue-router';
  15. import { PageEnum } from '/src/enums/pageEnum';
  16. import { storage } from '@/utils/storage';
  17. import { useUserStore } from '/src/store/modules/users';
  18. import { sendSms, updatePassword } from '../../login/api';
  19. interface FormState {
  20. mobile: string;
  21. password: string;
  22. grant_type: string;
  23. loginType: string;
  24. client_id: string;
  25. client_secret: string;
  26. }
  27. export default defineComponent({
  28. name: 'forgotPassword',
  29. emits: ['close'],
  30. props: {
  31. phone: {
  32. type: String,
  33. default: ''
  34. }
  35. },
  36. setup(props, { emit }) {
  37. const formRef = ref();
  38. const message = useMessage();
  39. const loading = ref(false);
  40. const showPwd = ref(false);
  41. const userStore = useUserStore();
  42. const formInline = reactive({
  43. mobile: props.phone,
  44. password: '',
  45. code: '',
  46. isCaptcha: true
  47. });
  48. const isDisabledCode = ref(false);
  49. const starTimer = ref(60);
  50. const codeName = '发送短信';
  51. const handleSubmit = async () => {
  52. formRef.value.validate(async (errors: any) => {
  53. if (!errors) {
  54. message.loading('修改中...');
  55. loading.value = true;
  56. try {
  57. await updatePassword({
  58. ...formInline,
  59. clientType: 'TEACHER'
  60. });
  61. message.success('修改成功');
  62. loading.value = false;
  63. emit('close');
  64. setTimeout(() => {
  65. userStore.logout();
  66. history.go(0);
  67. }, 500);
  68. return false;
  69. } catch (e: any) {
  70. loading.value = false;
  71. message.error(e.msg);
  72. return false;
  73. }
  74. }
  75. });
  76. return false;
  77. };
  78. const sendMessage = () => {
  79. formRef.value?.validate(
  80. (errors: any) => {
  81. if (errors) {
  82. return;
  83. }
  84. checkTimeOut();
  85. sendSms({
  86. clientId: 'cooleshow-teacher',
  87. mobile: formInline.mobile,
  88. type: 'PASSWORD'
  89. });
  90. },
  91. (rule: any) => {
  92. return rule.key === 'a';
  93. }
  94. );
  95. };
  96. const checkTimeOut = () => {
  97. if (isDisabledCode.value) {
  98. return;
  99. }
  100. isDisabledCode.value = true;
  101. const tiemr = setInterval(() => {
  102. starTimer.value--;
  103. console.log(starTimer.value);
  104. if (starTimer.value <= 0) {
  105. isDisabledCode.value = false;
  106. clearInterval(tiemr);
  107. }
  108. }, 1000);
  109. };
  110. return () => (
  111. <>
  112. <div class={styles.wrap}>
  113. <NForm
  114. ref={formRef}
  115. label-placement="left"
  116. size="large"
  117. model={formInline}>
  118. <NFormItem
  119. path="mobile"
  120. rule={[
  121. {
  122. key: 'a',
  123. required: true,
  124. message: '请输入手机号',
  125. trigger: 'blur'
  126. },
  127. {
  128. key: 'a',
  129. pattern: /^1[3456789]\d{9}$/,
  130. message: '手机号格式不正确',
  131. trigger: 'blur'
  132. }
  133. ]}>
  134. <NInput
  135. readonly
  136. type="text"
  137. disabled={true}
  138. maxlength={11}
  139. v-model:value={formInline.mobile}
  140. placeholder="请输入手机号"></NInput>
  141. </NFormItem>
  142. <NFormItem
  143. path="code"
  144. rule={[
  145. { required: true, message: '请输入验证码', trigger: 'blur' },
  146. {
  147. pattern: /^\d+$/,
  148. message: '请输入数字验证码',
  149. trigger: 'blur'
  150. }
  151. ]}>
  152. <NInputGroup>
  153. <NInput
  154. v-model:value={formInline.code}
  155. type="text"
  156. maxlength={6}
  157. placeholder="请输入验证码"
  158. class={styles.sendInput}></NInput>
  159. <NButton
  160. type="primary"
  161. class={styles.sendMsg}
  162. disabled={isDisabledCode.value}
  163. bordered={false}
  164. onClick={() => sendMessage()}>
  165. {isDisabledCode.value ? starTimer.value : codeName}
  166. </NButton>
  167. </NInputGroup>
  168. </NFormItem>
  169. <NFormItem
  170. path="password"
  171. rule={[
  172. {
  173. required: true,
  174. message: '请输入密码',
  175. trigger: 'blur'
  176. },
  177. {
  178. pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/,
  179. message: '密码为6-20位数字和字母组合',
  180. trigger: 'blur'
  181. }
  182. ]}>
  183. <NInput
  184. v-model:value={formInline.password}
  185. showPasswordOn="click"
  186. placeholder="请输入密码"
  187. inputProps={{ autocomplete: 'off' }}
  188. class={[showPwd.value ? '' : styles['no-pwd']]}>
  189. {{
  190. 'password-visible-icon': () => (
  191. <img src={openEye} class={styles.pwdIcon} />
  192. ),
  193. 'password-invisible-icon': () => (
  194. <img src={closeEye} class={styles.pwdIcon} />
  195. )
  196. }}
  197. </NInput>
  198. </NFormItem>
  199. </NForm>
  200. </div>
  201. <NSpace
  202. justify="space-around"
  203. style={{ width: '100%' }}
  204. wrap={false}
  205. wrapItem={false}>
  206. <NButton
  207. class={[styles.submitBtm, styles.submitForgoBtm]}
  208. onClick={() => emit('close')}
  209. size="large"
  210. round
  211. disabled={loading.value}>
  212. 取消
  213. </NButton>
  214. <NButton
  215. class={[styles.submitBtm, styles.submitForgoBtm]}
  216. type="primary"
  217. onClick={handleSubmit}
  218. size="large"
  219. round
  220. disabled={loading.value}>
  221. 确认修改
  222. </NButton>
  223. </NSpace>
  224. </>
  225. );
  226. }
  227. });