nbb-component-ui 1.4.8 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,476 @@
1
+ import dayjs, { OpUnitType, QUnitType } from 'dayjs'
2
+ import utc from 'dayjs/plugin/utc'
3
+ dayjs.extend(utc)
4
+ export const defaultRangeShortcuts = [
5
+ {
6
+ text: '最近七天',
7
+ value: () => {
8
+ const end = new Date()
9
+ const start = new Date()
10
+ start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
11
+ return [start, end]
12
+ }
13
+ },
14
+ {
15
+ text: '最近一个月',
16
+ value: () => {
17
+ const end = new Date()
18
+ const start = new Date(end.getFullYear(), end.getMonth() - 1, 1)
19
+ end.setDate(0)
20
+ return [start, end]
21
+ }
22
+ },
23
+ {
24
+ text: '最近三个月',
25
+ value: () => {
26
+ const end = new Date()
27
+ const start = new Date()
28
+ start.setMonth(start.getMonth() - 3)
29
+ start.setDate(1)
30
+ end.setDate(0)
31
+ return [start, end]
32
+ }
33
+ },
34
+ {
35
+ text: '最近六个月',
36
+ value: () => {
37
+ const start = new Date()
38
+ const end = new Date()
39
+ start.setMonth(start.getMonth() - 6)
40
+ start.setDate(1)
41
+ end.setDate(0)
42
+ return [start, end]
43
+ }
44
+ },
45
+ {
46
+ text: '最近一年',
47
+ value: () => {
48
+ const now = new Date()
49
+ const start = new Date(now.getFullYear() - 1, 0, 1)
50
+ const end = new Date(now.getFullYear() - 1, 12, 0)
51
+ return [start, end]
52
+ }
53
+ },
54
+ {
55
+ text: '本月至今',
56
+ value: () => {
57
+ const start = new Date()
58
+ const end = new Date(start.getFullYear(), start.getMonth(), 1)
59
+ return [end, start]
60
+ }
61
+ },
62
+ {
63
+ text: '本年至今',
64
+ value: () => {
65
+ const end = new Date()
66
+ const start = new Date(new Date().getFullYear(), 0)
67
+ return [start, end]
68
+ }
69
+ }
70
+ ]
71
+ /**
72
+ * 日期快捷选项适用于 el-date-picker
73
+ */
74
+ export const defaultShortcuts = [
75
+ {
76
+ text: '今天',
77
+ value: () => {
78
+ return new Date()
79
+ }
80
+ },
81
+ {
82
+ text: '昨天',
83
+ value: () => {
84
+ const date = new Date()
85
+ date.setTime(date.getTime() - 3600 * 1000 * 24)
86
+ return [date, date]
87
+ }
88
+ },
89
+ {
90
+ text: '最近七天',
91
+ value: () => {
92
+ const date = new Date()
93
+ date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
94
+ return [date, new Date()]
95
+ }
96
+ },
97
+ {
98
+ text: '最近 30 天',
99
+ value: () => {
100
+ const date = new Date()
101
+ date.setTime(date.getTime() - 3600 * 1000 * 24 * 30)
102
+ return [date, new Date()]
103
+ }
104
+ },
105
+ {
106
+ text: '本月',
107
+ value: () => {
108
+ const date = new Date()
109
+ date.setDate(1) // 设置为当前月的第一天
110
+ return [date, new Date()]
111
+ }
112
+ },
113
+ {
114
+ text: '今年',
115
+ value: () => {
116
+ const date = new Date()
117
+ return [new Date(`${date.getFullYear()}-01-01`), date]
118
+ }
119
+ }
120
+ ]
121
+ export function getTimeZone() {
122
+ let offsetMinutes = new Date().getTimezoneOffset()
123
+ const offsetHours = Math.abs(Math.floor(offsetMinutes / 60)) // 将偏差值转换为小时
124
+ const offsetSign = offsetMinutes < 0 ? '+' : '-' // 根据时区偏差的正负号确定东西半球
125
+ offsetMinutes = Math.abs(offsetMinutes % 60) // 获取分钟偏差
126
+
127
+ return (
128
+ offsetSign + offsetHours.toString().padStart(2, '0') + offsetMinutes.toString().padStart(2, '0')
129
+ )
130
+ }
131
+ /**
132
+ * 时间日期转换
133
+ * @param date 当前时间,new Date() 格式
134
+ * @param format 需要转换的时间格式字符串
135
+ * @param timezone 时区,默认获取当前时区
136
+ * @description format 字符串随意,如 `YYYY-mm、YYYY-mm-dd`
137
+ * @description format 季度:"YYYY-mm-dd HH:MM:SS QQQQ"
138
+ * @description format 星期:"YYYY-mm-dd HH:MM:SS WWW"
139
+ * @description format 几周:"YYYY-mm-dd HH:MM:SS ZZZ"
140
+ * @description format 季度 + 星期 + 几周:"YYYY-mm-dd HH:MM:SS WWW QQQQ ZZZ"
141
+ * @returns 返回拼接后的时间字符串
142
+ */
143
+ export function formatDate(date: dayjs.ConfigType, format?: string, timezone?: string): string {
144
+ // 日期不存在,则返回空
145
+ if (!date) {
146
+ return ''
147
+ }
148
+ // 日期存在,则进行格式化
149
+ if (format === undefined) {
150
+ format = 'YYYY-MM-DD HH:mm:ss'
151
+ }
152
+ // 获取当前时区 如:东八区 =>'+0800'
153
+ if (!timezone) {
154
+ timezone = getTimeZone()
155
+ }
156
+ return dayjs.utc(date).utcOffset(timezone).format(format)
157
+ }
158
+
159
+ /**
160
+ * 获取当前的日期+时间
161
+ */
162
+ export function getNowDateTime() {
163
+ return dayjs()
164
+ }
165
+
166
+ /**
167
+ * 获取当前日期是第几周
168
+ * @param dateTime 当前传入的日期值
169
+ * @returns 返回第几周数字值
170
+ */
171
+ export function getWeek(dateTime: Date): number {
172
+ const temptTime = new Date(dateTime.getTime())
173
+ // 周几
174
+ const weekday = temptTime.getDay() || 7
175
+ // 周1+5天=周六
176
+ temptTime.setDate(temptTime.getDate() - weekday + 1 + 5)
177
+ let firstDay = new Date(temptTime.getFullYear(), 0, 1)
178
+ const dayOfWeek = firstDay.getDay()
179
+ let spendDay = 1
180
+ if (dayOfWeek != 0) spendDay = 7 - dayOfWeek + 1
181
+ firstDay = new Date(temptTime.getFullYear(), 0, 1 + spendDay)
182
+ const d = Math.ceil((temptTime.valueOf() - firstDay.valueOf()) / 86400000)
183
+ return Math.ceil(d / 7)
184
+ }
185
+
186
+ /**
187
+ * 将时间转换为 `几秒前`、`几分钟前`、`几小时前`、`几天前`
188
+ * @param param 当前时间,new Date() 格式或者字符串时间格式
189
+ * @param format 需要转换的时间格式字符串
190
+ * @description param 10秒: 10 * 1000
191
+ * @description param 1分: 60 * 1000
192
+ * @description param 1小时: 60 * 60 * 1000
193
+ * @description param 24小时:60 * 60 * 24 * 1000
194
+ * @description param 3天: 60 * 60* 24 * 1000 * 3
195
+ * @returns 返回拼接后的时间字符串
196
+ */
197
+ export function formatPast(param: string | Date, format = 'YYYY-mm-dd HH:MM:SS'): string {
198
+ // 传入格式处理、存储转换值
199
+ let t: any, s: number
200
+ // 获取js 时间戳
201
+ let time: number = new Date().getTime()
202
+ // 是否是对象
203
+ typeof param === 'string' || 'object' ? (t = new Date(param).getTime()) : (t = param)
204
+ // 当前时间戳 - 传入时间戳
205
+ time = Number.parseInt(`${time - t}`)
206
+ if (time < 10000) {
207
+ // 10秒内
208
+ return '刚刚'
209
+ } else if (time < 60000 && time >= 10000) {
210
+ // 超过10秒少于1分钟内
211
+ s = Math.floor(time / 1000)
212
+ return `${s}秒前`
213
+ } else if (time < 3600000 && time >= 60000) {
214
+ // 超过1分钟少于1小时
215
+ s = Math.floor(time / 60000)
216
+ return `${s}分钟前`
217
+ } else if (time < 86400000 && time >= 3600000) {
218
+ // 超过1小时少于24小时
219
+ s = Math.floor(time / 3600000)
220
+ return `${s}小时前`
221
+ } else if (time < 259200000 && time >= 86400000) {
222
+ // 超过1天少于3天内
223
+ s = Math.floor(time / 86400000)
224
+ return `${s}天前`
225
+ } else {
226
+ // 超过3天
227
+ const date = typeof param === 'string' || 'object' ? new Date(param) : param
228
+ return formatDate(date, format)
229
+ }
230
+ }
231
+
232
+ /**
233
+ * 时间问候语
234
+ * @param param 当前时间,new Date() 格式
235
+ * @description param 调用 `formatAxis(new Date())` 输出 `上午好`
236
+ * @returns 返回拼接后的时间字符串
237
+ */
238
+ export function formatAxis(param: Date): string {
239
+ const hour: number = new Date(param).getHours()
240
+ if (hour < 6) return '凌晨好'
241
+ else if (hour < 9) return '早上好'
242
+ else if (hour < 12) return '上午好'
243
+ else if (hour < 14) return '中午好'
244
+ else if (hour < 17) return '下午好'
245
+ else if (hour < 19) return '傍晚好'
246
+ else if (hour < 22) return '晚上好'
247
+ else return '夜里好'
248
+ }
249
+
250
+ /**
251
+ * 将毫秒,转换成时间字符串。例如说,xx 分钟
252
+ *
253
+ * @param ms 毫秒
254
+ * @returns {string} 字符串
255
+ */
256
+ const locale = {
257
+ cn: {
258
+ seconds: '秒',
259
+ minutes: '分钟',
260
+ hours: '小时',
261
+ days: '天'
262
+ },
263
+ en: {
264
+ seconds: 's',
265
+ minutes: 'm',
266
+ hours: 'h',
267
+ days: 'd'
268
+ }
269
+ }
270
+ export function formatPast2(ms: any, lang: 'cn'|'en' = 'cn') {
271
+ const langObj = locale[lang]
272
+ const day = Math.floor(ms / (24 * 60 * 60 * 1000))
273
+ const hour = Math.floor(ms / (60 * 60 * 1000) - day * 24)
274
+ const minute = Math.floor(ms / (60 * 1000) - day * 24 * 60 - hour * 60)
275
+ const second = Math.floor(ms / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60)
276
+ if (day > 0) {
277
+ return day + langObj.days + hour + langObj.hours + minute + langObj.minutes
278
+ }
279
+ if (hour > 0) {
280
+ return hour + langObj.hours + minute + langObj.minutes
281
+ }
282
+ if (minute > 0) {
283
+ return minute + langObj.minutes
284
+ }
285
+ if (second > 0) {
286
+ return second + langObj.seconds
287
+ } else {
288
+ return 0 + langObj.seconds
289
+ }
290
+ }
291
+
292
+ /**
293
+ * element plus 的时间 Formatter 实现,使用 YYYY-MM-DD HH:mm:ss 格式
294
+ *
295
+ * @param row 行数据
296
+ * @param column 字段
297
+ * @param cellValue 字段值
298
+ */
299
+ // @ts-ignore
300
+ export const dateFormatter = (row, column, cellValue) => {
301
+ if (!cellValue) {
302
+ return ''
303
+ }
304
+ return formatDate(cellValue)
305
+ }
306
+
307
+ /**
308
+ * element plus 的时间 Formatter 实现,使用指定格式
309
+ *
310
+ * @param row 行数据
311
+ * @param column 字段
312
+ * @param cellValue 字段值
313
+ */
314
+ // @ts-ignore
315
+ export const dateColFormatter = (row, column, cellValue, format = 'YYYY-MM-DD HH:mm:ss') => {
316
+ if (!cellValue) {
317
+ return ''
318
+ }
319
+ return formatDate(cellValue, format)
320
+ }
321
+
322
+ /**
323
+ * element plus 的时间 Formatter 实现,使用 YYYY-MM-DD 格式
324
+ *
325
+ * @param row 行数据
326
+ * @param column 字段
327
+ * @param cellValue 字段值
328
+ */
329
+ // @ts-ignore
330
+ export const dateFormatter2 = (row, column, cellValue) => {
331
+ if (!cellValue) {
332
+ return
333
+ }
334
+ return formatDate(cellValue, 'YYYY-MM-DD')
335
+ }
336
+
337
+ /**
338
+ * 设置起始日期,时间为00:00:00
339
+ * @param param 传入日期
340
+ * @returns 带时间00:00:00的日期
341
+ */
342
+ export function beginOfDay(param: Date) {
343
+ return new Date(param.getFullYear(), param.getMonth(), param.getDate(), 0, 0, 0)
344
+ }
345
+
346
+ /**
347
+ * 设置结束日期,时间为23:59:59
348
+ * @param param 传入日期
349
+ * @returns 带时间23:59:59的日期
350
+ */
351
+ export function endOfDay(param: Date) {
352
+ return new Date(param.getFullYear(), param.getMonth(), param.getDate(), 23, 59, 59)
353
+ }
354
+
355
+ /**
356
+ * 计算两个日期间隔天数
357
+ * @param param1 日期1
358
+ * @param param2 日期2
359
+ */
360
+ export function betweenDay(param1: Date, param2: Date) {
361
+ param1 = convertDate(param1)
362
+ param2 = convertDate(param2)
363
+ // 计算差值
364
+ return Math.floor((param2.getTime() - param1.getTime()) / (24 * 3600 * 1000))
365
+ }
366
+
367
+ /**
368
+ * 日期计算
369
+ * @param param1 日期
370
+ * @param param2 添加的时间
371
+ */
372
+ export function addTime(param1: Date, param2: number) {
373
+ param1 = convertDate(param1)
374
+ return new Date(param1.getTime() + param2)
375
+ }
376
+
377
+ /**
378
+ * 日期转换
379
+ * @param param 日期
380
+ */
381
+ export function convertDate(param: Date | string) {
382
+ if (typeof param === 'string') {
383
+ return new Date(param)
384
+ }
385
+ return param
386
+ }
387
+
388
+ /**
389
+ * 指定的两个日期, 是否为同一天
390
+ * @param a 日期 A
391
+ * @param b 日期 B
392
+ */
393
+ export function isSameDay(a: dayjs.ConfigType, b: dayjs.ConfigType): boolean {
394
+ if (!a || !b) return false
395
+
396
+ const aa = dayjs(a)
397
+ const bb = dayjs(b)
398
+ return aa.year() == bb.year() && aa.month() == bb.month() && aa.day() == bb.day()
399
+ }
400
+
401
+ /**
402
+ * 获取一天的开始时间、截止时间
403
+ * @param date 日期
404
+ * @param days 天数
405
+ */
406
+ export function getDayRange(
407
+ date: dayjs.ConfigType,
408
+ days: number
409
+ ): [dayjs.ConfigType, dayjs.ConfigType] {
410
+ const day = dayjs(date).add(days, 'd')
411
+ return getDateRange(day, day)
412
+ }
413
+
414
+ /**
415
+ * 获取最近7天的开始时间、截止时间
416
+ */
417
+ export function getLast7Days(): [dayjs.ConfigType, dayjs.ConfigType] {
418
+ const lastWeekDay = dayjs().subtract(7, 'd')
419
+ const yesterday = dayjs().subtract(1, 'd')
420
+ return getDateRange(lastWeekDay, yesterday)
421
+ }
422
+
423
+ /**
424
+ * 获取最近30天的开始时间、截止时间
425
+ */
426
+ export function getLast30Days(): [dayjs.ConfigType, dayjs.ConfigType] {
427
+ const lastMonthDay = dayjs().subtract(30, 'd')
428
+ const yesterday = dayjs().subtract(1, 'd')
429
+ return getDateRange(lastMonthDay, yesterday)
430
+ }
431
+
432
+ /**
433
+ * 获取最近1年的开始时间、截止时间
434
+ */
435
+ export function getLast1Year(): [dayjs.ConfigType, dayjs.ConfigType] {
436
+ const lastYearDay = dayjs().subtract(1, 'y')
437
+ const yesterday = dayjs().subtract(1, 'd')
438
+ return getDateRange(lastYearDay, yesterday)
439
+ }
440
+
441
+ /**
442
+ * 获取指定日期的开始时间、截止时间
443
+ * @param beginDate 开始日期
444
+ * @param endDate 截止日期
445
+ */
446
+ export function getDateRange(
447
+ beginDate: dayjs.ConfigType,
448
+ endDate: dayjs.ConfigType
449
+ ): [dayjs.ConfigType, dayjs.ConfigType] {
450
+ return [dayjs(beginDate).startOf('d'), dayjs(endDate).endOf('d')]
451
+ }
452
+
453
+ /**
454
+ * 禁止选择今天之前的日期
455
+ */
456
+ export const disabledBeforeToday = (time: Date) => {
457
+ const today = new Date()
458
+ today.setHours(0, 0, 0, 0)
459
+ return time.getTime() < today.getTime()
460
+ }
461
+
462
+ /**
463
+ * 计算两个时间差
464
+ * @param startTime 开始时间
465
+ * @param endTime 结束时间
466
+ * @param unit 时差单位
467
+ */
468
+ export const timeBetween = (
469
+ startTime: dayjs.ConfigType,
470
+ endTime: dayjs.ConfigType,
471
+ unit?: QUnitType | OpUnitType
472
+ ) => {
473
+ const start = dayjs(startTime)
474
+ const end = dayjs(endTime)
475
+ return end.diff(start, unit)
476
+ }
package/vite.config.js CHANGED
@@ -30,10 +30,9 @@ export default defineConfig({
30
30
  rollupOptions: {
31
31
  // 【关键】外部化vue和element-plus,不打包进组件库
32
32
  external: [
33
- '@/utils/formatTime',
33
+ // '@/utils/formatTime',
34
34
  'vue',
35
35
  'element-plus',
36
- '@/hooks/web/useMessage'
37
36
  ],
38
37
  output: {
39
38
  // UMD模式下的全局变量名