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
<template>
  <a-form-model ref="form" :model="model" :rules="validatorRules">
    <a-form-model-item required prop="username">
      <div class="login-input">
        <img src="../../assets/icons/account.png" height="25" style="margin-right: 10px">
        <a-input v-model="model.username" size="large" placeholder="用户名" ref="inputRef" @focus="inputFocus"/>
      </div>
    </a-form-model-item>
    <a-form-model-item required prop="password">
      <div class="login-input">
        <img src="../../assets/icons/password.png" height="25" style="margin-right: 10px">
        <a-input v-model="model.password" size="large" type="password" autocomplete="false"
                 placeholder="密码"/>
      </div>
    </a-form-model-item>
 
    <a-row :gutter="0" style="height: 40px">
      <a-col :span="16">
        <a-form-model-item required prop="inputCode">
          <div class="login-input">
            <img src="../../assets/icons/inputCode.png" height="25" style="margin-right: 10px">
            <a-input v-model="model.inputCode" size="large" type="text" placeholder="请输入验证码"/>
          </div>
        </a-form-model-item>
      </a-col>
      <a-col :span="8" style="height: 55px;display: flex;justify-content: right;align-items: center;padding-left: 15px">
        <img v-if="requestCodeSuccess" style="height: 45px;width:100%;border-radius: 15px;" :src="randCodeImage"
             @click="handleChangeCheckCode"/>
        <img v-else style="margin-top: 2px;" src="../../assets/checkcode.png" @click="handleChangeCheckCode"/>
      </a-col>
    </a-row>
  </a-form-model>
</template>
 
<script>
  import { getAction } from '@/api/manage'
  import Vue from 'vue'
  import { mapActions } from 'vuex'
 
  export default {
    name: 'LoginAccount',
    data() {
      return {
        requestCodeSuccess: false,
        randCodeImage: '',
        currdatetime: '',
        loginType: 0,
        model: {
          username: 'admin',
          password: '123456',
          inputCode: ''
        },
        validatorRules: {
          username: [
            { required: true, message: '请输入用户名!' },
            { validator: this.handleUsernameOrEmail }
          ],
          password: [{
            required: true, message: '请输入密码!', validator: 'click'
          }],
          inputCode: [{
            required: true, message: '请输入验证码!'
          }]
        }
 
      }
    },
    created() {
      this.handleChangeCheckCode()
    },
    methods: {
      ...mapActions(['Login']),
      /**刷新验证码*/
      handleChangeCheckCode() {
        this.currdatetime = new Date().getTime()
        this.model.inputCode = ''
        getAction(`/sys/randomImage/${this.currdatetime}`).then(res => {
          if (res.success) {
            this.randCodeImage = res.result
            this.requestCodeSuccess = true
          } else {
            this.$message.error(res.message)
            this.requestCodeSuccess = false
          }
        }).catch(() => {
          this.requestCodeSuccess = false
        })
      },
      // 判断登录类型
      handleUsernameOrEmail(rule, value, callback) {
        const regex = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/
        if (regex.test(value)) {
          this.loginType = 0
        } else {
          this.loginType = 1
        }
        callback()
      },
      /**
       * 验证字段
       * @param arr
       * @param callback
       */
      validateFields(arr, callback) {
        let promiseArray = []
        for (let item of arr) {
          let p = new Promise((resolve, reject) => {
            this.$refs['form'].validateField(item, (err) => {
              if (!err) {
                resolve()
              } else {
                reject(err)
              }
            })
          })
          promiseArray.push(p)
        }
        Promise.all(promiseArray).then(() => {
          callback()
        }).catch(err => {
          callback(err)
        })
      },
      acceptUsername(username) {
        this.model['username'] = username
      },
      //账号密码登录
      handleLogin(rememberMe) {
        this.validateFields(['username', 'password', 'inputCode'], (err) => {
          if (!err) {
            let loginParams = {
              username: this.model.username,
              password: this.model.password,
              captcha: this.model.inputCode,
              checkKey: this.currdatetime,
              remember_me: rememberMe
            }
            console.log('登录参数', loginParams)
            this.Login(loginParams).then((res) => {
              this.$emit('success', res.result)
            }).catch((err) => {
              //update-begin-author: taoyan date:20220425 for: 登录页面,当输入验证码错误时,验证码图片要刷新一下,而不是保持旧的验证码图片不变 #41
              if (err && err.code === 412) {
                this.handleChangeCheckCode()
              }
              //update-end-author: taoyan date:20220425 for: 登录页面,当输入验证码错误时,验证码图片要刷新一下,而不是保持旧的验证码图片不变 #41
              this.$emit('fail', err)
            })
          } else {
            this.$emit('validateFail')
          }
        })
      },
 
      inputFocus() {
        console.log('触发focus', this.$refs.inputRef.$el.style)
        this.$refs.inputRef.$el.style = 'background-color:#f00'
      }
 
    }
 
  }
</script>
 
<style scoped>
 
 
</style>