/** * 常用方法封装 请求,文件上传等 **/ const http = { aa:'打印123', //接口地址 interfaceUrl: function() { return 'https://ht.xinmingyi.cn/api/' }, toast: function(text, duration, success) { uni.showToast({ title: text || "出错啦~", icon: success ? 'success' : 'none', duration: duration || 2000 }) }, 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; // #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, postDataa, method, isDelay, isForm, hideLoading) { let postData = postDataa || {} let token = uni.getStorageSync("us_token") || '' if (token) { postData.token = token; } //接口请求 uni.showLoading({ mask: true, title: '加载中...', success(res) {} }) return new Promise((resolve, reject) => { uni.request({ url: http.interfaceUrl() + url, data: postData, header: { 'content-type': 'application/x-www-form-urlencoded', }, method: method ? method : 'POST', //'GET','POST' dataType: 'json', success: (res) => { uni.hideLoading() if (res.data.msg=='账号信息已过期,请重新登录'||res.data.msg=='请先登录') { uni.navigateTo({ url: '/pages/login/login' }) // return http.modal('温馨提示', res.data.msg, true, (res => { // })) } resolve(res.data) }, fail: (res) => { clearTimeout(http.delayed) http.delayed = null; http.toast('网络错误,请稍后重试~') reject(res) } }) }) }, /** * 上传文件 * @param string url 请求地址 * @param string src 文件路径 */ uploadFile: function(url, src) { uni.showLoading({ title: '请稍候...' }) return new Promise((resolve, reject) => { const uploadTask = uni.uploadFile({ url: http.interfaceUrl() + url, filePath: src, name: 'file', header: { // 'Authorization': http.getToken() }, formData: { // sizeArrayText:"" }, success: function(res) { uni.hideLoading() let d = JSON.parse(res.data.replace(/\ufeff/g, "") || "{}") if (d.code % 100 == 0) { //返回图片地址 let fileObj = d.data; resolve(fileObj) } else { http.toast(res.msg); } }, fail: function(res) { reject(res) http.toast(res.msg); } }) }) }, qinJsonp: function(url, callback, callbackname) { // #ifdef H5 window[callbackname] = callback; let qinScript = document.createElement("script"); qinScript.src = url; qinScript.type = "text/javascript"; document.head.appendChild(qinScript); document.head.removeChild(qinScript); // #endif }, //设置用户信息 setUserInfo: function(mobile, token) { uni.setStorageSync("mobile", mobile) }, // //获取token // getToken() { // return uni.getStorageSync("token") // }, //判断是否登录 isLogin: function() { return uni.getStorageSync("token") ? true : false }, //判断微信浏览器 isweixin: function() { let type = false; // #ifdef MP-WEIXIN type = true //#endif // #ifdef H5 let en = window.navigator.userAgent.toLowerCase(); // 匹配en中是否含有MicroMessenger字符串 if (en.match(/MicroMessenger/i) == 'micromessenger') { // alert("在微信",11111) type = true } return type //#endif }, //跳转页面,校验登录状态 href(url, isVerify) { if (isVerify && !http.isLogin()) { uni.navigateTo({ url: '/pages/common/login/login' }) } else { uni.navigateTo({ url: url }); } }, //上一页 prePage() { let pages = getCurrentPages(); let prePage = pages[pages.length - 2]; // #ifdef H5 return prePage; // #endif return prePage.$vm; }, } export default http