login.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import { defineComponent } from 'vue';
  2. import { CellGroup, Field, Button, CountDown, showToast } from 'vant';
  3. import ImgCode from '@/components/m-img-code';
  4. import request from '@/helpers/request';
  5. import { setLogin, state } from '@/state';
  6. import { checkPhone, removeAuth, setAuth } from '@/helpers/utils';
  7. import styles from './login.module.less';
  8. import logo from '@/common/images/logo.png';
  9. type loginType = 'PWD' | 'SMS';
  10. export default defineComponent({
  11. name: 'layout-login',
  12. data() {
  13. return {
  14. loginType: 'SMS' as loginType,
  15. username: '',
  16. password: '',
  17. smsCode: '',
  18. countDownStatus: true, // 是否发送验证码
  19. countDownTime: 1000 * 120, // 倒计时时间
  20. // countDownRef: null as any, // 倒计时实例
  21. imgCodeStatus: false
  22. };
  23. },
  24. computed: {
  25. codeDisable() {
  26. let status = true;
  27. if (this.loginType === 'PWD') {
  28. this.username && this.password && (status = false);
  29. } else {
  30. this.username && this.smsCode && (status = false);
  31. }
  32. return status;
  33. }
  34. },
  35. mounted() {
  36. removeAuth();
  37. this.directNext();
  38. },
  39. methods: {
  40. directNext() {
  41. if (state.user.status === 'login' || state.user.status === 'error') {
  42. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  43. const { returnUrl, isRegister, ...rest } = this.$route.query;
  44. this.$router.replace({
  45. path: returnUrl as any,
  46. query: {
  47. ...rest
  48. }
  49. });
  50. }
  51. },
  52. async onLogin() {
  53. try {
  54. // let res: any
  55. const forms: any = {
  56. phone: this.username,
  57. clientId: 'EDUCATION',
  58. clientSecret: 'EDUCATION'
  59. };
  60. if (this.loginType === 'PWD') {
  61. forms.password = this.password;
  62. forms.grant_type = 'password';
  63. const { data } = await request.post('/api-auth/usernameLogin', {
  64. requestType: 'form',
  65. data: {
  66. ...forms
  67. }
  68. });
  69. setAuth(
  70. data.authentication.token_type +
  71. ' ' +
  72. data.authentication.access_token
  73. );
  74. } else {
  75. forms.smsCode = this.smsCode;
  76. const { data } = await request.post('/api-auth/smsLogin', {
  77. requestType: 'form',
  78. data: {
  79. ...forms
  80. }
  81. });
  82. setAuth(
  83. data.authentication.token_type +
  84. ' ' +
  85. data.authentication.access_token
  86. );
  87. }
  88. const userCash = await request.get(
  89. '/api-web/schoolStaff/queryUserInfo',
  90. {
  91. initRequest: true // 初始化接口
  92. }
  93. );
  94. setLogin(userCash.data);
  95. this.directNext();
  96. } catch {
  97. //
  98. }
  99. },
  100. async onSendCode() {
  101. // 发送验证码
  102. if (!checkPhone(this.username)) {
  103. return showToast('请输入正确的手机号码');
  104. }
  105. this.imgCodeStatus = true;
  106. },
  107. onCodeSend() {
  108. this.countDownStatus = false;
  109. this.$nextTick(() => {
  110. (this.$refs.countDownRef as any).start();
  111. });
  112. },
  113. onFinished() {
  114. this.countDownStatus = true;
  115. (this.$refs.countDownRef as any).reset();
  116. },
  117. onChange() {
  118. if (this.loginType === 'PWD') {
  119. this.loginType = 'SMS';
  120. } else if (this.loginType === 'SMS') {
  121. this.loginType = 'PWD';
  122. }
  123. }
  124. },
  125. render() {
  126. return (
  127. <div class={[styles.login]}>
  128. <div class={styles.logo}>
  129. <img src={logo} />
  130. </div>
  131. <CellGroup class={styles.container} border={false}>
  132. <Field
  133. v-model={this.username}
  134. name="手机号"
  135. placeholder="请输入您的手机号"
  136. type="tel"
  137. class={styles['input-group']}
  138. maxlength={11}
  139. />
  140. {this.loginType === 'PWD' ? (
  141. <Field
  142. v-model={this.password}
  143. type="password"
  144. name="密码"
  145. class={styles['input-group']}
  146. placeholder="请输入密码"
  147. />
  148. ) : (
  149. <Field
  150. v-model={this.smsCode}
  151. name="验证码"
  152. placeholder="请输入验证码"
  153. type="tel"
  154. class={styles['input-group']}
  155. maxlength={6}
  156. v-slots={{
  157. button: () =>
  158. this.countDownStatus ? (
  159. <span class={styles.codeText} onClick={this.onSendCode}>
  160. 获取验证码
  161. </span>
  162. ) : (
  163. <CountDown
  164. ref="countDownRef"
  165. auto-start={false}
  166. time={this.countDownTime}
  167. onFinish={this.onFinished}
  168. format="ss秒"
  169. />
  170. )
  171. }}
  172. />
  173. )}
  174. </CellGroup>
  175. <div class={styles.margin34}>
  176. <Button
  177. round
  178. block
  179. disabled={this.codeDisable}
  180. onClick={this.onLogin}>
  181. 提交
  182. </Button>
  183. <span class={styles['login-change']} onClick={this.onChange}>
  184. {this.loginType === 'PWD' ? '验证码登录' : '密码登录'}
  185. </span>
  186. </div>
  187. {this.imgCodeStatus ? (
  188. <ImgCode
  189. v-model:value={this.imgCodeStatus}
  190. phone={this.username}
  191. onClose={() => {
  192. this.imgCodeStatus = false;
  193. }}
  194. onSendCode={this.onCodeSend}
  195. />
  196. ) : null}
  197. </div>
  198. );
  199. }
  200. });