删除注释,优化保存token机制

This commit is contained in:
flashily 2025-03-27 10:18:36 +08:00
parent d1090f5053
commit 4f8810d919
3 changed files with 35 additions and 32 deletions

View file

@ -15,7 +15,8 @@
"vue-axios": "^3.5.2", "vue-axios": "^3.5.2",
"vue-router": "^3.6.5", "vue-router": "^3.6.5",
"vuex": "^3.6.2", "vuex": "^3.6.2",
"xiaozhi": "file:" "xiaozhi": "file:",
"js-cookie": "3.0.1"
}, },
"devDependencies": { "devDependencies": {
"@vue/cli-plugin-router": "~5.0.0", "@vue/cli-plugin-router": "~5.0.0",

View 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)
}

View file

@ -65,7 +65,7 @@
<script> <script>
import {getUUID, goToPage, showDanger, showSuccess} from '@/utils' import {getUUID, goToPage, showDanger, showSuccess} from '@/utils'
import Api from '@/apis/api'; import Api from '@/apis/api';
import { getToken, setToken, removeToken } from '@/utils/auth'
export default { export default {
name: 'login', name: 'login',
@ -83,11 +83,15 @@ export default {
} }
}, },
mounted() { mounted() {
this.captchaUuid = getUUID();
this.fetchCaptcha(); this.fetchCaptcha();
}, },
methods: { methods: {
fetchCaptcha() { fetchCaptcha() {
//如果有token,直接跳转到首页
if (getToken()) {
goToPage('/home')
} else {
this.captchaUuid = getUUID();
Api.user.getCaptcha(this.captchaUuid, (res) => { Api.user.getCaptcha(this.captchaUuid, (res) => {
if (res.status === 200) { if (res.status === 200) {
const blob = new Blob([res.data], {type: res.data.type}); const blob = new Blob([res.data], {type: res.data.type});
@ -98,22 +102,7 @@ export default {
showDanger('验证码加载失败,点击刷新') 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('验证码加载失败,点击刷新')
// }
// });
// }
}, },
// 封装输入验证逻辑 // 封装输入验证逻辑
@ -141,11 +130,9 @@ export default {
this.form.captchaId = this.captchaUuid this.form.captchaId = this.captchaUuid
Api.user.login(this.form, ({data}) => { Api.user.login(this.form, ({data}) => {
console.log(data)
showSuccess('登陆成功!') showSuccess('登陆成功!')
// 将令牌存储到 Vuex 中 // 保存token
this.$store.commit('setToken', JSON.stringify(data.data)) setToken(data.data.token)
goToPage('/home') goToPage('/home')
}) })