const tui = { //接口地址 interfaceUrl: function() { return 'https://hbkuaishi.com/hiver' //测试服务器 // return 'https://dev.xenjoyou.com/hiver' //正式服务器 //return 'http://192.168.1.9:8888/hiver' // return 'http://192.168.100.42:8888/hiver' }, toast: function(text, duration, success) { uni.showToast({ duration: duration || 2000, title: text || "出错啦~", icon: success ? 'success' : 'none', mask:true }) }, 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(); return res.platform.toLocaleLowerCase() == "android" }, 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; time = this.isAndroid() ? 300 : 0; return time }, delayed: null, loadding: false, showLoading: function(title, mask = true) { uni.showLoading({ mask: mask, title: title || '请稍候...' }) }, /** * 请求数据处理 * @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: async function(url, method, postData, isDelay, isForm, hideLoading) { //接口请求 tui.loadding && uni.hideLoading(); tui.loadding = false; if (!hideLoading) { if (isDelay) { tui.delayed = setTimeout(() => { tui.loadding = true tui.showLoading() }, 1000) } else { tui.loadding = true tui.showLoading() } } return new Promise((resolve, reject) => { let executeRequest = () => { wx.request({ url: tui.interfaceUrl() + url, data: postData, timeout:60000, header: { 'content-type': isForm ? 'application/x-www-form-urlencoded' : 'application/json', 'accessToken': tui.getToken() }, method: method, //'GET','POST' dataType: 'json', success: (res) => { if(res.data.code == 401){ if(uni.getStorageSync('codePage')){ uni.clearStorage() uni.setStorageSync('codePage',true) }else{ uni.clearStorage() } uni.navigateTo({ url: '/package2/login/login' }) } if(uni.getStorageSync(url)){ uni.removeStorageSync(url) } clearTimeout(tui.delayed) tui.delayed = null; if (tui.loadding && !hideLoading) { uni.hideLoading() } resolve(res.data) }, fail: (res) => { if(uni.getStorageSync(url)){ if(uni.getStorageSync(url)< 4){ let count = uni.getStorageSync(url) uni.setStorageSync(url,count + 1) executeRequest(); }else{ uni.removeStorageSync(url) reject(res) return } }else{ uni.setStorageSync(url,1) executeRequest(); } clearTimeout(tui.delayed) tui.delayed = null; } }) } executeRequest(); }) }, /** * 上传文件 * @param string url 请求地址 * @param string src 文件路径 */ uploadFile: function(url, src) { tui.showLoading() return new Promise((resolve, reject) => { const uploadTask = uni.uploadFile({ url: tui.interfaceUrl() + url, filePath: src, name: 'imageFile', header: { // 'Authorization': tui.getToken() 'accessToken': uni.getStorageSync("hiver_token") }, formData: { // sizeArrayText:"" }, success: function(res) { uni.hideLoading() let d = JSON.parse(res.result.replace(/\ufeff/g, "") || "{}") if (d.code % 100 == 0) { //返回图片地址 let fileObj = d.result; resolve(fileObj) } else { that.toast(res.message); } }, fail: function(res) { reject(res) that.toast(res.message); } }) }) }, 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("hiver_token", token) uni.setStorageSync("hiver_mobile", mobile) }, //获取token getToken() { return uni.getStorageSync("hiver_token") }, //判断是否登录 isLogin: function() { return uni.getStorageSync("hiver_mobile") ? true : false }, //跳转页面,校验登录状态 href(url, isVerify) { if (isVerify && !tui.isLogin()) { uni.navigateTo({ url: '/package2/login/login' }) } else { uni.navigateTo({ url: url }); } } } export default tui