123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <template>
- <div class="auth-page" v-if="showBind">
- <div class="form p-4">
- <div class="form-item mb-4">
- <input type="text" placeholder="手机号" v-model="formData.mobile" />
- </div>
- <div class="form-item mb-4 code">
- <input
- type="text"
- placeholder="验证码"
- class="code"
- v-model="formData.verifyCode"
- />
- <span class="send" @click="onSend">{{ sendCodeBtnText }}</span>
- </div>
- <div class="submit" @click="onSubmit">登录</div>
- </div>
- </div>
- </template>
- <script>
- import { isMobile } from '@/utils/validator'
- export default {
- data() {
- return {
- showBind: false,
- sendStatus: 0,
- formData: {
- authUserId: '',
- appId: '',
- openId: '',
- accessToken: '',
- mobile: '',
- verifyCode: '',
- },
- redirectPath: '',
- code: '',
- }
- },
- computed: {
- sendCodeBtnText() {
- return this.sendStatus === 0
- ? '发送验证码'
- : `再次发送${this.sendStatus}s`
- },
- },
- mounted() {
- this.initData()
- },
- beforeDestroy() {
- this.$removeStorage(this.redirectPath, 'login_redicret')
- },
- methods: {
- // 初始化数据
- initData() {
- // 获取链接参数
- const { code, type, authUserId, appId } = this.$route.query
- this.code = code
- this.formData.appId = appId
- this.formData.authUserId = parseInt(authUserId)
- this.redirectPath = `/${authUserId}/${type}`
- // 保存页面路由前缀
- this.$store.commit('app/SET_ROUTE_PREFIX', this.redirectPath)
- // 保存用户AppId
- this.$store.commit('user/SET_AUTH_USER_ID', this.formData.authUserId)
- // 尝试微信授权自动登录
- this.autoLogin()
- },
- // 绑定并登录
- async onSubmit() {
- try {
- const res = await this.$http.api.wechatLoginWithCode(this.formData)
- this.loginSuccess(res)
- } catch (error) {
- console.log(error)
- }
- },
- loginSuccess(res) {
- this.$toast('登录成功')
- this.$store.dispatch('user/login', res.data)
- this.$setStorage(this.redirectPath, 'userInfo', res.data)
- // 重定向
- const login_redicret = this.$getStorage(
- this.redirectPath,
- 'login_redicret'
- )
- if (login_redicret && login_redicret !== this.$route.path) {
- this.$router.push(login_redicret)
- } else {
- this.$router.push(this.redirectPath)
- }
- },
- // 发送验证码
- async onSend() {
- if (this.sendStatus > 0) return
- // 验证手机号是否合法
- if (!isMobile(this.formData.mobile)) {
- this.$toast('请输入正确的手机号')
- return
- }
- try {
- // 发送验证码
- const res = await this.$http.api.sendVerifyCode({
- mobile: this.formData.mobile,
- authUserId: this.formData.authUserId,
- type: 1,
- })
- this.$toast('验证码已发送')
- // 开启倒计时
- this.countdown()
- } catch (error) {
- console.log(error)
- }
- },
- // 短信倒计时
- countdown() {
- this.sendStatus = 30
- this.timer = setInterval(() => {
- if (this.sendStatus === 0) {
- clearInterval(this.timer)
- return
- }
- this.sendStatus--
- }, 1000)
- },
- // 微信授权登录
- async autoLogin() {
- try {
- const res = await this.$http.api.wechatLogin({
- code: this.code,
- appId: this.formData.appId,
- })
- this.loginSuccess(res)
- } catch (error) {
- // 未绑定微信号
- if (error.code === -2) {
- this.$toast('未绑定手机号')
- this.showBind = true
- this.formData.openId = error.data.openId
- this.formData.accessToken = error.data.accessToken
- } else {
- this.$toast('登陆失败请重试')
- // 其他错误跳转首页
- setTimeout(() => {
- this.$router.push(this.redirectPath)
- }, 1000)
- }
- }
- },
- },
- }
- </script>
- <style scoped lang="scss">
- .auth-page {
- .form {
- .form-item {
- position: relative;
- border-bottom: 0.1vw solid #d8d8d8;
- input {
- display: block;
- width: 100%;
- font-size: 4.2vw;
- line-height: 10vw;
- }
- .send {
- position: absolute;
- right: 2.4vw;
- top: 50%;
- transform: translateY(-50%);
- font-size: 3.4vw;
- color: #bc1724;
- }
- }
- .submit {
- background: #bc1724;
- font-size: 3.2vw;
- color: #fff;
- text-align: center;
- line-height: 10vw;
- }
- }
- }
- </style>
|