1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
| import { baseConfig, debugUser } from "../config/api.config"; import config from "../config/host.config"; import utils from "../js/utils"; import { Toast } from "vant";
axios.interceptors.request.use(async (config) => {
if ( config.data && Object.prototype.toString.call(config.data) == "[object FormData]" ) { config.headers["Content-Type"] = "multipart/form-data;charset=utf-8"; config.transformRequest = [ function (data) { return data; }, ]; }
if (config.params && typeof config.params != "string") config.params = utils.filterNull(config.params); if (process.env.NODE_ENV !== "production") { for (const key in debugUser) config.headers[key] = debugUser[key]; } if (typeof bsch != "undefined") { let autho = () => { return new Promise((resolve, reject) => { bsch.autho((resp) => { resolve(resp); }); }); }; let resp = await autho(); config.headers["schoolId"] = resp.schoolId; config.headers["roleId"] = resp.roleId; config.headers["userId"] = resp.userId; }
return config; });
axios.interceptors.response.use( (response) => { let { data, status } = response; return handeCallback(data, status); }, function axiosRetryInterceptor(err) { console.log(err.message); console.log(err.message.indexOf("exceeded")); if (err.message.indexOf("exceeded") > -1) Toast.fail("网络超时!"); } );
const createApi = (url, params = null, method = "GET") => { method = method.toLocaleUpperCase(); if (params == null) params = {}; const baseConfigCopy = { ...baseConfig, url, method, data: method === "POST" || method === "PUT" ? params : null, params: method === "GET" || method === "DELETE" ? params : null, };
try { return axios(baseConfigCopy); } catch (error) { console.error("error:" + error); return new Promise((reject) => reject(error)); } };
function createURL(url, param) { var paramStr = ""; for (let key in param) { if (param.hasOwnProperty(key)) { const element = param[key]; var link = "&" + key + "=" + element; paramStr += link; } } url = url + "?" + paramStr.substr(1); url = encodeURI(url); return url.replace(" ", ""); }
function handeCallback(resp, resCode) { var respStr = resp; if (typeof respStr === "string") { respStr = respStr.replace(/(\r\n)|(\n)|(\r)/g, "<br>"); respStr = respStr.replace(/(\t)/g, " "); respStr = respStr.replace(/☊/g, "'"); respStr = respStr.replace(/♤/g, "\\"); respStr = respStr.replace(/♢/g, "/"); respStr = respStr.replace(/♧/g, '"'); try { resp = JSON.parse(respStr); } catch (error) { alert("网络连接错误"); } }
if (resCode == 200) { if (resp.State || resp.code === 0) { return resp.data; } else { throw resp.msg; } } else { console.error(resCode, resp); throw typeof resp; } }
export { createApi };
|