01.cookie 设置
src/utils/auth.js
npm i cookies
- 代码 11 和 26 行,设置了失效 7 天;
- 代码 4 和 19 行,key 是可以自定义的;
import Cookies from 'js-cookie'
const TokenKey = 'mine_token';
export function getToken() {
return Cookies.get(TokenKey)
}
export function setToken(token) {
return Cookies.set(TokenKey, token, {expires: 7})
}
export function removeToken() {
return Cookies.remove(TokenKey)
}
const UserKey = 'mine_user';
export function getUser() {
return Cookies.get(UserKey)
}
export function setUser(user) {
return Cookies.set(UserKey, user, {expires: 7})
}
export function removeUser() {
return Cookies.remove(UserKey)
}
02.axios 拦截设置
前提要先设置全局 API,在文件 .env.development 和 .env.production
# base api
VUE_APP_BASE_API = 'http://192.168.182.13:8088'
src/utils/request.js
- 代码 9 行,
header 中的 token 不一定要 replace 替换。根据返回的token 字符串而定; - 代码 9 和 17 行是否重新设置,有冲突。江西矿山只设置了 20,也没影响;
- 代码 9 和 16 行,
getToken 方法获取 token 有可能为空 或 7 天到期失效。详见 src/utils/auth.js ;
import axios from 'axios'
import { Message } from 'element-ui'
import router from '@/router'
import { getToken } from '@/utils/auth'
const service = axios.create({
baseURL: process.env.VUE_APP_BASE_API,
timeout: 5000
})
service.interceptors.request.use(
config => {
if (getToken()) {
config.headers['X-Token'] = token;
}
return config
},
error => {
return Promise.reject(error)
}
)
service.interceptors.response.use(
response => {
const res = response.data
if(res.code == 401){
Message({
message: '身份验证过期,请登录',
type: 'warning',
duration: 5 * 1000
})
router.push('/')
}
return res
},
error => {
if (401 === error.response.status) {
Message({
message: '身份验证过期,请登录',
type: 'warning',
duration: 5 * 1000
});
router.push('/');
} else {
console.log('err' + error)
Message({
message: error.message,
type: 'error',
duration: 5 * 1000
})
}
return Promise.reject(error)
}
)
export default service
03.用户 API
src/api/user.js
- 注意如下 url 或请求方式
get 和post 以具体的接口而定 - 可能有 登录、获取用户信息、登出等;
import request from '@/utils/request'
export function getInfo(id) {
return request({
url: `/mapgis/gm/v1/user/${id}/info`,
method: 'get'
})
}
export function logout() {
return request({
url: '/vue-admin-template/user/logout',
method: 'get'
})
}
04.VueX user
src/store/modules/user.js
- 代码 34 - 45 行,不是使用 axios 拦截,因为登录不需要;
import { getInfo } from '@/api/user'
import { getToken, setToken, removeToken, getUser, setUser, removeUser } from '@/utils/auth'
const state = {
token: getToken(),
name: getUser(),
userId: '',
userRole: '',
userInfo: null
}
const mutations = {
SET_TOKEN: (state, token) => {
state.token = token;
},
SET_NAME: (state, name) => {
state.name = name;
},
SET_USERID: (state, userId) => {
state.userId = userId;
},
SET_USERROLE: (state, userRole) => {
state.userRole = userRole;
},
SET_USERINFO: (state, userInfo) => {
state.userInfo = userInfo;
}
}
const actions = {
login({ commit }, userInfo) {
const { form, axios } = userInfo;
return new Promise((resolve, reject) => {
let url = '/v1/auth/login';
axios.post(url, form).then((resp) => {
if (resp.data.succ) {
let obj = resp.data.obj;
setToken(obj.token);
setUser(form.user);
commit('SET_TOKEN', obj.token);
commit('SET_NAME', form.user);
resolve();
} else {
reject(resp)
}
});
})
},
getInfo({ commit, state }) {
return new Promise((resolve, reject) => {
getInfo(state.token).then(response => {
if(resp.succ) {
let info = resp.obj;
commit('SET_USERID', info.uid)
commit('SET_USERROLE', info.roleNames[0])
commit('SET_USERINFO', info)
resolve();
} else {
reject(resp)
}
}).catch(error => {
reject(error)
})
})
},
logout({ commit, state }) {
removeToken();
removeUser();
commit('SET_TOKEN', '');
commit('SET_NAME', '');
commit('SET_USERID', '');
}
}
export default {
namespaced: true,
state,
mutations,
actions
}
05.路由前置守卫
src/router/index.js
import { getToken } from '@/utils/auth'
router.beforeEach((to, from, next) => {
if (!getToken() && to.name !== 'login') {
next({ path: '/login' })
} else {
next()
}
})
06.登录/登出
登录
commit () {
if (this.inputName.trim() == '' || this.inputPw.trim() == '') {
this.$message({ type: 'error', message: '请输入用户名密码或验证码!' })
return
}
let form = {
"user": this.inputName,
"pwd": md5(this.inputPw),
"againPwd": md5(this.inputPw),
"needPhoneAuth": false,
"needEmailAuth": false
}
this.$store.dispatch('user/login', { form, axios: this.$axios }).then(data => {
this.$router.push('/');
}).catch((error) => {
console.error('登录失败', error);
this.$message({ type: 'error', message: '登录失败' })
})
}
登出
this.$store.dispatch('user/logout');
07.TopHeader
进入主页,显示用户名可设置计算属性 userName 。参考第三篇《03用户及切换列表》
computed: {
userName() {
return this.$store.state.user.name
}
}
|