123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- import axios from "axios";
- import qs from "qs";
- import ViewUI from "view-design";
- // import router from "../router";
- // import Vue from "vue";
- /**
- * errorCallback 请求失败的回调
- * @param {Object} error 请求失败时的错误信息
- */
- const errorCallback = error => {
- let content = "";
- if (error.response) {
- content =
- (error.response.data && error.response.data.message) || "服务错误";
- } else if (error.request) {
- content = "请求错误";
- if (error.message.indexOf("timeout") > -1) {
- content = "请求超时";
- }
- } else {
- return error;
- }
- content = content.indexOf("###") !== -1 ? "参数错误" : content;
- ViewUI.Notice.error({ title: "错误提示", desc: content });
- return error;
- };
- /**
- * errorDataCallback 请求成功,结果有误的回调
- * @param {Object} error Response中的data信息
- */
- // const errorDataCallback = error => {
- // let content = error.message || "数据错误";
- // content = content.indexOf("###") !== -1 ? "参数错误" : content;
- // // TODO:自定义处理逻辑,以下为epcc实例
- // if (error.code === -100) {
- // content = "身份验证失效,请重新登录";
- // ViewUI.Modal.confirm({
- // title: "重新登陆?",
- // content,
- // onOk: () => {
- // Vue.ls.clear();
- // router.push({ name: "Login" });
- // }
- // });
- // } else {
- // ViewUI.Notice.error({ title: "错误提示", desc: content });
- // }
- // return error;
- // };
- /**
- * response format
- * {
- config, header, data, request, status, statusText
- }
- *
- */
- /**
- * successCallback 请求成功的回调
- * @param {Object} data Response中的data信息
- */
- const successCallback = data => {
- return data;
- // if (data.code === 0) {
- // return data.data;
- // } else {
- // throw new Error(errorDataCallback(data));
- // }
- };
- /**
- * get请求
- * @param {String} url 请求地址
- * @param {Object} datas 请求数据
- */
- const $get = (url, datas) => {
- let sqDatas = "";
- if (datas) {
- sqDatas = qs.stringify(datas, {
- arrayFormat: "brackets"
- });
- url += "?" + sqDatas;
- }
- return axios
- .get(url)
- .then(rep => {
- return successCallback(rep.data);
- })
- .catch(error => {
- throw new Error(errorCallback(error));
- });
- };
- /**
- * post请求
- * @param {String} url 请求地址
- * @param {Object} datas 请求数据
- */
- const $post = (url, datas) => {
- return axios
- .post(url, datas)
- .then(rep => {
- return successCallback(rep.data);
- })
- .catch(error => {
- throw new Error(errorCallback(error));
- });
- };
- /**
- * delete请求
- * @param {String} url
- * @param {Object} datas
- */
- const $del = (url, datas) => {
- let sqDatas = "";
- if (datas) {
- sqDatas = qs.stringify(datas, { arrayFormat: "brackets" });
- url += "?" + sqDatas;
- }
- return axios
- .delete(url)
- .then(rep => {
- return rep.data;
- })
- .catch(error => {
- throw new Error(errorCallback(error));
- });
- };
- /**
- * put 请求
- * @param {String} url 请求地址
- * @param {Object} datas 请求数据
- */
- const $put = (url, datas) => {
- return axios
- .put(url, datas)
- .then(rep => {
- return rep.data;
- })
- .catch(error => {
- throw new Error(errorCallback(error));
- });
- };
- /**
- * patch请求
- * @param {String} url 请求地址
- * @param {Object} datas 请求数据
- */
- const $patch = (url, datas) => {
- return axios
- .patch(url, datas)
- .then(rep => {
- return rep.data;
- })
- .catch(error => {
- throw new Error(errorCallback(error));
- });
- };
- export { $get, $post, $del, $put, $patch };
|