This commit is contained in:
luyuan 2021-03-05 15:09:46 +08:00
parent 8019dd0949
commit 0d79c9245a
Signed by: theluyuan
GPG Key ID: A7972FD973317FF3
4 changed files with 55 additions and 9 deletions

View File

@ -1,10 +1,41 @@
<template> <template>
<div id="app"> <div id="app">
<router-view /> <router-view />
</div> </div>
</template> </template>
<style> <style>
</style> </style>
<script>
export default {
mounted() {
const token = localStorage.getItem("token");
console.log(token);
if (token) {
this.axios
.get("/getuserinfo", {
params: {
token: localStorage.getItem("token"),
},
})
.then((res) => {
console.log("jiekouqingqiuwan")
if (res.data.data.length == 0) {
localStorage.removeItem("token");
this.$router.push("/login");
} else {
const a = {
user: res.data.data[0].user,
token: res.data.data.num_key,
};
this.$store.commit("setuserinfo", a);
}
//
// remove token
//
});
}
},
};
</script>

View File

@ -5,7 +5,7 @@ import store from './store'
import "./element-ui.js" import "./element-ui.js"
import axios from "axios" import axios from "axios"
Vue.config.productionTip = false Vue.config.productionTip = false
axios.defaults.baseURL = 'http://127.0.0.1:3002'; axios.defaults.baseURL = 'http://127.0.0.1:3003';
Vue.prototype.axios = axios Vue.prototype.axios = axios
new Vue({ new Vue({
router, router,

View File

@ -1,13 +1,13 @@
import Vue from 'vue' import Vue from 'vue'
import VueRouter from 'vue-router' import VueRouter from 'vue-router'
import Home from '../views/Home.vue' import Home from '../views/Home.vue'
import store from "../store/index.js"
Vue.use(VueRouter) Vue.use(VueRouter)
const routes = [ const routes = [
{ {
path: '/', path: '/',
name: 'Home',
component: Home component: Home
}, },
{ {
@ -42,6 +42,9 @@ const routes = [
} }
] ]
const router = new VueRouter({ const router = new VueRouter({
mode: 'history', mode: 'history',
base: process.env.BASE_URL, base: process.env.BASE_URL,
@ -49,5 +52,17 @@ const router = new VueRouter({
}) })
router.beforeEach((to,from,next)=>{
console.log(store.state.userinfo,"vuex");
if(store.state.userinfo.token !="" || localStorage.getItem("token")){
next()
}else if(to.path == "/login"){
next()
}else{
next("/login")
}
})
export default router export default router

View File

@ -40,9 +40,9 @@ export default new Vuex.Store({
message: "登录成功", message: "登录成功",
type: "success", type: "success",
}); });
localStorage.setItem("token",res.data.data.token) localStorage.setItem("token",res.data.data.num_key)
context.commit("setuserinfo",res.data.data) context.commit("setuserinfo",res.data.data)
router.push("/list") router.push("/nav")
} }
}); });
} }