You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
shop-pc/plugins/axios.js

43 lines
980 B

2 years ago
3 years ago
/*
* @Author: ch
* @Date: 2022-05-03 23:04:45
* @LastEditors: ch
2 years ago
* @LastEditTime: 2022-06-12 14:35:43
3 years ago
* @Description: file content
*/
let axios = null;
import { UUID_KEY } from "@/constants";
2 years ago
import { CreateUUID } from "@/plugins/utils";
2 years ago
export default function ({ app, $axios, store, req }) {
let uuid = store.state.uuid;
if (!uuid && req.headers.cookie) {
uuid = req.headers.cookie.split(';').find(i => i.includes(UUID_KEY));
uuid = uuid && uuid.replace(`${UUID_KEY}=`,'')
}
2 years ago
if (!uuid) {
2 years ago
uuid = CreateUUID(16, 2);
2 years ago
store.commit('setUUID', uuid);
}
2 years ago
$axios.onRequest( config =>{
2 years ago
config.headers.uid = uuid;
return config;
});
3 years ago
$axios.onResponse(response => {
const result = response.data;
if(response.status === 200){
if(result.code === 'SUCCESS'){
return result.data;
}
return Promise.reject(result);
}
return Promise.reject({message:'请求出错'});
3 years ago
});
$axios.defaults.timeout = 3000;
3 years ago
axios = $axios;
}
export { axios }