forgotPassword.tsx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. import { defineComponent, reactive, ref } from 'vue';
  2. import styles from '../index.module.less';
  3. import pwdIcon from '../images/lock-icon.png';
  4. import lockIcon from '../images/pwdIcon.png';
  5. import useIcon from '../images/phoneIcon.png';
  6. import openEye from '../images/openEye.png';
  7. import closeEye from '../images/closeEye.png';
  8. import {
  9. useMessage,
  10. NForm,
  11. NFormItem,
  12. NInput,
  13. NButton,
  14. NInputGroup,
  15. NModal
  16. } from 'naive-ui';
  17. import { useRoute, useRouter } from 'vue-router';
  18. import { PageEnum } from '/src/enums/pageEnum';
  19. import { storage } from '@/utils/storage';
  20. import { useUserStore } from '/src/store/modules/users';
  21. import { sendSms, updatePassword } from '../api';
  22. import SendSms from './sendSms';
  23. import { modalClickMask } from '/src/state';
  24. interface FormState {
  25. mobile: string;
  26. password: string;
  27. grant_type: string;
  28. loginType: string;
  29. client_id: string;
  30. client_secret: string;
  31. }
  32. export default defineComponent({
  33. name: 'forgotPassword',
  34. props: {
  35. phone: {
  36. type: String,
  37. default: ''
  38. }
  39. },
  40. emits: ['changType'],
  41. setup(props, { emit }) {
  42. const router = useRouter();
  43. const route = useRoute();
  44. const formRef = ref();
  45. const message = useMessage();
  46. const loading = ref(false);
  47. const autoLogin = ref(true);
  48. const showSmsClass = ref(false);
  49. const LOGIN_NAME = PageEnum.BASE_LOGIN_NAME;
  50. const showPwd = ref(false);
  51. const showPwd2 = ref(false);
  52. const userStore = useUserStore();
  53. const formInline = reactive({
  54. mobile: '',
  55. password: '',
  56. password1: '',
  57. code: '',
  58. isCaptcha: true
  59. });
  60. const isDisabledCode = ref(false);
  61. const starTimer = ref(60);
  62. const codeName = '发送短信';
  63. const validatePass2 = (rule: any, value: any, callback: any) => {
  64. // const reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/;
  65. if (value === '') {
  66. callback(new Error('请再次输入密码'));
  67. } else if (value !== formInline.password) {
  68. callback(new Error('两次输入密码不一致!'));
  69. } else if (value.length < 8 || value.length > 20) {
  70. // callback(new Error('密码为6-20位数字和字母组合'));
  71. callback(new Error('8~20位含数字、字母、特殊字符(如:%、&、#等)组合'));
  72. } else {
  73. callback();
  74. }
  75. };
  76. const validatePass = (rule: any, value: any, callback: any) => {
  77. // const reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/;
  78. if (value === '') {
  79. callback(new Error('请输入密码'));
  80. } else if (value.length < 8 || value.length > 20) {
  81. // callback(new Error('密码为6-20位数字和字母组合'));
  82. callback(new Error('8~20位包数字、字母、特殊字符(如:%、&、#等)组合'));
  83. } else {
  84. callback();
  85. }
  86. };
  87. // 初始化手机号
  88. if (props.phone) {
  89. formInline.mobile = props.phone;
  90. }
  91. const handleSubmit = async () => {
  92. formRef.value.validate(async (errors: any) => {
  93. if (!errors) {
  94. message.loading('修改中...');
  95. loading.value = true;
  96. try {
  97. await updatePassword({
  98. ...formInline,
  99. clientType: 'TEACHER'
  100. });
  101. message.success('修改成功');
  102. loading.value = false;
  103. emit('changType');
  104. return false;
  105. } catch (e: any) {
  106. loading.value = false;
  107. message.error(e.msg);
  108. return false;
  109. console.log(e);
  110. }
  111. }
  112. });
  113. return false;
  114. };
  115. const sendMessage = async () => {
  116. if (!formInline.mobile) {
  117. message.error('请输入手机号');
  118. return;
  119. }
  120. showSmsClass.value = true;
  121. // try {
  122. // const res = await sendSms({
  123. // clientId: 'cooleshow-teacher',
  124. // mobile: formInline.mobile,
  125. // type: 'PASSWORD'
  126. // });
  127. // checkTimeOut();
  128. // } catch (e) {
  129. // console.log(e);
  130. // }
  131. };
  132. const checkTimeOut = () => {
  133. if (isDisabledCode.value) {
  134. return;
  135. }
  136. isDisabledCode.value = true;
  137. const tiemr = setInterval(() => {
  138. starTimer.value--;
  139. console.log(starTimer.value);
  140. if (starTimer.value <= 0) {
  141. isDisabledCode.value = false;
  142. clearInterval(tiemr);
  143. }
  144. }, 1000);
  145. };
  146. return () => (
  147. <div class={styles['view-account-form-wrap']}>
  148. {/* <div class={styles.formTitle}>
  149. <div class={styles.dot}></div>
  150. 酷乐秀课堂乐器
  151. </div> */}
  152. <NForm
  153. ref={formRef}
  154. label-placement="left"
  155. size="large"
  156. model={formInline}>
  157. <NFormItem
  158. path="mobile"
  159. rule={[
  160. { required: true, message: '请输入手机号', trigger: 'blur' }
  161. ]}>
  162. <NInput
  163. maxlength={11}
  164. v-model:value={formInline.mobile}
  165. placeholder="请输入手机号">
  166. {{
  167. prefix: () => (
  168. <img src={useIcon} class={styles.prefixIcon} alt="" />
  169. )
  170. }}
  171. </NInput>
  172. </NFormItem>
  173. <NFormItem
  174. path="password"
  175. rule={[
  176. {
  177. validator: validatePass as any,
  178. trigger: 'blur',
  179. required: true
  180. }
  181. ]}>
  182. <NInput
  183. v-model:value={formInline.password}
  184. type="text"
  185. showPasswordOn="click"
  186. placeholder="请输入密码"
  187. inputProps={{ autocomplete: 'off' }}
  188. class={[showPwd.value ? '' : styles['no-pwd']]}>
  189. {{
  190. prefix: () => (
  191. <img src={pwdIcon} class={styles.prefixIcon} alt="" />
  192. ),
  193. suffix: () => (
  194. <img
  195. src={showPwd.value ? openEye : closeEye}
  196. class={styles.pwdIcon}
  197. alt=""
  198. onClick={() => {
  199. showPwd.value = !showPwd.value;
  200. }}
  201. />
  202. )
  203. }}
  204. </NInput>
  205. </NFormItem>
  206. <NFormItem
  207. path="password1"
  208. rule={[
  209. {
  210. validator: validatePass2 as any,
  211. trigger: 'blur',
  212. required: true
  213. }
  214. ]}>
  215. <NInput
  216. v-model:value={formInline.password1}
  217. type="text"
  218. showPasswordOn="click"
  219. placeholder="请确认密码"
  220. inputProps={{ autocomplete: 'off' }}
  221. class={[showPwd2.value ? '' : styles['no-pwd']]}>
  222. {{
  223. prefix: () => (
  224. <img src={pwdIcon} class={styles.prefixIcon} alt="" />
  225. ),
  226. suffix: () => (
  227. <img
  228. src={showPwd2.value ? openEye : closeEye}
  229. class={styles.pwdIcon}
  230. alt=""
  231. onClick={() => {
  232. showPwd2.value = !showPwd2.value;
  233. }}
  234. />
  235. )
  236. }}
  237. </NInput>
  238. </NFormItem>
  239. <NFormItem
  240. path="code"
  241. rule={[
  242. { required: true, message: '请输入验证码', trigger: 'blur' }
  243. ]}>
  244. <NInputGroup>
  245. <NInput
  246. v-model:value={formInline.code}
  247. type="text"
  248. showPasswordOn="click"
  249. placeholder="请输入验证码"
  250. inputProps={{ autocomplete: 'off' }}
  251. class={styles.sendInput}
  252. onKeydown={(e: KeyboardEvent) => {
  253. if (e.code === 'Enter' || e.code === 'NumpadEnter') {
  254. handleSubmit();
  255. }
  256. }}>
  257. {{
  258. prefix: () => (
  259. <img src={lockIcon} class={styles.prefixIcon} alt="" />
  260. ),
  261. suffix: () => (
  262. <NButton
  263. class={styles.sendMsg}
  264. disabled={isDisabledCode.value}
  265. onClick={() => sendMessage()}>
  266. {isDisabledCode.value ? starTimer.value + 'S' : codeName}
  267. </NButton>
  268. )
  269. }}
  270. </NInput>
  271. </NInputGroup>
  272. </NFormItem>
  273. <NFormItem>
  274. <NButton
  275. class={[styles.submitBtm, styles.submitForgoBtm]}
  276. type="primary"
  277. onClick={handleSubmit}
  278. size="large"
  279. disabled={loading.value}
  280. block>
  281. 确认
  282. </NButton>
  283. </NFormItem>
  284. <NFormItem>
  285. <NButton
  286. text
  287. class={styles.forgetBtm}
  288. onClick={() => {
  289. emit('changType');
  290. }}
  291. size="large"
  292. block>
  293. 返回登录
  294. </NButton>
  295. </NFormItem>
  296. </NForm>
  297. <NModal maskClosable={modalClickMask} v-model:show={showSmsClass.value}>
  298. <SendSms
  299. phone={formInline.mobile}
  300. onClose={() => (showSmsClass.value = false)}
  301. onSendCode={() => {
  302. checkTimeOut();
  303. }}
  304. />
  305. </NModal>
  306. </div>
  307. );
  308. }
  309. });