login.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { defineComponent } from "vue";
  2. import { CellGroup, Field, Button, CountDown } from "vant";
  3. import ImgCode from "@/components/imgCode";
  4. import request from "@/helpers/request";
  5. import { setLogin, state } from "@/student/state";
  6. import { removeAuth, setAuth } from "@/helpers/utils";
  7. import styles from "./login.module.less";
  8. type loginType = 'PWD' | 'SMS';
  9. export default defineComponent({
  10. name: 'login',
  11. data() {
  12. return {
  13. loginType: 'SMS' as loginType,
  14. username: '',
  15. password: '',
  16. smsCode: '',
  17. countDownStatus: true, // 是否发送验证码
  18. countDownTime: 1000 * 120, // 倒计时时间
  19. countDownRef: null as any, // 倒计时实例
  20. imgCodeStatus: false,
  21. }
  22. },
  23. computed: {
  24. codeDisable() {
  25. let status = true;
  26. if (this.loginType === 'PWD') {
  27. this.username && this.password && (status = false);
  28. } else {
  29. this.username && this.smsCode && (status = false);
  30. }
  31. return status;
  32. },
  33. },
  34. mounted() {
  35. removeAuth();
  36. this.directNext();
  37. },
  38. methods: {
  39. directNext() {
  40. if (state.user.status === "login" || state.user.status === "error") {
  41. const { returnUrl, isRegister, ...rest } = this.$route.query;
  42. this.$router.replace({
  43. path: returnUrl as any,
  44. query: {
  45. ...rest,
  46. },
  47. });
  48. }
  49. },
  50. async onLogin() {
  51. try {
  52. let res: any;
  53. if(this.loginType === 'PWD') {
  54. res = await request.post('/api-auth/usernameLogin', {
  55. data: {
  56. username: this.username,
  57. password: this.password,
  58. clientId: 'student',
  59. clientSecret: 'student'
  60. }
  61. });
  62. } else {
  63. res = await request.post('/api-auth/smsLogin', {
  64. data: {
  65. clientId: 'student',
  66. clientSecret: 'student',
  67. phone: this.username,
  68. smsCode: this.smsCode,
  69. channel: 'H5'
  70. }
  71. });
  72. }
  73. const { authentication } = res.data;
  74. setAuth(authentication.token_type + " " + authentication.access_token);
  75. let userCash = await request.get('/api-student/userCashAccount/get', {
  76. initRequest: true // 初始化接口
  77. })
  78. setLogin(userCash.data)
  79. this.directNext();
  80. } catch{
  81. }
  82. },
  83. async onSendCode() { // 发送验证码
  84. // if(!checkPhone(this.phoneNumber)) {
  85. // return
  86. // }
  87. this.imgCodeStatus = true
  88. },
  89. onCodeSend() {
  90. this.countDownStatus = false;
  91. this.countDownRef.start();
  92. },
  93. onFinished() {
  94. this.countDownStatus = true;
  95. this.countDownRef.reset();
  96. },
  97. onChange() {
  98. if(this.loginType === 'PWD') {
  99. this.loginType = 'SMS'
  100. } else if(this.loginType === 'SMS') {
  101. this.loginType = 'PWD'
  102. }
  103. },
  104. },
  105. render() {
  106. return (
  107. <div class={styles.login}>
  108. <div class={styles.loginTitle}>您好,<br /> 欢迎使用酷乐秀</div>
  109. <CellGroup inset>
  110. <Field
  111. v-model={this.username}
  112. name="手机号"
  113. label="手机号"
  114. placeholder="手机号"
  115. type="tel"
  116. maxlength={11}
  117. />
  118. { this.loginType === 'PWD' ? <Field
  119. v-model={this.password}
  120. type="password"
  121. name="密码"
  122. label="密码"
  123. placeholder="密码"
  124. /> : <Field
  125. v-model={this.smsCode}
  126. name="验证码"
  127. label="验证码"
  128. placeholder="验证码"
  129. type="tel"
  130. maxlength={6}
  131. // @ts-ignore
  132. vSlots={{
  133. button: () => (
  134. this.countDownStatus ? <Button size="small" round type="primary" onClick={this.onSendCode}>发送验证码</Button> : <CountDown ref={this.countDownRef} auto-start="false" time={this.countDownTime} onFinish={this.onFinished} format="ss秒" />
  135. )
  136. }}
  137. /> }
  138. </CellGroup>
  139. <div style="margin: 16px;">
  140. <Button round block type="primary" disabled={this.codeDisable} onClick={this.onLogin}>
  141. 提交
  142. </Button>
  143. <Button plain onClick={this.onChange}>{ this.loginType === 'PWD' ? '验证码登录' : '密码登录' }</Button>
  144. </div>
  145. { this.imgCodeStatus ? <ImgCode v-model:value={this.imgCodeStatus} phone={this.username} onClose={() => { this.imgCodeStatus = false }} onSendCode={this.onCodeSend} /> : null }
  146. </div>
  147. )
  148. }
  149. })