Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import { _set,_get,_remove } from 'base/js/cookie'
// ๆฏๆฌก่ฏทๆฑๆบๅธฆLOGIN_TOKEN
axios.interceptors.request.use(
config => {
if (_get('_TOKEN_')) { // ๅคๆญๆฏๅฆๅญๅจtoken๏ผๅฆๆๅญๅจ็่ฏ๏ผๅๆฏไธชhttp header้ฝๅ ไธtoken
config.headers._TOKEN_ = _get('_TOKEN_');
}
return config;
},
err => {
return Promise.reject(err);
}
);
// ๆฏๆฌกๆฃๆฅTOKEN่ฟๆ
axios.interceptors.response.use(
response => {
if ( response.data.code == CONSTANT_CODE.TOKEN_MATURITY_LOGIN || response.data.code == CONSTANT_CODE.TOKEN_NOTUSER ) {
_remove('_TOKEN_')
store.commit(types.SET_NOW_USER_INFO,undefined)
router.replace({
path: '/login',
query: {redirect: router.currentRoute.fullPath}
})
} else if (response.data.code == CONSTANT_CODE.TOKEN_MATURITY) {
_remove('_TOKEN_')
store.commit(types.SET_NOW_USER_INFO,undefined)
}
return response;
},
err => {
return Promise.reject(err);
// text: 'ๅ ่ฝฝไธญ...',
// spinnerType: 'triple-bounce'
// });
return config;
}, function (error) {
// Do something with request error
Toast({
message: "ๆช็ฅ้่ฏฏ",
duration: 500
})
return Promise.reject(error);
});
// // ๅๅบๆฆๆชๅจ response
axios.interceptors.response.use(function (response) {
console.log(response)
// Indicator.close();
Toast({
message: response.data.msg,
duration: 500
});
if (response.data.code == '401') {
router.push({
name: 'login'
})
}
return response;
}, function (error) {
Toast({
message: "ๆช็ฅ้่ฏฏ",
duration: 500
// if (!checkLogin()) {
// window.location = '#login'
// return new Error('no login')
// }
// }
// config.withCredentials = true // ่ฎพ็ฝฎๆต่งๅจ่ชๅจๅญๅจๆๅกๅจcookie
let token = localStorage.getItem('token')
if (token) {
config.headers.Authorization = 'Bearer ' + token
}
return config
}, (error) => {
return Promise.reject(error)
})
// ๆทปๅ ๅๅบๆฆๆชๅจ
axios.interceptors.response.use(function (response) {
if (response.data.status !== 1) {
message.error(response.data.message);
}
return response.data
}, function (error) {
console.log(error)
// Toast.fail(error.message, 1);
return Promise.reject(error);
});
export default axios
init: function () {
let self = this;
// Add a request interceptor
axios.interceptors.request.use(function (config) {
// Do something before request is sent
self.show();
return config;
}, function (error) {
// Do something with request error
return Promise.reject(error);
}
);
// Add a response interceptor
axios.interceptors.response.use(function (response) {
// Do something with response data
self.hide();
return response;
}, function (error) {
// Do something with response error
self.hide();
// ๅบ็ก้
็ฝฎ
if (process.env.NODE_ENV === 'production') {
axios.defaults.baseURL = 'http://95.163.198.43:5000';
} else {
axios.defaults.baseURL = 'http://127.0.0.1:5000';
}
// axios.defaults.baseURL = 'http://127.0.0.1:5000'
// axios.defaults.timeout = 5000 // ่ถ
ๆถๆถ้ด๏ผๆฏซ็ง๏ผ
// axios.defaults.retry = 2 // ้่ฏๆฌกๆฐ
// axios.defaults.retryDelay = 100 // ้่ฏไน้ด็้ด้ๆถ้ด๏ผๆฏซ็ง๏ผ
// ่ฏทๆฑๆฆๆชๅจ
// Add a request interceptor
axios.interceptors.request.use(function (config) {
// Do something before request is sent
const token = window.localStorage.getItem('madblog-token')
if (token) {
config.headers.Authorization = `Bearer ${token}`
}
return config
}, function (error) {
// Do something with request error
return Promise.reject(error)
})
// ๅๅบๆฆๆชๅจ
// Add a response interceptor
axios.interceptors.response.use(function (response) {
// Do something with response data
return response
import Storage from "../utils/localStorage"
const storage = new Storage('NAV')
// axiosๆน้
// ่ฏทๆฑๅ
axios.interceptors.request.use(res => {
return res
}, (error) => {
return Promise.reject(error)
})
// ่ฏทๆฑๅ
axios.interceptors.response.use(response => {
if (response.token) {
storage.set('TOKEN', response.token);
}
if (response.status == 401) {
router.push('/login');
}
return response
}, error => {
router.push('/login');
// ็็ฅๅ
ถๅฎไปฃ็ ยทยทยทยทยทยท
return Promise.reject(error)
})
function request(url = '', methods = 'get', params = {}) {
import axios from 'axios'
import { BASE_URL } from '@/config/api'
import { message } from 'antd'
axios.defaults.baseURL = BASE_URL;
axios.interceptors.request.use((config) => {
// if (config.params) config.params.per_page_count = 4 // ๆต่ฏ
// Toast.loading('', 0)
// if (PRIVATE_URLS.includes(config.url)) {
// // ๅคๆญๅชไบurl้่ฆ้ช่ฏๆฏๅฆๅทฒ็ปๅฝ
// // ่ฆๆไน่ทณ่ฝฌ๏ผ่ฆๆไนๅๆถ่ฏทๆฑ๏ผ
// if (!checkLogin()) {
// window.location = '#login'
// return new Error('no login')
// }
// }
// config.withCredentials = true // ่ฎพ็ฝฎๆต่งๅจ่ชๅจๅญๅจๆๅกๅจcookie
let token = localStorage.getItem('token')
if (token) {
config.headers.Authorization = 'Bearer ' + token
}
return config
beforeCreate: function () {
var me = this;
//Load Language bundle (if not defined)
if (this.$store.state.lang === undefined) {
this.$store.commit('lang',"en");
}
loadLang(this.$store.state.lang);
//Set the header Item
if (this.$store.state.currentHeaderItem === undefined) {
//this.$store.commit('currentHeaderItem',TopNav.items[0]);
}
//Set Axios Defaults and global response Interceptor
axios.defaults.timeout = RestUtil.ajaxTimeout;
axios.interceptors.response.use(RestUtil.globalSuccessParser.bind(this), RestUtil.globalErrorParser.bind(this));
},
mounted(){
export default function setup() {
axios.interceptors.request.use(
function(config) {
const token = store.getters['account/accessToken']
if (token) {
config.headers.Authorization = `Bearer ${token}`
}
config.headers['Cache-Control'] = 'no-cache'
return config
},
function(err) {
return Promise.reject(err)
}
)
axios.interceptors.response.use(
function(response) {
return response
import axios from 'axios'
axios.defaults.baseURL = 'http://localhost:8080'
axios.interceptors.response.use(res => {
return res && res.data
}, err => {
return Promise.reject(err)
})
export default axios