code.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import {
  2. Teleport,
  3. defineComponent,
  4. nextTick,
  5. onMounted,
  6. reactive,
  7. watch
  8. } from 'vue';
  9. import styles from './login.module.less';
  10. import loginLogo from './images/login-logo.png';
  11. import {
  12. Button,
  13. CellGroup,
  14. Icon,
  15. NumberKeyboard,
  16. PasswordInput,
  17. showToast
  18. } from 'vant';
  19. import request from '@/helpers/request';
  20. import { useRouter } from 'vue-router';
  21. import { storage } from '@/helpers/storage';
  22. import { ACCESS_TOKEN } from '@/store/mutation-types';
  23. import { setLogin } from '@/state';
  24. import MImgCode from '@/components/m-img-code';
  25. export default defineComponent({
  26. name: 'layout-code',
  27. props: {
  28. phone: {
  29. type: String,
  30. default: ''
  31. },
  32. isRegister: {
  33. type: String,
  34. default: ''
  35. }
  36. },
  37. emits: ['close', 'confirm'],
  38. setup(props, { emit }) {
  39. const router = useRouter();
  40. const forms = reactive({
  41. imgCodeStatus: false,
  42. smsCode: '',
  43. showKeyboard: false,
  44. countDownStatus: true,
  45. countDownTime: 120, // 倒计时时间
  46. countTimer: null as any
  47. });
  48. // const onSendSms = async () => {
  49. // try {
  50. // await request.post('/edu-app/open/sendSmsVerify', {
  51. // requestType: 'form',
  52. // data: {
  53. // clientId: 'cooleshow-student',
  54. // type: props.isRegister ? 'REGISTER' : 'LOGIN',
  55. // mobile: props.phone,
  56. // code: ''
  57. // }
  58. // });
  59. // onCountDown();
  60. // setTimeout(() => {
  61. // showToast('验证码已发送');
  62. // }, 100);
  63. // } catch {
  64. // forms.countDownStatus = true;
  65. // }
  66. // };
  67. const onCountDown = () => {
  68. forms.countDownStatus = false;
  69. forms.countTimer = setInterval(() => {
  70. if (forms.countDownTime > 0) {
  71. forms.countDownTime--;
  72. } else {
  73. forms.countDownStatus = true;
  74. clearInterval(forms.countTimer);
  75. }
  76. }, 1000);
  77. forms.showKeyboard = true;
  78. };
  79. const onLogin = async () => {
  80. try {
  81. const params: any = {
  82. username: props.phone,
  83. client_id: 'cooleshow-student',
  84. client_secret: 'cooleshow-student',
  85. password: forms.smsCode,
  86. grant_type: 'password',
  87. loginType: 'SMS'
  88. };
  89. const { data } = await request.post('/edu-app/userlogin', {
  90. requestType: 'form',
  91. data: {
  92. ...params
  93. }
  94. });
  95. storage.set(ACCESS_TOKEN, data.token_type + ' ' + data.access_token);
  96. const userCash = await request.get('/edu-app/user/getUserInfo', {
  97. initRequest: true // 初始化接口
  98. });
  99. setLogin(userCash.data);
  100. emit('close', true);
  101. } catch {
  102. //
  103. }
  104. };
  105. watch(
  106. () => forms.smsCode,
  107. (val: any) => {
  108. if (val && val.length === 6) {
  109. onLogin();
  110. }
  111. }
  112. );
  113. onMounted(() => {
  114. nextTick(async () => {
  115. forms.imgCodeStatus = true;
  116. // await onSendSms();
  117. // forms.showKeyboard = true;
  118. });
  119. });
  120. return () => (
  121. <div class={[styles.login]}>
  122. <Icon
  123. name="arrow-left"
  124. class={styles.arrowLeft}
  125. onClick={() => emit('close')}
  126. />
  127. <div class={styles.loginContainer}>
  128. <img src={loginLogo} class={styles.logo} />
  129. <CellGroup class={styles.container} border={false}>
  130. <h2 class={styles['code-title']}>输入验证码</h2>
  131. <p class={styles.codePhone}>
  132. 已发送6位验证码至<span>{props.phone}</span>
  133. </p>
  134. <PasswordInput
  135. value={forms.smsCode}
  136. onFocus={() => {
  137. forms.showKeyboard = true;
  138. }}
  139. focused={forms.showKeyboard}
  140. length={6}
  141. gutter={12}
  142. />
  143. <NumberKeyboard
  144. v-model={forms.smsCode}
  145. show={forms.showKeyboard}
  146. maxlength={6}
  147. onBlur={() => {
  148. forms.showKeyboard = false;
  149. }}></NumberKeyboard>
  150. <div class={styles.btnWrap}>
  151. <Button
  152. type="primary"
  153. round
  154. onClick={() => {
  155. forms.imgCodeStatus = true;
  156. }}
  157. class={styles.btnSend}
  158. disabled={!forms.countDownStatus}>
  159. 重新发送
  160. {!forms.countDownStatus && <>({forms.countDownTime})</>}
  161. </Button>
  162. </div>
  163. </CellGroup>
  164. </div>
  165. {forms.imgCodeStatus ? (
  166. <Teleport to={'body'}>
  167. <MImgCode
  168. zIndex={2100}
  169. v-model:value={forms.imgCodeStatus}
  170. phone={props.phone}
  171. type="REGISTER"
  172. onClose={() => {
  173. forms.imgCodeStatus = false;
  174. }}
  175. onSendCode={onCountDown}
  176. />
  177. </Teleport>
  178. ) : null}
  179. </div>
  180. );
  181. }
  182. });