vue自己封装axios (统一设置请求头等等)
import axios from "axios"; // 是否允许请求中携带cookie axios.defaults.withCredentials = true; // 请求头 X-GW-NONCE中需要的参数 function uuid() { function S4() { return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1); } return ( S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4() ); } // 将 aioxs 封装为 vue 的插件 var myaxios = {}; // 挂载一个方法: install myaxios.install = function (Vue) { // 设置统一的请求路径 axios.defaults.baseURL = "http://localhost:8888/api/private/v1"; // 在 axios 的拦截器中添加一段内容: axios.interceptors.request.use(config => { // 动态设置请求头 config.headers.common = { 'Content-Type': 'application/json; charset=utf-8', 'X-GW-NONCE': uuid(), 'X-GW-TIME': new Date().getTime(), 'X-GW-APP-ID': 1103, CHANNEL_ID: "CZ", Authorization: window.sessionStorage.getItem("cookie") } return config; }, error => { return Promise.reject(error) }); // 将 axios 挂载到 vue 构造器中 Vue.prototype.$http = axios; }; export default myaxios;
如果差一点,那就再努力一点