const env = process.env.NODE_ENV==="development"?"dev":"prd"; let apis = { dev: { serverUrl: "http://localhost:8080/admin/", fileUrl: "https://zyp-1258963180.cos.ap-guangzhou.myqcloud.com/" }, uat: { serverUrl: "http://npt.free.idcfengye.com/", fileUrl: "https://zyp-1258963180.cos.ap-guangzhou.myqcloud.com/" }, prd: { serverUrl: "https://npww.net.cn/cms/", fileUrl: "https://zyp-1258963180.cos.ap-guangzhou.myqcloud.com/", }, }; const cfg = { key: { token: 'kuaiyuman.token', account:'kuaiyuman.account', }, env: env, api: { serverUrl: apis[env].serverUrl, fileUrl: apis[env].fileUrl, uploadUrl: apis[env].serverUrl + "file/upload" } }; const serverUrl = cfg.api.serverUrl; const fileUrl = cfg.api.fileUrl; const isEmptyOrNull = function (exp) { return !exp || typeof (exp) == "undefined" || exp.length === 0 || exp === '' || JSON.stringify(exp) === "{}"; }; /** * get请求封装 * @param url * @param param */ const get = (url, param = {}) => { let token = uni.getStorageSync(cfg.key.token) || ""; if (!isEmptyOrNull(param)) { var params = []; for (var key in param) { params.push(encodeURIComponent(key) + "=" + encodeURIComponent(param[key])); } param = params.join("&") } let options = { url: fillUrl(url) + (isEmptyOrNull(param) ? "" : "?" + param), data: param, method: 'GET', header: { "Accept": "application/json", "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", "satoken": token, }, }; return request(options) }; const post = (url, param = {}) => { let token = uni.getStorageSync(cfg.key.token) || ""; let options = { url: fillUrl(url), data: param, method: 'POST', header: { 'X-Requested-With': 'XMLHttpRequest', "Accept": "application/json", "Content-Type": "application/json; charset=UTF-8", "satoken": token, }, dataType: 'json' }; return request(options); }; const request = (options) => { return new Promise((resolve, reject) => { uni.request({ url: options.url, data: options.data, method: options.method, header: options.header, dataType: options.dataType }).then(res => { let response = res[1].data; if (response.code !== 200) { if (response.code == 10001) { uni.showToast({ title: response.message, icon: 'none' }); setTimeout(() => { uni.navigateTo({ url: `/pages/login/login` }) }, 300) } else { let errMsg = response.message || '网络异常,请稍后重试'; uni.showToast({ title: errMsg, icon: 'none' }); reject(errMsg); } } else { resolve(response.data); } }).catch(error => { uni.hideLoading(); uni.showToast({ title: '网络异常,请稍后重试', icon: 'none' }); console.error("error=>", error); reject(error.msg); }) }); }; const upload = opt => { opt = opt || {}; opt.url = opt.url || ''; opt.filePath = opt.filePath || null;//要上传文件资源的路径。 opt.name = opt.name || null;//文件对应的 key , 开发者在服务器端通过这个 key 可以获取到文件二进制内容 opt.filePath = opt.filePath || null; opt.success = opt.success || function () { }; uni.uploadFile({ url: fillUrl(opt.url), filePath: opt.filePath, name: opt.name, success: function (res) { console.log("upload file=>", res) opt.success(res); }, fail: function () { uni.showToast({ title: '请稍后重试' }); } }) }; const fillUrl = function (url) { if (url.indexOf("http") === 0) { return url; } else { return serverUrl + url; } }; const formatUrl = v => { if (v == null || v == "") { return "/static/missing-face.png"; } if (v.indexOf("http") === 0) { return v; } return fileUrl + v.replace(/\\/g, "/"); }; const msg = (title, icon = 'none', duration = 1800, mask = false) => { //统一提示方便全局修改 if (Boolean(title) === false) { return; } uni.showToast({ title, duration, mask, icon }); }; export { get, post, upload, cfg, serverUrl, fileUrl,formatUrl,msg }