import { BrowseRecordAdd } from '@/api/applist.js' import sha256 from 'crypto-js/sha256' //防抖函数 export function debounce(func, fnThis, wait) { var timer; return () => { timer && clearTimeout(timer); timer = setTimeout(() => { func.call(fnThis) timer = null }, wait); }; } //节流函数 export function throttling(func, fnThis, wait) { var timer; return () => { clearTimeout(timer); timer = setTimeout(func.bind(fnThis), wait); }; } let goMiniAppFlag = false //跳转小程序 export function goMiniApp(data, item) { if (goMiniAppFlag) return goMiniAppFlag = true let myData = JSON.parse(data) wx.navigateToMiniProgram({ ...myData, success: (res) => { // 打开成功 goMiniAppFlag = false item && BrowseRecordAdd(item.id) }, fail: () => { goMiniAppFlag = false } }) } //跳转页面 export function goPage(url, type, data) { if (type == 'reLaunch') { uni.reLaunch({ url }); return } if (type == 'redirectTo') { uni.redirectTo({ url }); return } uni.navigateTo({ url, success: function(res) { // 通过eventChannel向被打开页面传送数据 res.eventChannel.emit('passParameters', data) } }); } //接受参数 export function goPageGetData() { return new Promise((res) => { const eventChannel = this.getOpenerEventChannel() eventChannel.on('passParameters', function(data) { res(data) }) }) } //根据类型跳转 export function clickJumpType(item) { if (item.jumpUrlType == 'goMiniApp') { goMiniApp(item.jumpUrl) } if (item.jumpUrlType == 'goPage') { goPage(item.jumpUrl) } if (item.jumpUrlType == 'goWebView') { goPage(`/pages/webview/webview?src=${item.jumpUrl}`) } } function base64_encode(str) { var c1, c2, c3; var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; var i = 0, len = str.length, string = ''; while (i < len) { c1 = str.charCodeAt(i++) & 0xff; if (i == len) { string += base64EncodeChars.charAt(c1 >> 2); string += base64EncodeChars.charAt((c1 & 0x3) << 4); string += "=="; break; } c2 = str.charCodeAt(i++); if (i == len) { string += base64EncodeChars.charAt(c1 >> 2); string += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4)); string += base64EncodeChars.charAt((c2 & 0xF) << 2); string += "="; break; } c3 = str.charCodeAt(i++); string += base64EncodeChars.charAt(c1 >> 2); string += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4)); string += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6)); string += base64EncodeChars.charAt(c3 & 0x3F) } return string } //微信支付调起支付 export function wxPay(data) { wx.requestPayment({ ...data }) } //返回星期几 export function getUTCDay(date) { let UTCDay = new Date(date).getUTCDay() switch (UTCDay) { case 0: return '周天'; case 1: return '周一'; case 2: return '周二'; case 3: return '周三'; case 4: return '周四'; case 5: return '周五'; case 6: return '周六'; } } //返回今天明天后天周几 export function getDay(date) { let UTCDay = new Date(date).getUTCDay() let jetLag = new Date(date).getTime() - new Date().getTime() let dayDif = jetLag / (1000 * 60 * 60 * 24) if (dayDif < 2) { if (dayDif < 0) { return '今天' } if (dayDif < 1) { return '明天' } if (dayDif < 2) { return '后天' } } switch (UTCDay) { case 0: return '周天'; case 1: return '周一'; case 2: return '周二'; case 3: return '周三'; case 4: return '周四'; case 5: return '周五'; case 6: return '周六'; } } export default { goPageGetData, clickJumpType, debounce, throttling, goMiniApp, goPage, wxPay, getUTCDay, getDay }