/* @params url 接口地址 @params method 请求方式 @params data 参数 */ let BASE_URL = '' // #ifdef MP-WEIXIN // 仅在微信小程序可用 BASE_URL = // 'https://novel.ytpm.net/pro-api' uni.getAccountInfoSync().miniProgram.envVersion == 'release' ? 'https://novel.ytpm.net/pro-api' : "https://novel.test.ytpm.net/pro-api"; // 区分正式与体验版 // #endif // #ifdef H5 BASE_URL = "https://novel.test.ytpm.net/pro-api" // BASE_URL = "http://192.168.1.89:8685" // #endif const WebUrl = BASE_URL.split('/pro-api')[0] const imgApi = BASE_URL+"/file/uploadFile"; // 上传接口 const imgBaseApi = BASE_URL+ "/file/uploadBaseFile" const request = (url, method, data) => { let token = uni.getStorageSync("token"); //openId let header = { "Content-Type": "application/json", 'Accept':"application/json" }; if (token) { header.Authorization = token; } return new Promise((resolve, reject) => { uni.request({ url: BASE_URL + url, // 开发者服务器接口地址 method: method, //请求方式 timeout: 60000, //请求超时时间 data: data, //请求的参数 header: header, success(res) { if(res.statusCode == 401){ uni.showToast({ title: "身份验证过期,请重新登录", icon: "none", }); let firstView = uni.getStorageSync('firstView') uni.clearStorageSync() var pages = getCurrentPages(); var page = pages[pages.length - 1]; var fullpath = page.$page.fullPath uni.setStorageSync('firstView',firstView) uni.setStorageSync('tempUrl',fullpath) uni.setStorageSync('401Flag',true) // 401时用户手动点击登录 setTimeout(() => { uni.navigateTo({ url: "/pages/login/index", }); }, 500); return } //对请求请求到的信息进行处理 if (res.data.status == 200 || res.data.status == 500 || res.data.status == 0) { if (res.data.description) { if (res.data.data) { res.data.data = res.data.data; } else { res.data.data = { description: "" }; } // res.data.data.description = res.data.description; } resolve(res.data.data); if (res.data.Authorization) { // 更新 token uni.setStorageSync("token", res.data.Authorization); } } else if (res.data.status == 403) { uni.showToast({ title: "身份验证过期,请重新登录", icon: "none", }); setTimeout(() => { uni.navigateTo({ url: "/pages/login/index", }); }, 1000); } else if (res.data.status == 40305) { uni.showToast({ title: "该账号已被冻结,请联系客服", icon: "none", }); // uni.navigateTo({ // url: "/pages/login/index", // }); } else if (res.data.status == 100) { uni.showToast({ title: res.data.description, icon: "none", }); } else { uni.showToast({ title: "请求失败,请重新获取数据", icon: "none", }); } }, fail(err) { reject(err); }, }); }); }; export default { request, //向外暴露request imgApi, imgBaseApi, BASE_URL, WebUrl };