inleft
2022-02-09 9bcb19959eeb9da9bde2561e7278f6d0a55eb151
commit | author | age
9bcb19 1 <template>
I 2   <div class="main">
3     <a-form
4       id="formLogin"
5       class="user-layout-login"
6       ref="formLogin"
7       :form="form"
8       @submit="handleSubmit"
9     >
10       <a-tabs
11         :activeKey="customActiveKey"
12         :tabBarStyle="{ textAlign: 'center', borderBottom: 'unset' }"
13         @change="handleTabClick"
14       >
15         <a-tab-pane key="tab1" tab="账号密码登录">
16           <a-alert v-if="isLoginError" type="error" showIcon style="margin-bottom: 24px;" :message="this.accountLoginErrMsg" />
17
18           <a-form-item>
19             <a-input
20               size="large"
21               type="text"
22               placeholder="账号"
23               v-decorator="[
24                 'account',
25                 { initialValue:'superAdmin', rules: [{ required: true, message: '请输入帐户名' }, { validator: handleUsernameOrEmail }], validateTrigger: 'change'}
26               ]"
27             >
28               <a-icon slot="prefix" type="user" :style="{ color: 'rgba(0,0,0,.25)' }"/>
29             </a-input>
30           </a-form-item>
31
32           <a-form-item>
33             <a-input
34               size="large"
35               type="password"
36               autocomplete="false"
37               placeholder="密码"
38               v-decorator="[
39                 'password',
40                 { initialValue:'123456', rules: [{ required: true, message: '请输入密码' }], validateTrigger: 'blur'}
41               ]"
42             >
43               <a-icon slot="prefix" type="lock" :style="{ color: 'rgba(0,0,0,.25)' }"/>
44             </a-input>
45           </a-form-item>
46         </a-tab-pane>
47         <a-tab-pane key="tab2" tab="手机号登录">
48           <a-alert v-if="isLoginError" type="error" showIcon style="margin-bottom: 24px;" :message="this.accountLoginErrMsg" />
49           <a-form-item>
50             <a-input size="large" type="text" placeholder="手机号" v-decorator="['mobile', {rules: [{ required: true, pattern: /^1[34578]\d{9}$/, message: '请输入正确的手机号' }], validateTrigger: 'change'}]">
51               <a-icon slot="prefix" type="mobile" :style="{ color: 'rgba(0,0,0,.25)' }"/>
52             </a-input>
53           </a-form-item>
54
55           <a-row :gutter="16">
56             <a-col class="gutter-row" :span="16">
57               <a-form-item>
58                 <a-input size="large" type="text" placeholder="验证码" v-decorator="['captcha', {rules: [{ required: true, message: '请输入验证码' }], validateTrigger: 'blur'}]">
59                   <a-icon slot="prefix" type="mail" :style="{ color: 'rgba(0,0,0,.25)' }"/>
60                 </a-input>
61               </a-form-item>
62             </a-col>
63             <a-col class="gutter-row" :span="8">
64               <a-button
65                 class="getCaptcha"
66                 tabindex="-1"
67                 :disabled="state.smsSendBtn"
68                 @click.stop.prevent="getCaptcha"
69                 v-text="!state.smsSendBtn && '获取验证码' || (state.time+' s')"
70               ></a-button>
71             </a-col>
72           </a-row>
73         </a-tab-pane>
74       </a-tabs>
75
76       <a-form-item>
77         <a-checkbox v-decorator="['rememberMe', { valuePropName: 'checked' }]">自动登录</a-checkbox>
78         <router-link
79           :to="{ name: 'recover', params: { user: 'aaa'} }"
80           class="forge-password"
81           style="float: right;"
82         >忘记密码</router-link>
83       </a-form-item>
84
85       <a-form-item>
86         <Verify
87           @success="verifySuccess"
88           :mode="'pop'"
89           :captchaType="captchaType"
90           :imgSize="{ width: '330px', height: '155px' }"
91           ref="verify"
92         ></Verify>
93       </a-form-item>
94
95       <a-form-item style="margin-top:24px">
96         <a-button
97           size="large"
98           type="primary"
99           htmlType="submit"
100           class="login-button"
101           :loading="state.loginBtn"
102           :disabled="state.loginBtn"
103         >确定</a-button>
104       </a-form-item>
105
106       <div class="user-login-other">
107         <span>其他登录方式</span>
108         <a>
109           <a-icon class="item-icon" type="alipay-circle"></a-icon>
110         </a>
111         <a>
112           <a-icon class="item-icon" type="taobao-circle"></a-icon>
113         </a>
114         <a>
115           <a-icon class="item-icon" type="weibo-circle"></a-icon>
116         </a>
117         <router-link class="register" :to="{ name: 'register' }">注册账户</router-link>
118       </div>
119     </a-form>
120
121     <two-step-captcha
122       v-if="requiredTwoStepCaptcha"
123       :visible="stepCaptchaVisible"
124       @success="stepCaptchaSuccess"
125       @cancel="stepCaptchaCancel"
126     ></two-step-captcha>
127   </div>
128 </template>
129
130 <script>
131 import TwoStepCaptcha from '@/components/tools/TwoStepCaptcha'
132 import { mapActions } from 'vuex'
133 import { getSmsCaptcha, getCaptchaOpen } from '@/api/modular/system/loginManage'
134 import Verify from '@/components/verifition/Verify'
135
136 export default {
137   components: {
138     TwoStepCaptcha,
139     Verify
140   },
141   data () {
142     var captchaTypeValue = 'clickWord'
143     var min = 0
144     var max = 100
145     var random = Math.floor(Math.random() * (max - min)) + min
146
147     if (random % 2 === 0) {
148       captchaTypeValue = 'blockPuzzle'
149     }
150     if (random % 2 === 1) {
151       captchaTypeValue = 'clickWord'
152     }
153     return {
154       customActiveKey: 'tab1',
155       loginBtn: false,
156       // login type: 0 email, 1 username, 2 telephone
157       loginType: 0,
158       isLoginError: false,
159       requiredTwoStepCaptcha: false,
160       stepCaptchaVisible: false,
161       form: this.$form.createForm(this),
162       state: {
163         time: 60,
164         loginBtn: false,
165         // login type: 0 email, 1 username, 2 telephone
166         loginType: 0,
167         smsSendBtn: false
168       },
169       accountLoginErrMsg: '',
170       tenantOpen: false,
171       captchaOpen: false, // 是否开启验证码
172       tenantsList: [],
173       loginParams: [], // 登录参数
174       captchaType: captchaTypeValue
175     }
176   },
177   created () {
178     this.getCaptchaOpen()
179   },
180   methods: {
181     ...mapActions(['Login', 'Logout', 'dictTypeData']),
182     /**
183      * 获取验证码开关
184      */
185     getCaptchaOpen () {
186       getCaptchaOpen().then((res) => {
187         if (res.success) {
188           this.captchaOpen = res.data
189         }
190       })
191     },
192     // handler
193     handleUsernameOrEmail (rule, value, callback) {
194       const { state } = this
195       const regex = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/
196       if (regex.test(value)) {
197         state.loginType = 0
198       } else {
199         state.loginType = 1
200       }
201       callback()
202     },
203     handleTabClick (key) {
204       this.isLoginError = false
205       this.customActiveKey = key
206       // this.form.resetFields()
207     },
208     handleSubmit (e) {
209       e.preventDefault()
210       const {
211         form: { validateFields },
212         state,
213         customActiveKey,
214         Login
215       } = this
216
217       state.loginBtn = true
218       const validateFieldsKey = customActiveKey === 'tab1' ? ['account', 'password'] : ['mobile', 'captcha']
219       if (this.tenantOpen) {
220         validateFieldsKey.push('tenantCode')
221       }
222       validateFields(validateFieldsKey, { force: true }, (err, values) => {
223         this.loginParams = values
224         if (!err) {
225           // 是否开启验证码
226           if (this.captchaOpen) {
227             this.$refs.verify.show()
228             state.loginBtn = false
229             return
230           }
231           const loginParams = { ...values }
232           delete loginParams.account
233           loginParams.account = values.account
234
235           if (this.tenantOpen) {
236             loginParams.tenantCode = values.tenantCode
237           }
238
239           Login(loginParams)
240             .then((res) => this.loginSuccess(res))
241             .catch(err => this.requestFailed(err))
242             .finally(() => {
243               state.loginBtn = false
244             })
245         } else {
246           setTimeout(() => {
247             state.loginBtn = false
248           }, 600)
249         }
250       })
251     },
252     /**
253      * 获取验证码
254      */
255     verifySuccess(params) {
256       this.loginParams.code = params.captchaVerification
257       console.log(JSON.stringify(this.loginParams))
258       this.Login(this.loginParams).then((res) => this.loginSuccess(res))
259         .catch(err => this.requestFailed(err))
260         .finally(() => {
261           this.state.loginBtn = false
262         })
263     },
264     getCaptcha (e) {
265       e.preventDefault()
266       const { form: { validateFields }, state } = this
267
268       validateFields(['mobile'], { force: true }, (err, values) => {
269         if (!err) {
270           state.smsSendBtn = true
271
272           const interval = window.setInterval(() => {
273             if (state.time-- <= 0) {
274               state.time = 60
275               state.smsSendBtn = false
276               window.clearInterval(interval)
277             }
278           }, 1000)
279
280           const hide = this.$message.loading('验证码发送中..', 0)
281           getSmsCaptcha({ mobile: values.mobile }).then(res => {
282             setTimeout(hide, 2500)
283             this.$notification['success']({
284               message: '提示',
285               description: '验证码获取成功,您的验证码为:' + res.result.captcha,
286               duration: 8
287             })
288           }).catch(err => {
289             setTimeout(hide, 1)
290             clearInterval(interval)
291             state.time = 60
292             state.smsSendBtn = false
293             this.requestFailed(err)
294           })
295         }
296       })
297     },
298     stepCaptchaSuccess () {
299       this.loginSuccess()
300     },
301     stepCaptchaCancel () {
302       this.Logout().then(() => {
303         this.loginBtn = false
304         this.stepCaptchaVisible = false
305       })
306     },
307     loginSuccess (res) {
308       this.$router.push({ path: '/' })
309       this.isLoginError = false
310       // 加载字典所有字典到缓存中
311       this.dictTypeData().then((res) => { })
312     },
313     requestFailed (err) {
314       this.accountLoginErrMsg = err
315       this.isLoginError = true
316     }
317   }
318 }
319 </script>
320
321 <style lang="less" scoped>
322 .user-layout-login {
323   label {
324     font-size: 14px;
325   }
326
327   .getCaptcha {
328     display: block;
329     width: 100%;
330     height: 40px;
331   }
332
333   .forge-password {
334     font-size: 14px;
335   }
336
337   button.login-button {
338     padding: 0 15px;
339     font-size: 16px;
340     height: 40px;
341     width: 100%;
342   }
343
344   .user-login-other {
345     text-align: left;
346     margin-top: 24px;
347     line-height: 22px;
348
349     .item-icon {
350       font-size: 24px;
351       color: rgba(0, 0, 0, 0.2);
352       margin-left: 16px;
353       vertical-align: middle;
354       cursor: pointer;
355       transition: color 0.3s;
356
357       &:hover {
358         color: #1890ff;
359       }
360     }
361
362     .register {
363       float: right;
364     }
365   }
366 }
367 </style>