index.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <template>
  2. <div class="auth-page" v-if="showBind">
  3. <div class="form p-4">
  4. <div class="form-item mb-4">
  5. <input type="text" placeholder="手机号" v-model="formData.mobile" />
  6. </div>
  7. <div class="form-item mb-4 code">
  8. <input
  9. type="text"
  10. placeholder="验证码"
  11. class="code"
  12. v-model="formData.verifyCode"
  13. />
  14. <span class="send" @click="onSend">{{ sendCodeBtnText }}</span>
  15. </div>
  16. <div class="submit" @click="onSubmit">登录</div>
  17. </div>
  18. </div>
  19. </template>
  20. <script>
  21. import { isMobile } from '@/utils/validator'
  22. export default {
  23. data() {
  24. return {
  25. showBind: false,
  26. sendStatus: 0,
  27. formData: {
  28. authUserId: '',
  29. appId: '',
  30. openId: '',
  31. accessToken: '',
  32. mobile: '',
  33. verifyCode: '',
  34. },
  35. redirectPath: '',
  36. code: '',
  37. }
  38. },
  39. computed: {
  40. sendCodeBtnText() {
  41. return this.sendStatus === 0
  42. ? '发送验证码'
  43. : `再次发送${this.sendStatus}s`
  44. },
  45. },
  46. mounted() {
  47. this.initData()
  48. },
  49. methods: {
  50. // 初始化数据
  51. initData() {
  52. // 获取链接参数
  53. const { code, type, authUserId, appId } = this.$route.query
  54. this.code = code
  55. this.formData.appId = appId
  56. this.formData.authUserId = parseInt(authUserId)
  57. this.redirectPath = `/${authUserId}/${type}`
  58. // 尝试微信授权自动登录
  59. this.autoLogin()
  60. },
  61. // 绑定并登录
  62. async onSubmit() {
  63. try {
  64. const res = await this.$http.api.customLogin(this.formData)
  65. this.$store.dispatch('user/login', res.data)
  66. this.$setStorage(this.redirectPath, 'userInfo', res.data)
  67. this.$toast('登录成功')
  68. setTimeout(() => {
  69. this.$router.push(this.redirectPath)
  70. }, 500)
  71. } catch (error) {
  72. console.log(error)
  73. }
  74. },
  75. // 发送验证码
  76. async onSend() {
  77. if (this.sendStatus > 0) return
  78. // 验证手机号是否合法
  79. if (!isMobile(this.formData.mobile)) {
  80. this.$toast('请输入正确的手机号')
  81. return
  82. }
  83. try {
  84. // 发送验证码
  85. const res = await this.$http.api.sendVerifyCode({
  86. mobile: this.formData.mobile,
  87. authUserId: this.formData.authUserId,
  88. type: 1,
  89. })
  90. this.$toast('验证码已发送')
  91. // 开启倒计时
  92. this.countdown()
  93. } catch (error) {
  94. console.log(error)
  95. }
  96. },
  97. // 短信倒计时
  98. countdown() {
  99. this.sendStatus = 30
  100. this.timer = setInterval(() => {
  101. if (this.sendStatus === 0) {
  102. clearInterval(this.timer)
  103. return
  104. }
  105. this.sendStatus--
  106. }, 1000)
  107. },
  108. // 微信授权登录
  109. async autoLogin() {
  110. try {
  111. const res = await this.$http.api.wechatLogin({
  112. code: this.code,
  113. appId: this.formData.appId,
  114. })
  115. this.$store.dispatch('user/login', res.data)
  116. this.$router.push(this.path)
  117. } catch (error) {
  118. // 未绑定微信号
  119. if (error.code === -2) {
  120. this.$toast('未绑定手机号')
  121. this.showBind = true
  122. this.formData.openId = error.data.openId
  123. this.formData.accessToken = error.data.accessToken
  124. } else {
  125. // 其他错误跳转首页
  126. setTimeout(() => {
  127. this.$router.push(this.path)
  128. }, 1000)
  129. }
  130. }
  131. },
  132. },
  133. }
  134. </script>
  135. <style scoped lang="scss">
  136. .auth-page {
  137. .form {
  138. .form-item {
  139. position: relative;
  140. border-bottom: 0.1vw solid #d8d8d8;
  141. input {
  142. display: block;
  143. width: 100%;
  144. font-size: 4.2vw;
  145. line-height: 10vw;
  146. }
  147. .send {
  148. position: absolute;
  149. right: 2.4vw;
  150. top: 50%;
  151. transform: translateY(-50%);
  152. font-size: 3.4vw;
  153. }
  154. }
  155. .submit {
  156. background: #bc1724;
  157. font-size: 3.2vw;
  158. color: #fff;
  159. text-align: center;
  160. line-height: 10vw;
  161. }
  162. }
  163. }
  164. </style>