login.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import { defineComponent } from 'vue'
  2. import { CellGroup, Field, Button, CountDown, Row, Col, showToast, Popup } from 'vant'
  3. import ImgCode from '@/components/o-img-code'
  4. import { checkPhone } from '@/helpers/validate'
  5. import { setLogin, state } from '@/state'
  6. import { removeAuth, setAuth } from './utils'
  7. import styles from './index.module.less'
  8. import request from '@/helpers/request'
  9. import { browser } from '@/helpers/utils'
  10. type loginType = 'PWD' | 'SMS'
  11. export default defineComponent({
  12. name: 'login-music',
  13. data() {
  14. return {
  15. loginType: 'SMS' as loginType,
  16. username: '',
  17. password: '',
  18. smsCode: '',
  19. countDownStatus: true, // 是否发送验证码
  20. countDownTime: 1000 * 120, // 倒计时时间
  21. // countDownRef: null as any, // 倒计时实例
  22. imgCodeStatus: false,
  23. showPopup: false
  24. }
  25. },
  26. computed: {
  27. codeDisable() {
  28. let status = true
  29. this.username && this.smsCode && (status = false)
  30. return status
  31. }
  32. },
  33. mounted() {
  34. removeAuth()
  35. this.directNext()
  36. // 判断是否是微信,只能微信中打开
  37. // if (!browser().weixin) {
  38. // this.showPopup = true
  39. // } else {
  40. // this.getAppId()
  41. // }
  42. },
  43. methods: {
  44. async getAppId() {
  45. try {
  46. const { data } = await request.get('/api-student/open/paramConfig/wechatAppId')
  47. console.log(data)
  48. } catch {
  49. //
  50. }
  51. },
  52. directNext() {
  53. if (state.user.status === 'login' || state.user.status === 'error') {
  54. const { returnUrl, isRegister, ...rest } = this.$route.query
  55. this.$router.replace({
  56. path: returnUrl as any,
  57. query: {
  58. ...rest
  59. }
  60. })
  61. }
  62. },
  63. async onLogin() {
  64. try {
  65. // let res: any
  66. const forms: any = {
  67. username: this.username,
  68. client_id: 'jmedu-student',
  69. client_secret: 'jmedu-student',
  70. autoRegister: true,
  71. password: this.smsCode,
  72. loginType: 'SMS',
  73. grant_type: 'SMS'
  74. }
  75. const { data } = await request.post('/api-oauth/userlogin', {
  76. requestType: 'form',
  77. data: {
  78. ...forms
  79. }
  80. })
  81. setAuth(data.token_type + ' ' + data.access_token)
  82. const userCash = await request.get('/api-student/appLoginUser/getUserInfo', {
  83. initRequest: true // 初始化接口
  84. })
  85. setLogin(userCash.data)
  86. this.directNext()
  87. } catch {
  88. //
  89. }
  90. },
  91. async onSendCode() {
  92. // 发送验证码
  93. if (!checkPhone(this.username)) {
  94. return showToast('请输入正确的手机号码')
  95. }
  96. this.imgCodeStatus = true
  97. },
  98. onCodeSend() {
  99. this.countDownStatus = false
  100. this.$nextTick(() => {
  101. ;(this.$refs.countDownRef as any).start()
  102. })
  103. },
  104. onFinished() {
  105. this.countDownStatus = true
  106. ;(this.$refs.countDownRef as any).reset()
  107. },
  108. onChange() {
  109. if (this.loginType === 'PWD') {
  110. this.loginType = 'SMS'
  111. } else if (this.loginType === 'SMS') {
  112. this.loginType = 'PWD'
  113. }
  114. }
  115. },
  116. render() {
  117. return (
  118. <div class={styles.login}>
  119. <div class={styles.loginTitle}>
  120. 您好,
  121. <br /> 欢迎使用管乐团学生端
  122. </div>
  123. <CellGroup class={styles.margin34} border={false}>
  124. <Row style={{ marginBottom: '16px' }}>
  125. <Col span={24} class={styles.formTitle}>
  126. 手机号
  127. </Col>
  128. <Col span={24} class="van-hairline--bottom">
  129. <Field
  130. v-model={this.username}
  131. name="手机号"
  132. placeholder="请输入您的手机号"
  133. type="tel"
  134. maxlength={11}
  135. />
  136. </Col>
  137. </Row>
  138. <Row>
  139. <Col span={24} class={styles.formTitle}>
  140. 验证码
  141. </Col>
  142. <Col span={24} class="van-hairline--bottom">
  143. <Field
  144. v-model={this.smsCode}
  145. name="验证码"
  146. placeholder="请输入验证码"
  147. type="tel"
  148. maxlength={6}
  149. v-slots={{
  150. button: () =>
  151. this.countDownStatus ? (
  152. <span class={styles.codeText} onClick={this.onSendCode}>
  153. 获取验证码
  154. </span>
  155. ) : (
  156. <CountDown
  157. ref="countDownRef"
  158. auto-start={false}
  159. time={this.countDownTime}
  160. onFinish={this.onFinished}
  161. format="ss秒"
  162. />
  163. )
  164. }}
  165. />
  166. </Col>
  167. </Row>
  168. </CellGroup>
  169. <div class={styles.margin34}>
  170. <Button round block type="primary" disabled={this.codeDisable} onClick={this.onLogin}>
  171. 提交
  172. </Button>
  173. </div>
  174. {this.imgCodeStatus ? (
  175. <ImgCode
  176. v-model:value={this.imgCodeStatus}
  177. phone={this.username}
  178. onClose={() => {
  179. this.imgCodeStatus = false
  180. }}
  181. onSendCode={this.onCodeSend}
  182. />
  183. ) : null}
  184. <Popup
  185. v-model:show={this.showPopup}
  186. round
  187. style={{ width: '92%' }}
  188. closeOnClickOverlay={false}
  189. >
  190. <div class={styles.popupContainer}>
  191. <div class={styles.dialogTitle}>
  192. <i></i>
  193. 提示
  194. </div>
  195. <p class={styles.popupTips}>请使用微信打开</p>
  196. </div>
  197. </Popup>
  198. </div>
  199. )
  200. }
  201. })