wwf
12 小时以前 9a6cd220224fd3a9a6c84b5bb37c6410a470969f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<template>
  <div class="login">
    <el-form ref="form" :model="form">
      <el-form-item :rules="[$rules.required('请输入绑定手机号') , $rules.phone()]" prop="mobile">
        <el-input v-model="form.mobile" placeholder="请输入绑定手机号" style="width: 100%" size="large" />
      </el-form-item>
      <el-form-item prop="code" :rules="[$rules.required('请输入验证码'), $rules.code()]">
        <el-input 
          v-model="form.code" 
          placeholder="请输入验证码" 
          style="width: 100%" size="large"
        >
          <template #append>
            <el-row style="width: 70px;justify-content: center;">
              <el-button 
                v-if="countdown == 180"
                style="color: var(--el-color-primary);"
                class="cursor-p" 
                :loading="sendCodeLoading"
                @click="sendCode()"
              >
                获取验证码
              </el-button>
              <el-text v-else>{{ countdown }}s</el-text>
            </el-row>
          </template>
        </el-input>
      </el-form-item>
    </el-form>
    <el-button @click="login()" :loading="loginLoading" type="primary" size="large" class="mt-2" style="width: 100%">绑定并登录</el-button>
  </div>
</template>
<script>
import { tokenUtils } from '@/utils/axios.js';
import { useLoginStore } from '@/stores/login.js'
import { storeToRefs } from 'pinia';
export default {
  setup() {
    const { lastRouteInfo } = storeToRefs(useLoginStore())
    return { lastRouteInfo }
  },
  data() {
    return {
      form: {
        mobile: '',
        code: '',
      },
      countdown: 180,
      sendCodeLoading: false,
      loginLoading: false,
      countdownInterval: null
    }
  },
  created() {
  },
  computed: {
    state() {
      return this.$route.query.state
    },
    openid() {
      return this.$route.query.openid
    },
    wxCode() {
      return this.$route.query.code
    }
  },
  mounted() {
    document.title = this.$route.name
  },
  methods: {
    startCountdownInterval() {
      this.clearCountdownInterval()
      this.countdown--
      this.countdownInterval = setInterval(() => {
        if (this.countdown > 0) {
          this.countdown--
        } else {
          this.countdown = 180
          this.clearCountdownInterval()
        }
      }, 1000)
    },
    clearCountdownInterval() {
      clearInterval(this.countdownInterval)
      this.countdownInterval = null
    },
    async sendCode() {
      const validate = await this.$refs.form.validateField('mobile')
      if (validate) {
        const data = {
          captchaVerification: '',
          mobile: this.form.mobile,
          scene: 21,
        }
        this.sendCodeLoading = true
        this.$axios.post('/system/auth/send-sms-code', data).then(res => {
          if (res.data.code == 0) {
            this.startCountdownInterval()
            this.$message.success('已发送验证码,请注意查收')
          } else {
            this.$message.error(res.data.msg || '获取验证码失败')
          }
        }).finally(() => {
          this.sendCodeLoading = false
        })
      }
    },
    login() {
      const data = {
        mobile: this.form.mobile,
        code: this.form.code,
        state: this.state,
        openid: this.openid,
        wxCode: this.wxCode
      }
      this.loginLoading = true
      this.$axios.post('/system/auth/staff/checkin/bind', data).then(async res => {
        if (res.data.code == 0) {
          const resData = res.data.data
          tokenUtils.setTokens(resData.accessToken, resData.refreshToken)
          this.$message.success('绑定成功')
          const path = localStorage.getItem('verify_url')
          if (path) {
            this.$router.replace(path)
          }
        } else {
          this.$message.error(res.data.msg || '登录失败')
        }
      }).finally(() => {
        this.loginLoading = false
      })
    },
  }
}
</script>
<style scoped>
.login {
  padding: 40px 20px 20px;
}
</style>