wwf
15 小时以前 a32100e31b93bc378b11ab93617a9c40b081ad70
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<template>
  <el-dialog
    v-model="loginDialogVisible"
    width="500"
    align-center
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <div class="px-4">
      <el-row justify="center">
        <el-text class="text-2xl font-bold">{{ title }}</el-text>
      </el-row>
      <el-form v-if="loginType == 'mobile'" ref="accountForm" :model="form" :rules="rules" class="pt-6 pb-3">
        <el-form-item prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item prop="code" class="mb-2">
          <el-input v-model="form.code" placeholder="请输入验证码">
            <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-item prop="agreement" style="height: 30px;">
          <el-checkbox v-model="form.agreement" label="同意xxx服务协议" size="large" />
        </el-form-item> -->
 
        <el-button 
          class="mt-3" 
          @click="submitLogin()" 
          :loading="loginLoading" 
          type="primary" size="large" 
          style="width: 100%;"
        >
          <el-text class="text-lg text-white">登录</el-text>
        </el-button>
      </el-form>
 
      <el-row v-else-if="loginType == 'qrCode'" justify="center" class="mt-7">
        <el-image style="width: 200px;" :src="$getImageUrl('/qrCode.png')"></el-image>
      </el-row>
 
      <el-form v-else-if="loginType == 'register'" ref="registerForm" :model="form" :rules="rules" class="pt-6 pb-3">
        <el-form-item prop="name" class="mb-2">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item prop="sex" style="height: 20px;">
          <el-radio-group v-model="form.sex">
            <el-radio :value="1">男</el-radio>
            <el-radio :value="0">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="idCardType">
          <el-select v-model="form.idCardType" placeholder="证件类型">
            <el-option
              v-for="item in idCardTypeItems"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="idCard" :rules="[$rules.required('请输入证件号码'), form.idCardType == 1 ? $rules.idCard('请输入正确的身份证号'): '' ]">
          <el-input v-model="form.idCard" placeholder="请输入证件号码" />
        </el-form-item>
        <el-form-item prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item prop="code" class="mb-2">
          <el-input v-model="form.code" placeholder="请输入验证码">
            <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-item prop="agreement" style="height: 30px;">
          <el-checkbox v-model="form.agreement" label="同意xxx服务协议" size="large" />
        </el-form-item> -->
 
        <el-button 
          class="mt-5"
          :loading="registerLoading"
          @click="submitRegister()" 
          type="primary" size="large" 
          style="width: 100%;"
        >
          <el-text class="text-lg text-white">注册</el-text>
        </el-button>
      </el-form>
      
      <el-row class="mt-7" justify="center">
        <el-button v-if="loginType!='register'" text type="primary" @click="changeLoginType('register')">注册账号</el-button>
        <el-divider v-if="loginType!='register'" direction="vertical" class="m-0 mt-1 mx-4" style="height: 24px !important" />
        <el-button v-if="loginType!='qrCode'" text type="primary" @click="changeLoginType('qrCode')">微信扫码登录</el-button>
        <el-divider v-if="loginType=='register'" direction="vertical" class="m-0 mt-1 mx-4" style="height: 24px !important" />
        <el-button v-if="loginType!='mobile'" text type="primary" @click="changeLoginType('mobile')">手机号登录</el-button>
      </el-row>
    </div>
  </el-dialog>
</template>
 
<script>
import { useLoginStore } from '@/stores/login.js'
import { useSessionStore } from '@/stores/session.js'
import { storeToRefs } from 'pinia';
import idCardTypeItems from '@/assets/json/idCardTypeItems.json'
import { tokenUtils } from '@/utils/axios.js';
export default {
  components: {},
  setup() {
    const { loginDialogVisible } = storeToRefs(useLoginStore())
    const { setUserInfo } = useSessionStore()
    return { loginDialogVisible, setUserInfo }
  },
  data() {
    return {
      loginType: 'mobile', // mobile、qrCode
      form: {
        name: '',
        sex: 1,
        idCardType: 1,
        idCard: '',
        mobile: '',
        code: '',
        agreement: false
      },
      idCardTypeItems: idCardTypeItems,
      rules: {
        name: [ this.$rules.required('请输入姓名') ],
        mobile: [ this.$rules.required('请输入手机号'), this.$rules.phone() ],
        code: [ this.$rules.required('请填写验证码'), this.$rules.code() ],
        agreement: [ this.$rules.checkbox('请阅读并勾选同意协议') ]
      },
      countdown: 180,
      countdownInterval: null,
      sendCodeLoading: false,
      loginLoading: false,
      registerLoading: false
    }
  },
  computed: {
    title() {
      let obj = {
        register: '注 册',
        mobile: '手 机 号 登 录',
        qrCode: '微 信 扫 码 登 录'
      }
      return obj[this.loginType]
    }
  },
  created() {
    
  },
  unmounted() {
    this.clearCountdownInterval()
  },
  watch: {
    loginDialogVisible: function(val) {
      if (!val) {
        this.clearCountdownInterval()
      }
    }
  },
  methods: {
    async sendCode() {
      try {
        let ref = null
        let scene = 0
        if (this.loginType == 'mobile') {
          ref = this.$refs.accountForm
          scene = 33
        } else if (this.loginType == 'register') {
          ref = this.$refs.registerForm
          scene = 32
        }
        const validate = await ref.validateField('mobile')
        if (validate) {
          const data = {
            captchaVerification: '',
            mobile: this.form.mobile,
            scene: scene,
          }
          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
          })
        }
      } catch (error) {
        console.log(error)
        this.$message.error('请输入手机号码')
      }
    },
    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 submitLogin() {
      try {
        await this.$refs.accountForm.validate()
        this.loginLoading = true
        const data = {
          mobile: this.form.mobile,
          code: this.form.code
        }
        this.$axios.post('/system/kw/examinee/login', data).then(res => {
          if (res.data.code == 0) {
            const resData = res.data.data || {}
              tokenUtils.setTokens(resData.accessToken, resData.refreshToken)
              this.getUserInfo()
              this.$message.success('登录成功')
              this.loginDialogVisible = false
          } else {
            this.$message.error(res.data.msg)
          }
        }).finally(() => {
          this.loginLoading = false
        })
      } catch (error) {
        console.log()
      }
    },
    getUserInfo() {
      this.$axios.get('/system/kw/examinee/profile').then(res => {
        if (res.data.code == 0) {
          const resData = res.data.data || {}
          this.setUserInfo(resData)
        } else {
          this.$message.error(res.data.msg)
        }
      })
    },
    async submitRegister() {
      try {
        const validate = await this.$refs.registerForm.validate()
        if (validate) {
          const data = { ...this.form }
          this.$axios.post('/system/kw/examinee/register', data).then(res => {
            if (res.data.code == 0) {
              const resData = res.data.data || {}
              tokenUtils.setTokens(resData.accessToken, resData.refreshToken)
              this.getUserInfo()
              this.$message.success('注册成功')
              this.loginDialogVisible = false
            } else {
              this.$message.error(res.data.msg)
            }
          })
        }
      } catch (error) {
        console.log(error)
        this.$message.error('请检查必填字段')
      }
    },
    changeLoginType(type) {
      this.loginType = type
    }
  }
}
</script>
 
<style scoped>
</style>