import Validate from './validate.js'; import regeneratorRuntime from './wxPromise.min.js' export default { //验证 Validate, regeneratorRuntime, //格式化时间 formatTime(date, format) { let newFormat = format || 'YY-M-D h:m:s'; let formatNumber = this.formatNumber; let newDate = date || new Date(); if (Object.prototype.toString.call(newDate).slice(8, -1) !== "Date") { newDate = new Date(date); } let week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', '日', '一', '二', '三', '四', '五', '六']; return newFormat.replace(/YY|Y|M|D|h|m|s|week|星期/g, function (a) { switch (a) { case 'YY': return newDate.getFullYear(); case 'Y': return (newDate.getFullYear() + '').slice(2); case 'M': return formatNumber(newDate.getMonth() + 1); case 'D': return formatNumber(newDate.getDate()); case 'h': return formatNumber(newDate.getHours()); case 'm': return formatNumber(newDate.getMinutes()); case 's': return formatNumber(newDate.getSeconds()); case '星期': return "星期" + week[newDate.getDay() + 7]; case 'week': return week[newDate.getDay()]; } }) }, //格式化数字 formatNumber(n) { n = n.toString(); return n[1] ? n : '0' + n }, /** * 人性话格式时间 */ ctDate(date) { const minute = 1000 * 60; const hour = minute * 60; const day = hour * 24; const month = day * 30; if (!date) return ""; const now = Date.now(); let diffValue; let result; date = typeof date === "number" ? date : +(new Date(date)); diffValue = now - date; let monthC = diffValue / month; let weekC = diffValue / (7 * day); let dayC = diffValue / day; let hourC = diffValue / hour; let minC = diffValue / minute; if (monthC >= 1) { result = parseInt(monthC) + "个月前"; } else if (weekC >= 1) { result = parseInt(weekC) + "个星期前"; } else if (dayC >= 1) { result = parseInt(dayC) + "天前"; } else if (hourC >= 1) { result = parseInt(hourC) + "个小时前"; } else if (minC >= 1) { result = parseInt(minC) + "分钟前"; } else { result = "刚刚发表"; } return result; }, //返回类型 typeOf(param) { return Object.prototype.toString.call(param).slice(8, -1) }, //判断是否为空 isEmpty(param) { //基本类型为空 let condition1 = param === '' || param === null || param === undefined || param === "NaN" || param === false; let condition2; let condition3 //引用类型为空 if (!condition1) { condition2 = this.typeOf(param) === "Object" && Object.keys(param).length < 1; condition3 = this.typeOf(param) === "Array" && param.length < 1; } return condition1 || condition2 || condition3; }, // 判断是否是数字 isNum(param) { var reg = /^[0-9]+.?[0-9]*$/; if (reg.test(param)) { return true } return false }, //检查授权 checkAuth(name) { let that = this; return new Promise((resove, reject) => { wx.getSetting({ success(res) { console.log(res.authSetting[`scope.${name}`], "res.authSetting") if (res.authSetting[`scope.${name}`]) { resove(true) } else { resove(false) } }, fail() { that.networkError() } }) }) }, //小程序自带获取定位 getLocation() { let that = this; return new Promise((resove, reject) => { wx.getLocation({ success: function (res) { resove(res) }, fail: function (res) { reject(res) that.hideAll() } }) }) }, //授权后调用百度地图接口获取当前城市 getLocation(_this) { // 微信获得经纬度 let ak = 'GoI7BxLpfvBEyf1TcMXCloi99Vov7flZ' return new Promise((resolve, reject) => { wx.getLocation({ type: 'wgs84', //gcj02 // wgs84 success: function (res) { let latitude = res.latitude let longitude = res.longitude wx.request({ url: 'https://api.map.baidu.com/reverse_geocoding/v3/?ak=sHZTomd7grslfP7sPKB8tRgT49FK9TEu&output=json&coordtype=gcj02&location=' + latitude + ',' + longitude, data: {}, header: { 'Content-Type': 'application/json' }, success: function (ops) { let { province, city, district } = ops.data.result.addressComponent let region = province + city + district resolve({ region, latitude, longitude }) }, fail: function (err) { wx.showModal({ title: '信息提示', content: '请求失败', showCancel: false, confirmColor: '#f37938' }); }, complete: function () { } }) }, fail(err) { wx.showModal({ title: '未授权', content: `为保证功能正常使用,点击「右上角」-「关于**」-「右上角」-「设置」,打开位置权限后重试`, showCancel: false, }) resolve() }, complete: (data) => { } }) }) }, //网络错误提示 networkError(status = 0) { const netMessage = { "0": "网络异常", "1": "请求超时", "500": "服务器错误", "404": "请求地址错误" } if (status != 500) return; this.showFail(netMessage[status]) }, /* 打开提示信息 */ showModal(content = "服务器错误") { wx.showModal({ title: "提示", content, showCancel: false }) }, showLoading(title = "加载中") { wx.showLoading({ title, mask: true }) }, showSuccess(title = "操作成功") { wx.showToast({ title, }) }, showFail(title = "操作失败") { wx.showToast({ title, icon: 'none' }) }, hideLoading() { wx.hideLoading() }, /* 隐藏所有提示信息 */ hideAll() { wx.hideLoading(); wx.stopPullDownRefresh(); wx.hideNavigationBarLoading(); }, //获取标签上data getFormData(e) { return e.detail.target.dataset }, getData(e) { return e.currentTarget.dataset }, //获表单控件值 getValue(e) { return e.detail.value }, //跳转 goUrl(url, method = "navigateTo") { if (!url) { return; } //拨打电话 if (url.indexOf('tel:') > -1) { wx.makePhoneCall({ phoneNumber: url.split(':')[1], }) return; } //网页跳转 if (url.indexOf('http') > -1) { wx.navigateTo({ url: `/pages/common/webview/webview?url=${url}`, }) return; } //小程序跳转 if (url.indexOf('wx') == 0) { var appIdData, pathData = '', envVersionData = 'release'; var urlArr = url.split(':'); if (urlArr.length == 1) { appIdData = urlArr[0]; } else if (urlArr.length == 2) { appIdData = urlArr[0]; pathData = urlArr[1]; } else if (urlArr.length == 3) { appIdData = urlArr[0]; pathData = urlArr[1]; envVersionData = urlArr[2]; } wx.navigateToMiniProgram({ appId: appIdData, path: pathData, extraData: { lb: 'longbing' }, envVersion: envVersionData, success(res) { // 打开成功 } }) return; } //消息插件跳转 if (url.indexOf('/nova-chat') == 0) { getApp().JIMServ.chatNavigate(url); return } //商城组件跳转 if (url.indexOf('/mshop') == 0) { wx.navigateTo({ url }) return } //正常页面跳转 wx[method]({ url }) }, //复制文本 setClipboard(content) { wx.setClipboardData({ data: content, success: function (res) { wx.getClipboardData({ success: function (res) { console.log('复制文本成功 ==>>', res.data); } }); } }); }, //格式化参数对象 setOptions(o) { return encodeURIComponent(JSON.stringify(o)) }, //解析参数对象 getOptions(o) { return JSON.parse(decodeURIComponent(o)) }, //获取页面对象,0时为当前页面 getPage(index = 0) { let pages = getCurrentPages(); let page = pages[pages.length - 1 + index] return page }, //发起支付 pay(orderInfo) { let that = this; return new Promise((resove, reject) => { wx.requestPayment({ timeStamp: orderInfo.timeStamp, nonceStr: orderInfo.nonceStr, 'package': orderInfo.package, signType: orderInfo.signType, paySign: orderInfo.paySign, success: function (res) { resove(true) }, fail: function (res) { that.showFail("支付失败") }, complete: function (res) { console.log(res) }, }) }) }, //深拷贝 deepCopy(o) { let that = this; if (o instanceof Array) { var n = []; for (var i = 0; i < o.length; ++i) { n[i] = that.deepCopy(o[i]); } return n; } else if (o instanceof Function) { var n = new Function("return " + o.toString())(); return n } else if (o instanceof Object) { var n = {} for (var i in o) { n[i] = that.deepCopy(o[i]); } return n; } else { return o; } }, //获取数组中的id字符串,以逗号隔开 getIds: function (o) { let ids = []; o = o || []; o.forEach((item) => { ids.push(item.id || item) }) return ids.join(','); }, //查询某个字符在字符串的位置 searchSubStr: function (str, subStr) { let positions = []; let pos = str.indexOf(subStr); while (pos > -1) { positions.push(pos); pos = str.indexOf(subStr, pos + 1); } return positions }, //将一个数组根据规则分为两个 partition: function (arr, isValid) { arr.reduce( ([pass, fail], elem) => isValid(elem) ? [ [...pass, elem], fail ] : [pass, [...fail, elem]], [ [], [] ], ) }, /* * 获取链接某个参数 * url 链接地址 * name 参数名称 */ getUrlParam: function (url, name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象 var r = url.split('?')[1].match(reg); //匹配目标参数 if (r != null) return unescape(r[2]); return null; //返回参数值 }, /* * 格式化扫码二维码的参数scene例如a=1&b=2转化为对象{a:1,b:2} * param 参数scene */ getSceneParam: function (param) { if (!param) return {}; let paramArr = param.split('&'); let paramObj = {}; for (let item of paramArr) { let sp = item.split("=") paramObj[sp[0]] = sp[1] } return paramObj }, /** * @desc 函数防抖 * @param func 函数 * @param wait 延迟执行毫秒数 * @param immediate true 表立即执行,false 表非立即执行 */ debounce: function (func, wait, immediate) { var timeout = null; return function () { var context = this; var args = arguments; if (timeout) clearTimeout(timeout); if (immediate) { let callNow = !timeout; timeout = setTimeout(function () { timeout = null; }, wait) if (callNow) func.apply(context, args) } else { timeout = setTimeout(function () { func.apply(context, args) }, wait); } } }, getHostname(url) { var reg = /^http(s)?:\/\/(.*?)\// // 必须是http开头或者https开头,结尾为'/' var ToReplace = 'Host/' url.replace(reg, ToReplace) url = reg.exec(url)[2]; return url; }, nowPageIndex(tabBar) { let pages = getCurrentPages(); let page = pages[pages.length - 1] let route = page.__route__ let nowPageIndex; for (let i in tabBar.list) { let item = tabBar.list[i] if (item.pagePath.includes(route)) { nowPageIndex = i } } return nowPageIndex; }, getNavBarHeight() { let { statusBarHeight } = wx.getSystemInfoSync(); let custom = wx.getMenuButtonBoundingClientRect(); let navBarHeight = custom.bottom + custom.top - statusBarHeight; //let navBarHeight = statusBarHeight+44 return navBarHeight }, splitArr(list, num) { let newList = []; let index = 0; while (index < list.length) { newList.push(list.slice(index, index += num)); } return newList }, getPageConfig(configInfo, arr = []) { let pageConfig = {}; for (let i in arr) { let key = arr[i] pageConfig[key] = configInfo[key] } return pageConfig }, //解析文件类型 getFileType(url) { let pdfReg = /^.+(\.pdf)$/ let txtReg = /^.+(\.txt)$/ let wordReg = /^.+(\.doc|\.docx)$/ let excelReg = /^.+(\.xls|\.xlsx)$/ let jpgPng = /^.+(\.png)$/ let jpgJpg = /^.+(\.jpg)$/ let jpgJpeg = /^.+(\.jpeg)$/ if (pdfReg.test(url)) { return 'pdf' } if (txtReg.test(url)) { return 'txt' } if (wordReg.test(url)) { return 'docx' } if (excelReg.test(url)) { return 'xls' } if (jpgPng.test(url)) { return 'png' } if (jpgJpg.test(url)) { return 'jpg' } if (jpgJpeg.test(url)) { return 'jpeg' } }, }