/** * 常用方法封装 请求,文件上传等 * @author echo. **/ const axg = { shopId: function() { return "10108618787549"; }, //接口地址 interfaceUrl: function() { // #ifdef H5 return '/api'; // #endif // #ifdef MP // return 'https://apiv.jsshuita.com.cn/mini' return 'http://127.0.0.1:8190/api/dy' // #endif }, toast: function(text, duration, success) { // #ifdef APP-PLUS plus.nativeUI.toast(text || "出错啦~"); // #endif // #ifndef MP uni.showToast({ title: text || "出错啦~", icon: success ? 'success' : 'none', duration: duration || 2000 }) // #endif }, modal: function(title, content, showCancel, callback, confirmColor, confirmText) { uni.showModal({ title: title || '提示', content: content, showCancel: showCancel, cancelColor: "#555", confirmColor: confirmColor || "#5677fc", confirmText: confirmText || "确定", success(res) { if (res.confirm) { callback && callback(true) } else { callback && callback(false) } } }) }, isAndroid: function() { const res = uni.getSystemInfoSync(); if(res.platform.toLocaleLowerCase() == 'android') { return true; } return false; }, isPhoneX: function() { const res = uni.getSystemInfoSync(); let iphonex = false; let models=['iphonex','iphonexr','iphonexsmax','iphone11','iphone11pro','iphone11promax'] const model=res.model.replace(/\s/g,"").toLowerCase() if (models.includes(model)) { iphonex = true; } return iphonex; }, constNum: function() { let time = 0; // #ifdef APP-PLUS time = this.isAndroid() ? 300 : 0; // #endif return time }, delayed: null, /** * 请求数据处理 * @param string url 请求地址 * @param string method 请求方式 * GET or POST * @param {*} postData 请求参数 * @param bool isDelay 是否延迟显示loading * @param bool isForm 数据格式 * true: 'application/x-www-form-urlencoded' * false:'application/json' * @param bool hideLoading 是否隐藏loading * true: 隐藏 * false:显示 */ request: function(url, method, postData, isDelay, isForm, hideLoading) { //接口请求 let loadding = false; axg.delayed && uni.hideLoading(); clearTimeout(axg.delayed); axg.delayed = null; if (!hideLoading) { axg.delayed = setTimeout(() => { uni.showLoading({ mask: true, title: '请求中...', success(res) { loadding = true } }) }, isDelay ? 1000 : 0) } var header = { 'Content-Type': isForm ? 'application/x-www-form-urlencoded' : 'application/json', 'authorization': "Bearer "+axg.getToken(), 'platform': "mini" } // postData.shop = axg.shopId(); axg.toast(axg.interfaceUrl() + url) return new Promise((resolve, reject) => { uni.request({ url: axg.interfaceUrl() + url, data: postData, header: header, method: method, //'GET','POST' dataType: 'json', success: (res) => { clearTimeout(axg.delayed) axg.delayed = null; uni.hideLoading() if (loadding && !hideLoading) { uni.hideLoading() } // if (res.data && res.data.code == 401) { // uni.showToast({ // title: res.data.msg, // icon: 'none', // mask: true, // duration: 1500, // success() { // // uni.navigateTo({ // // url:"/pages/user/login" // // }); // uni.clearStorageSync(); // } // }); // return ; // } resolve(res.data) }, fail: (res) => { clearTimeout(axg.delayed) axg.delayed = null; axg.toast("网络不给力,请稍后再试~") reject(res) } }) }) }, /** * 上传文件 * @param string url 请求地址 * @param string src 文件路径 */ uploadFile: function(url, src, param) { return new Promise((resolve, reject) => { const uploadTask = uni.uploadFile({ url: axg.interfaceUrl() + url, filePath: src, name: 'file', header: { 'authorization': "Bearer "+axg.getToken(), 'platform': "mini" }, formData:param, success: function(res) { uni.hideLoading() let d = JSON.parse(res.data.replace(/\ufeff/g, "") || "{}") if (d.code == 200) { //返回图片地址 let fileObj = d.data; resolve(fileObj) } else { axg.toast(res.msg); } }, fail: function(res) { console.log('上传错误:',res) reject(res) axg.toast(res.msg); } }) }) }, tuiJsonp: function(url, callback, callbackname) { // #ifdef H5 window[callbackname] = callback; let tuiScript = document.createElement("script"); tuiScript.src = url; tuiScript.type = "text/javascript"; document.head.appendChild(tuiScript); document.head.removeChild(tuiScript); // #endif }, //设置用户信息 setUserInfo: function(mobile, token) { //uni.setStorageSync("thorui_token", token) uni.setStorageSync("user_token", mobile) }, //获取token getToken() { return uni.getStorageSync("user_token") || 'None' }, //判断是否登录 isLogin: function() { return uni.getStorageSync("user_token") ? true : false }, //跳转页面,校验登录状态 href(url, isVerify) { if (isVerify && !axg.isLogin()) { uni.navigateTo({ url: '/pages/user/login/login' }) } else { uni.navigateTo({ url: url }); } } } export default axg