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

3 years ago
3 years ago
/*
* @Author: ch
* @Date: 2022-05-03 23:04:45
* @LastEditors: ch
3 years ago
* @LastEditTime: 2022-06-12 14:35:43
3 years ago
* @Description: file content
*/
let axios = null;
import { UUID_KEY } from "@/constants";
3 years ago
import { CreateUUID } from "@/plugins/utils";
3 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}=`,'')
}
3 years ago
if (!uuid) {
3 years ago
uuid = CreateUUID(16, 2);
3 years ago
store.commit('setUUID', uuid);
}
3 years ago
$axios.onRequest( config =>{
3 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 }