hy-app 0.5.11 → 0.5.13

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,350 +1,336 @@
1
- // 内部使用方法
2
- import { inject } from 'vue'
3
-
4
- /**
5
- * 生成bem规则类名
6
- * 由于微信小程序,H5,nvue之间绑定class的差异,无法通过:class="[bem()]"的形式进行同用
7
- * 故采用如下折中做法,最后返回的是数组(一般平台)或字符串(支付宝和字节跳动平台),类似['a', 'b', 'c']或'a b c'的形式
8
- * @param {String} name 组件名称
9
- * @param props
10
- * @param {Array} fixed 一直会存在的类名
11
- * @param {Array} change 会根据变量值为true或者false而出现或者隐藏的类名
12
- * @returns {Array|string}
13
- */
14
- export const bem = (
15
- name: string,
16
- props: Record<string, any>,
17
- fixed: string[],
18
- change?: string[]
19
- ): string | string[] => {
20
- // 类名前缀
21
- const prefix = `hy-${name}--`
22
- const classes: Record<string, string | boolean> = {}
23
- if (fixed) {
24
- fixed.map((item: string) => {
25
- // 这里的类名,会一直存在
26
- classes[prefix + props[item]] = true
27
- if (item === 'type' && props['plain']) {
28
- classes[prefix + props[item] + '__plain'] = true
29
- }
30
- })
31
- }
32
- if (change) {
33
- change.map((item: string) => {
34
- // 这里的类名,会根据this[item]的值为true或者false,而进行添加或者移除某一个类
35
- props[item] ? (classes[prefix + item] = props[item]) : delete classes[prefix + item]
36
- })
37
- }
38
- return (
39
- Object.keys(classes)
40
- // 支付宝,头条小程序无法动态绑定一个数组类名,否则解析出来的结果会带有",",而导致失效
41
- // #ifdef MP-ALIPAY || MP-TOUTIAO || MP-LARK
42
- .join(' ')
43
- // #endif
44
- )
45
- }
46
-
47
- /**
48
- * @description 在u-form的子组件内容发生变化,或者失去焦点时,尝试通知u-form执行校验方法
49
- * @param {*} instance
50
- * @param {*} event
51
- */
52
- export function formValidate(event) {
53
- // const formItem = $parent.call(instance, "u-form-item");
54
- const form = inject('uForm')
55
- // 如果发生变化的input或者textarea等,其父组件中有u-form-item或者u-form等,就执行form的validate方法
56
- // 同时将form-item的pros传递给form,让其进行精确对象验证
57
- // if (formItem && form) {
58
- // form.validateField(formItem.prop, () => {}, event);
59
- // }
60
- }
61
-
62
- /**
63
- * @description error提示
64
- * @param {*} err 错误内容
65
- */
66
- export function error(err: string) {
67
- // 开发环境才提示,生产环境不会提示
68
- if (process.env.NODE_ENV === 'development') {
69
- console.error(`华玥组件提示:${err}`)
70
- }
71
- }
72
-
73
- export const sleep = (value = 100) => {
74
- return new Promise((resolve) => {
75
- setTimeout(() => {
76
- resolve(null)
77
- }, value)
78
- })
79
- }
80
-
81
- /**
82
- * @param {Number} len uuid的长度
83
- * @param {Boolean} firstU 将返回的首字母置为"u"
84
- * @param {Nubmer} radix 生成uuid的基数(意味着返回的字符串都是这个基数),2-二进制,8-八进制,10-十进制,16-十六进制
85
- */
86
- export function guid(len = 32, firstU = true, radix = null) {
87
- const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
88
- const uuid = []
89
- radix = radix || chars.length
90
-
91
- if (len) {
92
- // 如果指定uuid长度,只是取随机的字符,0|x为位运算,能去掉x的小数位,返回整数位
93
- for (let i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)]
94
- } else {
95
- let r
96
- // rfc4122标准要求返回的uuid中,某些位为固定的字符
97
- uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'
98
- uuid[14] = '4'
99
-
100
- for (let i = 0; i < 36; i++) {
101
- if (!uuid[i]) {
102
- r = 0 | (Math.random() * 16)
103
- uuid[i] = chars[i == 19 ? (r & 0x3) | 0x8 : r]
104
- }
105
- }
106
- }
107
- // 移除第一个字符,并用u替代,因为第一个字符为数值时,该guuid不能用作id或者class
108
- if (firstU) {
109
- uuid.shift()
110
- return `hy${uuid.join('')}`
111
- }
112
- return uuid.join('')
113
- }
114
-
115
- /**
116
- * @description 获取设备信息
117
- * */
118
- export const getWindowInfo = (): UniNamespace.GetWindowInfoResult => {
119
- let ret: UniNamespace.GetWindowInfoResult
120
- // #ifdef APP || H5
121
- ret = uni.getWindowInfo()
122
- // #endif
123
- // #ifndef APP || H5
124
- ret = uni.getSystemInfoSync()
125
- // #endif
126
- return ret
127
- }
128
-
129
- function pickExclude(obj, keys) {
130
- // 某些情况下,type可能会为
131
- if (!['[object Object]', '[object File]'].includes(Object.prototype.toString.call(obj))) {
132
- return {}
133
- }
134
- return Object.keys(obj).reduce((prev, key) => {
135
- if (!keys.includes(key)) {
136
- prev[key] = obj[key]
137
- }
138
- return prev
139
- }, {})
140
- }
141
-
142
- function formatImage(res) {
143
- return res.tempFiles.map((item) => ({
144
- ...pickExclude(item, ['path']),
145
- type: 'image',
146
- url: item.path,
147
- thumb: item.path,
148
- size: item.size,
149
- // #ifdef H5
150
- name: item.name,
151
- file: item
152
- // #endif
153
- }))
154
- }
155
-
156
- function formatVideo(res) {
157
- return [
158
- {
159
- ...pickExclude(res, ['tempFilePath', 'thumbTempFilePath', 'errMsg']),
160
- type: 'video',
161
- url: res.tempFilePath,
162
- thumb: res.thumbTempFilePath,
163
- size: res.size,
164
- // #ifdef H5
165
- name: res.name,
166
- file: res
167
- // #endif
168
- }
169
- ]
170
- }
171
-
172
- function formatMedia(res) {
173
- return res.tempFiles.map((item) => ({
174
- ...pickExclude(item, ['fileType', 'thumbTempFilePath', 'tempFilePath']),
175
- type: res.type,
176
- url: item.tempFilePath,
177
- thumb: res.type === 'video' ? item.thumbTempFilePath : item.tempFilePath,
178
- size: item.size,
179
- // #ifdef H5
180
- file: item
181
- // #endif
182
- }))
183
- }
184
-
185
- function formatFile(res) {
186
- return res.tempFiles.map((item) => ({
187
- ...pickExclude(item, ['path']),
188
- url: item.path,
189
- size: item.size,
190
- // #ifdef H5
191
- name: item.name,
192
- type: item.type,
193
- file: item
194
- // #endif
195
- }))
196
- }
197
-
198
- export function chooseFile({
199
- accept,
200
- multiple,
201
- capture,
202
- compressed,
203
- maxDuration,
204
- sizeType,
205
- camera,
206
- maxCount,
207
- extension
208
- }: any) {
209
- return new Promise((resolve, reject) => {
210
- switch (accept) {
211
- case 'image':
212
- uni.chooseImage({
213
- count: multiple ? Math.min(maxCount, 9) : 1,
214
- sourceType: capture,
215
- sizeType,
216
- success: (res) => resolve(formatImage(res)),
217
- fail: reject
218
- })
219
- break
220
- // #ifdef MP-WEIXIN
221
- // 只有微信小程序才支持chooseMedia接口
222
- case 'media':
223
- wx.chooseMedia({
224
- count: multiple ? Math.min(maxCount, 9) : 1,
225
- sourceType: capture,
226
- maxDuration,
227
- sizeType,
228
- camera,
229
- success: (res) => resolve(formatMedia(res)),
230
- fail: reject
231
- })
232
- break
233
- // #endif
234
- case 'video':
235
- uni.chooseVideo({
236
- sourceType: capture,
237
- compressed,
238
- maxDuration,
239
- camera,
240
- success: (res) => resolve(formatVideo(res)),
241
- fail: reject
242
- })
243
- break
244
- // #ifdef MP-WEIXIN || H5
245
- // 只有微信小程序才支持chooseMessageFile接口
246
- case 'file':
247
- // #ifdef MP-WEIXIN
248
- wx.chooseMessageFile({
249
- count: multiple ? maxCount : 1,
250
- type: accept,
251
- success: (res) => resolve(formatFile(res)),
252
- fail: reject
253
- })
254
- // #endif
255
- // #ifdef H5
256
- // 需要hx2.9.9以上才支持uni.chooseFile
257
- let params = {
258
- count: multiple ? maxCount : 1,
259
- type: accept,
260
- success: (res) => resolve(formatFile(res)),
261
- fail: reject
262
- }
263
- if (extension.length && extension.length > 0) {
264
- params.extension = extension
265
- }
266
- uni.chooseFile(params)
267
- // #endif
268
- break
269
- // #endif
270
- default:
271
- // 此为保底选项,在accept不为上面任意一项的时候选取全部文件
272
- // #ifdef MP-WEIXIN
273
- wx.chooseMessageFile({
274
- count: multiple ? maxCount : 1,
275
- type: 'all',
276
- success: (res) => resolve(formatFile(res)),
277
- fail: reject
278
- })
279
- // #endif
280
- // #ifdef H5
281
- // 需要hx2.9.9以上才支持uni.chooseFile
282
- let paramsFile = {
283
- count: multiple ? maxCount : 1,
284
- type: 'all',
285
- success: (res) => resolve(formatFile(res)),
286
- fail: reject
287
- }
288
- if (extension.length && extension.length > 0) {
289
- paramsFile.extension = extension
290
- }
291
- uni.chooseFile(paramsFile)
292
- // #endif
293
- }
294
- })
295
- }
296
-
297
- /**
298
- * @description 数字格式化
299
- * @param {number|string} number 要格式化的数字
300
- * @param {number} decimals 保留几位小数
301
- * @param {string} decimalPoint 小数点符号
302
- * @param {string} thousandsSeparator 千分位符号
303
- * @returns {string} 格式化后的数字
304
- */
305
- export function priceFormat(
306
- number: string | number,
307
- decimals = 0,
308
- decimalPoint = '.',
309
- thousandsSeparator = ','
310
- ) {
311
- number = `${number}`.replace(/[^0-9+-Ee.]/g, '')
312
- const n = !isFinite(+number) ? 0 : +number
313
- const prec = !isFinite(+decimals) ? 0 : Math.abs(decimals)
314
- const sep = typeof thousandsSeparator === 'undefined' ? ',' : thousandsSeparator
315
- const dec = typeof decimalPoint === 'undefined' ? '.' : decimalPoint
316
- let s = ''
317
-
318
- s = (prec ? n + '' : `${Math.round(n)}`).split('.')
319
- const re = /(-?\d+)(\d{3})/
320
- while (re.test(s[0])) {
321
- s[0] = s[0].replace(re, `$1${sep}$2`)
322
- }
323
-
324
- if ((s[1] || '').length < prec) {
325
- s[1] = s[1] || ''
326
- s[1] += new Array(prec - s[1].length + 1).join('0')
327
- }
328
- return s.join(dec)
329
- }
330
-
331
- /**
332
- * @description 默认的姓名脱敏规则
333
- * @param name 名字
334
- * @return {string}
335
- * */
336
- export const formatName = (name: string): string => {
337
- let value = ''
338
- if (name.length === 2) {
339
- value = name.substring(0, 1) + '*'
340
- } else if (name.length > 2) {
341
- let char = ''
342
- for (let i = 0, len = name.length - 2; i < len; i++) {
343
- char += '*'
344
- }
345
- value = name.substring(0, 1) + char + name.substring(name.length - 1, name.length)
346
- } else {
347
- value = name
348
- }
349
- return value
350
- }
1
+ /**
2
+ * 生成bem规则类名
3
+ * 由于微信小程序,H5,nvue之间绑定class的差异,无法通过:class="[bem()]"的形式进行同用
4
+ * 故采用如下折中做法,最后返回的是数组(一般平台)或字符串(支付宝和字节跳动平台),类似['a', 'b', 'c']或'a b c'的形式
5
+ * @param {String} name 组件名称
6
+ * @param props
7
+ * @param {Array} fixed 一直会存在的类名
8
+ * @param {Array} change 会根据变量值为true或者false而出现或者隐藏的类名
9
+ * @returns {Array|string}
10
+ */
11
+ export const bem = (
12
+ name: string,
13
+ props: Record<string, any>,
14
+ fixed: string[],
15
+ change?: string[]
16
+ ): string | string[] => {
17
+ // 类名前缀
18
+ const prefix = `hy-${name}--`
19
+ const classes: Record<string, string | boolean> = {}
20
+ if (fixed) {
21
+ fixed.map((item: string) => {
22
+ // 这里的类名,会一直存在
23
+ classes[prefix + props[item]] = true
24
+ if (item === 'type' && props['plain']) {
25
+ classes[prefix + props[item] + '__plain'] = true
26
+ }
27
+ })
28
+ }
29
+ if (change) {
30
+ change.map((item: string) => {
31
+ // 这里的类名,会根据this[item]的值为true或者false,而进行添加或者移除某一个类
32
+ props[item] ? (classes[prefix + item] = props[item]) : delete classes[prefix + item]
33
+ })
34
+ }
35
+ return (
36
+ Object.keys(classes)
37
+ // 支付宝,头条小程序无法动态绑定一个数组类名,否则解析出来的结果会带有",",而导致失效
38
+ // #ifdef MP-ALIPAY || MP-TOUTIAO || MP-LARK
39
+ .join(' ')
40
+ // #endif
41
+ )
42
+ }
43
+
44
+ /**
45
+ * @description error提示
46
+ * @param {*} err 错误内容
47
+ */
48
+ export function error(err: string) {
49
+ // 开发环境才提示,生产环境不会提示
50
+ if (process.env.NODE_ENV === 'development') {
51
+ console.error(`华玥组件提示:${err}`)
52
+ }
53
+ }
54
+
55
+ /**
56
+ * 定时器同步执行,等待时间
57
+ * @param {Number} value 等待时间
58
+ */
59
+ export const sleep = (value: number = 100) => {
60
+ return new Promise((resolve) => {
61
+ setTimeout(() => {
62
+ resolve(null)
63
+ }, value)
64
+ })
65
+ }
66
+
67
+ /**
68
+ * @param {Number} len uuid的长度
69
+ * @param {Boolean} firstU 将返回的首字母置为"hy"
70
+ * @param {Number | Null} radix 生成uuid的基数(意味着返回的字符串都是这个基数),2-二进制,8-八进制,10-十进制,16-十六进制
71
+ */
72
+ export function guid(len: number = 32, firstU: boolean = true, radix: number | null = null) {
73
+ const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
74
+ const uuid = []
75
+ radix = radix || chars.length
76
+
77
+ if (len) {
78
+ // 如果指定uuid长度,只是取随机的字符,0|x为位运算,能去掉x的小数位,返回整数位
79
+ for (let i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)]
80
+ } else {
81
+ let r
82
+ // rfc4122标准要求返回的uuid中,某些位为固定的字符
83
+ uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'
84
+ uuid[14] = '4'
85
+
86
+ for (let i = 0; i < 36; i++) {
87
+ if (!uuid[i]) {
88
+ r = 0 | (Math.random() * 16)
89
+ uuid[i] = chars[i == 19 ? (r & 0x3) | 0x8 : r]
90
+ }
91
+ }
92
+ }
93
+ // 移除第一个字符,并用u替代,因为第一个字符为数值时,该guuid不能用作id或者class
94
+ if (firstU) {
95
+ uuid.shift()
96
+ return `hy${uuid.join('')}`
97
+ }
98
+ return uuid.join('')
99
+ }
100
+
101
+ /**
102
+ * @description 获取设备信息
103
+ * */
104
+ export const getWindowInfo = (): UniNamespace.GetWindowInfoResult => {
105
+ let ret: UniNamespace.GetWindowInfoResult
106
+ // #ifdef APP || H5
107
+ ret = uni.getWindowInfo()
108
+ // #endif
109
+ // #ifndef APP || H5
110
+ ret = uni.getSystemInfoSync()
111
+ // #endif
112
+ return ret
113
+ }
114
+
115
+ function pickExclude(obj, keys) {
116
+ // 某些情况下,type可能会为
117
+ if (!['[object Object]', '[object File]'].includes(Object.prototype.toString.call(obj))) {
118
+ return {}
119
+ }
120
+ return Object.keys(obj).reduce((prev, key) => {
121
+ if (!keys.includes(key)) {
122
+ prev[key] = obj[key]
123
+ }
124
+ return prev
125
+ }, {})
126
+ }
127
+
128
+ function formatImage(res) {
129
+ return res.tempFiles.map((item) => ({
130
+ ...pickExclude(item, ['path']),
131
+ type: 'image',
132
+ url: item.path,
133
+ thumb: item.path,
134
+ size: item.size,
135
+ // #ifdef H5
136
+ name: item.name,
137
+ file: item
138
+ // #endif
139
+ }))
140
+ }
141
+
142
+ function formatVideo(res) {
143
+ return [
144
+ {
145
+ ...pickExclude(res, ['tempFilePath', 'thumbTempFilePath', 'errMsg']),
146
+ type: 'video',
147
+ url: res.tempFilePath,
148
+ thumb: res.thumbTempFilePath,
149
+ size: res.size,
150
+ // #ifdef H5
151
+ name: res.name,
152
+ file: res
153
+ // #endif
154
+ }
155
+ ]
156
+ }
157
+
158
+ function formatMedia(res) {
159
+ return res.tempFiles.map((item) => ({
160
+ ...pickExclude(item, ['fileType', 'thumbTempFilePath', 'tempFilePath']),
161
+ type: res.type,
162
+ url: item.tempFilePath,
163
+ thumb: res.type === 'video' ? item.thumbTempFilePath : item.tempFilePath,
164
+ size: item.size,
165
+ // #ifdef H5
166
+ file: item
167
+ // #endif
168
+ }))
169
+ }
170
+
171
+ function formatFile(res) {
172
+ return res.tempFiles.map((item) => ({
173
+ ...pickExclude(item, ['path']),
174
+ url: item.path,
175
+ size: item.size,
176
+ // #ifdef H5
177
+ name: item.name,
178
+ type: item.type,
179
+ file: item
180
+ // #endif
181
+ }))
182
+ }
183
+
184
+ export function chooseFile({
185
+ accept,
186
+ multiple,
187
+ capture,
188
+ compressed,
189
+ maxDuration,
190
+ sizeType,
191
+ camera,
192
+ maxCount,
193
+ extension
194
+ }: any) {
195
+ return new Promise((resolve, reject) => {
196
+ switch (accept) {
197
+ case 'image':
198
+ uni.chooseImage({
199
+ count: multiple ? Math.min(maxCount, 9) : 1,
200
+ sourceType: capture,
201
+ sizeType,
202
+ success: (res) => resolve(formatImage(res)),
203
+ fail: reject
204
+ })
205
+ break
206
+ // #ifdef MP-WEIXIN
207
+ // 只有微信小程序才支持chooseMedia接口
208
+ case 'media':
209
+ wx.chooseMedia({
210
+ count: multiple ? Math.min(maxCount, 9) : 1,
211
+ sourceType: capture,
212
+ maxDuration,
213
+ sizeType,
214
+ camera,
215
+ success: (res) => resolve(formatMedia(res)),
216
+ fail: reject
217
+ })
218
+ break
219
+ // #endif
220
+ case 'video':
221
+ uni.chooseVideo({
222
+ sourceType: capture,
223
+ compressed,
224
+ maxDuration,
225
+ camera,
226
+ success: (res) => resolve(formatVideo(res)),
227
+ fail: reject
228
+ })
229
+ break
230
+ // #ifdef MP-WEIXIN || H5
231
+ // 只有微信小程序才支持chooseMessageFile接口
232
+ case 'file':
233
+ // #ifdef MP-WEIXIN
234
+ wx.chooseMessageFile({
235
+ count: multiple ? maxCount : 1,
236
+ type: accept,
237
+ success: (res) => resolve(formatFile(res)),
238
+ fail: reject
239
+ })
240
+ // #endif
241
+ // #ifdef H5
242
+ // 需要hx2.9.9以上才支持uni.chooseFile
243
+ let params = {
244
+ count: multiple ? maxCount : 1,
245
+ type: accept,
246
+ success: (res) => resolve(formatFile(res)),
247
+ fail: reject
248
+ }
249
+ if (extension.length && extension.length > 0) {
250
+ params.extension = extension
251
+ }
252
+ uni.chooseFile(params)
253
+ // #endif
254
+ break
255
+ // #endif
256
+ default:
257
+ // 此为保底选项,在accept不为上面任意一项的时候选取全部文件
258
+ // #ifdef MP-WEIXIN
259
+ wx.chooseMessageFile({
260
+ count: multiple ? maxCount : 1,
261
+ type: 'all',
262
+ success: (res) => resolve(formatFile(res)),
263
+ fail: reject
264
+ })
265
+ // #endif
266
+ // #ifdef H5
267
+ // 需要hx2.9.9以上才支持uni.chooseFile
268
+ let paramsFile = {
269
+ count: multiple ? maxCount : 1,
270
+ type: 'all',
271
+ success: (res) => resolve(formatFile(res)),
272
+ fail: reject
273
+ }
274
+ if (extension.length && extension.length > 0) {
275
+ paramsFile.extension = extension
276
+ }
277
+ uni.chooseFile(paramsFile)
278
+ // #endif
279
+ }
280
+ })
281
+ }
282
+
283
+ /**
284
+ * @description 数字格式化
285
+ * @param {number|string} number 要格式化的数字
286
+ * @param {number} decimals 保留几位小数
287
+ * @param {string} decimalPoint 小数点符号
288
+ * @param {string} thousandsSeparator 千分位符号
289
+ * @returns {string} 格式化后的数字
290
+ */
291
+ export function priceFormat(
292
+ number: string | number,
293
+ decimals: number = 0,
294
+ decimalPoint: string = '.',
295
+ thousandsSeparator: string = ','
296
+ ) {
297
+ number = `${number}`.replace(/[^0-9+-Ee.]/g, '')
298
+ const n = !isFinite(+number) ? 0 : +number
299
+ const prec = !isFinite(+decimals) ? 0 : Math.abs(decimals)
300
+ const sep = typeof thousandsSeparator === 'undefined' ? ',' : thousandsSeparator
301
+ const dec = typeof decimalPoint === 'undefined' ? '.' : decimalPoint
302
+ let s = ''
303
+
304
+ s = (prec ? n + '' : `${Math.round(n)}`).split('.')
305
+ const re = /(-?\d+)(\d{3})/
306
+ while (re.test(s[0])) {
307
+ s[0] = s[0].replace(re, `$1${sep}$2`)
308
+ }
309
+
310
+ if ((s[1] || '').length < prec) {
311
+ s[1] = s[1] || ''
312
+ s[1] += new Array(prec - s[1].length + 1).join('0')
313
+ }
314
+ return s.join(dec)
315
+ }
316
+
317
+ /**
318
+ * @description 默认的姓名脱敏规则
319
+ * @param name 名字
320
+ * @return {string}
321
+ * */
322
+ export const formatName = (name: string): string => {
323
+ let value = ''
324
+ if (name.length === 2) {
325
+ value = name.substring(0, 1) + '*'
326
+ } else if (name.length > 2) {
327
+ let char = ''
328
+ for (let i = 0, len = name.length - 2; i < len; i++) {
329
+ char += '*'
330
+ }
331
+ value = name.substring(0, 1) + char + name.substring(name.length - 1, name.length)
332
+ } else {
333
+ value = name
334
+ }
335
+ return value
336
+ }