login.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import { defineComponent } from "vue";
  2. import { CellGroup, Field, Button, CountDown, Row, Col, Toast } from "vant";
  3. import ImgCode from "@/components/ImgCode";
  4. import { checkPhone } from "@/helpers/validate";
  5. import request from "@/helpers/request";
  6. import { setLogin, state } from "@/state";
  7. import { removeAuth, setAuth } from "@/helpers/utils";
  8. import styles from "./login.module.less";
  9. type loginType = 'PWD' | 'SMS';
  10. export default defineComponent({
  11. name: '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. const { returnUrl, isRegister, ...rest } = this.$route.query;
  43. this.$router.replace({
  44. path: returnUrl as any,
  45. query: {
  46. ...rest,
  47. },
  48. });
  49. }
  50. },
  51. async onLogin() {
  52. try {
  53. let res: any;
  54. if (this.loginType === 'PWD') {
  55. res = await request.post('/api-auth/usernameLogin', {
  56. data: {
  57. username: this.username,
  58. password: this.password,
  59. clientId: 'student',
  60. clientSecret: 'student'
  61. }
  62. });
  63. } else {
  64. res = await request.post('/api-auth/smsLogin', {
  65. data: {
  66. clientId: 'student',
  67. clientSecret: 'student',
  68. phone: this.username,
  69. smsCode: this.smsCode,
  70. channel: 'H5'
  71. }
  72. });
  73. }
  74. const { authentication } = res.data;
  75. setAuth(authentication.token_type + " " + authentication.access_token);
  76. let userCash = await request.get('/api-student/userCashAccount/get', {
  77. initRequest: true // 初始化接口
  78. })
  79. setLogin(userCash.data)
  80. this.directNext();
  81. } catch {
  82. }
  83. },
  84. async onSendCode() { // 发送验证码
  85. if(!checkPhone(this.username)) {
  86. return Toast('请输入正确的手机号码');
  87. }
  88. this.imgCodeStatus = true
  89. },
  90. onCodeSend() {
  91. this.countDownStatus = false;
  92. this.countDownRef.start();
  93. },
  94. onFinished() {
  95. this.countDownStatus = true;
  96. this.countDownRef.reset();
  97. },
  98. onChange() {
  99. if (this.loginType === 'PWD') {
  100. this.loginType = 'SMS'
  101. } else if (this.loginType === 'SMS') {
  102. this.loginType = 'PWD'
  103. }
  104. },
  105. },
  106. render() {
  107. return (
  108. <div class={styles.login}>
  109. <div class={styles.loginTitle}>您好,<br /> 欢迎使用酷乐秀</div>
  110. <CellGroup class={styles.margin34} border={false}>
  111. <Row style={{ marginBottom: '16px' }}>
  112. <Col span={24} class={styles.formTitle}>手机号</Col>
  113. <Col span={24} class="van-hairline--bottom">
  114. <Field
  115. v-model={this.username}
  116. name="手机号"
  117. placeholder="请输入您的手机号"
  118. type="tel"
  119. maxlength={11}
  120. />
  121. </Col>
  122. </Row>
  123. {this.loginType === 'PWD' ? <Row>
  124. <Col span={24} class={styles.formTitle}>密码</Col>
  125. <Col span={24} class="van-hairline--bottom">
  126. <Field
  127. v-model={this.password}
  128. type="password"
  129. name="密码"
  130. placeholder="请输入密码"
  131. />
  132. </Col>
  133. </Row> : <Row>
  134. <Col span={24} class={styles.formTitle}>密码</Col>
  135. <Col span={24} class="van-hairline--bottom">
  136. <Field
  137. v-model={this.smsCode}
  138. name="验证码"
  139. placeholder="请输入验证码"
  140. type="tel"
  141. maxlength={6}
  142. // @ts-ignore
  143. vSlots={{
  144. button: () => (
  145. this.countDownStatus ? <span class={styles.codeText} onClick={this.onSendCode}>获取验证码</span> : <CountDown ref={this.countDownRef} auto-start={false} time={this.countDownTime} onFinish={this.onFinished} format="ss秒" />
  146. )
  147. }}
  148. />
  149. </Col>
  150. </Row>}
  151. </CellGroup>
  152. <div class={styles.margin34}>
  153. <Button round block type="primary" disabled={this.codeDisable} onClick={this.onLogin}>
  154. 提交
  155. </Button>
  156. <Button block round color="#F5F7FB" onClick={this.onChange}>{this.loginType === 'PWD' ? '验证码登录' : '密码登录'}</Button>
  157. </div>
  158. {this.imgCodeStatus ? <ImgCode v-model:value={this.imgCodeStatus} phone={this.username} onClose={() => { this.imgCodeStatus = false }} onSendCode={this.onCodeSend} /> : null}
  159. </div>
  160. )
  161. }
  162. })