index.vue
9.88 KB
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<template>
<div class="login-container">
<div class="logoHead">
<div class="logo"></div>
</div>
<!-- 登录 -->
<el-form
ref="loginForm"
:model="loginForm"
:rules="loginRules"
class="login-form"
autocomplete="on"
label-position="left"
@keyup.enter.native="handleLogin()"
>
<div class="loginBox">
<div class="title-container">
<span class="loginLog">登录LOGIN</span>
</div>
<!-- 用户名 -->
<el-form-item prop="account">
<span class="svg-container">
<svg-icon name="user" />
</span>
<el-input
ref="account"
v-model="loginForm.account"
name="username"
type="text"
autocomplete="on"
placeholder="请输入账号"
/>
</el-form-item>
<!-- end -->
<!-- 密码 -->
<el-form-item prop="password">
<span class="svg-container">
<svg-icon name="password" />
</span>
<el-input
:key="passwordType"
ref="password"
v-model="loginForm.password"
:type="passwordType"
placeholder="请输入账号"
name="password"
autocomplete="on"
@keyup.enter.native="handleLogin"
/>
</el-form-item>
<!-- end -->
<!-- 验证码 -->
<div class="codeInfo">
<el-form-item prop="code">
<span class="svg-container">
<svg-icon name="code" />
</span>
<el-input
ref="code"
v-model="loginForm.code"
name="code"
type="text"
autocomplete="on"
placeholder="请输入验证码"
class="inputW"
/>
</el-form-item>
<img
:src="imageCode"
alt="codeImage"
class="code-image"
@click="getCode"
>
</div>
<!-- end -->
<!-- 登录 -->
<el-button
:loading="loading"
type="primary"
style="width: 100%; margin-top: 30px"
@click.native.prevent="handleLogin"
>
立即登录
</el-button>
<!-- end -->
</div>
</el-form>
</div>
</template>
<script lang="ts">
import { Component, Vue, Watch } from 'vue-property-decorator'
import { Route } from 'vue-router'
import { Dictionary } from 'vue-router/types/router'
import { Form as ElForm, Input } from 'element-ui'
import { UserModule } from '@/store/modules/user'
// 用户信息
import { setToken, setUser, setApply, getApply, removeApply } from '@/utils/cookies'
// 随机码
import { randomNum } from '@/utils'
// api接口
import { getAllList } from '@/pages/adhibition/api/index'
import { getCode, loginInfo } from '@/pages/base/api/api'
@Component({
name: 'Logins'
})
export default class extends Vue {
// 密码验证
private validatePassword = (rule: any, value: string, callback: Function) => {
if (value.length < 6) {
callback(new Error('密码不能少于6位'))
} else {
callback()
}
}
// 验证码验证
private validateVerificationCode = (
rule: any,
value: string,
callback: Function
) => {
if (value.length < 4) {
callback(new Error('验证码不能少于4位'))
} else {
callback()
}
}
// 验证
private loginRules = {
account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
password: [{ validator: this.validatePassword, trigger: 'blur' }],
code: [{ validator: this.validateVerificationCode, trigger: 'blur' }]
}
private imageCode = ''
private randomId = randomNum(24, 16)
private loginForm = {
account: '',
code: '',
key: '',
password: ''
}
private passwordType = 'password'
private loading = false
private showDialog = false
private redirect?: string
private isPostBack = true
private otherQuery: Dictionary<string> = {}
@Watch('$route', { immediate: true })
private onRouteChange(route: Route) {
const query = route.query as Dictionary<string>
if (query) {
this.redirect = query.redirect
this.otherQuery = this.getOtherQuery(query)
}
}
// 挂载结束
mounted() {
if (this.loginForm.account === '') {
(this.$refs.account as Input).focus()
} else if (this.loginForm.password === '') {
(this.$refs.password as Input).focus()
}
// if (this.isPostBack) {
this.getCode(this.randomId)
// }
}
// 获取验证码图片
private async getCode(val: string) {
this.loginForm.code = ''
const response = await getCode(this.randomId)
const res = response.data
const data =
'data:image/png;base64,' +
btoa(
new Uint8Array(res).reduce(
(data, byte) => data + String.fromCharCode(byte),
''
)
)
this.imageCode = data
}
// 显示隐藏密码
private showPwd() {
if (this.passwordType === 'password') {
this.passwordType = ''
} else {
this.passwordType = 'password'
}
this.$nextTick(() => {
(this.$refs.password as Input).focus()
})
}
// 登录
private handleLogin() {
(this.$refs.loginForm as ElForm).validate(async(valid: boolean) => {
this.loginForm.key = this.randomId
let params = {} as any
if (valid) {
this.loading = true
const res = await UserModule.Login(this.loginForm)
if (res.isSuccess) {
await UserModule.GetUserInfo()
setUser(res.data.user)
setToken(res.data.token.token)
const { data } = await getAllList({})
// 默认赋值应用
if (res.data.user.id >= 10) {
// 非管理员
setApply(JSON.stringify(data.data[0]))
} else {
// 管理员
setApply(JSON.stringify({ 'id': '1', 'createTime': null, 'createUser': null, 'updateTime': null, 'updateUser': null, 'name': '应用选择', 'abbreviation': '权限管家', 'status': null, 'describe': null }))
this.$store.commit('updatedTheme', '')
this.$store.commit('updatedThemeColor', '#409EFF')
}
this.$router.push({
path: this.redirect || '/',
query: this.otherQuery
}, () => {})
params.description = '登录成功!'
this.loading = false
} else {
this.$message.error(res.msg || '操作失败')
this.loading = false
this.getCode(this.randomId)
params.description = res.msg
}
params.name = this.loginForm.account
// 记录日志
const { data } = await loginInfo(params)
} else {
return false
}
})
}
private getOtherQuery(query: Dictionary<string>) {
return Object.keys(query).reduce((acc, cur) => {
if (cur !== 'redirect') {
acc[cur] = query[cur]
}
return acc
}, {} as Dictionary<string>)
}
}
</script>
<style lang="scss">
.logoHead {
margin: 58px 0 0 70px;
}
.logo {
width: 169px;
height: 41px;
background: url(../../assets/images/logoLogin.png) no-repeat;
background-size: contain;
}
.loginLog {
font-size: 16px;
font-weight: 700;
text-align: left;
color: #333333;
line-height: 22px;
}
.login-container {
background: linear-gradient(to bottom, #2e88ff 0%, #4495ff 100%);
background-size: cover;
.el-input {
display: inline-block;
// height: 47px;
// width: 100%;
border-bottom: 2px solid #eaedf0 !important;
input {
height: auto !important;
background: transparent !important;
border: 0px !important;
border-radius: 0px !important;
padding: 9px 5px 7px 26px !important;
color: #303133 !important;
line-height: 30px !important;
// caret-color: $loginCursorColor;
-webkit-appearance: none;
font-size: 14px !important;
&:-webkit-autofill {
box-shadow: 0 0 0px 1000px #fff inset !important;
-webkit-text-fill-color: #333 !important;
}
&:-webkit-input-placeholder {
color: #c0c4cc;
}
}
}
.el-form-item {
margin-bottom: 0 !important;
}
.el-form-item__error {
position: static;
padding: 6px 0 0;
}
}
.code-image {
vertical-align: top;
margin: 8px 0 0;
width: 100px;
cursor: pointer;
}
.login-container {
.el-form-item {
background: none !important;
}
}
.codeInfo {
.el-form-item {
width: 60% !important;
display: inline-block !important;
}
}
</style>
<style lang="scss" scoped>
.login-container {
height: 100%;
width: 100%;
overflow: hidden;
background-color: $loginBg;
.login-form {
position: absolute;
width: 980px;
height: 570px;
max-width: 100%;
top: calc(50% - 280px);
left: calc(50% - 490px);
margin: 0 auto;
// overflow: hidden;
}
.tips {
font-size: 14px;
color: #fff;
margin-bottom: 10px;
span {
&:first-of-type {
margin-right: 16px;
}
}
}
.svg-container {
color: $darkGray;
display: inline-block;
position: absolute;
top: 14px;
z-index: 1;
}
.loginBox {
height: 570px;
background: url(../../assets/images/loginBg.png) no-repeat;
padding: 140px 111px 0 615px;
}
.title-container {
position: relative;
text-align: right;
margin-bottom: 33px;
padding-right: 70px;
image {
display: inline;
}
.title {
font-size: 26px;
color: $lightGray;
margin: 0px auto 40px auto;
text-align: center;
font-weight: bold;
}
}
.show-pwd {
position: absolute;
right: 10px;
top: 7px;
font-size: 16px;
color: $darkGray;
cursor: pointer;
user-select: none;
}
}
.el-button--primary {
height: 36px;
background-color: #409EFF;
border-radius: 25px;
color: #fff;
border: 0 none;
&:hover,
&:focus {
background-color: #409EFF !important;
}
}
</style>