const app = getApp() const formatTime = date => { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() const hour = date.getHours() const minute = date.getMinutes() const second = date.getSeconds() return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':') } const formatNumber = n => { n = n.toString() return n[1] ? n : '0' + n } function beforeDay(day, format) { var date = new Date() var formateArr = ['Y', 'M', 'D', 'h', 'm', 's']; var returnArr = []; returnArr.push(date.getFullYear()); returnArr.push(formatNumber(date.getMonth() + 1)); returnArr.push(formatNumber(date.getDate() - day)); returnArr.push(formatNumber(date.getHours())); returnArr.push(formatNumber(date.getMinutes())); returnArr.push(formatNumber(date.getSeconds())); for (var i in returnArr) { format = format.replace(formateArr[i], returnArr[i]); } return format; } function formatDate(date, format) { var formateArr = ['Y', 'M', 'D', 'h', 'm', 's']; var returnArr = []; returnArr.push(date.getFullYear()); returnArr.push(formatNumber(date.getMonth() + 1)); returnArr.push(formatNumber(date.getDate())); returnArr.push(formatNumber(date.getHours())); returnArr.push(formatNumber(date.getMinutes())); returnArr.push(formatNumber(date.getSeconds())); for (var i in returnArr) { format = format.replace(formateArr[i], returnArr[i]); } return format; } function json2Form(json) { var str = []; for (var p in json) { str.push(encodeURIComponent(p) + "=" + encodeURIComponent(json[p])); } return str.join("&"); } function isEmpty(val) { return typeof val === 'undefined' || val === '' || val === null } function showTip(content) { wx.showModal({ content: content, showCancel: false }) } function showToast(content) { if (isEmpty(content)) { return } wx.showToast({ title: content, icon: 'none' }) } function extend(obj) { var o = {}, attr = Array.prototype.slice.call(arguments).slice(1); attr.forEach(function (val, index) { if (val in obj) { o[val] = obj[val]; } }); return o; } function playDing() { const innerAudioContext = wx.createInnerAudioContext(); //新建一个createInnerAudioContext(); innerAudioContext.autoplay = true; //音频自动播放设置 innerAudioContext.src = '/assets/raw/ptt_playfinish.mp3'; //链接到音频的地址 innerAudioContext.onPlay(() => {}); //播放音效 innerAudioContext.onError((res) => { //打印错误 console.log(res.errMsg) //错误信息 console.log(res.errCode) //错误码 }) } function showBackToast(content) { if (isEmpty(content)) { setTimeout(function () { wx.navigateBack() }, 500) return } wx.showToast({ title: content, icon: 'none', duration: 500, success: function () { setTimeout(function () { wx.navigateBack() }, 500) } }) } /* 验证手机号 */ function checkPhone(phone) { var re = /^1\d{10}$/ return re.test(phone) && phone.length === 11 } function checkId(id) { // 1 "验证通过!", 0 //校验不通过 // id为身份证号码 var format = /^(([1][1-5])|([2][1-3])|([3][1-7])|([4][1-6])|([5][0-4])|([6][1-5])|([7][1])|([8][1-2]))\d{4}(([1][9]\d{2})|([2]\d{3}))(([0][1-9])|([1][0-2]))(([0][1-9])|([1-2][0-9])|([3][0-1]))\d{3}[0-9xX]$/; //号码规则校验 if (!format.test(id)) { return { 'status': 0, 'msg': '身份证号码不合规' } } //区位码校验 //出生年月日校验 前正则限制起始年份为1900; var year = id.substr(6, 4), //身份证年 month = id.substr(10, 2), //身份证月 date = id.substr(12, 2), //身份证日 time = Date.parse(month + '-' + date + '-' + year), //身份证日期时间戳date now_time = Date.parse(new Date()), //当前时间戳 dates = (new Date(year, month, 0)).getDate(); //身份证当月天数 if (time > now_time || date > dates) { return { 'status': 0, 'msg': '出生日期不合规' } } //校验码判断 var c = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); //系数 var b = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); //校验码对照表 var id_array = id.split(""); var sum = 0; for (var k = 0; k < 17; k++) { sum += parseInt(id_array[k]) * parseInt(c[k]); } if (id_array[17].toUpperCase() != b[sum % 11].toUpperCase()) { return { 'status': 0, 'msg': '身份证校验码不合规' } } return { 'status': 1, 'msg': '校验通过' } } function combination(arr, m) { let r = []; _([], arr, m); return r; function _(t, a, m) { //t:临时数组 a:目标数组 m:多少个数进行组合 if (m === 0) { r[r.length] = t; //相当于push return; } for (let i = 0; i <= a.length - m; i++) { //从0开始 到n-m let b = t.slice(); //将t赋值给b 不能用=赋值,使用slice会形成新的数组赋值 b.push(a[i]) _(b, a.slice(i + 1), m - 1); } } } const tokenList = [ ] function navigateTo(url){ var white = true for (let index = 0; index < tokenList.length; index++) { if(url.indexOf(tokenList[index]) == -1){ white = false break } } if(white && !app.userInfo){ wx.navigateTo({ url: '/pages/login/index' }) return } wx.navigateTo({ url }) } function navigateTarget(redirectInfo){ if(redirectInfo.targetView == 'showImage'){ navigateTo('/pages/htmls/banner/index?src=' + redirectInfo.params.id) } else if(redirectInfo.targetView == 'proxySellActivity'){ navigateTo('/pages/agent/ability-list/index') } } function substring(string, length){ if(string.length > length){ return string.substring(0, length) + '...' } return string } /*函数节流*/ function throttle(fn, interval) { var enterTime = 0//触发的时间 interval = interval || 300 //间隔时间,如果interval不传,则默认300ms return function() { var backTime = Date.now()//第一次函数return即触发的时间 if (backTime - enterTime > interval) { fn.apply(this, arguments) enterTime = backTime//赋值给第一次触发的时间,这样就保存了第二次触发的时间 } } } module.exports = { isEmpty: isEmpty, extend: extend, formatTime: formatTime, beforeDay: beforeDay, formatDate: formatDate, json2Form: json2Form, showToast: showToast, showTip: showTip, playDing: playDing, showBackToast: showBackToast, checkPhone: checkPhone, checkId: checkId, combination: combination, navigateTo: navigateTo, navigateTarget: navigateTarget, substring: substring, throttle: throttle }