300 lines
6.6 KiB
JavaScript
300 lines
6.6 KiB
JavaScript
import log from 'log.js'
|
|
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(':')
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取时间戳
|
|
* @param {*}
|
|
*/
|
|
const getTimestamp = () => {
|
|
return new Date().getTime()
|
|
}
|
|
|
|
|
|
const formatNumber = n => {
|
|
n = n.toString()
|
|
return n[1] ? n : '0' + n
|
|
}
|
|
|
|
const padding = (s, len) => {
|
|
len = len - (s + '').length
|
|
for (var i = 0; i < len; i++) {
|
|
s = '0' + s
|
|
}
|
|
return s
|
|
}
|
|
var DEFAULT_PATTERN = 'yyyy-MM-dd'
|
|
var SIGN_REGEXP = /([yMdhsm])(\1*)/g
|
|
|
|
const formatDate = {
|
|
formatYYMMDD: function (time) {
|
|
let date = new Date(time)
|
|
return date.toLocaleDateString().replace(RegExp('/', 'g'), '-') + ' 00:00:00'
|
|
},
|
|
format: function (date, pattern) {
|
|
pattern = pattern || DEFAULT_PATTERN
|
|
return pattern.replace(SIGN_REGEXP, function ($0) {
|
|
switch ($0.charAt(0)) {
|
|
case 'y':
|
|
return padding(date.getFullYear(), $0.length)
|
|
case 'M':
|
|
return padding(date.getMonth() + 1, $0.length)
|
|
case 'd':
|
|
return padding(date.getDate(), $0.length)
|
|
case 'w':
|
|
return date.getDay() + 1
|
|
case 'h':
|
|
return padding(date.getHours(), $0.length)
|
|
case 'm':
|
|
return padding(date.getMinutes(), $0.length)
|
|
case 's':
|
|
return padding(date.getSeconds(), $0.length)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
const getNowTimeFormate = (TYPE) => {
|
|
var date = ''
|
|
switch (TYPE) {
|
|
case 'DAY':
|
|
date = formatDate.format(new Date())
|
|
// return date.slice(0, 4)+'-'+date.slice(4, 6)+'-'+date.slice(6, 8)
|
|
return date
|
|
break;
|
|
case 'MONTH':
|
|
date = formatDate.format(new Date(), 'yyyyMM')
|
|
return date.slice(0, 4) + '-' + date.slice(4, 6)
|
|
break;
|
|
case 'TIME':
|
|
date = formatDate.format(new Date(), 'yyyyMMddhhmmss')
|
|
return date.slice(0, 4) + '-' + date.slice(4, 6) + '-' + date.slice(6, 8) + ' ' + date.slice(8, 10) + ':' + date.slice(10, 12) + ':' + date.slice(12, 14)
|
|
break;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 打开地图
|
|
*/
|
|
const openLocation = function (params) {
|
|
wx.openLocation({
|
|
latitude: params.latitude,
|
|
longitude: params.longitude,
|
|
name: params.name || '',
|
|
address: params.address || '',
|
|
scale: params.scale || 18
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 提示框
|
|
*/
|
|
const $toast = function (message, time = 1500) {
|
|
return new Promise((resolve, reject) => {
|
|
wx.showToast({
|
|
title: message,
|
|
icon: 'none',
|
|
duration: time,
|
|
success: function (res) {
|
|
resolve(res)
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 对话框
|
|
*/
|
|
const $modal = function (title, content, confirmText = '确定', cancelText = '取消') {
|
|
return new Promise((resolve, reject) => {
|
|
wx.showModal({
|
|
title: title,
|
|
content: content,
|
|
confirmText: confirmText,
|
|
cancelText: cancelText,
|
|
success: function (res) {
|
|
if (res.confirm) {
|
|
// 确认按钮点击
|
|
resolve(true)
|
|
} else if (res.cancel) {
|
|
// 取消按钮点击
|
|
resolve(false)
|
|
}
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取状态栏高度
|
|
*/
|
|
const getStatusBarHeight = () => {
|
|
let barHeight = ''
|
|
wx.getSystemInfo({
|
|
success: function (res) {
|
|
barHeight = res.statusBarHeight
|
|
}
|
|
})
|
|
return barHeight
|
|
}
|
|
|
|
/**
|
|
* 获取当前路径
|
|
*/
|
|
|
|
function getCurrentPath() {
|
|
var pages = getCurrentPages() //获取加载的页面
|
|
var currentPage = pages[pages.length - 1] //获取当前页面的对象
|
|
return {
|
|
pages: pages, //获取所有页面
|
|
url: currentPage.route, //当前页面url
|
|
options: currentPage.options //如果要获取url中所带的参数可以查看options
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取路由跳转过来的数据
|
|
*/
|
|
function getRouterData (URL) {
|
|
return wx.getStorageSync('router-/' + URL + '-params')
|
|
}
|
|
|
|
|
|
/**
|
|
* 防止多次重复点击 (函数节流)
|
|
*/
|
|
function throttle(fn, gapTime) {
|
|
if (gapTime == null || gapTime == undefined) {
|
|
gapTime = 1000
|
|
}
|
|
|
|
let _lastTime = null
|
|
|
|
// 返回新的函数
|
|
return (function (e) {
|
|
console.log(this)
|
|
let _nowTime = +new Date()
|
|
if (_nowTime - _lastTime > gapTime || !_lastTime) {
|
|
// fn.apply(this, arguments) //将this和参数传给原函数
|
|
fn(this, e) //上方法不可行的解决办法 改变this和e
|
|
_lastTime = _nowTime
|
|
}
|
|
})()
|
|
}
|
|
|
|
/*函数防抖*/
|
|
function debounce(fn, interval) {
|
|
var timer;
|
|
var gapTime = interval || 200;//间隔时间,如果interval不传,则默认200ms
|
|
return function () {
|
|
clearTimeout(timer);
|
|
var context = this;
|
|
var args = arguments;//保存此处的arguments,因为setTimeout是全局的,arguments不是防抖函数需要的。
|
|
timer = setTimeout(function () {
|
|
fn.call(context, args);
|
|
}, gapTime);
|
|
};
|
|
}
|
|
|
|
//去掉前后空格
|
|
function trim(s){
|
|
return s.toString().replace(/(^\s*)|(\s*$)/g, "");
|
|
}
|
|
|
|
//过滤所以空格
|
|
function filterSpace(s) {
|
|
return s.toString().replace(/\ +/g, "");
|
|
}
|
|
|
|
|
|
function getNumber(str) {
|
|
return str.toString().match(/\d+(\.\d{0,2})?/)[0]
|
|
}
|
|
|
|
function compareVersion(v1, v2) {
|
|
v1 = v1.split('.')
|
|
v2 = v2.split('.')
|
|
const len = Math.max(v1.length, v2.length)
|
|
|
|
while (v1.length < len) {
|
|
v1.push('0')
|
|
}
|
|
while (v2.length < len) {
|
|
v2.push('0')
|
|
}
|
|
|
|
for (let i = 0; i < len; i++) {
|
|
const num1 = parseInt(v1[i])
|
|
const num2 = parseInt(v2[i])
|
|
|
|
if (num1 > num2) {
|
|
return 1
|
|
} else if (num1 < num2) {
|
|
return -1
|
|
}
|
|
}
|
|
return 0
|
|
}
|
|
|
|
/**
|
|
* 内存报警
|
|
* 报警后,使用reLaunch,并记录当前页面及页面栈
|
|
*/
|
|
function memoryWarning(){
|
|
wx.onMemoryWarning(function () {
|
|
let getPath = getCurrentPath()
|
|
wx.reportAnalytics('outofmemory', getPath)
|
|
wx.reportMonitor('0', 500)
|
|
wx.reLaunch(getPath.url)
|
|
app.printErrorClient('filteroutofmemory')
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 取消内存报警
|
|
*/
|
|
function offMemoryWarning(){
|
|
if (wx.canIUse('offMemoryWarning')) {
|
|
wx.offMemoryWarning()
|
|
app.printErrorClient('offmemory')
|
|
} else {
|
|
app.printErrorClient('offmemorylowversion')
|
|
log.warn('当前微信版本'+wx.getSystemInfoSync().SDKVersion+'过低,无法使用该功能,请升级到最新微信版本后重试。')
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
log,
|
|
formatTime,
|
|
openLocation,
|
|
$toast,
|
|
$modal,
|
|
getStatusBarHeight,
|
|
getCurrentPath,
|
|
getRouterData,
|
|
throttle,
|
|
debounce,
|
|
getNowTimeFormate,
|
|
getTimestamp,
|
|
trim,
|
|
filterSpace,
|
|
getNumber,
|
|
memoryWarning,
|
|
offMemoryWarning,
|
|
// printErrorClient
|
|
} |