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.

113 lines
3.0 KiB

3 years ago
import http from "./http";
import { baseUrl } from "../config/env";
3 years ago
// import qs from "qs";
// import {cache} from '@/store/store';
// 通用公用方法
const _get = (url, params, success, error) => {
return request("GET", url, params, success, error);
};
const _post = (url, params, success, error) => {
return request("POST", url, params, success, error);
};
const _post_paging = (url, params, success, error) => {
return request(
"POST",
url + "?pageNum=" + params.pageNum + "&pageSize=" + params.pageSize,
params,
success,
error
);
};
const _put = (url, params, success, error) => {
return request("PUT", url, params, success, error);
};
const _delete = (url, params, success, error) => {
return request("DELETE", url, params, success, error);
};
const _form = (url, params, success, error) => {
return request("FORM", url, params, success, error);
};
const isHttpLink = (url) =>{
return url.indexOf("http://") === 0 || url.indexOf("https://") === 0;
}
3 years ago
function request(type, url, params, success, error) {
var targetUrl = "";
if(isHttpLink(url)){
targetUrl = url
}else{
targetUrl = baseUrl + url
}
3 years ago
let config = {
method: type,
url: targetUrl,
data: params,
3 years ago
};
if (type == "GET") {
config.params = params;
} else {
config.data = params;
}
if (type == "FORM") {
config.method = "POST";
config.headers = { "Content-Type": "application/x-www-form-urlencoded" };
// config.data = qs.stringify(params);
}
3 years ago
return new Promise((resolve, reject) => {
http
.request(config)
.then(res => {
resolve(res.data);
if (res && res.data && res.data.code === 500) {
error && error(res.data);
return;
}
if (res.data) {
success && success(res.data);
return;
}
if (res.response.status == 403) {
error && error(res.data);
return;
}
if (res.response.data && res.response.data.message) {
error && error(res.data);
return Message.error({
type: "error",
message: res.response.data.message,
duration: 1500
});
}
if (res.response.status != 403) {
// let show = cache.getMemoryCache("isShow");
// if(!show){
// // Message.error({
// // type: "error",
// // message: "系统异常,程序员正在紧急修复",
// // duration: 1500
// // });
// }
error && error(res.data);
}
})
.catch(err => {
console.log(err);
reject(err.data);
// let show = cache.getMemoryCache("isShow");
// if(!show){
// // Message.error({
// // type: "error",
// // message: "系统异常,程序员正在紧急修复",
// // duration: 1500
// // });
// }
error && error(err.data);
});
});
}
export { _get, _post, _put, _delete, _form, _post_paging };