网页端 - requst.js
import axios from 'axios'
const _url = process.env.VUE_APP_BASE_API
const service = axios.create({
baseURL: _url,
timeout: 15000
});
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8'
service.interceptors.request.use(config => {
config.headers['Authorization'] = localStorage.getItem("token") || '';
if (config.requestType == 'other') {
config.baseURL = process.env.OUTSIDE_API;
}
return config;
});
service.interceptors.response.use(res => {
if (res.status == 200) {
return Promise.resolve(res.data);
} else {
return Promise.reject(res);
}
}, error => {
return Promise.reject(error)
})
let http = {
get: function(url, params = null, config) {
return service.get(url, {
params,
...config
})
},
post: function(url, params = null, config) {
return service.post(url, params, config)
},
delete: function(url, params = null, config) {
return service.delete(url, {
params,
...config
})
}
}
export { http, axios}
正常接口封装-rules.js
import {http} from '@/utils/request';
const BASE_PATH = = "/project"
export function saveRule(data) {
return http.post(`${BASE_PATH}/admin/rule/saveRule`, data);
}
export function getRuleCount(data) {
return http.get(`${BASE_PATH}/admin/rule/countState`, data);
}
export function saveCategory(data, config) {
return http.post(`${BASE_PATH}/category/saveCategory`, data, config);
}
export function getPersonDetail(data, config) {
return http.post(`${BASE_PATH}/service/person/detail`, data, config);
}
使用–index.vue
import { saveRule } from '@/services/rules.js';
var params = {}
saveRule(params).then(res => {
if (res.code == 200) {
}
}).catch(err => {
});
使用–多域名–index.vue
import { saveRule, saveCategory } from '@/services/rules.js';
var params = {}
saveRule(params).then(res => {
if (res.code == 200) {
}
}).catch(err => {
});
var params2 = {}
var config = {
baseURL: 1 < 2 ? "https://www.baidu.com" : "https://cloud.tencent.com/",
requestType: "other"
}
saveCategory(params2, config).then(res => {
if (res.code == 200) {
}
}).catch(err => {
});
使用–终止请求(cancelToken)–index.vue
import { getPersonDetail } from '@/services/rules.js';
import axios from 'axios';
export default {
methods:{
mounted() {
setInterval(() => {
this.loadData();
}, 10);
},
cancelWordtuneFun() {
if (typeof this.source === "function") {
this.source("singoo_facebook_content_replace");
}
},
loadData(){
this.cancelWordtuneFun();
var params2 = {}
var config = {
cancelToken: new axios.CancelToken((c) => {
this.source = c;
})
}
saveCategory(params2, config).then(res => {
if (res.code == 200) {
}
}).catch(err => {
});
}
}
}
uniapp - requst.js - 用法一致
import axios from 'axios'
const _url = "http://www.xxx.com"
const service = axios.create({
baseURL: _url,
timeout: 15000
});
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8'
service.interceptors.request.use(config => {
config.headers['Authorization'] = localStorage.getItem("token") || '';
if (config.requestType == 'other') {
config.baseURL = "http://www.xxx.com";
}
return config;
});
service.interceptors.response.use(res => {
if (res.status == 200) {
return Promise.resolve(res.data);
} else {
return Promise.reject(res);
}
}, error => {
return Promise.reject(error)
})
axios.defaults.adapter = function(config) {
return new Promise((resolve, reject) => {
var settle = require('axios/lib/core/settle');
var buildURL = require('axios/lib/helpers/buildURL');
uni.request({
method: config.method.toUpperCase(),
url: config.baseURL + buildURL(config.url, config.params, config.paramsSerializer),
header: config.headers,
data: config.data,
dataType: config.dataType,
responseType: config.responseType,
sslVerify: config.sslVerify,
complete: function complete(response) {
response = {
data: response.data,
status: response.statusCode,
errMsg: response.errMsg,
header: response.header,
config: config
};
settle(resolve, reject, response);
}
})
})
}
let http = {
get: function(url, params = null, config) {
return service.get(url, {
params,
...config
})
},
post: function(url, params = null, config) {
return service.post(url, params, config)
},
delete: function(url, params = null, config) {
return service.delete(url, {
params,
...config
})
}
}
export { http, axios}
|