import $g from "./sg"; //常用路径________________________________________________________________________________ const API_ROOT_URL = "https://www.shuzhiqiang.com/api"; //生产的IP //一些前提条件和方法________________________________________________________________________________ //跳转到登录页面 const jumpLoginPage = () => { uni.clearStorageSync(); //清空对象键、值 if (!location.href.includes('/pages/login/index')) { uni.redirectTo({ url: `/pages/login/index?url=${encodeURIComponent(location.href)}` }); } } // 请求报错提示 const errAlert = (url, d) => sgAlert(`【可能是后端报错】\n\r【接口地址】${url}\n\r【返回报文】${JSON.stringify(d, null, 4)}`); // 普通报错提示 const sgAlert = (d) => { uni.showToast({ icon: 'none', title: d }); } //主体请求________________________________________________________________________________ export default { jumpLoginPage, API_ROOT_URL, imgPrePath: API_ROOT_URL + '/', //API FUNCTION ________________________________________________________________________________ __sd(type, url, data, doing, otherConfig = {}) { type || (type = 'POST'); //noAutoToken=true代表该接口无需保持登录状态就可以获取信息,譬如一些公共对外开放的接口 let token = uni.getStorageSync('token'); //获取token if (!otherConfig.noAutoToken) { if (!token) return jumpLoginPage();//判断如果本地没有token,否者直接跳转到登录页面 } let headers = { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', }; if (!otherConfig.noAutoToken) { token && (headers["kkToken"] || (headers["kkToken"] = token)); data || (data = {}); } // 防止下载导出文件乱码 let xhrFields = otherConfig.isDownload ? {responseType: "arraybuffer"} : null; // ________________________________________________________________________________ doing.s && (doing.success = doing.s); //精简别名 doing.f && (doing.fail = doing.f); //精简别名 doing.e && (doing.error = doing.e); //精简别名 doing.l && (doing.loading = doing.l, doing.loading.show && doing.loading.show()); //精简别名[加载] uni.request({ ...xhrFields, method: type, url, data, header: headers, success: d => { d = d.data; // typeof d === 'string' && (d = JSON.parse(d));//如果是字符串返回值,则转换为JSON格式 doing.loading && doing.loading.close && doing.loading.close(); //关闭加载 if (otherConfig.isDownload) return doing.success(d); //如果是下载,直接返回文档流 switch (d.success) { case true: //登录成功 ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ doing.success && doing.success(otherConfig.isGetAllData ? d : (d.data || d)); break; case false: //登录失败(当token密码失效的时候) ☠ ☠ ☠ ☠ doing.fail ? doing.fail(d) : sgAlert(d.msg); break; } switch (d.code) { case 403: //登录失败(当token密码失效的时候) ☠ ☠ ☠ ☠ case 404: //登录失败(当token密码失效的时候) ☠ ☠ ☠ ☠ jumpLoginPage(); doing.fail ? doing.fail(d) : sgAlert(d.msg); break; case -2: //扫码失败 ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ case -1: //请求失败 ✖ ✖ ✖ ✖ doing.fail ? doing.fail(d) : sgAlert(d.msg); break; } }, error: d => { //请求报错 ✖ ✖ ✖ ✖ if (d.status == 403) { jumpLoginPage(); return doing.error ? doing.error(d) : sgAlert(JSON.parse(d.responseText).msg); } doing.loading && doing.loading.close && doing.loading.close(); //关闭加载 doing.error ? doing.error(d) : errAlert(url, d); console.log("【报错】" + JSON.stringify(d, null, 4),d); }, }); }, //【通用接口demo】________________________________________________________________________________ API_NAME({ data, doing, config }) { this.__sd("post", `${API_ROOT_URL}/XXX/bill/print${$g.formatAsUrlParamString(data)}`, data, doing, config); }, //提交用例________________________________________ get({doing}) { this.__sd("get", `${API_ROOT_URL}/get.do`, null, doing) { isGetAllData: true }; }, post({data, doing}) { this.__sd("post", `${API_ROOT_URL}/post.do`, data, doing); }, login({data, doing}) { this.__sd("post", `${API_ROOT_URL}/post.do`, data, doing, { noAutoToken: true }); }, download({data, doing}) { this.__sd("post", `${API_ROOT_URL}/post.do`, data, doing, { isDownload: true }); }, }