login.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import { defineComponent } from 'vue'
  2. import { CellGroup, Field, Button, CountDown, Row, Col, Toast } from 'vant'
  3. import ImgCode from '@/components/col-img-code'
  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. requestType: 'form',
  57. data: {
  58. username: this.username,
  59. password: this.password,
  60. clientId: 'student',
  61. clientSecret: 'student'
  62. }
  63. })
  64. } else {
  65. res = await request.post('/api-auth/smsLogin', {
  66. requestType: 'form',
  67. data: {
  68. clientId: 'student',
  69. clientSecret: 'student',
  70. phone: this.username,
  71. smsCode: this.smsCode
  72. }
  73. })
  74. }
  75. const { authentication } = res.data
  76. setAuth(authentication.token_type + ' ' + authentication.access_token)
  77. let userCash = await request.get('/api-student/student/queryUserInfo', {
  78. initRequest: true // 初始化接口
  79. })
  80. setLogin(userCash.data)
  81. this.directNext()
  82. } catch {}
  83. },
  84. async onSendCode() {
  85. // 发送验证码
  86. if (!checkPhone(this.username)) {
  87. return Toast('请输入正确的手机号码')
  88. }
  89. this.imgCodeStatus = true
  90. },
  91. onCodeSend() {
  92. this.countDownStatus = false
  93. this.countDownRef.start()
  94. },
  95. onFinished() {
  96. this.countDownStatus = true
  97. this.countDownRef.reset()
  98. },
  99. onChange() {
  100. if (this.loginType === 'PWD') {
  101. this.loginType = 'SMS'
  102. } else if (this.loginType === 'SMS') {
  103. this.loginType = 'PWD'
  104. }
  105. }
  106. },
  107. render() {
  108. return (
  109. <div class={styles.login}>
  110. <div class={styles.loginTitle}>
  111. 您好,
  112. <br /> 欢迎使用酷乐秀
  113. </div>
  114. <CellGroup class={styles.margin34} border={false}>
  115. <Row style={{ marginBottom: '16px' }}>
  116. <Col span={24} class={styles.formTitle}>
  117. 手机号
  118. </Col>
  119. <Col span={24} class="van-hairline--bottom">
  120. <Field
  121. v-model={this.username}
  122. name="手机号"
  123. placeholder="请输入您的手机号"
  124. type="tel"
  125. maxlength={11}
  126. />
  127. </Col>
  128. </Row>
  129. {this.loginType === 'PWD' ? (
  130. <Row>
  131. <Col span={24} class={styles.formTitle}>
  132. 密码
  133. </Col>
  134. <Col span={24} class="van-hairline--bottom">
  135. <Field
  136. v-model={this.password}
  137. type="password"
  138. name="密码"
  139. placeholder="请输入密码"
  140. />
  141. </Col>
  142. </Row>
  143. ) : (
  144. <Row>
  145. <Col span={24} class={styles.formTitle}>
  146. 验证码
  147. </Col>
  148. <Col span={24} class="van-hairline--bottom">
  149. <Field
  150. v-model={this.smsCode}
  151. name="验证码"
  152. placeholder="请输入验证码"
  153. type="tel"
  154. maxlength={6}
  155. // @ts-ignore
  156. vSlots={{
  157. button: () =>
  158. this.countDownStatus ? (
  159. <span class={styles.codeText} onClick={this.onSendCode}>
  160. 获取验证码
  161. </span>
  162. ) : (
  163. <CountDown
  164. ref={this.countDownRef}
  165. auto-start={false}
  166. time={this.countDownTime}
  167. onFinish={this.onFinished}
  168. format="ss秒"
  169. />
  170. )
  171. }}
  172. />
  173. </Col>
  174. </Row>
  175. )}
  176. </CellGroup>
  177. <div class={styles.margin34}>
  178. <Button
  179. round
  180. block
  181. type="primary"
  182. disabled={this.codeDisable}
  183. onClick={this.onLogin}
  184. >
  185. 提交
  186. </Button>
  187. <Button block round color="#F5F7FB" onClick={this.onChange}>
  188. {this.loginType === 'PWD' ? '验证码登录' : '密码登录'}
  189. </Button>
  190. </div>
  191. {this.imgCodeStatus ? (
  192. <ImgCode
  193. v-model:value={this.imgCodeStatus}
  194. phone={this.username}
  195. onClose={() => {
  196. this.imgCodeStatus = false
  197. }}
  198. onSendCode={this.onCodeSend}
  199. />
  200. ) : null}
  201. </div>
  202. )
  203. }
  204. })