@hlw-uni/mp-core 1.0.0 → 1.0.2

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/dist/index.mjs CHANGED
@@ -4,7 +4,7 @@ var __publicField = (obj, key, value) => {
4
4
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
5
  return value;
6
6
  };
7
- import { ref } from "vue";
7
+ import { ref, onBeforeUpdate, createSSRApp, defineComponent, computed, useSlots } from "vue";
8
8
  const cosAdapter = {
9
9
  name: "cos",
10
10
  buildFormData(ctx) {
@@ -227,9 +227,124 @@ class HttpClient {
227
227
  }
228
228
  }
229
229
  }
230
- new HttpClient({
230
+ const http = new HttpClient({
231
231
  baseURL: define_import_meta_env_default.VITE_API_BASE_URL ?? ""
232
232
  });
233
+ function useRequest(options = {}) {
234
+ const { initialData = null, manual = false, onSuccess, onError } = options;
235
+ const loading = ref(false);
236
+ const data = ref(initialData);
237
+ const error = ref(null);
238
+ async function run(config) {
239
+ loading.value = true;
240
+ error.value = null;
241
+ try {
242
+ const res = await http.request(config);
243
+ data.value = res.data;
244
+ onSuccess == null ? void 0 : onSuccess(res.data, res);
245
+ return res;
246
+ } catch (e) {
247
+ error.value = e;
248
+ onError == null ? void 0 : onError(e);
249
+ throw e;
250
+ } finally {
251
+ loading.value = false;
252
+ }
253
+ }
254
+ function get(url, data2) {
255
+ return run({ url, method: "GET", data: data2 });
256
+ }
257
+ function post(url, data2) {
258
+ return run({ url, method: "POST", data: data2 });
259
+ }
260
+ function put(url, data2) {
261
+ return run({ url, method: "PUT", data: data2 });
262
+ }
263
+ function del(url, data2) {
264
+ return run({ url, method: "DELETE", data: data2 });
265
+ }
266
+ return { loading, data, error, run, get, post, put, del };
267
+ }
268
+ function useUpload() {
269
+ const uploading = ref(false);
270
+ async function upload(options) {
271
+ uploading.value = true;
272
+ try {
273
+ return await http.upload(options);
274
+ } finally {
275
+ uploading.value = false;
276
+ }
277
+ }
278
+ return { uploading, upload };
279
+ }
280
+ function useLoading() {
281
+ function showLoading(message = "加载中...") {
282
+ uni.showLoading({ title: message, mask: true });
283
+ }
284
+ function hideLoading() {
285
+ uni.hideLoading();
286
+ }
287
+ return {
288
+ showLoading,
289
+ hideLoading
290
+ };
291
+ }
292
+ function useMsg() {
293
+ function toast(opts) {
294
+ const { message, icon = "none", image, duration = 2e3, mask = false, position = "center" } = opts;
295
+ uni.showToast({ title: message, icon, image, duration, mask, position });
296
+ }
297
+ function success(message) {
298
+ uni.showToast({ title: message, icon: "success", duration: 2e3 });
299
+ }
300
+ function error(message) {
301
+ uni.showToast({ title: message, icon: "fail", duration: 2e3 });
302
+ }
303
+ function fail(message) {
304
+ uni.showToast({ title: message, icon: "fail", duration: 2e3 });
305
+ }
306
+ function showLoading(message = "加载中...") {
307
+ uni.showLoading({ title: message, mask: true });
308
+ }
309
+ function hideLoading() {
310
+ uni.hideLoading();
311
+ }
312
+ function confirm(opts) {
313
+ return new Promise((resolve) => {
314
+ const { title = "提示", content, confirmText = "确定", cancelText = "取消", confirmColor = "#3b82f6", cancelColor = "#999999" } = opts;
315
+ uni.showModal({
316
+ title,
317
+ content,
318
+ confirmText,
319
+ cancelText,
320
+ confirmColor,
321
+ cancelColor,
322
+ success: (res) => resolve(res.confirm),
323
+ fail: () => resolve(false)
324
+ });
325
+ });
326
+ }
327
+ function modal(opts) {
328
+ return confirm(opts);
329
+ }
330
+ function setLoadingBar(progress) {
331
+ const clamped = Math.max(0, Math.min(100, progress));
332
+ uni.setNavigationBarTitle({
333
+ title: `${"█".repeat(Math.round(clamped / 2))}${"░".repeat(50 - Math.round(clamped / 2))} ${clamped}%`
334
+ });
335
+ }
336
+ return {
337
+ toast,
338
+ success,
339
+ error,
340
+ fail,
341
+ showLoading,
342
+ hideLoading,
343
+ confirm,
344
+ modal,
345
+ setLoadingBar
346
+ };
347
+ }
233
348
  const _info = ref(null);
234
349
  function collect() {
235
350
  var _a;
@@ -306,4 +421,661 @@ function useDevice() {
306
421
  ensure();
307
422
  return _info;
308
423
  }
309
- useDevice();
424
+ function deviceToQuery() {
425
+ ensure();
426
+ if (!_info.value)
427
+ return "";
428
+ return Object.entries(_info.value).filter(([, v]) => v !== "" && v !== 0).map(([k, v]) => `${k}=${encodeURIComponent(String(v))}`).join("&");
429
+ }
430
+ function clearDeviceCache() {
431
+ _info.value = null;
432
+ }
433
+ function useRefs() {
434
+ const refs = ref({});
435
+ onBeforeUpdate(() => {
436
+ refs.value = {};
437
+ });
438
+ const setRefs = (key) => (el) => {
439
+ if (el)
440
+ refs.value[key] = el;
441
+ };
442
+ return { refs, setRefs };
443
+ }
444
+ function usePageMeta() {
445
+ function setTitle(title) {
446
+ uni.setNavigationBarTitle({ title });
447
+ }
448
+ function setOptions(options) {
449
+ if (options.title)
450
+ setTitle(options.title);
451
+ if (options.enablePullDownRefresh !== void 0) {
452
+ uni.setBackgroundTextStyle({ textStyle: "dark" });
453
+ }
454
+ }
455
+ return {
456
+ setTitle,
457
+ setOptions
458
+ };
459
+ }
460
+ function useStorage() {
461
+ function get(key) {
462
+ try {
463
+ const value = uni.getStorageSync(key);
464
+ return value ?? null;
465
+ } catch {
466
+ return null;
467
+ }
468
+ }
469
+ function set(key, value) {
470
+ try {
471
+ uni.setStorageSync(key, value);
472
+ return true;
473
+ } catch {
474
+ return false;
475
+ }
476
+ }
477
+ function remove(key) {
478
+ try {
479
+ uni.removeStorageSync(key);
480
+ return true;
481
+ } catch {
482
+ return false;
483
+ }
484
+ }
485
+ function clear() {
486
+ try {
487
+ uni.clearStorageSync();
488
+ return true;
489
+ } catch {
490
+ return false;
491
+ }
492
+ }
493
+ function info() {
494
+ try {
495
+ return uni.getStorageInfoSync();
496
+ } catch {
497
+ return null;
498
+ }
499
+ }
500
+ return { get, set, remove, clear, info };
501
+ }
502
+ function useValidate() {
503
+ function phone(value) {
504
+ return /^1[3-9]\d{9}$/.test(value);
505
+ }
506
+ function email(value) {
507
+ return /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(value);
508
+ }
509
+ function url(value) {
510
+ try {
511
+ new URL(value);
512
+ return true;
513
+ } catch {
514
+ return false;
515
+ }
516
+ }
517
+ function idCard(value) {
518
+ return /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$/.test(value);
519
+ }
520
+ function carNumber(value) {
521
+ return /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-Z0-9]{5}$/.test(value);
522
+ }
523
+ function password(value) {
524
+ return /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/.test(value);
525
+ }
526
+ function empty(value) {
527
+ if (value == null)
528
+ return true;
529
+ if (typeof value === "string")
530
+ return value.trim() === "";
531
+ if (Array.isArray(value))
532
+ return value.length === 0;
533
+ if (typeof value === "object")
534
+ return Object.keys(value).length === 0;
535
+ return false;
536
+ }
537
+ return {
538
+ phone,
539
+ email,
540
+ url,
541
+ idCard,
542
+ carNumber,
543
+ password,
544
+ empty
545
+ };
546
+ }
547
+ function useFormat() {
548
+ function date(date2, format = "YYYY-MM-DD HH:mm:ss") {
549
+ const d = new Date(date2);
550
+ if (isNaN(d.getTime()))
551
+ return "";
552
+ const year = d.getFullYear();
553
+ const month = String(d.getMonth() + 1).padStart(2, "0");
554
+ const day = String(d.getDate()).padStart(2, "0");
555
+ const hours = String(d.getHours()).padStart(2, "0");
556
+ const minutes = String(d.getMinutes()).padStart(2, "0");
557
+ const seconds = String(d.getSeconds()).padStart(2, "0");
558
+ return format.replace("YYYY", String(year)).replace("MM", month).replace("DD", day).replace("HH", hours).replace("mm", minutes).replace("ss", seconds);
559
+ }
560
+ function fileSize(bytes) {
561
+ if (bytes === 0)
562
+ return "0 B";
563
+ const k = 1024;
564
+ const sizes = ["B", "KB", "MB", "GB", "TB"];
565
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
566
+ return `${parseFloat((bytes / Math.pow(k, i)).toFixed(2))} ${sizes[i]}`;
567
+ }
568
+ function phone(value) {
569
+ return value.replace(/(\d{3})\d{4}(\d{4})/, "$1****$2");
570
+ }
571
+ function money(amount, decimals = 2, decPoint = ".", thousandsSep = ",") {
572
+ return amount.toFixed(decimals).replace(/\B(?=(\d{3})+(?!\d))/g, thousandsSep);
573
+ }
574
+ return { date, fileSize, phone, money };
575
+ }
576
+ const _msg = useMsg();
577
+ const _device = useDevice();
578
+ const hlw = {
579
+ $msg: _msg,
580
+ $device: _device,
581
+ $http: http
582
+ };
583
+ function getDefaultExportFromCjs(x) {
584
+ return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, "default") ? x["default"] : x;
585
+ }
586
+ var md5$1 = { exports: {} };
587
+ var crypt = { exports: {} };
588
+ (function() {
589
+ var base64map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", crypt$1 = {
590
+ // Bit-wise rotation left
591
+ rotl: function(n, b) {
592
+ return n << b | n >>> 32 - b;
593
+ },
594
+ // Bit-wise rotation right
595
+ rotr: function(n, b) {
596
+ return n << 32 - b | n >>> b;
597
+ },
598
+ // Swap big-endian to little-endian and vice versa
599
+ endian: function(n) {
600
+ if (n.constructor == Number) {
601
+ return crypt$1.rotl(n, 8) & 16711935 | crypt$1.rotl(n, 24) & 4278255360;
602
+ }
603
+ for (var i = 0; i < n.length; i++)
604
+ n[i] = crypt$1.endian(n[i]);
605
+ return n;
606
+ },
607
+ // Generate an array of any length of random bytes
608
+ randomBytes: function(n) {
609
+ for (var bytes = []; n > 0; n--)
610
+ bytes.push(Math.floor(Math.random() * 256));
611
+ return bytes;
612
+ },
613
+ // Convert a byte array to big-endian 32-bit words
614
+ bytesToWords: function(bytes) {
615
+ for (var words = [], i = 0, b = 0; i < bytes.length; i++, b += 8)
616
+ words[b >>> 5] |= bytes[i] << 24 - b % 32;
617
+ return words;
618
+ },
619
+ // Convert big-endian 32-bit words to a byte array
620
+ wordsToBytes: function(words) {
621
+ for (var bytes = [], b = 0; b < words.length * 32; b += 8)
622
+ bytes.push(words[b >>> 5] >>> 24 - b % 32 & 255);
623
+ return bytes;
624
+ },
625
+ // Convert a byte array to a hex string
626
+ bytesToHex: function(bytes) {
627
+ for (var hex = [], i = 0; i < bytes.length; i++) {
628
+ hex.push((bytes[i] >>> 4).toString(16));
629
+ hex.push((bytes[i] & 15).toString(16));
630
+ }
631
+ return hex.join("");
632
+ },
633
+ // Convert a hex string to a byte array
634
+ hexToBytes: function(hex) {
635
+ for (var bytes = [], c = 0; c < hex.length; c += 2)
636
+ bytes.push(parseInt(hex.substr(c, 2), 16));
637
+ return bytes;
638
+ },
639
+ // Convert a byte array to a base-64 string
640
+ bytesToBase64: function(bytes) {
641
+ for (var base64 = [], i = 0; i < bytes.length; i += 3) {
642
+ var triplet = bytes[i] << 16 | bytes[i + 1] << 8 | bytes[i + 2];
643
+ for (var j = 0; j < 4; j++)
644
+ if (i * 8 + j * 6 <= bytes.length * 8)
645
+ base64.push(base64map.charAt(triplet >>> 6 * (3 - j) & 63));
646
+ else
647
+ base64.push("=");
648
+ }
649
+ return base64.join("");
650
+ },
651
+ // Convert a base-64 string to a byte array
652
+ base64ToBytes: function(base64) {
653
+ base64 = base64.replace(/[^A-Z0-9+\/]/ig, "");
654
+ for (var bytes = [], i = 0, imod4 = 0; i < base64.length; imod4 = ++i % 4) {
655
+ if (imod4 == 0)
656
+ continue;
657
+ bytes.push((base64map.indexOf(base64.charAt(i - 1)) & Math.pow(2, -2 * imod4 + 8) - 1) << imod4 * 2 | base64map.indexOf(base64.charAt(i)) >>> 6 - imod4 * 2);
658
+ }
659
+ return bytes;
660
+ }
661
+ };
662
+ crypt.exports = crypt$1;
663
+ })();
664
+ var cryptExports = crypt.exports;
665
+ var charenc = {
666
+ // UTF-8 encoding
667
+ utf8: {
668
+ // Convert a string to a byte array
669
+ stringToBytes: function(str) {
670
+ return charenc.bin.stringToBytes(unescape(encodeURIComponent(str)));
671
+ },
672
+ // Convert a byte array to a string
673
+ bytesToString: function(bytes) {
674
+ return decodeURIComponent(escape(charenc.bin.bytesToString(bytes)));
675
+ }
676
+ },
677
+ // Binary encoding
678
+ bin: {
679
+ // Convert a string to a byte array
680
+ stringToBytes: function(str) {
681
+ for (var bytes = [], i = 0; i < str.length; i++)
682
+ bytes.push(str.charCodeAt(i) & 255);
683
+ return bytes;
684
+ },
685
+ // Convert a byte array to a string
686
+ bytesToString: function(bytes) {
687
+ for (var str = [], i = 0; i < bytes.length; i++)
688
+ str.push(String.fromCharCode(bytes[i]));
689
+ return str.join("");
690
+ }
691
+ }
692
+ };
693
+ var charenc_1 = charenc;
694
+ /*!
695
+ * Determine if an object is a Buffer
696
+ *
697
+ * @author Feross Aboukhadijeh <https://feross.org>
698
+ * @license MIT
699
+ */
700
+ var isBuffer_1 = function(obj) {
701
+ return obj != null && (isBuffer(obj) || isSlowBuffer(obj) || !!obj._isBuffer);
702
+ };
703
+ function isBuffer(obj) {
704
+ return !!obj.constructor && typeof obj.constructor.isBuffer === "function" && obj.constructor.isBuffer(obj);
705
+ }
706
+ function isSlowBuffer(obj) {
707
+ return typeof obj.readFloatLE === "function" && typeof obj.slice === "function" && isBuffer(obj.slice(0, 0));
708
+ }
709
+ (function() {
710
+ var crypt2 = cryptExports, utf8 = charenc_1.utf8, isBuffer2 = isBuffer_1, bin = charenc_1.bin, md52 = function(message, options) {
711
+ if (message.constructor == String)
712
+ if (options && options.encoding === "binary")
713
+ message = bin.stringToBytes(message);
714
+ else
715
+ message = utf8.stringToBytes(message);
716
+ else if (isBuffer2(message))
717
+ message = Array.prototype.slice.call(message, 0);
718
+ else if (!Array.isArray(message) && message.constructor !== Uint8Array)
719
+ message = message.toString();
720
+ var m = crypt2.bytesToWords(message), l = message.length * 8, a = 1732584193, b = -271733879, c = -1732584194, d = 271733878;
721
+ for (var i = 0; i < m.length; i++) {
722
+ m[i] = (m[i] << 8 | m[i] >>> 24) & 16711935 | (m[i] << 24 | m[i] >>> 8) & 4278255360;
723
+ }
724
+ m[l >>> 5] |= 128 << l % 32;
725
+ m[(l + 64 >>> 9 << 4) + 14] = l;
726
+ var FF = md52._ff, GG = md52._gg, HH = md52._hh, II = md52._ii;
727
+ for (var i = 0; i < m.length; i += 16) {
728
+ var aa = a, bb = b, cc = c, dd = d;
729
+ a = FF(a, b, c, d, m[i + 0], 7, -680876936);
730
+ d = FF(d, a, b, c, m[i + 1], 12, -389564586);
731
+ c = FF(c, d, a, b, m[i + 2], 17, 606105819);
732
+ b = FF(b, c, d, a, m[i + 3], 22, -1044525330);
733
+ a = FF(a, b, c, d, m[i + 4], 7, -176418897);
734
+ d = FF(d, a, b, c, m[i + 5], 12, 1200080426);
735
+ c = FF(c, d, a, b, m[i + 6], 17, -1473231341);
736
+ b = FF(b, c, d, a, m[i + 7], 22, -45705983);
737
+ a = FF(a, b, c, d, m[i + 8], 7, 1770035416);
738
+ d = FF(d, a, b, c, m[i + 9], 12, -1958414417);
739
+ c = FF(c, d, a, b, m[i + 10], 17, -42063);
740
+ b = FF(b, c, d, a, m[i + 11], 22, -1990404162);
741
+ a = FF(a, b, c, d, m[i + 12], 7, 1804603682);
742
+ d = FF(d, a, b, c, m[i + 13], 12, -40341101);
743
+ c = FF(c, d, a, b, m[i + 14], 17, -1502002290);
744
+ b = FF(b, c, d, a, m[i + 15], 22, 1236535329);
745
+ a = GG(a, b, c, d, m[i + 1], 5, -165796510);
746
+ d = GG(d, a, b, c, m[i + 6], 9, -1069501632);
747
+ c = GG(c, d, a, b, m[i + 11], 14, 643717713);
748
+ b = GG(b, c, d, a, m[i + 0], 20, -373897302);
749
+ a = GG(a, b, c, d, m[i + 5], 5, -701558691);
750
+ d = GG(d, a, b, c, m[i + 10], 9, 38016083);
751
+ c = GG(c, d, a, b, m[i + 15], 14, -660478335);
752
+ b = GG(b, c, d, a, m[i + 4], 20, -405537848);
753
+ a = GG(a, b, c, d, m[i + 9], 5, 568446438);
754
+ d = GG(d, a, b, c, m[i + 14], 9, -1019803690);
755
+ c = GG(c, d, a, b, m[i + 3], 14, -187363961);
756
+ b = GG(b, c, d, a, m[i + 8], 20, 1163531501);
757
+ a = GG(a, b, c, d, m[i + 13], 5, -1444681467);
758
+ d = GG(d, a, b, c, m[i + 2], 9, -51403784);
759
+ c = GG(c, d, a, b, m[i + 7], 14, 1735328473);
760
+ b = GG(b, c, d, a, m[i + 12], 20, -1926607734);
761
+ a = HH(a, b, c, d, m[i + 5], 4, -378558);
762
+ d = HH(d, a, b, c, m[i + 8], 11, -2022574463);
763
+ c = HH(c, d, a, b, m[i + 11], 16, 1839030562);
764
+ b = HH(b, c, d, a, m[i + 14], 23, -35309556);
765
+ a = HH(a, b, c, d, m[i + 1], 4, -1530992060);
766
+ d = HH(d, a, b, c, m[i + 4], 11, 1272893353);
767
+ c = HH(c, d, a, b, m[i + 7], 16, -155497632);
768
+ b = HH(b, c, d, a, m[i + 10], 23, -1094730640);
769
+ a = HH(a, b, c, d, m[i + 13], 4, 681279174);
770
+ d = HH(d, a, b, c, m[i + 0], 11, -358537222);
771
+ c = HH(c, d, a, b, m[i + 3], 16, -722521979);
772
+ b = HH(b, c, d, a, m[i + 6], 23, 76029189);
773
+ a = HH(a, b, c, d, m[i + 9], 4, -640364487);
774
+ d = HH(d, a, b, c, m[i + 12], 11, -421815835);
775
+ c = HH(c, d, a, b, m[i + 15], 16, 530742520);
776
+ b = HH(b, c, d, a, m[i + 2], 23, -995338651);
777
+ a = II(a, b, c, d, m[i + 0], 6, -198630844);
778
+ d = II(d, a, b, c, m[i + 7], 10, 1126891415);
779
+ c = II(c, d, a, b, m[i + 14], 15, -1416354905);
780
+ b = II(b, c, d, a, m[i + 5], 21, -57434055);
781
+ a = II(a, b, c, d, m[i + 12], 6, 1700485571);
782
+ d = II(d, a, b, c, m[i + 3], 10, -1894986606);
783
+ c = II(c, d, a, b, m[i + 10], 15, -1051523);
784
+ b = II(b, c, d, a, m[i + 1], 21, -2054922799);
785
+ a = II(a, b, c, d, m[i + 8], 6, 1873313359);
786
+ d = II(d, a, b, c, m[i + 15], 10, -30611744);
787
+ c = II(c, d, a, b, m[i + 6], 15, -1560198380);
788
+ b = II(b, c, d, a, m[i + 13], 21, 1309151649);
789
+ a = II(a, b, c, d, m[i + 4], 6, -145523070);
790
+ d = II(d, a, b, c, m[i + 11], 10, -1120210379);
791
+ c = II(c, d, a, b, m[i + 2], 15, 718787259);
792
+ b = II(b, c, d, a, m[i + 9], 21, -343485551);
793
+ a = a + aa >>> 0;
794
+ b = b + bb >>> 0;
795
+ c = c + cc >>> 0;
796
+ d = d + dd >>> 0;
797
+ }
798
+ return crypt2.endian([a, b, c, d]);
799
+ };
800
+ md52._ff = function(a, b, c, d, x, s, t) {
801
+ var n = a + (b & c | ~b & d) + (x >>> 0) + t;
802
+ return (n << s | n >>> 32 - s) + b;
803
+ };
804
+ md52._gg = function(a, b, c, d, x, s, t) {
805
+ var n = a + (b & d | c & ~d) + (x >>> 0) + t;
806
+ return (n << s | n >>> 32 - s) + b;
807
+ };
808
+ md52._hh = function(a, b, c, d, x, s, t) {
809
+ var n = a + (b ^ c ^ d) + (x >>> 0) + t;
810
+ return (n << s | n >>> 32 - s) + b;
811
+ };
812
+ md52._ii = function(a, b, c, d, x, s, t) {
813
+ var n = a + (c ^ (b | ~d)) + (x >>> 0) + t;
814
+ return (n << s | n >>> 32 - s) + b;
815
+ };
816
+ md52._blocksize = 16;
817
+ md52._digestsize = 16;
818
+ md5$1.exports = function(message, options) {
819
+ if (message === void 0 || message === null)
820
+ throw new Error("Illegal argument " + message);
821
+ var digestbytes = crypt2.wordsToBytes(md52(message, options));
822
+ return options && options.asBytes ? digestbytes : options && options.asString ? bin.bytesToString(digestbytes) : crypt2.bytesToHex(digestbytes);
823
+ };
824
+ })();
825
+ var md5Exports = md5$1.exports;
826
+ const md5 = /* @__PURE__ */ getDefaultExportFromCjs(md5Exports);
827
+ let _installed = false;
828
+ const _defaultOpts = {
829
+ tokenHeader: "x-token",
830
+ autoToastError: true
831
+ };
832
+ function useApp() {
833
+ function install(app) {
834
+ if (_installed) {
835
+ console.warn("[hlw] useApp().install() 应只调用一次");
836
+ }
837
+ _installed = true;
838
+ const mainApp = createSSRApp(app);
839
+ mainApp.config.globalProperties["hlw"] = hlw;
840
+ return { app: mainApp };
841
+ }
842
+ return { install, hlw, http };
843
+ }
844
+ function buildSignString(url) {
845
+ try {
846
+ const [path, query] = url.split("?");
847
+ if (!query)
848
+ return path + "&";
849
+ const params = query.split("&").filter(Boolean);
850
+ params.sort();
851
+ return params.join("&") + "&";
852
+ } catch {
853
+ return url;
854
+ }
855
+ }
856
+ let _sigSecret = "";
857
+ function setupDefaultInterceptors(options = {}) {
858
+ const opts = { ..._defaultOpts, ...options };
859
+ if (opts.sigSecret)
860
+ _sigSecret = opts.sigSecret;
861
+ http.onRequest((config) => {
862
+ const device = useDevice();
863
+ if (device.value) {
864
+ const params = new URLSearchParams();
865
+ params.append("appid", device.value.appid);
866
+ params.append("device_brand", device.value.device_brand);
867
+ params.append("device_model", device.value.device_model);
868
+ params.append("device_id", device.value.device_id);
869
+ params.append("device_type", device.value.device_type);
870
+ params.append("platform", device.value.platform);
871
+ params.append("version", device.value.version);
872
+ config.url = config.url + (config.url.includes("?") ? "&" : "?") + params.toString();
873
+ }
874
+ if (_sigSecret) {
875
+ const signStr = buildSignString(config.url);
876
+ const sig = md5(signStr + _sigSecret);
877
+ config.url = config.url + "&sig=" + sig;
878
+ }
879
+ if (opts.getToken) {
880
+ const token = opts.getToken();
881
+ if (token) {
882
+ config.headers = {
883
+ ...config.headers,
884
+ [opts.tokenHeader]: token
885
+ };
886
+ }
887
+ }
888
+ return config;
889
+ });
890
+ http.onResponse((res) => {
891
+ if (opts.autoToastError && res.code !== 0) {
892
+ uni.showToast({ title: res.message || "请求失败", icon: "none" });
893
+ }
894
+ return res;
895
+ });
896
+ http.onError((err) => {
897
+ var _a;
898
+ if (err.message.includes("401")) {
899
+ (_a = opts.onUnauthorized) == null ? void 0 : _a.call(opts);
900
+ }
901
+ });
902
+ }
903
+ const Avatar = defineComponent({
904
+ name: "Avatar",
905
+ props: {
906
+ src: { type: String },
907
+ name: { type: String },
908
+ size: { type: String, default: "medium" }
909
+ },
910
+ setup(props) {
911
+ const loadError = ref(false);
912
+ const initial = computed(() => {
913
+ if (!props.name)
914
+ return "?";
915
+ return props.name.charAt(0).toUpperCase();
916
+ });
917
+ function onError() {
918
+ loadError.value = true;
919
+ }
920
+ return () => {
921
+ const sizeClass = `hlw-avatar--${props.size}`;
922
+ return (
923
+ // @ts-ignore - uni app nodes
924
+ h("view", { class: `hlw-avatar ${sizeClass}` }, [
925
+ props.src && !loadError.value ? (
926
+ // @ts-ignore
927
+ h("image", {
928
+ class: "hlw-avatar__image",
929
+ src: props.src,
930
+ mode: "aspectFill",
931
+ onError
932
+ })
933
+ ) : (
934
+ // @ts-ignore
935
+ h("view", { class: "hlw-avatar__placeholder" }, [
936
+ // @ts-ignore
937
+ h("text", { class: "hlw-avatar__initial" }, initial.value)
938
+ ])
939
+ )
940
+ ])
941
+ );
942
+ };
943
+ }
944
+ });
945
+ const Empty = defineComponent({
946
+ name: "Empty",
947
+ props: {
948
+ text: { type: String },
949
+ image: { type: String }
950
+ },
951
+ setup(props) {
952
+ const slots = useSlots();
953
+ return () => {
954
+ var _a;
955
+ return (
956
+ // @ts-ignore
957
+ h("view", { class: "hlw-empty" }, [
958
+ props.image ? (
959
+ // @ts-ignore
960
+ h("image", { class: "hlw-empty__image", src: props.image, mode: "aspectFit" })
961
+ ) : (
962
+ // @ts-ignore
963
+ h("view", { class: "hlw-empty__icon" }, [
964
+ // @ts-ignore
965
+ h("text", "📦")
966
+ ])
967
+ ),
968
+ // @ts-ignore
969
+ h("text", { class: "hlw-empty__text" }, props.text || "暂无数据"),
970
+ (_a = slots.default) == null ? void 0 : _a.call(slots)
971
+ ])
972
+ );
973
+ };
974
+ }
975
+ });
976
+ const Loading = defineComponent({
977
+ name: "Loading",
978
+ props: {
979
+ text: { type: String }
980
+ },
981
+ setup(props) {
982
+ return () => {
983
+ const dots = Array.from(
984
+ { length: 12 },
985
+ (_, i) => (
986
+ // @ts-ignore
987
+ h("view", { key: i + 1, class: "hlw-loading__dot" })
988
+ )
989
+ );
990
+ return (
991
+ // @ts-ignore
992
+ h("view", { class: "hlw-loading" }, [
993
+ // @ts-ignore
994
+ h("view", { class: "hlw-loading__spinner" }, dots),
995
+ props.text ? (
996
+ // @ts-ignore
997
+ h("text", { class: "hlw-loading__text" }, props.text)
998
+ ) : null
999
+ ])
1000
+ );
1001
+ };
1002
+ }
1003
+ });
1004
+ const MenuList = defineComponent({
1005
+ name: "MenuList",
1006
+ props: {
1007
+ items: { type: Array, required: true }
1008
+ },
1009
+ emits: ["click"],
1010
+ setup(props, { emit }) {
1011
+ function onTap(item) {
1012
+ if (item.url) {
1013
+ uni.navigateTo({ url: item.url });
1014
+ } else if (item.action) {
1015
+ item.action();
1016
+ }
1017
+ emit("click", item);
1018
+ }
1019
+ return () => {
1020
+ const items = props.items.map(
1021
+ (item) => (
1022
+ // @ts-ignore
1023
+ h("view", {
1024
+ class: "hlw-menu-list__item",
1025
+ key: item.key,
1026
+ onClick: () => onTap(item)
1027
+ }, [
1028
+ // @ts-ignore
1029
+ h("view", { class: "hlw-menu-list__left" }, [
1030
+ item.icon ? (
1031
+ // @ts-ignore
1032
+ h("text", { class: "hlw-menu-list__icon" }, item.icon)
1033
+ ) : null,
1034
+ // @ts-ignore
1035
+ h("text", { class: "hlw-menu-list__label" }, item.label)
1036
+ ]),
1037
+ // @ts-ignore
1038
+ h("view", { class: "hlw-menu-list__right" }, [
1039
+ item.value ? (
1040
+ // @ts-ignore
1041
+ h("text", { class: "hlw-menu-list__value" }, item.value)
1042
+ ) : null,
1043
+ // @ts-ignore
1044
+ h("text", { class: "hlw-menu-list__arrow" }, "›")
1045
+ ])
1046
+ ])
1047
+ )
1048
+ );
1049
+ return h("view", { class: "hlw-menu-list" }, items);
1050
+ };
1051
+ }
1052
+ });
1053
+ export {
1054
+ Avatar,
1055
+ Empty,
1056
+ HttpClient,
1057
+ Loading,
1058
+ MenuList,
1059
+ adapters,
1060
+ alistAdapter,
1061
+ clearDeviceCache,
1062
+ cosAdapter,
1063
+ deviceToQuery,
1064
+ getAdapter,
1065
+ hlw,
1066
+ http,
1067
+ ossAdapter,
1068
+ qiniuAdapter,
1069
+ setupDefaultInterceptors,
1070
+ useApp,
1071
+ useDevice,
1072
+ useFormat,
1073
+ useLoading,
1074
+ useMsg,
1075
+ usePageMeta,
1076
+ useRefs,
1077
+ useRequest,
1078
+ useStorage,
1079
+ useUpload,
1080
+ useValidate
1081
+ };