mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-29 05:09:46 +01:00
删除注释,优化保存token机制
This commit is contained in:
parent
d1090f5053
commit
4f8810d919
3 changed files with 35 additions and 32 deletions
|
|
@ -15,7 +15,8 @@
|
|||
"vue-axios": "^3.5.2",
|
||||
"vue-router": "^3.6.5",
|
||||
"vuex": "^3.6.2",
|
||||
"xiaozhi": "file:"
|
||||
"xiaozhi": "file:",
|
||||
"js-cookie": "3.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@vue/cli-plugin-router": "~5.0.0",
|
||||
|
|
|
|||
15
main/manager-web/src/utils/auth.js
Normal file
15
main/manager-web/src/utils/auth.js
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import Cookies from 'js-cookie'
|
||||
|
||||
const TokenKey = 'Admin-Token'
|
||||
|
||||
export function getToken() {
|
||||
return Cookies.get(TokenKey)
|
||||
}
|
||||
|
||||
export function setToken(token) {
|
||||
return Cookies.set(TokenKey, token)
|
||||
}
|
||||
|
||||
export function removeToken() {
|
||||
return Cookies.remove(TokenKey)
|
||||
}
|
||||
|
|
@ -65,7 +65,7 @@
|
|||
<script>
|
||||
import {getUUID, goToPage, showDanger, showSuccess} from '@/utils'
|
||||
import Api from '@/apis/api';
|
||||
|
||||
import { getToken, setToken, removeToken } from '@/utils/auth'
|
||||
|
||||
export default {
|
||||
name: 'login',
|
||||
|
|
@ -83,37 +83,26 @@ export default {
|
|||
}
|
||||
},
|
||||
mounted() {
|
||||
this.captchaUuid = getUUID();
|
||||
this.fetchCaptcha();
|
||||
},
|
||||
methods: {
|
||||
fetchCaptcha() {
|
||||
Api.user.getCaptcha(this.captchaUuid, (res) => {
|
||||
if (res.status === 200) {
|
||||
const blob = new Blob([res.data], {type: res.data.type});
|
||||
this.captchaUrl = URL.createObjectURL(blob);
|
||||
//如果有token,直接跳转到首页
|
||||
if (getToken()) {
|
||||
goToPage('/home')
|
||||
} else {
|
||||
this.captchaUuid = getUUID();
|
||||
Api.user.getCaptcha(this.captchaUuid, (res) => {
|
||||
if (res.status === 200) {
|
||||
const blob = new Blob([res.data], {type: res.data.type});
|
||||
this.captchaUrl = URL.createObjectURL(blob);
|
||||
|
||||
} else {
|
||||
console.error('验证码加载异常:', error);
|
||||
showDanger('验证码加载失败,点击刷新')
|
||||
}
|
||||
});
|
||||
// if (this.$store.getters.getToken) {
|
||||
// // goToPage('/home')
|
||||
// } else {
|
||||
// this.captchaUuid = getUUID();
|
||||
|
||||
// Api.user.getCaptcha(this.captchaUuid, (res) => {
|
||||
// if (res.status === 200) {
|
||||
// const blob = new Blob([res.data], {type: res.data.type});
|
||||
// this.captchaUrl = URL.createObjectURL(blob);
|
||||
|
||||
// } else {
|
||||
// console.error('验证码加载异常:', error);
|
||||
// showDanger('验证码加载失败,点击刷新')
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
} else {
|
||||
console.error('验证码加载异常:', error);
|
||||
showDanger('验证码加载失败,点击刷新')
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
// 封装输入验证逻辑
|
||||
|
|
@ -141,11 +130,9 @@ export default {
|
|||
|
||||
this.form.captchaId = this.captchaUuid
|
||||
Api.user.login(this.form, ({data}) => {
|
||||
console.log(data)
|
||||
showSuccess('登陆成功!')
|
||||
// 将令牌存储到 Vuex 中
|
||||
this.$store.commit('setToken', JSON.stringify(data.data))
|
||||
|
||||
// 保存token
|
||||
setToken(data.data.token)
|
||||
goToPage('/home')
|
||||
})
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue