index.js
6.93 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import Logger from './logger'
import WxData from './wxData'
let systemInfo
export function getSystemInfo() {
if (systemInfo) return systemInfo
systemInfo = wx.getSystemInfoSync()
return systemInfo
}
export function isIos() {
const sys = getSystemInfo()
return /iphone|ios/i.test(sys.platform)
}
class Gesture {
/**
* 左滑
* @param {object} e 事件对象
* @returns {boolean} 布尔值
*/
isLeft(gesture = {}, touche = {}) {
const { startX, startY } = gesture
const deltaX = touche.clientX - startX
const deltaY = touche.clientY - startY
if (deltaX < -60 && deltaY < 20 && deltaY > -20) {
return true
} else {
return false
}
}
/**
* 右滑
* @param {object} e 事件对象
* @returns {boolean} 布尔值
*/
isRight(gesture = {}, touche = {}) {
const { startX, startY } = gesture
const deltaX = touche.clientX - startX
const deltaY = touche.clientY - startY
if (deltaX > 60 && deltaY < 20 && deltaY > -20) {
return true
} else {
return false
}
}
}
class DateUtil {
newDate(year, month, date) {
let cur = `${+year}-${+month}-${+date}`
if (isIos()) {
cur = `${+year}/${+month}/${+date}`
}
return new Date(cur)
}
/**
* 计算指定日期时间戳
* @param {object} date
*/
getTimeStamp(dateInfo) {
if (typeof dateInfo === 'string') {
dateInfo = this.transformDateRow2Dict(dateInfo)
}
if (Object.prototype.toString.call(dateInfo) !== '[object Object]') return
const dateUtil = new DateUtil()
return dateUtil
.newDate(dateInfo.year, dateInfo.month, dateInfo.date)
.getTime()
}
/**
* 计算指定月份共多少天
* @param {number} year 年份
* @param {number} month 月份
*/
getDatesCountOfMonth(year, month) {
return new Date(Date.UTC(year, month, 0)).getUTCDate()
}
/**
* 计算指定月份第一天星期几
* @param {number} year 年份
* @param {number} month 月份
*/
firstDayOfWeek(year, month) {
return new Date(Date.UTC(year, month - 1, 1)).getUTCDay()
}
/**
* 计算指定日期星期几
* @param {number} year 年份
* @param {number} month 月份
* @param {number} date 日期
*/
getDayOfWeek(year, month, date) {
return new Date(Date.UTC(year, month - 1, date)).getUTCDay()
}
formatTimestamp(timestamp) {
const date = new Date(timestamp)
return {
year: date.getFullYear(),
month: date.getMonth() + 1,
date: date.getDate()
}
}
todayTimestamp() {
return new Date().getTime()
}
todayFMD() {
const timestamp = this.todayTimestamp()
return this.formatTimestamp(timestamp)
}
toTimeStr(dateInfo = {}) {
return `${+dateInfo.year}-${+dateInfo.month}-${+dateInfo.date}`
}
transformDateRow2Dict(dateStr) {
if (typeof dateStr === 'string' && dateStr.includes('-')) {
const [year, month, date] = dateStr.split('-')
return this.tranformStr2NumOfDate({
year,
month,
date
})
}
return {}
}
tranformStr2NumOfDate(date = {}) {
const target = { ...date }
// 可能传入字符串
target.year = +target.year
target.month = +target.month
target.date = +target.date
return target
}
sortDatesByTime(dates = [], sortType) {
return dates.sort((a, b) => {
const at = this.getTimeStamp(a)
const bt = this.getTimeStamp(b)
if (at < bt && sortType !== 'desc') {
return -1
} else {
return 1
}
})
}
getPrevMonthInfo(date = {}) {
const prevMonthInfo =
Number(date.month) > 1
? {
year: +date.year,
month: Number(date.month) - 1
}
: {
year: Number(date.year) - 1,
month: 12
}
return prevMonthInfo
}
getNextMonthInfo(date = {}) {
const nextMonthInfo =
Number(date.month) < 12
? {
year: +date.year,
month: Number(date.month) + 1
}
: {
year: Number(date.year) + 1,
month: 1
}
return nextMonthInfo
}
getPrevYearInfo(date = {}) {
return {
year: Number(date.year) - 1,
month: +date.month
}
}
getNextYearInfo(date = {}) {
return {
year: Number(date.year) + 1,
month: +date.month
}
}
findDateIndexInArray(target, dates) {
return dates.findIndex(
item => dateUtil.toTimeStr(item) === dateUtil.toTimeStr(target)
)
}
calcDates(year, month) {
const datesCount = this.getDatesCountOfMonth(year, month)
const dates = []
const today = dateUtil.todayFMD()
for (let i = 1; i <= datesCount; i++) {
const week = dateUtil.getDayOfWeek(+year, +month, i)
const date = {
year: +year,
id: i - 1,
month: +month,
date: i,
week,
isToday:
+today.year === +year && +today.month === +month && i === +today.date
}
dates.push(date)
}
return dates
}
/**
* 日期数组根据日期去重
* @param {array} array 数组
*/
uniqueArrayByDate(array = []) {
let uniqueObject = {}
let uniqueArray = []
array.forEach(item => {
uniqueObject[dateUtil.toTimeStr(item)] = item
})
for (let i in uniqueObject) {
uniqueArray.push(uniqueObject[i])
}
return uniqueArray
}
/**
* 筛选指定年月日期
* @param {object} target 指定年月
* @param {array} dates 待筛选日期
*/
filterDatesByYM(target, dates) {
if (target) {
const { year, month } = target
const _dates = dates.filter(
item => +item.year === +year && +item.month === +month
)
return _dates
}
return dates
}
getWeekHeader(firstDayOfWeek) {
let weeksCh = ['日', '一', '二', '三', '四', '五', '六']
if (firstDayOfWeek === 'Mon') {
weeksCh = ['一', '二', '三', '四', '五', '六', '日']
}
return weeksCh
}
}
/**
* 获取当前页面实例
*/
export function getCurrentPage() {
const pages = getCurrentPages() || []
const last = pages.length - 1
return pages[last] || {}
}
export function getComponentById(componentId) {
const logger = new Logger()
let page = getCurrentPage() || {}
if (page.selectComponent && typeof page.selectComponent === 'function') {
if (componentId) {
return page.selectComponent(componentId)
} else {
logger.warn('请传入组件ID')
}
} else {
logger.warn('该基础库暂不支持多个小程序日历组件')
}
}
export const logger = new Logger()
export const calendarGesture = new Gesture()
export const dateUtil = new DateUtil()
export const getCalendarData = (key, component) =>
new WxData(component).getData(key)
export const setCalendarData = (data, component) =>
new WxData(component).setData(data)
export const getCalendarConfig = component =>
getCalendarData('config', component)
export const setCalendarConfig = (config, component) =>
setCalendarData(
{
config
},
component
)