index.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. beforeDestroy() {
  50. this.$removeStorage(this.redirectPath, 'login_redicret')
  51. },
  52. methods: {
  53. // 初始化数据
  54. initData() {
  55. // 获取链接参数
  56. const { code, type, authUserId, appId } = this.$route.query
  57. this.code = code
  58. this.formData.appId = appId
  59. this.formData.authUserId = parseInt(authUserId)
  60. this.redirectPath = `/${authUserId}/${type}`
  61. // 保存页面路由前缀
  62. this.$store.commit('app/SET_ROUTE_PREFIX', this.redirectPath)
  63. // 保存用户AppId
  64. this.$store.commit('user/SET_AUTH_USER_ID', this.formData.authUserId)
  65. // 尝试微信授权自动登录
  66. this.autoLogin()
  67. },
  68. // 绑定并登录
  69. async onSubmit() {
  70. try {
  71. const res = await this.$http.api.wechatLoginWithCode(this.formData)
  72. this.loginSuccess(res)
  73. } catch (error) {
  74. console.log(error)
  75. }
  76. },
  77. loginSuccess(res) {
  78. this.$toast('登录成功')
  79. this.$store.dispatch('user/login', res.data)
  80. this.$setStorage(this.redirectPath, 'userInfo', res.data)
  81. // 重定向
  82. const login_redicret = this.$getStorage(
  83. this.redirectPath,
  84. 'login_redicret'
  85. )
  86. if (login_redicret && login_redicret !== this.$route.path) {
  87. this.$router.push(login_redicret)
  88. } else {
  89. this.$router.push(this.redirectPath)
  90. }
  91. },
  92. // 发送验证码
  93. async onSend() {
  94. if (this.sendStatus > 0) return
  95. // 验证手机号是否合法
  96. if (!isMobile(this.formData.mobile)) {
  97. this.$toast('请输入正确的手机号')
  98. return
  99. }
  100. try {
  101. // 发送验证码
  102. const res = await this.$http.api.sendVerifyCode({
  103. mobile: this.formData.mobile,
  104. authUserId: this.formData.authUserId,
  105. type: 1,
  106. })
  107. this.$toast('验证码已发送')
  108. // 开启倒计时
  109. this.countdown()
  110. } catch (error) {
  111. console.log(error)
  112. }
  113. },
  114. // 短信倒计时
  115. countdown() {
  116. this.sendStatus = 30
  117. this.timer = setInterval(() => {
  118. if (this.sendStatus === 0) {
  119. clearInterval(this.timer)
  120. return
  121. }
  122. this.sendStatus--
  123. }, 1000)
  124. },
  125. // 微信授权登录
  126. async autoLogin() {
  127. try {
  128. const res = await this.$http.api.wechatLogin({
  129. code: this.code,
  130. appId: this.formData.appId,
  131. })
  132. this.loginSuccess(res)
  133. } catch (error) {
  134. // 未绑定微信号
  135. if (error.code === -2) {
  136. this.$toast('未绑定手机号')
  137. this.showBind = true
  138. this.formData.openId = error.data.openId
  139. this.formData.accessToken = error.data.accessToken
  140. } else {
  141. this.$toast('登陆失败请重试')
  142. // 其他错误跳转首页
  143. setTimeout(() => {
  144. this.$router.push(this.redirectPath)
  145. }, 1000)
  146. }
  147. }
  148. },
  149. },
  150. }
  151. </script>
  152. <style scoped lang="scss">
  153. .auth-page {
  154. .form {
  155. .form-item {
  156. position: relative;
  157. border-bottom: 0.1vw solid #d8d8d8;
  158. input {
  159. display: block;
  160. width: 100%;
  161. font-size: 4.2vw;
  162. line-height: 10vw;
  163. }
  164. .send {
  165. position: absolute;
  166. right: 2.4vw;
  167. top: 50%;
  168. transform: translateY(-50%);
  169. font-size: 3.4vw;
  170. color: #bc1724;
  171. }
  172. }
  173. .submit {
  174. background: #bc1724;
  175. font-size: 3.2vw;
  176. color: #fff;
  177. text-align: center;
  178. line-height: 10vw;
  179. }
  180. }
  181. }
  182. </style>