forgotPassword.tsx 6.9 KB

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