@/utils/auth#getToken JavaScript Examples
The following examples show how to use
@/utils/auth#getToken.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: user.js From NCF with Apache License 2.0 | 6 votes |
state = {
token: getToken(),
name: '',
avatar: '',
introduction: '',
roles: [],
menuTree: [],
permissionCodes: []
}
Example #2
Source File: request.js From yshop-gin-vue with Apache License 2.0 | 6 votes |
// request拦截器
service.interceptors.request.use(
config => {
if (getToken()) {
config.headers['Authorization'] = "Bearer " + getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
}
config.headers['Content-Type'] = 'application/json'
return config
},
error => {
// Do something with request error
console.log(error) // for debug
Promise.reject(error)
}
)
Example #3
Source File: request.js From EduAdmin with MIT License | 6 votes |
// request interceptor
service.interceptors.request.use(
config => {
if (store.getters.token) {
config.headers['token'] = store.getters.token
} else {
config.headers['token'] = getToken()
}
if (config.headers['token'] == undefined) {
config.headers['token'] = ''
}
return config
},
error => {
// do something with request error
console.log(error) // for debug
return Promise.reject(error)
}
)
Example #4
Source File: request.js From iota-payment-module with MIT License | 6 votes |
// request interceptor
service.interceptors.request.use(
config => {
// do something before request is sent
if (store.getters.token) {
// let each request carry token
// ['X-Token'] is a custom headers key
// please modify it according to the actual situation
config.headers['authorization'] = getToken()
}
return config
},
error => {
// do something with request error
console.log(error) // for debug
return Promise.reject(error)
}
)
Example #5
Source File: request.js From report with Apache License 2.0 | 6 votes |
// request拦截器
service.interceptors.request.use(
config => {
config.headers['Authorization'] = (getToken() == null || getToken() == undefined) ? '' : getToken()
return config
},
error => {
// Do something with request error
Promise.reject(error)
}
)
Example #6
Source File: permission.js From report with Apache License 2.0 | 6 votes |
// 判断是否需要登录权限 以及是否登录
router.beforeEach((to, from, next) => {
NProgress.start()
let token = getToken();
let gaeaUser = getAccessUser();
if (token) {
if (to.path == '/login') {
next('/index')
NProgress.done()
}else{
next()
}
}else {
if (whiteList.includes(to.path)) {
next()
}else {
if ((token == null || token == '' || token ==undefined || gaeaUser == {}) && (to.meta != null && to.meta.requireAuth == true)) {
next(`/login?redirect=${to.path}`);
NProgress.done();
} else {
next();
}
}
}
})
Example #7
Source File: bigscreen.js From report with Apache License 2.0 | 6 votes |
// 导出大屏
export function exportDashboard(data) {
return new Promise((resolve) =>{
axios({
method:'get',
url: process.env.BASE_API + '/reportDashboard/export',
headers: { 'Authorization': getToken() },
params:data,
responseType:'blob'
}).then(res =>{
resolve(res.data);
}).catch(err =>{
resolve('error');
})
})
}
Example #8
Source File: bigscreen.js From report with Apache License 2.0 | 6 votes |
// 获取动态数据
export function getData(data) {
return request({
url: 'reportDashboard/getData',
method: 'post',
headers: { 'Share-Token': getShareToken(), 'Authorization': getToken() },
data,
})
}
Example #9
Source File: ImgHttp.js From EduAdmin with MIT License | 6 votes |
//请求验证拦截器
instanceImg.interceptors.request.use(config => {
instanceImg.defaults.headers.post['Content-Type'] = 'application/json';
// 全局配置默认路径
config.baseURL = '/api'
config.headers = {}
if (store.getters.token) {
config.headers['token'] = store.getters.token
} else {
config.headers['token'] = getToken()
}
return config;
});
Example #10
Source File: request.js From NCF with Apache License 2.0 | 6 votes |
// request interceptor
service.interceptors.request.use(
config => {
// do something before request is sent
if (store.getters.token) {
// let each request carry token
// ['X-Token'] is a custom headers key
// please modify it according to the actual situation
const _token = getToken()
config.headers['Authorization'] = `Bearer ${_token}`
}
return config
},
error => {
// do something with request error
console.log(error) // for debug
return Promise.reject(error)
}
)
Example #11
Source File: axiosReq.js From react-admin-template with MIT License | 6 votes |
// 请求拦截
service.interceptors.request.use(
(req) => {
// console.log('req', req)
// token配置
req.headers['AUTHORIZE_TOKEN'] = getToken()
/* 下载文件*/
if (req.isDownLoadFile) {
req.responseType = 'blob'
}
if (req.isUploadFile) {
req.headers['Content-Type'] = 'multipart/form-data'
}
if (req.bfLoading) {
// loadingE = ElLoading.service({
// lock: true,
// text: '数据载入中',
// spinner: 'el-icon-loading',
// background: 'rgba(0, 0, 0, 0.1)'
// })
}
/*
*params会拼接到url上
* */
if (req.isParams) {
req.params = req.data
req.data = {}
}
//save req for res to using
reqConfig = req
return req
},
(err) => {
Promise.reject(err)
}
)
Example #12
Source File: request.js From react-antd-admin-template with MIT License | 6 votes |
// 请求拦截器
service.interceptors.request.use(
(config) => {
// Do something before request is sent
if (store.getState().user.token) {
// 让每个请求携带token-- ['Authorization']为自定义key 请根据实际情况自行修改
config.headers.Authorization = getToken();
}
return config;
},
(error) => {
// Do something with request error
console.log(error); // for debug
Promise.reject(error);
}
);
Example #13
Source File: request.js From fund_management-eastmoney with Apache License 2.0 | 6 votes |
// request interceptor
service.interceptors.request.use(
config => {
// do something before request is sent
if (store.getters.token) {
// let each request carry token
// ['X-Token'] is a custom headers key
// please modify it according to the actual situation
config.headers['X-Token'] = getToken()
}
return config
},
error => {
// do something with request error
console.log(error) // for debug
return Promise.reject(error)
}
)
Example #14
Source File: user.js From ferry_web with GNU Lesser General Public License v3.0 | 6 votes |
state = {
token: getToken(),
name: '',
avatar: '',
introduction: '',
roles: [],
permissions: [],
permisaction: []
}
Example #15
Source File: request.js From ferry_web with GNU Lesser General Public License v3.0 | 6 votes |
// request interceptor
service.interceptors.request.use(
config => {
// do something before request is sent
if (store.getters.token) {
// let each request carry token
// ['X-Token'] is a custom headers key
// please modify it according to the actual situation
config.headers['Authorization'] = 'Bearer ' + getToken()
}
return config
},
error => {
// do something with request error
return Promise.reject(error)
}
)
Example #16
Source File: request.js From SystemManagement with MIT License | 6 votes |
// request interceptor
service.interceptors.request.use(
config => {
// do something before request is sent
var token = getToken()
if (token) {
config.headers['Authorization'] = `Bearer ${token}` // 让每个请求携带自定义token 请根据实际情况自行修改
}
return config
},
error => {
// do something with request error
console.log(error) // for debug
return Promise.reject(error)
}
)
Example #17
Source File: zipdownload.js From ferry_web with GNU Lesser General Public License v3.0 | 6 votes |
export function downLoadZip(str, filename) {
var url = baseUrl + str
axios({
method: 'get',
url: url,
responseType: 'blob',
headers: { 'Authorization': 'Bearer ' + getToken() }
}).then(res => {
resolveBlob(res, mimeMap.zip)
})
}
Example #18
Source File: manager.js From EduAdmin with MIT License | 5 votes |
state = { token: getToken(), manager: {} }
Example #19
Source File: index.js From yshop-gin-vue with Apache License 2.0 | 5 votes |
// no redirect whitelist
router.beforeEach((to, from, next) => {
if (to.meta.title) {
document.title = to.meta.title + ' - ' + Config.title
}
NProgress.start()
if (getToken()) {
// 已登录且要跳转的页面是登录页
if (to.path === '/login') {
next({ path: '/' })
NProgress.done()
} else {
if (store.getters.roles.length === 0) { // 判断当前用户是否已拉取完user_info信息
store.dispatch('GetInfo').then(res => { // 拉取user_info
// 动态路由,拉取菜单
loadMenus(next, to)
}).catch((err) => {
console.log(err)
store.dispatch('LogOut').then(() => {
location.reload() // 为了重新实例化vue-router对象 避免bug
})
})
// 登录时未拉取 菜单,在此处拉取
} else if (store.getters.loadMenus) {
// 修改成false,防止死循环
store.dispatch('updateLoadMenus').then(res => {})
loadMenus(next, to)
} else {
next()
}
}
} else {
/* has no token*/
if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单,直接进入
next()
} else {
next(`/login?redirect=${to.path}`) // 否则全部重定向到登录页
NProgress.done()
}
}
})
Example #20
Source File: permission.js From iota-payment-module with MIT License | 5 votes |
// no redirect whitelist
router.beforeEach(async (to, from, next) => {
// start progress bar
NProgress.start()
// set page title
document.title = getPageTitle(to.meta.title)
// determine whether the user has logged in
const hasToken = getToken()
if (hasToken) {
if (to.path === '/login') {
// if is logged in, redirect to the home page
next({ path: '/' })
NProgress.done()
} else {
const hasGetUserInfo = store.getters.name
if (hasGetUserInfo) {
next()
} else {
try {
// get user info
await store.dispatch('user/getInfo')
next()
} catch (error) {
// remove token and go to login page to re-login
await store.dispatch('user/resetToken')
Message.error(error || 'Has Error')
next(`/login?redirect=${to.path}`)
NProgress.done()
}
}
}
} else {
/* has no token*/
if (whiteList.indexOf(to.path) !== -1) {
// in the free login whitelist, go directly
next()
} else {
// other pages that do not have permission to access are redirected to the login page.
next(`/login?redirect=${to.path}`)
NProgress.done()
}
}
})
Example #21
Source File: user.js From iota-payment-module with MIT License | 5 votes |
state = {
token: getToken(),
name: '',
avatar: ''
}
Example #22
Source File: user.js From react-admin-template with MIT License | 5 votes |
userInfo = {
name: '',
role: '',
avatar: '',
token: getToken()
}
Example #23
Source File: permission.js From ferry_web with GNU Lesser General Public License v3.0 | 5 votes |
// no redirect whitelist
router.beforeEach(async(to, from, next) => {
// start progress bar
NProgress.start()
// set page title
document.title = getPageTitle(to.meta.title, store.state.settings.title)
// determine whether the user has logged in
const hasToken = getToken()
if (hasToken) {
if (to.path === '/login') {
// if is logged in, redirect to the home page
next({ path: '/' })
NProgress.done()
} else {
// determine whether the user has obtained his permission roles through getInfo
const hasRoles = store.getters.roles && store.getters.roles.length > 0
if (hasRoles) {
next()
} else {
try {
// get user info
// note: roles must be a object array! such as: ['admin'] or ,['developer','editor']
const { roles } = await store.dispatch('user/getInfo')
// generate accessible routes map based on roles
const accessRoutes = await store.dispatch('permission/generateRoutes', roles)
// dynamically add accessible routes
router.addRoutes(accessRoutes)
// hack method to ensure that addRoutes is complete
// set the replace: true, so the navigation will not leave a history record
next({ ...to, replace: true })
} catch (error) {
// remove token and go to login page to re-login
// await store.dispatch('user/resetToken')
Message.error(error || 'Has Error')
next(`/login?redirect=${to.path}`)
NProgress.done()
}
}
}
} else {
/* has no token*/
if (whiteList.indexOf(to.path) !== -1) {
// in the free login whitelist, go directly
next()
} else {
// other pages that do not have permission to access are redirected to the login page.
next(`/login?redirect=${to.path}`)
NProgress.done()
}
}
})
Example #24
Source File: permission.js From EduAdmin with MIT License | 5 votes |
// no redirect whitelist
router.beforeEach(async(to, from, next) => {
// start progress bar
NProgress.start()
// set page title
document.title = getPageTitle(to.meta.title)
// determine whether the user has logged in
const hasToken = getToken()
if (hasToken) {
if (to.path === '/login') {
// if is logged in, redirect to the home page
next({ path: '/' })
NProgress.done()
} else {
try {
// 如果还没有计算出路由,则计算
if (store.getters.permission_routes.length == 0) {
const accessRoutes = await store.dispatch('permission/generateRoutes')
router.addRoutes(accessRoutes)
next({ ...to, replace: true })
} else {
next()
}
} catch (error) {
// remove token and go to login page to re-login
// await store.dispatch('user/resetToken')
// Message.error(error || 'Has Error')
next(`/login?redirect=${to.path}`)
NProgress.done()
}
}
} else {
/* has no token*/
if (whiteList.indexOf(to.path) !== -1) {
// in the free login whitelist, go directly
next()
} else {
// other pages that do not have permission to access are redirected to the login page.
next(`/login?redirect=${to.path}`)
NProgress.done()
}
}
})
Example #25
Source File: upload.js From yshop-gin-vue with Apache License 2.0 | 5 votes |
export function upload(api, file) {
var data = new FormData()
data.append('file', file)
const config = {
headers: { 'Authorization': getToken() }
}
return axios.post(api, data, config)
}
Example #26
Source File: user.js From yshop-gin-vue with Apache License 2.0 | 5 votes |
user = {
state: {
token: getToken(),
user: {},
roles: [],
// 第一次加载菜单时用到
loadMenus: false
},
mutations: {
SET_TOKEN: (state, token) => {
state.token = token
},
SET_USER: (state, user) => {
state.user = user
},
SET_ROLES: (state, roles) => {
state.roles = roles
},
SET_LOAD_MENUS: (state, loadMenus) => {
state.loadMenus = loadMenus
}
},
actions: {
// 登录
Login({ commit }, userInfo) {
const rememberMe = userInfo.rememberMe
return new Promise((resolve, reject) => {
login(userInfo.username, userInfo.password, userInfo.code, userInfo.uuid).then(res => {
setToken(res.data.token, rememberMe)
commit('SET_TOKEN', res.data.token)
setUserInfo(res.data.user, commit)
// 第一次加载菜单时用到, 具体见 src 目录下的 permission.js
commit('SET_LOAD_MENUS', true)
resolve()
}).catch(error => {
reject(error)
})
})
},
// 获取用户信息
GetInfo({ commit }) {
return new Promise((resolve, reject) => {
getInfo().then(res => {
setUserInfo(res.data, commit)
resolve(res)
}).catch(error => {
reject(error)
})
})
},
// 登出
LogOut({ commit }) {
return new Promise((resolve, reject) => {
logout().then(res => {
logOut(commit)
resolve()
}).catch(error => {
logOut(commit)
reject(error)
})
})
},
updateLoadMenus({ commit }) {
return new Promise((resolve, reject) => {
commit('SET_LOAD_MENUS', false)
})
}
}
}
Example #27
Source File: permission.js From SystemManagement with MIT License | 5 votes |
// no redirect whitelist
router.beforeEach(async(to, from, next) => {
// start progress bar
NProgress.start()
// set page title
document.title = getPageTitle(to.meta.title)
// determine whether the user has logged in
const hasToken = getToken()
if (hasToken) {
if (to.path === '/login') {
// if is logged in, redirect to the home page
next({ path: '/' })
NProgress.done()
} else {
const hasRoles = store.getters.roles && store.getters.roles.length > 0
if (hasRoles) {
next()
} else {
try {
await store.dispatch('user/getInfo')
const accessRoutes = await store.dispatch('menu/getSideMenus');
router.addRoutes(accessRoutes)
next({ ...to, replace: true })
} catch (error) {
console.log(error)
// remove token and go to login page to re-login
await store.dispatch('user/resetToken')
Message.error(error || 'Has Error')
next(`/login?redirect=${to.path}`)
NProgress.done()
}
}
}
} else {
/* has no token*/
if (whiteList.indexOf(to.path) !== -1) {
// in the free login whitelist, go directly
next()
} else {
// other pages that do not have permission to access are redirected to the login page.
next(`/login?redirect=${to.path}`)
NProgress.done()
}
}
})
Example #28
Source File: user.js From vue3-element-admin with MIT License | 5 votes |
state = {
token: getToken(),
roles: getRoles(),
name: getName(),
avatar: getAvatar(),
introduction: ''
}
Example #29
Source File: bigscreen.js From report with Apache License 2.0 | 5 votes |
// 预览、查询大屏详情
export function detailDashboard(data) {
return request({
url: 'reportDashboard/' + data,
headers: { 'Share-Token': getShareToken(), 'Authorization': getToken() },
method: 'get',
})
}