wwf
17 小时以前 4e6f18dfa08e2f2f4f02aaa1b8e8e51852b7a9a1
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" v-if="loginType == 'mobilePhone'">
    <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 { isWeixin } from '@/utils/UA.js'
export default {
  setup() {
    const { lastRouteInfo } = useLoginStore()
    return { lastRouteInfo }
  },
  data() {
    return {
      loginType: '', //mobile、weixin
      form: {
        mobile: '',
        code: '',
      },
      countdown: 180,
      sendCodeLoading: false,
      loginLoading: false,
      countdownInterval: null
    }
  },
  created() {
    tokenUtils.clearTokens()
    this.loginType = isWeixin ? 'weixin' : 'mobilePhone'
    this.loginType = 'mobile'
  },
  computed: {
    appId() {
      return this.$route.query.appId
    }
  },
  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,
      }
      this.loginLoading = true
      this.$axios.post('/system/auth/staff/checkin/sms-login', data).then(async res => {
        if (res.data.code == 0) {
          const resData = res.data.data
          tokenUtils.setTokens(resData.accessToken, resData.refreshToken)
          this.$message.success('登录成功')
          if (this.lastRouteInfo.name) {
            this.$router.replace(this.lastRouteInfo)
          }
        } else {
          this.$message.error(res.data.msg || '登录失败')
        }
      }).finally(() => {
        this.loginLoading = false
      })
    },
    verify() {
      this.$router.push('/h5/verify')
    },
    signup() {
      this.$router.push('/h5/signup')
    }
  }
}
</script>
<style scoped>
.login {
  padding: 40px 20px 20px;
}
</style>