Commit 081ee4bd by hank

api 优化

parent 665b06ad
import axios from 'axios'
import { Toast } from 'mint-ui'
import store from '@/modules/store'
// import { getToken } from '@/utils/auth'
// 创建axios实例
// const service = axios.create({
// // baseURL: process.env.BASE_API, // api的base_url
// timeout: 10000 // 请求超时时间
// })
// console.log(service)
// request拦截器
axios.interceptors.request.use(
config => {
// Do something before request is sent
// console.log(config.url)
if (store.state.token) {
config.headers['accessToken'] = store.state.token // 让每个请求携带token--['ticket']为自定义key 请根据实际情况自行修改
}
if (!config.headers['Content-Type']) {
if (config.url != '/upload') {
config.headers['Content-Type'] = 'application/json'
}
}
// config.headers['Content-Type'] = 'application/json'
// config.headers['platform'] = 'front'
return config
},
error => {
// Do something with request error
console.log(error) // for debug
Promise.reject(error)
}
)
// respone拦截器
axios.interceptors.response.use(
response => {
if (response.data.code === '0000' || response.data.code === '4010' || response.data.code === '1041') {
// 1041 公司存在报错
return response
} else {
response.status = 400
Toast({
message: response.data.message,
position: 'center',
duration: 3 * 1000
})
return Promise.reject(response)
throw new Error({
response
})
// Promise.reject(new Error({
// response
// }))
}
},
/**
* 下面的注释为通过response自定义code来标示请求状态,当code返回如下情况为权限有问题,登出并返回到登录页
* 如通过xmlhttprequest 状态码标识 逻辑可写在下面error中
*/
err => {
if (err && err.response) {
switch (err.response.status) {
case 400:
err.message = `${err.response.data.message}`
break
case 401:
err.message = `${err.response.data.message}`
break
case 403:
err.message = `${err.response.data.message}`
break
case 404:
err.message = `请求地址出错: ${err.response.config.url}`
break
case 405:
err.message = `方法错误: ${err.response.data.message}`
break
case 408:
err.message = '请求超时'
break
case 500:
err.message = `服务器出了点小问题,请稍后再试。`
break
case 501:
err.message = '服务未实现'
break
case 502:
err.message = '网关错误'
break
case 503:
err.message = '服务不可用'
break
case 504:
err.message = '网关超时'
break
case 505:
err.message = 'HTTP版本不受支持'
break
}
}
Toast({
message: err.message,
position: 'center',
duration: 3 * 1000
})
// Toast.fail(err.message)
console.log('❎❎', err.message)
return Promise.reject(err)
}
)
......@@ -14,7 +14,8 @@ export default new Vuex.Store({
activeInfo:'',
ruweiBoxScroll: 0,
ruweigonggao: null,
userInfo: localStorage.getItem('userInfo') || {}
userInfo: localStorage.getItem('userInfo') || {},
token: localStorage.getItem('token') || ''
},
mutations: {
changeLastBoxScrollY(state, num) {
......@@ -92,6 +93,7 @@ export default new Vuex.Store({
}
}).then(res => {
window.localStorage.setItem('token', res.data.data.accessToken)
state.token = res.data.data.accessToken
// location.href = res.data.data.url + '/video/2.html'
commit('getUserInfo')
})
......
......@@ -50,10 +50,10 @@ export default {
if(localStorage.getItem('token')) {
store.commit('getUserInfo')
} else {
store.dispatch('getAuth').then()
// store.dispatch('getAuth').then()
}
if(this.$route.query.code) {
store.dispatch('getToken', this.$route.query.code).then()
if(this.getQuery('code')) {
store.dispatch('getToken', this.getQuery('code')).then()
}
var tar_userinfo = localStorage.getItem('tar_userinfo');
console.log(tar)
......@@ -77,6 +77,22 @@ export default {
}
},
methods: {
getQuery(key) {
var url = location.search; //获取url中"?"符后的字串
var theRequest = new Object();
if (url.indexOf('?') != -1) {
var str = url.substr(1);
strs = str.split('&');
for (var i = 0; i < strs.length; i++) {
theRequest[strs[i].split('=')[0]] = decodeURI(strs[i].split('=')[1]);
}
}
if (key) {
return theRequest[key];
} else {
return theRequest;
}
},
hiddenBar() {
document.addEventListener("WeixinJSBridgeReady", function onBridgeReady() {
WeixinJSBridge.call("hideToolbar"); // 隐藏底部状态栏
......
......@@ -18,9 +18,9 @@ import VueLazyload from 'vue-lazyload';
axios.defaults.baseURL = api.server;
axios.defaults.timeout = 10000;
axios.defaults.headers.common['systemtype'] = api.extra;
// axios.defaults.headers.common['systemtype'] = api.extra;
import '@/modules/js/request'
import fastclick from 'fastclick'
fastclick.attach(document.body)
......
......@@ -50,6 +50,14 @@ export default {
// this.hiddenBar();
},
mounted() {
if(localStorage.getItem('token')) {
store.commit('getUserInfo')
} else {
store.dispatch('getAuth').then()
}
if(this.getQuery('code')) {
store.dispatch('getToken', this.getQuery('code')).then()
}
var tar_userinfo = localStorage.getItem('tar_userinfo');
if (tar_userinfo) {
var tar_openid = localStorage.getItem('tar-uuid-129831');
......@@ -71,6 +79,22 @@ export default {
}
},
methods: {
getQuery(key) {
var url = location.search; //获取url中"?"符后的字串
var theRequest = new Object();
if (url.indexOf('?') != -1) {
var str = url.substr(1);
strs = str.split('&');
for (var i = 0; i < strs.length; i++) {
theRequest[strs[i].split('=')[0]] = decodeURI(strs[i].split('=')[1]);
}
}
if (key) {
return theRequest[key];
} else {
return theRequest;
}
},
hiddenBar() {
document.addEventListener("WeixinJSBridgeReady", function onBridgeReady() {
WeixinJSBridge.call("hideToolbar"); // 隐藏底部状态栏
......
......@@ -13,7 +13,7 @@ import '@/modules/js/flexible.js';
import axios from 'axios';
import api from '@/modules/js/api.js';
axios.defaults.baseURL = api.server;
axios.defaults.headers.common['systemtype'] = api.extra;
// axios.defaults.headers.common['systemtype'] = api.extra;
import VConsole from 'vconsole';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment