forgotPassword.tsx 6.5 KB

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