API
axios.js import axios from "axios"; let AUTH_TOKEN=(function(){ return localStorage.getItem("token"); })(); var instance = axios.create({ }); instance.defaults.headers.common["Authorization"] = AUTH_TOKEN; instance.interceptors.request.use(function(config){ let url = config.url; if(url.indexOf("login")>-1){ localStorage.setItem('token',""); config.headers.Authorization = ""; } if(url.indexOf("user")>-1 && url.indexOf("login")<0){ config.headers.Authorization =localStorage.getItem("token"); } return config; },function(err){ return Promise.reject(err); }); instance.interceptors.response.use(function(res){ if(res.headers.token){ localStorage.setItem('token',res.headers.token); } return res; },function(err){ return err; }); export default instance;
import axios from '@/api/axios' export function loginBywx(){ return axios.get('/api/v1/wechat/login') } export function login(params){ return axios.post("/api/v1/user/info/login",params); } export function updateUser(params){ return axios.put("/api/v1/user/info/update",params); }
建一个请求前API的判断,在需要请求带token的地方引入