var app = getApp(); var api = require('../config/api.js'); function formatTime(date) { var year = date.getFullYear(); var month = date.getMonth() + 1; var day = date.getDate(); var hour = date.getHours(); var minute = date.getMinutes(); var second = date.getSeconds(); return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':'); } function formetdate(date) { var year = date.getFullYear(); var month = date.getMonth() + 1; var day = date.getDate(); return [year, month, day].map(formatNumber).join('-'); } function formatNumber(n) { n = n.toString(); return n[1] ? n : '0' + n; } /** * 封封微信的的request */ function request(url, data = {}, method = 'GET') { return new Promise(function (resolve, reject) { uni.request({ url: url, data: data, method: method, header: { 'Content-Type': 'application/json', 'X-DOUYIN-Token': uni.getStorageSync('token'), 'app-id':api.appid }, success: function (res) { if (res.statusCode == 200) { if (res.data.errno == 501) { // 清除登录相关内容 try { uni.removeStorageSync('userInfo'); uni.removeStorageSync('token'); uni.removeStorageSync('openId'); } catch (e) { console.log('CatchClause', e); console.log('CatchClause', e); } // Do something when catch error // 切换到登录页面 uni.navigateTo({ url: '/pages/auth/login/login' }); } else { resolve(res.data); } } else { reject(res.errMsg); } }, fail: function (err) { reject(err); } }); }); } function redirect(url) { //判断页面是否需要登录 uni.redirectTo({ url: url }); } function showErrorToast(msg) { uni.showToast({ title: msg, image: '/static/images/icon_error.png' }); } function formatweek(date) { let week = date.getDay(); let weekArr = ''; switch (week) { case 1: weekArr = '周一'; break; case 2: weekArr = '周二'; break; case 3: weekArr = '周三'; break; case 4: weekArr = '周四'; break; case 5: weekArr = '周五'; break; case 6: weekArr = '周六'; break; case 0: weekArr = '周日'; break; } return weekArr; } function getDateList(date, interval, length) { let dateList = []; let len = length ? length : 7; for (let i = 0; i < len; i++) { let datestamp = new Date(date.getTime() + i * 60 * 60 * 1000 * 24 * interval); let dateItem = { week: formatweek(datestamp), date: formetdate(datestamp), id: new Date(datestamp).getDay(), isBook: true, //是否可约 name: i == 0 ? '今天' : i == 1 ? '明天' : '', mark: '' //标注是否不可约的原因之类的 }; //如果当前时间+4大于18点 不返回当日可约时间 if (new Date().getHours() >= 14 && i == 0) { continue; } dateList.push(dateItem); } return dateList; } function dateDifference(end) { // //比较2个日期 // console.log(start + ",," + end); // var stime = new Date(start).getTime(); var stime = new Date().getTime(); console.log(end.replace(/-/g, '/')); var etime = new Date(end.replace(/-/g, '/')).getTime(); var usedTime = etime - stime; //两个时间戳相差的毫秒数 var days = Math.floor(usedTime / 86400000); //计算出小时数 var leave1 = usedTime % 86400000; //计算天数后剩余的毫秒数 var hours = Math.floor(leave1 / 3600000); //计算相差分钟数 var leave2 = leave1 % 3600000; //计算小时数后剩余的毫秒数 var minutes = Math.floor(leave2 / 60000); if (days == 0 && hours == 0) { return minutes + '分钟'; } if (days == 0) { return hours + '时' + minutes + '分钟'; } var time = days + '天' + hours + '时' + minutes + '分钟'; // var time = days; return time; } function getBeginTimeList(time, type, dateName) { let timeList = []; if (time && time != null) { let startTime = 8; let endTime = ''; if (type == 1) { startTime = 8; endTime = 21 - time > 12 ? 12 : 21 - time; } else { startTime = 12; endTime = 21 - time > 12 ? 21 - time : 12; } //控制当天的日期返回:当天可约时间 不能小于12点或当前时间+4小时 过14点 今日全天不可约 if (dateName == '今天') { let date = new Date(); let hour = date.getHours() + 4; if (date.getMinutes() > 30) { hour = hour + 1; } startTime = hour; if (hour > 18 || type == 1) { return []; } else if (hour < 12 && type == 2) { startTime = 12; } } //如果是今天20点后 预约明天 也是不可以约12点前 if (dateName == '明天') { let date = new Date(); if (date.getHours() >= 20 && type == 1) { return []; } } for (let i = startTime; i <= endTime; i++) { if (type == 2 && i == 12) { let halfTime = i < 10 ? '0' + i + ':30' : i + ':30'; timeList.push({ time: halfTime, value: i + 0.5 }); } else if (i < endTime) { let intTime = i < 10 ? '0' + i + ':00' : i + ':00'; let halfTime = i < 10 ? '0' + i + ':30' : i + ':30'; timeList.push({ time: intTime, value: i }); timeList.push({ time: halfTime, value: i + 0.5 }); } else { let intTime = i < 10 ? '0' + i + ':00' : i + ':00'; timeList.push({ time: intTime }); } } } return timeList; } function creadUuid(){ var s = []; var hexDigits = "0123456789abcdef"; for (var i = 0; i < 36; i++) { s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1); } s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010 s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01 s[8] = s[13] = s[18] = s[23] = "-"; let uuid = s.join(""); return uuid; }; // function addLog(inorout,pageId,eventId,parms){ var pages = getCurrentPages(); var prePageUrl=""; if(pages.length>=2){ var prevPage = pages[pages.length - 2];//取上个页面 prePageUrl=prevPage.route;//上个页面路径 } var app = getApp(); let userInfo = uni.getStorageSync('userInfo'); request(api.addLog,{ "uunid":app.globalData.uuid, "userOpenid":app.globalData.openId, "userId":userInfo.id?userInfo.id:null, "inorout":inorout?inorout:1, "pageId":pageId, "prePageUrl":prePageUrl, "eventId":eventId, "parms":parms?JSON.stringify(parms):null, // #ifdef MP-TOUTIAO "ofSys":1, // #endif // #ifdef MP-ALIPAY "ofSys":3, //#endif },'POST').then((res) => { if (res.errno === 0) { console.log('addLog'+res); } }) } module.exports = { formatTime, formetdate, request, redirect, showErrorToast, formatweek, getDateList, dateDifference, getBeginTimeList, creadUuid, addLog };