lxui-uni 0.0.7 → 0.0.9

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.
@@ -1,294 +1,294 @@
1
- /**
2
- * 公共跳转方法
3
- * @author liux
4
- * @date 2023-08-15 14:17
5
- * @param { string } url 跳转路径
6
- * @param { "navigateTo" | "redirectTo" | "reLaunch" | "switchTab" } [mode=navigateTo] 跳转模式
7
- * @param { object } params 跳转传参
8
- * @example
9
- * goToPage({ url: 'pages/index/index', mode: 'navigateTo', params: {'id': 1} })
10
- * @returns { void }
11
- */
12
- type pageMode = 'navigateTo' | 'redirectTo' | 'reLaunch' | 'switchTab'
13
-
14
- interface goToPageInt {
15
- url: string
16
- mode?: pageMode
17
- params?: {
18
- [n: string]: string | number | boolean
19
- }
20
- }
21
-
22
- export const goToPage = ({ url, mode = 'navigateTo', params = {} }: goToPageInt): void => {
23
- if (!url || url.length === 0) {
24
- throw Error('"url" is a required parameter')
25
- }
26
-
27
- const urlEncode = (params: any = {}) => {
28
- const result :string[] = []
29
- for (const k in params) {
30
- if (!params[k]) continue
31
- result.push(k + '=' + params[k])
32
- }
33
-
34
- return result.join('&')
35
- }
36
- // const storage = JSON.parse(uni.getStorageSync('LX_user'))
37
- // const token = storage?.userInfo?.token
38
- // if(!token) {
39
- // url = 'pages/login/loginXcx'
40
- // mode = 'navigateTo'
41
- // }
42
- const queryStr = !isEmpty(params) ? '?' + urlEncode(params) : ''
43
- const obj = { url: `/${url}${queryStr}` }
44
- // console.log('obj', obj)
45
- switch (mode) {
46
- case 'navigateTo':
47
- uni.navigateTo(obj)
48
- break
49
- case 'redirectTo':
50
- uni.redirectTo(obj)
51
- break
52
- case 'reLaunch':
53
- uni.reLaunch(obj)
54
- break
55
- case 'switchTab':
56
- uni.switchTab(obj)
57
- break
58
- default:
59
- throw Error(`${mode} does not exist`)
60
- }
61
- }
62
-
63
- /**
64
- * 判断是否为空对象
65
- * @author liux
66
- * @date 2023-08-15 14:17
67
- * @license MIT
68
- * @param {*} object 源对象
69
- * @returns { boolean }
70
- */
71
- export const isEmptyObject = (object: any): boolean => {
72
- return Object.keys(object).length === 0
73
- }
74
-
75
- /**
76
- * 判断是否为对象
77
- * @author liux
78
- * @date 2023-08-15 14:17
79
- * @license MIT
80
- * @param {*} object 源对象
81
- * @returns { boolean }
82
- */
83
- export const isObject = (object: any): boolean => {
84
- return Object.prototype.toString.call(object) === '[object Object]'
85
- }
86
-
87
- /**
88
- * 判断是否为数组
89
- * @author liux
90
- * @license MIT
91
- * @param {*} object 源对象
92
- * @returns { boolean }
93
- */
94
- export const isArray = (object: any): boolean => {
95
- return Object.prototype.toString.call(object) === '[object Array]'
96
- }
97
-
98
- /**
99
- * 判断是否为空
100
- * @author liux
101
- * @license MIT
102
- * @param {*} value 源对象
103
- * @returns { boolean }
104
- */
105
- export const isEmpty = (value: any): boolean => {
106
- if (isArray(value)) {
107
- return value.length === 0
108
- }
109
- if (isObject(value)) {
110
- return isEmptyObject(value)
111
- }
112
- return !value
113
- }
114
-
115
- /**
116
- * 格式化时间戳(多格式)
117
- * @author liux
118
- * @license MIT
119
- * @param { number } time 长度为 10 | 13 的时间戳
120
- * @param { string } [format=yyyy-MM-dd] format 转换格式
121
- * @example
122
- * formatTime(1691744378556, 'yyyy-MM-dd HH:mm:ss')
123
- * @returns { string }
124
- */
125
-
126
- export const formatTime = (time: number, format: string = 'yyyy-MM-dd HH:mm:ss'): string => {
127
- const len = time.toString().trim().length
128
- if (len !== 10 && len !== 13) {
129
- throw Error('"time" is a error parameter')
130
- }
131
-
132
- time = len !== 13 ? time * 1000 : time
133
-
134
- if (!time) return ''
135
- const date = new Date(time)
136
- const M = (date.getMonth() + 1).toString()
137
- const d = date.getDate().toString()
138
- const H = date.getHours().toString()
139
- const m = date.getMinutes().toString()
140
- const s = date.getSeconds().toString()
141
- const timeObject: {
142
- [n: string]: string
143
- } = {
144
- yyyy: date.getFullYear().toString(),
145
- MM: M.padStart(2, '0'),
146
- dd: d.padStart(2, '0'),
147
- HH: H.padStart(2, '0'),
148
- mm: m.padStart(2, '0'),
149
- ss: s.padStart(2, '0'),
150
- M: M,
151
- d: d,
152
- H: H,
153
- m: m,
154
- s: s
155
- }
156
- const reg = new RegExp(Object.keys(timeObject).join('|'), 'g')
157
- const res = format.replace(reg, (k) => {
158
- return timeObject[k]
159
- })
160
- return res
161
- }
162
-
163
- // 判断当前时间是否在一个时间区间内
164
- export const isTimeIn = (start: string, end: string) => {
165
- // 获取当前时间
166
- const currentTime = new Date()
167
- const startArr = start.split(':').map(Number)
168
- // 设置开始时间为10:00
169
- const startTime = new Date()
170
- startTime.setHours(startArr[0], startArr[1])
171
-
172
- // 设置结束时间为20:00
173
- const endArr = end.split(':').map(Number)
174
- const endTime = new Date()
175
- endTime.setHours(endArr[0], endArr[1])
176
-
177
- // 检查当前时间是否在10:00到20:00之间
178
- if (currentTime >= startTime && currentTime <= endTime) {
179
- // console.log('当前时间在之间')
180
- } else {
181
- // console.log('当前时间不在之间')
182
- }
183
- }
184
-
185
- /**
186
- * 防抖函数
187
- * @author liux
188
- * @license MIT
189
- * @param {function} fn
190
- * @param {umber} [wait=1000] wait
191
- * @returns { void }
192
- */
193
- export const debounce = <T extends (...args: any[]) => any>(fn: T, wait: number = 1000): ((...args: Parameters<T>) => void) => {
194
- let timer: any
195
-
196
- return function (this: any, ...args: Parameters<T>) {
197
- if (timer) clearTimeout(timer)
198
-
199
- timer = setTimeout(() => {
200
- fn.apply(this, args)
201
- }, wait)
202
- }
203
- }
204
-
205
- /**
206
- * 节流函数
207
- * @author liux
208
- * @date 2023-08-15 14:17
209
- * @license MIT
210
- * @param { function } fn
211
- * @param { number } [wait=1000] wait
212
- */
213
- export const throttle = <T extends (...args: any[]) => any>(fn: T, wait: number = 1000) => {
214
- let timer: number = Date.now()
215
- return function (this: any, ...args: Parameters<T>) {
216
- if (Date.now() - timer >= wait) {
217
- fn.apply(this, args)
218
- timer = Date.now()
219
- }
220
- }
221
- }
222
-
223
- /**
224
- * 保存图片到本地
225
- * @author liux
226
- * @param { string } url 需要下载的图片
227
- * @example
228
- * saveImgData('/upload/images/img.png')
229
- * @returns
230
- */
231
- export const saveImgData = debounce((url: string) => {
232
- uni.showLoading({ title: '图片保存中...', mask: true })
233
- // 判断图片地址是否有http
234
- if (url.indexOf('http') === -1) {
235
- url = import.meta.env.VITE_APP_BASE_URL + url
236
- }
237
- uni.downloadFile({
238
- url,
239
- success: (res: any) => {
240
- if (res.statusCode === 200) {
241
- uni.saveImageToPhotosAlbum({
242
- filePath: res.tempFilePath,
243
- success: () => {
244
- uni.showToast({
245
- title: '保存成功~',
246
- icon: 'none',
247
- duration: 2000
248
- })
249
- },
250
- fail: () => {
251
- uni.showToast({
252
- title: '保存失败~',
253
- icon: 'none',
254
- duration: 2000
255
- })
256
- },
257
- complete: () => {
258
- uni.hideLoading()
259
- }
260
- })
261
- }
262
- }
263
- })
264
- })
265
-
266
- /**
267
- * 设置剪贴板
268
- * @author liux
269
- * @param { string } data 需要复制的内容
270
- * @example
271
- * setClipboardData('123456')
272
- * @returns
273
- */
274
- export const setClipboardData = (data: string) => {
275
- uni.setClipboardData({
276
- data: data,
277
- success: () => {
278
- uni.showToast({
279
- title: '复制成功',
280
- icon: 'none',
281
- duration: 2000
282
- })
283
- }
284
- })
285
- }
286
-
287
- export default {
288
- goToPage,
289
- formatTime,
290
- debounce,
291
- throttle,
292
- saveImgData,
293
- setClipboardData
294
- }
1
+ /**
2
+ * 公共跳转方法
3
+ * @author liux
4
+ * @date 2023-08-15 14:17
5
+ * @param { string } url 跳转路径
6
+ * @param { "navigateTo" | "redirectTo" | "reLaunch" | "switchTab" } [mode=navigateTo] 跳转模式
7
+ * @param { object } params 跳转传参
8
+ * @example
9
+ * goToPage({ url: 'pages/index/index', mode: 'navigateTo', params: {'id': 1} })
10
+ * @returns { void }
11
+ */
12
+ type pageMode = 'navigateTo' | 'redirectTo' | 'reLaunch' | 'switchTab'
13
+
14
+ interface goToPageInt {
15
+ url: string
16
+ mode?: pageMode
17
+ params?: {
18
+ [n: string]: string | number | boolean
19
+ }
20
+ }
21
+
22
+ export const goToPage = ({ url, mode = 'navigateTo', params = {} }: goToPageInt): void => {
23
+ if (!url || url.length === 0) {
24
+ throw Error('"url" is a required parameter')
25
+ }
26
+
27
+ const urlEncode = (params: any = {}) => {
28
+ const result :string[] = []
29
+ for (const k in params) {
30
+ if (!params[k]) continue
31
+ result.push(k + '=' + params[k])
32
+ }
33
+
34
+ return result.join('&')
35
+ }
36
+ // const storage = JSON.parse(uni.getStorageSync('LX_user'))
37
+ // const token = storage?.userInfo?.token
38
+ // if(!token) {
39
+ // url = 'pages/login/loginXcx'
40
+ // mode = 'navigateTo'
41
+ // }
42
+ const queryStr = !isEmpty(params) ? '?' + urlEncode(params) : ''
43
+ const obj = { url: `/${url}${queryStr}` }
44
+ // console.log('obj', obj)
45
+ switch (mode) {
46
+ case 'navigateTo':
47
+ uni.navigateTo(obj)
48
+ break
49
+ case 'redirectTo':
50
+ uni.redirectTo(obj)
51
+ break
52
+ case 'reLaunch':
53
+ uni.reLaunch(obj)
54
+ break
55
+ case 'switchTab':
56
+ uni.switchTab(obj)
57
+ break
58
+ default:
59
+ throw Error(`${mode} does not exist`)
60
+ }
61
+ }
62
+
63
+ /**
64
+ * 判断是否为空对象
65
+ * @author liux
66
+ * @date 2023-08-15 14:17
67
+ * @license MIT
68
+ * @param {*} object 源对象
69
+ * @returns { boolean }
70
+ */
71
+ export const isEmptyObject = (object: any): boolean => {
72
+ return Object.keys(object).length === 0
73
+ }
74
+
75
+ /**
76
+ * 判断是否为对象
77
+ * @author liux
78
+ * @date 2023-08-15 14:17
79
+ * @license MIT
80
+ * @param {*} object 源对象
81
+ * @returns { boolean }
82
+ */
83
+ export const isObject = (object: any): boolean => {
84
+ return Object.prototype.toString.call(object) === '[object Object]'
85
+ }
86
+
87
+ /**
88
+ * 判断是否为数组
89
+ * @author liux
90
+ * @license MIT
91
+ * @param {*} object 源对象
92
+ * @returns { boolean }
93
+ */
94
+ export const isArray = (object: any): boolean => {
95
+ return Object.prototype.toString.call(object) === '[object Array]'
96
+ }
97
+
98
+ /**
99
+ * 判断是否为空
100
+ * @author liux
101
+ * @license MIT
102
+ * @param {*} value 源对象
103
+ * @returns { boolean }
104
+ */
105
+ export const isEmpty = (value: any): boolean => {
106
+ if (isArray(value)) {
107
+ return value.length === 0
108
+ }
109
+ if (isObject(value)) {
110
+ return isEmptyObject(value)
111
+ }
112
+ return !value
113
+ }
114
+
115
+ /**
116
+ * 格式化时间戳(多格式)
117
+ * @author liux
118
+ * @license MIT
119
+ * @param { number } time 长度为 10 | 13 的时间戳
120
+ * @param { string } [format=yyyy-MM-dd] format 转换格式
121
+ * @example
122
+ * formatTime(1691744378556, 'yyyy-MM-dd HH:mm:ss')
123
+ * @returns { string }
124
+ */
125
+
126
+ export const formatTime = (time: number, format: string = 'yyyy-MM-dd HH:mm:ss'): string => {
127
+ const len = time.toString().trim().length
128
+ if (len !== 10 && len !== 13) {
129
+ throw Error('"time" is a error parameter')
130
+ }
131
+
132
+ time = len !== 13 ? time * 1000 : time
133
+
134
+ if (!time) return ''
135
+ const date = new Date(time)
136
+ const M = (date.getMonth() + 1).toString()
137
+ const d = date.getDate().toString()
138
+ const H = date.getHours().toString()
139
+ const m = date.getMinutes().toString()
140
+ const s = date.getSeconds().toString()
141
+ const timeObject: {
142
+ [n: string]: string
143
+ } = {
144
+ yyyy: date.getFullYear().toString(),
145
+ MM: M.padStart(2, '0'),
146
+ dd: d.padStart(2, '0'),
147
+ HH: H.padStart(2, '0'),
148
+ mm: m.padStart(2, '0'),
149
+ ss: s.padStart(2, '0'),
150
+ M: M,
151
+ d: d,
152
+ H: H,
153
+ m: m,
154
+ s: s
155
+ }
156
+ const reg = new RegExp(Object.keys(timeObject).join('|'), 'g')
157
+ const res = format.replace(reg, (k) => {
158
+ return timeObject[k]
159
+ })
160
+ return res
161
+ }
162
+
163
+ // 判断当前时间是否在一个时间区间内
164
+ export const isTimeIn = (start: string, end: string) => {
165
+ // 获取当前时间
166
+ const currentTime = new Date()
167
+ const startArr = start.split(':').map(Number)
168
+ // 设置开始时间为10:00
169
+ const startTime = new Date()
170
+ startTime.setHours(startArr[0], startArr[1])
171
+
172
+ // 设置结束时间为20:00
173
+ const endArr = end.split(':').map(Number)
174
+ const endTime = new Date()
175
+ endTime.setHours(endArr[0], endArr[1])
176
+
177
+ // 检查当前时间是否在10:00到20:00之间
178
+ if (currentTime >= startTime && currentTime <= endTime) {
179
+ // console.log('当前时间在之间')
180
+ } else {
181
+ // console.log('当前时间不在之间')
182
+ }
183
+ }
184
+
185
+ /**
186
+ * 防抖函数
187
+ * @author liux
188
+ * @license MIT
189
+ * @param {function} fn
190
+ * @param {umber} [wait=1000] wait
191
+ * @returns { void }
192
+ */
193
+ export const debounce = <T extends (...args: any[]) => any>(fn: T, wait: number = 1000): ((...args: Parameters<T>) => void) => {
194
+ let timer: any
195
+
196
+ return function (this: any, ...args: Parameters<T>) {
197
+ if (timer) clearTimeout(timer)
198
+
199
+ timer = setTimeout(() => {
200
+ fn.apply(this, args)
201
+ }, wait)
202
+ }
203
+ }
204
+
205
+ /**
206
+ * 节流函数
207
+ * @author liux
208
+ * @date 2023-08-15 14:17
209
+ * @license MIT
210
+ * @param { function } fn
211
+ * @param { number } [wait=1000] wait
212
+ */
213
+ export const throttle = <T extends (...args: any[]) => any>(fn: T, wait: number = 1000) => {
214
+ let timer: number = Date.now()
215
+ return function (this: any, ...args: Parameters<T>) {
216
+ if (Date.now() - timer >= wait) {
217
+ fn.apply(this, args)
218
+ timer = Date.now()
219
+ }
220
+ }
221
+ }
222
+
223
+ /**
224
+ * 保存图片到本地
225
+ * @author liux
226
+ * @param { string } url 需要下载的图片
227
+ * @example
228
+ * saveImgData('/upload/images/img.png')
229
+ * @returns
230
+ */
231
+ export const saveImgData = debounce((url: string) => {
232
+ uni.showLoading({ title: '图片保存中...', mask: true })
233
+ // 判断图片地址是否有http
234
+ if (url.indexOf('http') === -1) {
235
+ url = import.meta.env.VITE_APP_BASE_URL + url
236
+ }
237
+ uni.downloadFile({
238
+ url,
239
+ success: (res: any) => {
240
+ if (res.statusCode === 200) {
241
+ uni.saveImageToPhotosAlbum({
242
+ filePath: res.tempFilePath,
243
+ success: () => {
244
+ uni.showToast({
245
+ title: '保存成功~',
246
+ icon: 'none',
247
+ duration: 2000
248
+ })
249
+ },
250
+ fail: () => {
251
+ uni.showToast({
252
+ title: '保存失败~',
253
+ icon: 'none',
254
+ duration: 2000
255
+ })
256
+ },
257
+ complete: () => {
258
+ uni.hideLoading()
259
+ }
260
+ })
261
+ }
262
+ }
263
+ })
264
+ })
265
+
266
+ /**
267
+ * 设置剪贴板
268
+ * @author liux
269
+ * @param { string } data 需要复制的内容
270
+ * @example
271
+ * setClipboardData('123456')
272
+ * @returns
273
+ */
274
+ export const setClipboardData = (data: string) => {
275
+ uni.setClipboardData({
276
+ data: data,
277
+ success: () => {
278
+ uni.showToast({
279
+ title: '复制成功',
280
+ icon: 'none',
281
+ duration: 2000
282
+ })
283
+ }
284
+ })
285
+ }
286
+
287
+ export default {
288
+ goToPage,
289
+ formatTime,
290
+ debounce,
291
+ throttle,
292
+ saveImgData,
293
+ setClipboardData
294
+ }
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
- {
2
- "name": "lxui-uni",
3
- "version": "0.0.7",
4
- "description": "快速构建页面",
5
- "author": "2399270194@qq.com",
6
- "license": "MIT",
7
- "keywords": [
8
- "uview",
9
- "ui",
10
- "uni-app",
11
- "lxui-uni"
12
- ]
13
- }
1
+ {
2
+ "name": "lxui-uni",
3
+ "version": "0.0.9",
4
+ "description": "快速构建页面",
5
+ "author": "2399270194@qq.com",
6
+ "license": "MIT",
7
+ "keywords": [
8
+ "uview",
9
+ "ui",
10
+ "uni-app",
11
+ "lxui-uni"
12
+ ]
13
+ }