index.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. path: '',
  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, id, appId } = this.$route.query
  54. this.code = code
  55. this.formData.appId = appId
  56. this.formData.authUserId = parseInt(id)
  57. this.path = `/${id}/${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.$toast('登录成功')
  67. setTimeout(() => {
  68. this.$router.push(this.path)
  69. }, 1000)
  70. } catch (error) {
  71. console.log(error)
  72. }
  73. },
  74. // 发送验证码
  75. async onSend() {
  76. if (this.sendStatus > 0) return
  77. // 验证手机号是否合法
  78. if (!isMobile(this.formData.mobile)) {
  79. this.$toast('请输入正确的手机号')
  80. return
  81. }
  82. try {
  83. // 发送验证码
  84. const res = await this.$http.api.sendVerifyCode({
  85. mobile: this.formData.mobile,
  86. authUserId: this.formData.authUserId,
  87. type: 1,
  88. })
  89. this.$toast('验证码已发送')
  90. // 开启倒计时
  91. this.countdown()
  92. } catch (error) {
  93. console.log(error)
  94. }
  95. },
  96. // 短信倒计时
  97. countdown() {
  98. this.sendStatus = 30
  99. this.timer = setInterval(() => {
  100. if (this.sendStatus === 0) {
  101. clearInterval(this.timer)
  102. return
  103. }
  104. this.sendStatus--
  105. }, 1000)
  106. },
  107. // 微信授权登录
  108. async autoLogin() {
  109. try {
  110. const res = await this.$http.api.wechatLogin({
  111. code: this.code,
  112. appId: this.formData.appId,
  113. })
  114. this.$store.dispatch('user/login', res.data)
  115. this.$router.push(this.path)
  116. } catch (error) {
  117. // 未绑定微信号
  118. if (error.code === -2) {
  119. this.$toast('未绑定手机号')
  120. this.showBind = true
  121. this.formData.openId = error.data.openId
  122. this.formData.accessToken = error.data.accessToken
  123. } else {
  124. // 其他错误跳转首页
  125. setTimeout(() => {
  126. this.$router.push(this.path)
  127. }, 1000)
  128. }
  129. }
  130. },
  131. },
  132. }
  133. </script>
  134. <style scoped lang="scss">
  135. .auth-page {
  136. .form {
  137. .form-item {
  138. position: relative;
  139. border-bottom: 0.1vw solid #d8d8d8;
  140. input {
  141. display: block;
  142. width: 100%;
  143. font-size: 4.2vw;
  144. line-height: 10vw;
  145. }
  146. .send {
  147. position: absolute;
  148. right: 2.4vw;
  149. top: 50%;
  150. transform: translateY(-50%);
  151. font-size: 3.4vw;
  152. }
  153. }
  154. .submit {
  155. background: #bc1724;
  156. font-size: 3.2vw;
  157. color: #fff;
  158. text-align: center;
  159. line-height: 10vw;
  160. }
  161. }
  162. }
  163. </style>