util.js
3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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
}
//获取时间
//获取d当前时间多少天后的日期和对应星期
function getDates(days, todate = new Date()) { //todate默认参数是当前日期,可以传入对应时间
// function getDates(days, todate =getCurrentMonthFirst()) { //todate默认参数是当前日期,可以传入对应时间
var dateArry = [];
for (var i = 0; i < days - 3; i++) {
var dateObj = dateLater(todate, i);
console.log(dateObj)
dateArry.push(dateObj)
}
return dateArry;
}
//传入时间后几天 param:传入时间:dates:"2018-04-02",later:往后多少天
function dateLater(dates, later) {
let dateObj = {};
let show_day = new Array('周日', '周一', '周二', '周三', '周四', '周五', '周六');
let date = new Date(dates);
date.setDate(date.getDate() + later);
let day = date.getDay();
dateObj.year = date.getFullYear();
dateObj.month = date.getMonth() + 1
// dateObj.month = ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth() + 1);
dateObj.day = date.getDate()
// dateObj.day = (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate());
dateObj.week = show_day[day];
return dateObj;
}
//获取页数
function onReachListData(page, pagenum, data, doFunction) {
var that = page;
var tiaoshu = data.length / pagenum;
if (tiaoshu >= 10) {
that.setData({
pageNum: ++pagenum,
});
doFunction();
} else {
wx.showToast({
title: '已加载全部',
icon: 'none'
})
}
}
//获取登录信息
function getUser(msg) {
wx.navigateTo({
url: '/pages/index/index',
})
}
//防抖
function throttle(fn, gapTime) {
if (gapTime == null || gapTime == undefined) {
gapTime = 1500
}
let _lastTime = null;
return function () {
let _nowTime = +new Date()
if (_nowTime - _lastTime > gapTime || !_lastTime) {
fn.apply(this, arguments) //将this和参数传给原函数
_lastTime = _nowTime
}
}
}
// //获取位置信息
// function getLocation(that) {
// wx.getLocation({
// type: 'wgs84',
// success: function (res) {
// // 经纬度
// var latitude = res.latitude
// var longitude = res.longitude
// var aK = that.data.aK
// wx.request({
// url: 'https://api.map.baidu.com/geocoder/v2/?ak=' + aK + '&location=' + latitude + ',' + longitude + '&output=json',
// data: {},
// header: {
// 'content-type': 'application/json'
// },
// success: function (res) {
// var city = res.data.result.addressComponent.city;
// that.setData({
// currentCity: city
// })
// wx.request({
// url: 'xxx' + city,
// data: {},
// header: {
// 'content-type': 'application/json'
// },
// success: function (res) {
// that.setData({
// county: res.data,
// })
// },
// })
// }
// })
// },
// fail: function () {
// wx.showToast({
// title: '授权失败',
// icon: 'success',
// duration: 1000
// })
// }
// })
// }
module.exports = {
formatTime: formatTime,
onReachListData: onReachListData,
getUser: getUser,
getDates: getDates,
throttle: throttle
}