Axios request data interface package Method

lib folder contents in the file http.js

 

It contains the requested data, intercept routing, while exposed to the outside is a method, the method has three parameters, namely the mode request, address, data,

 1 import axios from 'axios';
 2 import qs from 'qs';
 3 
 4 const server=axios.create({
 5     timeout:5000,
 6     withCredentials:true
 7 })
 8 
 9 server.interceptors.request.use((config)=>{
10     if(config.method.toUpperCase()=="GET"){
11         config.params={...config.data}
12     }else if(config.method.toUpperCase()=="POST"){
13         config.headers["content-type"] = "appliaction/x-www-form-urlencoded";
14         config.data=qs.stringify(config.data);
15     }
16     vm.handlemount();
17     return config;
18 },(err)=>{
19     Promise.reject(err);
20 })
21 
22 
23 server.interceptors.response.use((res)=>{
24     if(res.statusText=="OK"){
25         vm.handleDestory();
26         return res.data
27     }
28 },(err)=>{
29     Promise.reject(err);
30 })
31 
32 
33 export default (method,url,data={})=>{
34     if(method.toUpperCase()=="GET"){
35         return server.get(url,{
36             params:data
37         })
38     }else if(method.toUpperCase()=="POST"){
39         return server.post(url,data);
40     }
41 }

Data Request

1 http("get", "/client/v1/goods/newactivityTemplate?aTId=130").then(data => {
2 
3     })    

The above method can be encapsulated

API documentation for the file contents movie.js

import http from "utils/http.js"


export const city_api = ()=>http("get","/api/cityList")

export const movie_coming_api = (cityId=10)=>http("get","/api/movieComingList",{cityId:cityId})

When the code data for the request interface

import {city_api} from "api/movie"
 
async created() { if(!sessionStorage.getItem("comingList")){ let data = await movie_coming_api(this.cityId); this.comingList = data.data.comingList; sessionStorage.setItem("comingList",JSON.stringify(data.data.comingList)) } }, async handleGetCityAction({commit}){ let data = await city_api(); commit("handleGetCityMutation",data.data.cities) }

 

Guess you like

Origin www.cnblogs.com/muzishijie/p/11348964.html