123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- /**函数防抖
- * @param { function } func
- * @param { number } wait 延迟执行毫秒数
- * @param { boolean } immediate true 表立即执行,false 表非立即执行
- */
- function debounce(func, wait = 100, immediate = false) {
- let timeout
- return function() {
- let context = this
- let args = arguments
- if (timeout) clearTimeout(timeout)
- if (immediate) {
- let callNow = !timeout
- timeout = setTimeout(() => {
- timeout = null
- }, wait)
- if (callNow) func.apply(context, args)
- } else {
- timeout = setTimeout(() => {
- func.apply(context, args)
- }, wait)
- }
- }
- }
- /**函数节流
- * @param { function } func 函数
- * @param { number } wait 延迟执行毫秒数
- * @param { number } type 1 表时间戳版,2 表定时器版
- */
- function throttle(func, wait = 100, type = 2) {
- let previous, timeout
- if (type === 1) {
- previous = 0
- } else if (type === 2) {
- timeout = null
- }
- return function() {
- let context = this
- let args = arguments
- if (type === 1) {
- let now = Date.now()
- if (now - previous > wait) {
- func.apply(context, args)
- previous = now
- }
- } else if (type === 2) {
- if (!timeout) {
- timeout = setTimeout(() => {
- timeout = null
- func.apply(context, args)
- }, wait)
- }
- }
- }
- }
- function forMatNum(num) {
- return num < 10 ? '0' + num : num + '';
- }
- function initDays(year, month) {
- let totalDays = new Date(year, month, 0).getDate();
- let dates = [];
- for (let d = 1; d <= totalDays; d++) {
- dates.push(forMatNum(d));
- };
- return dates;
- }
- function initPicker(start, end, mode = "date", step) {
- let initstartDate = new Date(start);
- let endDate = new Date(end);
- let startYear = initstartDate.getFullYear();
- let startMonth = initstartDate.getMonth();
- let endYear = endDate.getFullYear();
- let years = [],
- months = [],
- days = [],
- hours = [],
- minutes = [],
- seconds = [];
- let totalDays = new Date(startYear, startMonth, 0).getDate();
- for (let s = startYear; s <= endYear; s++) {
- years.push(s + '');
- };
- for (let m = 1; m <= 12; m++) {
- months.push(forMatNum(m));
- };
- for (let d = 1; d <= totalDays; d++) {
- days.push(forMatNum(d));
- }
- for (let h = 0; h < 24; h++) {
- hours.push(forMatNum(h));
- }
- for (let m = 0; m < 60; m += step * 1) {
- minutes.push(forMatNum(m));
- }
- for (let s = 0; s < 60; s++) {
- seconds.push(forMatNum(s));
- }
- switch (mode) {
- case "date":
- return {
- years,
- months,
- days
- }
- break;
- case "yearMonth":
- return {
- years,
- months
- }
- break;
- case "dateTime":
- return {
- years,
- months,
- days,
- hours,
- minutes,
- seconds
- }
- break;
- case "time":
- return {
- hours,
- minutes,
- seconds
- }
- break;
- }
- }
- function getDate(type) {
- const date = new Date();
- let year = date.getFullYear();
- if (type === 'start') {
- year = year - 60;
- } else if (type === 'end') {
- year = year + 2;
- }
- return `${year}`;
- }
- function formatTime(time) {
- if (typeof time !== 'number' || time < 0) {
- return time
- }
- var hour = parseInt(time / 3600)
- time = time % 3600
- var minute = parseInt(time / 60)
- time = time % 60
- var second = time
- return ([hour, minute, second]).map(function(n) {
- n = n.toString()
- return n[1] ? n : '0' + n
- }).join(':')
- }
- function formatLocation(longitude, latitude) {
- if (typeof longitude === 'string' && typeof latitude === 'string') {
- longitude = parseFloat(longitude)
- latitude = parseFloat(latitude)
- }
- longitude = longitude.toFixed(2)
- latitude = latitude.toFixed(2)
- return {
- longitude: longitude.toString().split('.'),
- latitude: latitude.toString().split('.')
- }
- }
- var dateUtils = {
- UNITS: {
- '年': 31557600000,
- '月': 2629800000,
- '天': 86400000,
- '小时': 3600000,
- '分钟': 60000,
- '秒': 1000
- },
- humanize: function(milliseconds) {
- var humanize = '';
- for (var key in this.UNITS) {
- if (milliseconds >= this.UNITS[key]) {
- humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前';
- break;
- }
- }
- return humanize || '刚刚';
- },
- format: function(dateStr) {
- var date = this.parse(dateStr)
- var diff = Date.now() - date.getTime();
- if (diff < this.UNITS['天']) {
- return this.humanize(diff);
- }
- var _format = function(number) {
- return (number < 10 ? ('0' + number) : number);
- };
- return date.getFullYear() + '/' + _format(date.getMonth() + 1) + '/' + _format(date.getDay()) + '-' +
- _format(date.getHours()) + ':' + _format(date.getMinutes());
- },
- parse: function(str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串,转化为一个Date对象
- var a = str.split(/[^0-9]/);
- return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
- }
- };
- function formatDate(value, fmt) {
- let regPos = /^\d+(\.\d+)?$/;
- if (regPos.test(value)) {
- //如果是数字
- let getDate = new Date(value);
- let o = {
- 'M+': getDate.getMonth() + 1,
- 'd+': getDate.getDate(),
- 'h+': getDate.getHours(),
- 'm+': getDate.getMinutes(),
- 's+': getDate.getSeconds(),
- 'q+': Math.floor((getDate.getMonth() + 3) / 3),
- 'S': getDate.getMilliseconds()
- };
- if (/(y+)/.test(fmt)) {
- fmt = fmt.replace(RegExp.$1, (getDate.getFullYear() + '').substr(4 - RegExp.$1.length))
- }
- for (let k in o) {
- if (new RegExp('(' + k + ')').test(fmt)) {
- fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k])
- .length)))
- }
- }
- return fmt;
- } else {
- //TODO
- value = value.trim();
- return value.substr(0, fmt.length);
- }
- }
- //获取当前的平台(终端类型 1 android 2 ios 3 h5 4 微信小程序 5 web)
- function getPlatform() {
- //默认web
- let platform = 5;
- //#ifdef APP-PLUS || APP-NVUE
- const system = uni.getSystemInfoSync();
- if (system.platform === "ios") {
- platform = 1;
- } else {
- platform = 2;
- }
- //#endif
- //#ifdef H5
- platform = 3;
- //#endif
- //#ifdef MP-WEIXIN
- platform = 4;
- //#endif
- return platform;
- };
- //判断浏览器是否是微信浏览器
- function checkIsWechatBrowser() {
- let platform = getPlatform();
- if (platform === 3) {
- return (
- navigator.userAgent.toLowerCase().match(/MicroMessenger/i) ==
- "micromessenger"
- );
- } else {
- return false;
- }
- };
- module.exports = {
- formatTime: formatTime,
- formatLocation: formatLocation,
- dateUtils: dateUtils,
- getDate: getDate,
- initPicker: initPicker,
- forMatNum: forMatNum,
- initDays: initDays,
- getPlatform: getPlatform,
- checkIsWechatBrowser: checkIsWechatBrowser,
- debounce: debounce,
- throttle: throttle,
- formatDate: formatDate
- }
|