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
|
<template>
<div class="logins">
<div class="inside">
<div class="title">
<img src="../public/images/logo-title.svg" alt=""/>
</div>
<el-form class="login__box">
<el-form-item>
<el-input class="login--input login__input" v-model="username">
<template #prefix>
<i class="cn-icon cn-icon-user2"></i>
</template>
</el-input>
</el-form-item>
<el-form-item>
<el-input
class="login--input"
prefix-icon="Lock"
type="password"
@keyup.enter="login"
v-model="pin"
></el-input>
</el-form-item>
<el-form-item>
<!-- disabled 状态的改变,会影响loading图标的位置,所以将2个按钮分开处理 -->
<el-button v-if="licenseStatus === -1"
disabled
v-loading="loading"
type="primary"
class="login--input login--button"
style="font-size: 16px;"
>Checking License
</el-button>
<el-button v-else
:disabled="licenseStatus !== 0"
v-loading="loading"
type="primary"
class="login--input login--button"
:class="{'login-btn__license-error':licenseStatus !== 0}"
@click="login"
@keyup.enter="login"
style="font-size: 16px;"
>Login
</el-button>
</el-form-item>
<el-form-item v-if="licenseStatus !== -1 && licenseStatus !== 0">
<div class="license-error-msg">{{licenseStatusErrMsg}}</div>
<div class="license-file">
<button class="license__btn margin-r-20" @click.prevent="downloadFile" @keyup.enter="login">
<i class="cn-icon-download1 cn-icon margin-r-6"></i><span>Download c2v file</span>
</button>
<el-upload :action="`${baseUrl}sys/license/upload`"
ref="licenseUpload"
id="licenseUpload"
:multiple="false"
:show-file-list="false"
:accept="fileTypeLimit"
:file-list="fileList"
:auto-upload="false"
:on-change="fileChange"
:on-success="uploadSuccess"
:on-error="uploadError">
<button class="license__btn" @click.prevent="">
<i class="cn-icon-upload1 cn-icon margin-r-6"></i><span>Upload license</span>
</button>
</el-upload>
</div>
</el-form-item>
</el-form>
</div>
</div>
</template>
<script>
import { mapActions } from 'vuex'
import axios from 'axios'
import { useRouter } from 'vue-router'
import { storageKey, defaultLang } from '@/utils/constants'
import { api } from '@/utils/api'
import dayjs from 'dayjs'
import _ from 'lodash'
import utc from 'dayjs/plugin/utc'
import { ref } from 'vue'
dayjs.extend(utc)
export default {
name: 'Login',
data () {
return {
loading: true,
username: '',
pin: '',
language: '',
licenseStatus: -1, // -1 未获取后台数据状态;0 获取后台数据且验证成功;1 获取后台数据且验证失败;
licenseStatusErrMsg: '',
downloadC2vUrl: api.downloadLicenseC2v,
supportID: ''
}
},
methods: {
...mapActions(['loginSuccess']),
login () {
if (!this.username || !this.pin) {
return
}
if (!this.blockOperation.query) {
this.blockOperation.query = true
} else {
return
}
if (this.licenseStatus !== 0) {
return
}
this.loading = true
axios.post(api.login, { username: this.username, pin: this.pin }).then(
res => {
if (res.status === 200) {
if (!_.isEmpty(res.data.data.user.lang)) {
localStorage.setItem(storageKey.language, res.data.data.user.lang)
}
if (!localStorage.getItem(storageKey.language)) {
localStorage.setItem(storageKey.language, this.language)
}
if (!_.isEmpty(res.data.data.user.theme)) {
localStorage.setItem(storageKey.theme, res.data.data.user.theme)
}
res.loginSuccessPath = this.$route.query.redirect
this.loginSuccess(res)
localStorage.setItem(storageKey.username, this.username)
localStorage.setItem(storageKey.nickName, res.data.data.user.name)
// localStorage.setItem(storageKey.userId, res.data.data.user.userId)
localStorage.setItem(storageKey.userId, res.data.data.user.id)
localStorage.setItem(storageKey.token, res.data.data.token)
// const theme =
this.$i18n.locale = localStorage.getItem(storageKey.language)
}
}
).catch(e => {
console.error(e)
this.loading = false
this.blockOperation.query = false
if (_.get(e, 'response.data.code', 0) === 518005) {
this.$message.error(this.$t('tip.incorrectUsernameOrPassword'))
} else {
this.$message.error(this.errorMsgHandler(e))
}
})
},
downloadFile () {
axios.get(this.downloadC2vUrl, { responseType: 'blob' }).then(res => {
const disposition = res.headers['content-disposition']
const fileName = decodeURI(disposition.split('filename=')[1])
if (window.navigator.msSaveOrOpenBlob) {
// 兼容ie11
const blobObject = new Blob([res.data])
window.navigator.msSaveOrOpenBlob(blobObject, fileName)
} else {
const url = URL.createObjectURL(new Blob([res.data]))
const a = document.createElement('a')
document.body.appendChild(a) // 此处增加了将创建的添加到body当中
a.href = url
a.download = fileName
a.target = '_blank'
a.click()
a.remove() // 将a标签移除
}
}, error => {
const $self = this
const reader = new FileReader()
reader.onload = function () {
const responseText = reader.result
const exception = JSON.parse(responseText)
if (exception.message) {
$self.$message.error(exception.message)
} else {
console.error(error)
}
}
reader.readAsText(error.response.data)
})
},
fileChange (file, fileList) {
if (file.status !== 'ready') return
if (!_.endsWith(file.name, '.xml')) {
this.fileList = []
this.$message.error('Only support ' + this.fileTypeLimit + ' files')
} else {
this.fileList = fileList.slice(-1)
this.$refs.licenseUpload.submit()
}
},
uploadSuccess () {
this.$message.success('Success')
this.licenseStatus = 0
},
uploadError (error) {
let errorMsg
if (error.message) {
errorMsg = JSON.parse(error.message).message
} else {
errorMsg = 'error'
}
this.licenseStatus = 1
this.$message.error('Upload failed: ' + errorMsg)
},
checkLicenseStatus () {
axios.get(api.licenseStatus).then(res => {
if (res.status === 200) {
this.licenseStatus = res.data.data.status
} else {
this.licenseStatus = 1
}
this.loading = false
}).catch(e => {
this.licenseStatus = 1
this.loading = false
this.licenseStatusErrMsg = this.errorMsgHandler(e)
})
},
queryAppearance () {
axios.get(api.appearance).then(res => {
if (res.status === 200) {
this.appearanceOut(res.data.data)
}
})
},
appearanceOut (data) {
this.language = data.lang || defaultLang
if (_.isEmpty(localStorage.getItem(storageKey.language))) {
localStorage.setItem(storageKey.language, data.lang || defaultLang)
}
if (_.isEmpty(localStorage.getItem(storageKey.theme))) {
localStorage.setItem(storageKey.theme, data.theme || 'light')
}
localStorage.setItem(storageKey.s3Enable, data.s3_enable)
localStorage.setItem(storageKey.sysTimezone, data.timezone)
window.$dayJs.tz.setDefault(data.timezone)
localStorage.setItem(storageKey.timezoneOffset, window.$dayJs.tz().utcOffset() / 60)
localStorage.setItem(storageKey.timezoneLocalOffset, dayjs().utcOffset() / 60)
localStorage.setItem(storageKey.dateFormat, data.date_format)
localStorage.setItem(storageKey.sysName, data.system_name)
localStorage.setItem(storageKey.sysLogo, data.system_logo)
localStorage.setItem(storageKey.mapConfig, data.map_config)
}
},
async mounted () {
this.queryAppearance()
this.checkLicenseStatus()
},
setup () {
const { currentRoute } = useRouter()
return {
loginSuccessPath: currentRoute.value.query.redirect,
baseUrl: BASE_CONFIG.baseUrl,
fileTypeLimit: '.xml',
fileList: ref([])
}
}
}
</script>
<style>
.logins {
background-image: url("../public/images/bg.png");
}
</style>
|