// @ts-ignore import {fetchToken} from "@/utils/auth" declare const process: any; declare const __wxConfig: any; let isDevelopment = false; // #ifdef MP-WEIXIN const e = typeof __wxConfig !== "undefined" ? __wxConfig.envVersion || "release" : "release"; console.log("env", e); isDevelopment = e === "develop" || e === "trial"; // #endif const env: string = isDevelopment ? "dev" : "prd"; const apis = { dev: { serverUrl: "https://dev.kuaiyuman.cn/api/", 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: 'wash.token' }, 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: any) { return !exp || typeof (exp) == "undefined" || exp.length === 0 || exp === '' || JSON.stringify(exp) === "{}"; }; /** * get请求封装 * @param url * @param param */ const get = (url: string, param = {}) => { let token = fetchToken() || "" if (!isEmptyOrNull(param)) { var params = []; for (var key in param) { // @ts-ignore 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", "token": token, }, }; return request(options) }; const body = (url: string, param = {}) => { let token = fetchToken() || "" let options = { url: fillUrl(url), data: param, method: 'POST', header: { 'X-Requested-With': 'XMLHttpRequest', "Accept": "application/json", "Content-Type": "application/json; charset=UTF-8", "token": token, }, dataType: 'json' }; return request(options); }; const request = (options: any) => { return new Promise((resolve, reject) => { uni.request({ url: options.url, data: options.data, method: options.method, header: options.header, dataType: options.dataType }).then(res => { // @ts-ignore let response = res[1].data; if (response.code !== 200) { if (response.code == 92213 || response.code == 92305) { uni.showToast({ title: response.msg, icon: 'none' }); setTimeout(() => { uni.navigateTo({ url: `/pages/login/login` }) }, 300) } else { let errMsg = response.msg || '网络异常,请稍后重试'; 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 = (url: string, param: any = {}) => { let token = fetchToken() || "" console.log("upload", url, param, token) return new Promise((resolve, reject) => { uni.uploadFile({ url: fillUrl(url), filePath: param.filePath, name: param.name, header: { token }, success: function (res) { console.log("upload file=>", res) let response = JSON.parse(res.data); if (response.code === 200) { resolve(response); }else if (response.code === 500) { uni.showToast({ title: '系统繁忙,请稍后重试', icon: 'none' }); reject(); } else { console.error() uni.showToast({ title: response.message }); reject(); } }, fail: function (e) { console.error(e) uni.showToast({ title: '请稍后重试' }); reject(); } }); }) } const uploadV2 = (opt: any) => { 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: string) { if (url.indexOf("http") === 0) { return url; } else { return serverUrl + url; } }; const formatUrl = (v: string) => { if (v == null || v == "") { return "/static/missing-face.png"; } if (v.indexOf("http") === 0) { return v; } return fileUrl + v.replace(/\\/g, "/"); }; export { get, body, upload, uploadV2, cfg, serverUrl, fileUrl, formatUrl }