import {buriedPoint,reportBuriedPoint,checkQuan} from './publicAPI.js'; const tools = { timer:'', // 埋点倒计时 daoTime(){ let daoTime = uni.getStorageSync('daoTime') if(daoTime==''){//初次判断倒计时是否为空 uni.setStorageSync('daoTime',60)//设置倒计时 daoTime = uni.getStorageSync('daoTime') this.timer = setInterval(()=>{ uni.setStorageSync('daoTime',daoTime--)//设置倒计时 // console.log('埋点倒计时初次:',daoTime); // console.log('埋点长度初次:',uni.getStorageSync('maiList').length); if(uni.getStorageSync('daoTime')<=0 || uni.getStorageSync('maiList').length==5){ uni.removeStorageSync('daoTime')//清空倒计时 clearInterval(this.timer)//关闭倒计时 // console.log('上/报,埋点'); reportBuriedPoint(uni.getStorageSync('maiList'))//上报事件 uni.removeStorageSync('maiList')//清空上报参数 this.daoTime()//重新倒计时 } },1000) } else {//继续当前倒计时倒计 this.timer = setInterval(()=>{ uni.setStorageSync('daoTime',daoTime--)//设置倒计时 // console.log('埋点倒计时:',daoTime); // console.log('埋点长度:',uni.getStorageSync('maiList').length); if(uni.getStorageSync('daoTime')<=0 || uni.getStorageSync('maiList').length==5){ uni.removeStorageSync('daoTime')//清空倒计时 clearInterval(this.timer)//关闭倒计时 // console.log('上报,埋点'); reportBuriedPoint(uni.getStorageSync('maiList'))//上报事件 uni.removeStorageSync('maiList')//清空上报参数 this.daoTime()//重新倒计时 } },1000) } }, // 关闭埋点倒计时 closeTimer(){ clearInterval(this.timer); console.log('倒计时清空了'); }, // 设置埋点数据事件 maiDian(data){ let maiList = uni.getStorageSync('maiList') // console.log(maiList); if(maiList==''){ maiList = [data] } else maiList.push(data) uni.setStorageSync('maiList',maiList) }, // 种植埋点 plantPoint(num, id=''){ console.log(num,id,67); let maiOjb = { e: num, //内容访问 c: id * 1, t: new Date().getTime() //当前时间戳 } this.maiDian(maiOjb); }, // 立即执行埋点上报 reportImmediately() { reportBuriedPoint(uni.getStorageSync('maiList'))//上报事件 }, // 查券 checkQuan(){ checkQuan(); }, networkStatus(){//检查网络状态 uni.getNetworkType({ success: function (res) { console.log('当前网络状态:',res.networkType);//none:当前无网络连接 if(res.networkType=='none'){ uni.setStorageSync('isNet',false) } else { uni.setStorageSync('isNet',true) } } }); }, buriedPointAll(){//查询埋点类型事件 buriedPoint() }, weekDate(){//获取未来七天星期几,几号 let date = new Date() let year = date.getFullYear() let month = date.getMonth()+1 let day = date.getDate() let nth = date.getDay()//星期几 // console.log(year,month,day); let xingq = ['周一','周二','周三','周四','周五','周六','周日'] }, // 手机号验证 isPhone:function(phone){ // 手机号正则表达式 let reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/; if(!reg_tel.test(phone)){ return true } return false }, // 手机号中间四位用"****"带替 hideMPhone(phone){ return `${phone.substr(0, 3)}****${phone.substr(7)}` }, // 显示中文第一个字,其余的使用"*"代替 hideName(name,num){ return `${name.substr(0, 1)}****${name.substr(name.length-1)}` || '' }, // 整数添加.00,小数就不添加 addXiaoShu(num){ let str = num.toString(); if(str.length > 9){ str = str*1; str = str.toFixed(2); str = str+''; } return str.includes('.') ? str*1 : str = num + '.00'; }, // 时间戳===>日期 timestampToTime(timestamp) { var date = timestamp.toString().length==13 ? new Date(timestamp*1) : new Date(timestamp * 1000);//时间戳为10位需*1000,时间戳为13位的话不需乘1000 var Y = date.getFullYear(); var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1); var D = date.getDate() < 10 ? '0'+date.getDate() : date.getDate(); var h = date.getHours() < 10 ? '0'+date.getHours() : date.getHours(); var m = date.getMinutes() < 10 ? '0'+date.getMinutes() : date.getMinutes(); var s = date.getSeconds() < 10 ? '0'+date.getSeconds() : date.getSeconds(); return Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' +s; }, // 日期===>时间戳 timeToTimestamp(time){ var date = new Date(time); var timestamp = date.getTime();//精确到毫秒 return timestamp // var date = new Date('2014-04-23 18:55:49:123'); // 有三种方式获取 // var time1 = date.getTime();//精确到毫秒 // var time2 = date.valueOf();//精确到毫秒 // var time3 = Date.parse(date);//只能精确到秒,毫秒用000替代 // console.log(time1);//1398250549123 // console.log(time2);//1398250549123 // console.log(time3);//1398250549000 }, // 随机数生成 randomStr(){ var strData = ""; //如果觉得12个数太少也可以多放点,将i<4修改即可 for(var i=0;i<4;i++){ var num = random(0,9); //数字 var upper = String.fromCharCode(random(65,90)); //大写字母 var lower = String.fromCharCode(random(97,122)); //小写字母 strData = strData+num+upper+lower; //将所有结果放进strData中 } var str = ""; for (var i = 0; i < 4; i++) { str += strData[random(0,strData.length-1)]; //在strData里面随机抽取四个数 } return str; }, // 金额输入框验证 checkPrice(number,zong){ let reg = /^[0-9]*$/;//数字正则表达式 let newObj = {} zong = parseInt(zong).toString()//取小数点左边的整数 if(!reg.test(number)){//不是数字时 newObj = { len:zong.length,//动态设置长度 val:zong//动态设置值正整数的总金额 } } else {//是数字时 newObj = { len:zong.length, val:number//动态设置当前输入的值 } if(number*1 > zong*1){//输入的金额大于总金额 newObj.val = zong//赋值总金额 } } return newObj }, // 提示方法 showToast: function(msg, icon,time) { // 弹框图标:none默认无图标、loading、success var newIncon = 'none'; if (icon) {newIncon = icon;} // 弹框显示时间:默认2秒 var newTime = 2000 if (time) {newTime = time;} return uni.showToast({ title: msg, icon: newIncon, duration:newTime }) }, // 格式化时间 formatTime: function(mss) { // let dangTime = Math.round(new Date()/1000)//获取当前时间戳 var hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); var minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60)); var seconds = (mss % (1000 * 60)) / 1000; hours = hours < 10 ? ('0' + hours) : hours; minutes = minutes < 10 ? ('0' + minutes) : minutes; seconds = seconds < 10 && seconds >= 1 ? ('0' + seconds) : seconds; return hours + ' : ' + minutes + ' : ' + seconds; }, // 富文本转换 escape2Html(str) { var arrEntities = { 'lt': '<', 'gt': '>', 'nbsp': ' ', 'amp': '&', 'quot': '"' }; return str.replace(/&(lt|gt|nbsp|amp|quot|src);/ig, function (all, t) { return arrEntities[t]; }) .replace(' { if(res.data.code==0){ res.data.data.v==0 ? uni.setStorageSync('isVedio',false) : uni.setStorageSync('isVedio',true); let appletImg = 'https://oss.hmzfyy.cn' + res.data.data.logo; uni.setStorageSync('appletImg',appletImg); } } }) }, // 判断当前环境 currentContext(){ // #ifdef APP-PLUS if(uni.getSystemInfoSync().platform != "devtools"){//devtools:开发版 值域为:ios、android、mac(3.1.10+)、windows(3.1.10+)、linux(3.1.10+) console.log = () =>{} } // #endif // 微信小程序原生API性能优化 // #ifdef MP-WEIXIN let hInfo = wx.getAccountInfoSync(); // console.log(hInfo.envVersion);//develop:开发版 trial:体验版 release:正式版 // if(hInfo.miniProgram.envVersion == "develop"){ if(hInfo.miniProgram.envVersion == "develop" || hInfo.miniProgram.envVersion == "trial"){ // (开发版,体验版)-配置全局域名 uni.setStorageSync('hostapi','https://hengmei.scdxtc.cn/api/'); // uni.setStorageSync('hostapi','https://hm.hmzfyy.cn/api/'); } else { // 清除所有输出日志 console.log = () =>{}; // 正式版-配置全局域名 uni.setStorageSync('hostapi','https://hm.hmzfyy.cn/api/'); // 开启埋点倒计时 this.daoTime();//开启埋点倒计时 } // #endif }, isLogin(){//是否已经登录 var date = new Date(); var timestamp = date.getTime();//精确到毫秒 // 如果过期时间 减 10分钟 小于当前时间,刷新token if((uni.getStorageSync('expire')*1000 - 6565575) < timestamp && uni.getStorageSync('phone_active')!=0 && uni.getStorageSync('is_active')!=0) { // 调用登录事件 this.loginEv(); } console.log('进入了检测是否登录过期事件'); }, timeToken:null, // 开启每十分钟刷新token refreshToken(){ clearInterval(this.timeToken); this.timeToken = setInterval(()=>{ console.log('进入了每十分钟刷新token'); this.isLogin(); },600000) }, // 登录事件 loginEv(){ uni.login({ provider: 'weixin', success: (res)=> { var params = {code:res.code} uni.request({ url: `${uni.getStorageSync('hostapi')}user/login`, method: 'post', data: params, success: res => { if(res.data.data.token!=''){ uni.setStorageSync('token',res.data.data.token); // 缓存token uni.setStorageSync('openid',res.data.data.openid)//缓存openid uni.setStorageSync('expire',res.data.data.expire); // 缓存失效时间(时间戳格式) uni.setStorageSync('phone_active',res.data.data.phone_active); // 是否绑定手机号 uni.setStorageSync('is_active',res.data.data.is_active)//是否第一次授权 } } }) }, }); }, // 禁止小程序使用分享 disableShareEv(){ wx.hideShareMenu({ menus: ['shareAppMessage', 'shareTimeline'] }) } } export default { tools }