mirror of
https://github.com/iczer/vue-antd-admin
synced 2025-04-06 04:00:06 +08:00
feat: add auth support for axios request; 🌟
新增:axios 请求添加 auth 认证支持;
This commit is contained in:
parent
886a19ddfc
commit
82e5955c3b
@ -4,7 +4,6 @@ import router from './router'
|
|||||||
import './theme/index.less'
|
import './theme/index.less'
|
||||||
import Antd from 'ant-design-vue'
|
import Antd from 'ant-design-vue'
|
||||||
import Viser from 'viser-vue'
|
import Viser from 'viser-vue'
|
||||||
import axios from 'axios'
|
|
||||||
import '@/mock'
|
import '@/mock'
|
||||||
import store from './store'
|
import store from './store'
|
||||||
import PouchDB from 'pouchdb'
|
import PouchDB from 'pouchdb'
|
||||||
@ -12,7 +11,6 @@ import 'animate.css/source/animate.css'
|
|||||||
import VueI18n from 'vue-i18n'
|
import VueI18n from 'vue-i18n'
|
||||||
import Plugins from '@/plugins'
|
import Plugins from '@/plugins'
|
||||||
|
|
||||||
Vue.prototype.$axios = axios
|
|
||||||
Vue.config.productionTip = false
|
Vue.config.productionTip = false
|
||||||
Vue.use(Viser)
|
Vue.use(Viser)
|
||||||
Vue.use(Antd)
|
Vue.use(Antd)
|
||||||
|
@ -22,7 +22,7 @@ Mock.mock('/login', 'post', ({body}) => {
|
|||||||
result.message = Mock.mock('@TIMEFIX').CN + ',欢迎回来'
|
result.message = Mock.mock('@TIMEFIX').CN + ',欢迎回来'
|
||||||
result.data = {}
|
result.data = {}
|
||||||
result.data.user = user
|
result.data.user = user
|
||||||
result.data.token = 'Authorization'
|
result.data.token = 'Authorization:' + Math.random()
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
})
|
})
|
||||||
|
@ -76,7 +76,7 @@
|
|||||||
<script>
|
<script>
|
||||||
import CommonLayout from '@/layouts/CommonLayout'
|
import CommonLayout from '@/layouts/CommonLayout'
|
||||||
import {login} from '@/services'
|
import {login} from '@/services'
|
||||||
import Cookie from 'js-cookie'
|
import {setAuthorization} from '@/utils/request'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'Login',
|
name: 'Login',
|
||||||
@ -112,7 +112,7 @@ export default {
|
|||||||
const user = result.data.user
|
const user = result.data.user
|
||||||
this.$router.push('/dashboard/workplace')
|
this.$router.push('/dashboard/workplace')
|
||||||
this.$store.commit('account/setUser', user)
|
this.$store.commit('account/setUser', user)
|
||||||
Cookie.set('token', result.data.token)
|
setAuthorization({token: result.data.token})
|
||||||
this.$message.success(result.message, 3)
|
this.$message.success(result.message, 3)
|
||||||
} else {
|
} else {
|
||||||
this.error = result.message
|
this.error = result.message
|
||||||
|
@ -1,13 +1,23 @@
|
|||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import Cookie from 'js-cookie'
|
import Cookie from 'js-cookie'
|
||||||
|
|
||||||
|
// 跨域认证信息 header 名
|
||||||
|
const xsrfHeaderName = 'Authorization'
|
||||||
|
|
||||||
axios.defaults.timeout = 5000
|
axios.defaults.timeout = 5000
|
||||||
axios.defaults.withCredentials= true
|
axios.defaults.withCredentials= true
|
||||||
|
axios.defaults.xsrfHeaderName= xsrfHeaderName
|
||||||
|
axios.defaults.xsrfCookieName= xsrfHeaderName
|
||||||
|
|
||||||
// const cookies = Cookie.get()
|
// 认证类型
|
||||||
// Object.keys(cookies).forEach(key => {
|
const AUTH_TYPE = {
|
||||||
// axios.defaults.headers.common[key] = cookies[key]
|
BEARER: 'Bearer',
|
||||||
// })
|
BASIC: 'basic',
|
||||||
|
AUTH1: 'auth1',
|
||||||
|
AUTH2: 'auth2',
|
||||||
|
}
|
||||||
|
|
||||||
|
// http method
|
||||||
const METHOD = {
|
const METHOD = {
|
||||||
GET: 'get',
|
GET: 'get',
|
||||||
POST: 'post'
|
POST: 'post'
|
||||||
@ -21,20 +31,37 @@ const METHOD = {
|
|||||||
* @returns {Promise<AxiosResponse<T>>}
|
* @returns {Promise<AxiosResponse<T>>}
|
||||||
*/
|
*/
|
||||||
async function request(url, method, params) {
|
async function request(url, method, params) {
|
||||||
// header 加入 token
|
|
||||||
const token = Cookie.get('Authorization')
|
|
||||||
const config = token ? {headers: {Authorization: token}} : {}
|
|
||||||
switch (method) {
|
switch (method) {
|
||||||
case METHOD.GET:
|
case METHOD.GET:
|
||||||
return axios.get(url, {params, ...config})
|
return axios.get(url, {params})
|
||||||
case METHOD.POST:
|
case METHOD.POST:
|
||||||
return axios.post(url, params, config)
|
return axios.post(url, params)
|
||||||
default:
|
default:
|
||||||
return axios.get(url, {params, ...config})
|
return axios.get(url, {params})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置认证信息
|
||||||
|
* @param token {Object}
|
||||||
|
* @param authType {AUTH_TYPE} 认证类型,默认:{AUTH_TYPE.BEARER}
|
||||||
|
*/
|
||||||
|
function setAuthorization(auth, authType = AUTH_TYPE.BEARER) {
|
||||||
|
switch (authType) {
|
||||||
|
case AUTH_TYPE.BEARER:
|
||||||
|
Cookie.set(xsrfHeaderName, 'Bearer ' + auth.token)
|
||||||
|
break
|
||||||
|
case AUTH_TYPE.BASIC:
|
||||||
|
case AUTH_TYPE.AUTH1:
|
||||||
|
case AUTH_TYPE.AUTH2:
|
||||||
|
default:
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
METHOD,
|
METHOD,
|
||||||
request
|
AUTH_TYPE,
|
||||||
|
request,
|
||||||
|
setAuthorization
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user