hy-app 0.2.6 → 0.2.7

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