uview-pro 0.0.1

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.
Files changed (149) hide show
  1. package/changelog.md +6 -0
  2. package/components/u-action-sheet/u-action-sheet.vue +205 -0
  3. package/components/u-alert-tips/u-alert-tips.vue +241 -0
  4. package/components/u-avatar/u-avatar.vue +220 -0
  5. package/components/u-avatar-cropper/u-avatar-cropper.vue +329 -0
  6. package/components/u-avatar-cropper/weCropper.d.ts +54 -0
  7. package/components/u-avatar-cropper/weCropper.js +1267 -0
  8. package/components/u-avatar-cropper/weCropper.ts +1254 -0
  9. package/components/u-back-top/u-back-top.vue +156 -0
  10. package/components/u-badge/u-badge.vue +189 -0
  11. package/components/u-button/u-button.vue +562 -0
  12. package/components/u-calendar/u-calendar.vue +725 -0
  13. package/components/u-car-keyboard/u-car-keyboard.vue +236 -0
  14. package/components/u-card/u-card.vue +240 -0
  15. package/components/u-cell-group/u-cell-group.vue +56 -0
  16. package/components/u-cell-item/u-cell-item.vue +245 -0
  17. package/components/u-checkbox/u-checkbox.vue +310 -0
  18. package/components/u-checkbox-group/u-checkbox-group.vue +134 -0
  19. package/components/u-circle-progress/u-circle-progress.vue +210 -0
  20. package/components/u-col/u-col.vue +135 -0
  21. package/components/u-collapse/u-collapse.vue +82 -0
  22. package/components/u-collapse-item/u-collapse-item.vue +190 -0
  23. package/components/u-column-notice/u-column-notice.vue +264 -0
  24. package/components/u-count-down/u-count-down.vue +333 -0
  25. package/components/u-count-to/u-count-to.vue +297 -0
  26. package/components/u-divider/u-divider.vue +141 -0
  27. package/components/u-dropdown/u-dropdown.vue +311 -0
  28. package/components/u-dropdown-item/u-dropdown-item.vue +135 -0
  29. package/components/u-empty/u-empty.vue +111 -0
  30. package/components/u-field/u-field.vue +469 -0
  31. package/components/u-form/u-form.vue +162 -0
  32. package/components/u-form-item/u-form-item.vue +476 -0
  33. package/components/u-full-screen/u-full-screen.vue +80 -0
  34. package/components/u-gap/u-gap.vue +48 -0
  35. package/components/u-grid/u-grid.vue +101 -0
  36. package/components/u-grid-item/u-grid-item.vue +136 -0
  37. package/components/u-icon/u-icon.vue +389 -0
  38. package/components/u-image/types.ts +48 -0
  39. package/components/u-image/u-image.vue +218 -0
  40. package/components/u-index-anchor/u-index-anchor.vue +101 -0
  41. package/components/u-index-list/u-index-list.vue +376 -0
  42. package/components/u-input/u-input.vue +462 -0
  43. package/components/u-keyboard/u-keyboard.vue +188 -0
  44. package/components/u-lazy-load/u-lazy-load.vue +288 -0
  45. package/components/u-line/u-line.vue +71 -0
  46. package/components/u-line-progress/u-line-progress.vue +128 -0
  47. package/components/u-link/u-link.vue +87 -0
  48. package/components/u-loading/u-loading.vue +111 -0
  49. package/components/u-loadmore/u-loadmore.vue +205 -0
  50. package/components/u-mask/u-mask.vue +137 -0
  51. package/components/u-message-input/u-message-input.vue +315 -0
  52. package/components/u-modal/u-modal.vue +284 -0
  53. package/components/u-navbar/u-navbar.vue +314 -0
  54. package/components/u-no-network/image.ts +2 -0
  55. package/components/u-no-network/u-no-network.vue +311 -0
  56. package/components/u-notice-bar/u-notice-bar.vue +274 -0
  57. package/components/u-number-box/u-number-box.vue +344 -0
  58. package/components/u-number-keyboard/u-number-keyboard.vue +170 -0
  59. package/components/u-parse/libs/CssHandler.js +100 -0
  60. package/components/u-parse/libs/MpHtmlParser.js +580 -0
  61. package/components/u-parse/libs/config.js +80 -0
  62. package/components/u-parse/libs/handler.wxs +22 -0
  63. package/components/u-parse/libs/trees.vue +505 -0
  64. package/components/u-parse/u-parse.vue +645 -0
  65. package/components/u-picker/u-picker.vue +808 -0
  66. package/components/u-popup/u-popup.vue +404 -0
  67. package/components/u-radio/u-radio.vue +272 -0
  68. package/components/u-radio-group/u-radio-group.vue +116 -0
  69. package/components/u-rate/u-rate.vue +349 -0
  70. package/components/u-read-more/u-read-more.vue +199 -0
  71. package/components/u-row/u-row.vue +95 -0
  72. package/components/u-row-notice/u-row-notice.vue +273 -0
  73. package/components/u-search/u-search.vue +298 -0
  74. package/components/u-section/u-section.vue +175 -0
  75. package/components/u-select/u-select.vue +387 -0
  76. package/components/u-skeleton/u-skeleton.vue +230 -0
  77. package/components/u-slider/u-slider.vue +293 -0
  78. package/components/u-steps/u-steps.vue +200 -0
  79. package/components/u-sticky/u-sticky.vue +189 -0
  80. package/components/u-subsection/u-subsection.vue +388 -0
  81. package/components/u-swipe-action/u-swipe-action.vue +289 -0
  82. package/components/u-swiper/u-swiper.vue +305 -0
  83. package/components/u-switch/u-switch.vue +146 -0
  84. package/components/u-tabbar/u-tabbar.vue +347 -0
  85. package/components/u-table/u-table.vue +104 -0
  86. package/components/u-tabs/u-tabs.vue +322 -0
  87. package/components/u-tabs-swiper/u-tabs-swiper.vue +426 -0
  88. package/components/u-tag/u-tag.vue +270 -0
  89. package/components/u-td/u-td.vue +76 -0
  90. package/components/u-th/u-th.vue +70 -0
  91. package/components/u-time-line/u-time-line.vue +39 -0
  92. package/components/u-time-line-item/u-time-line-item.vue +88 -0
  93. package/components/u-toast/types.ts +4 -0
  94. package/components/u-toast/u-toast.vue +238 -0
  95. package/components/u-top-tips/u-top-tips.vue +118 -0
  96. package/components/u-tr/u-tr.vue +24 -0
  97. package/components/u-upload/u-upload.vue +600 -0
  98. package/components/u-verification-code/u-verification-code.vue +194 -0
  99. package/components/u-waterfall/u-waterfall.vue +186 -0
  100. package/iconfont.css +910 -0
  101. package/index.scss +23 -0
  102. package/index.ts +166 -0
  103. package/libs/config/config.ts +26 -0
  104. package/libs/config/zIndex.ts +37 -0
  105. package/libs/css/color.scss +155 -0
  106. package/libs/css/common.scss +176 -0
  107. package/libs/css/style.components.scss +7 -0
  108. package/libs/css/style.h5.scss +8 -0
  109. package/libs/css/style.mp.scss +72 -0
  110. package/libs/css/style.nvue.scss +3 -0
  111. package/libs/css/style.vue.scss +175 -0
  112. package/libs/function/$parent.ts +22 -0
  113. package/libs/function/addUnit.ts +13 -0
  114. package/libs/function/color.ts +37 -0
  115. package/libs/function/colorGradient.ts +123 -0
  116. package/libs/function/debounce.ts +28 -0
  117. package/libs/function/deepClone.ts +39 -0
  118. package/libs/function/deepMerge.ts +34 -0
  119. package/libs/function/getParent.ts +59 -0
  120. package/libs/function/getRect.ts +26 -0
  121. package/libs/function/guid.ts +42 -0
  122. package/libs/function/md5.ts +397 -0
  123. package/libs/function/parent.ts +21 -0
  124. package/libs/function/queryParams.ts +60 -0
  125. package/libs/function/random.ts +16 -0
  126. package/libs/function/randomArray.ts +11 -0
  127. package/libs/function/route.ts +118 -0
  128. package/libs/function/sys.ts +15 -0
  129. package/libs/function/test.ts +229 -0
  130. package/libs/function/throttle.ts +31 -0
  131. package/libs/function/timeFormat.ts +54 -0
  132. package/libs/function/timeFrom.ts +48 -0
  133. package/libs/function/toast.ts +14 -0
  134. package/libs/function/trim.ts +21 -0
  135. package/libs/function/type2icon.ts +36 -0
  136. package/libs/hooks/useEmitter.ts +77 -0
  137. package/libs/hooks/useParent.ts +29 -0
  138. package/libs/request/index.ts +237 -0
  139. package/libs/store/index.ts +88 -0
  140. package/libs/util/area.ts +1 -0
  141. package/libs/util/async-validator.js +1356 -0
  142. package/libs/util/city.ts +1 -0
  143. package/libs/util/emitter.ts +112 -0
  144. package/libs/util/mitt.ts +118 -0
  145. package/libs/util/parent.ts +20 -0
  146. package/libs/util/province.ts +1 -0
  147. package/package.json +98 -0
  148. package/readme.md +165 -0
  149. package/theme.scss +38 -0
@@ -0,0 +1,175 @@
1
+ page {
2
+ color: $u-main-color;
3
+ font-size: 28rpx;
4
+ }
5
+
6
+ /* start--去除webkit的默认样式--start */
7
+ .u-fix-ios-appearance {
8
+ -webkit-appearance:none;
9
+ }
10
+ /* end--去除webkit的默认样式--end */
11
+
12
+ /* start--icon图标外层套一个view,让其达到更好的垂直居中的效果--start */
13
+ .u-icon-wrap {
14
+ display: flex;
15
+ align-items: center;
16
+ }
17
+ /* end-icon图标外层套一个view,让其达到更好的垂直居中的效果--end */
18
+
19
+ /* start--iPhoneX底部安全区定义--start */
20
+ .safe-area-inset-bottom {
21
+ padding-bottom: 0;
22
+ padding-bottom: constant(safe-area-inset-bottom);
23
+ padding-bottom: env(safe-area-inset-bottom);
24
+ }
25
+ /* end-iPhoneX底部安全区定义--end */
26
+
27
+ /* start--各种hover点击反馈相关的类名-start */
28
+ .u-hover-class {
29
+ // background-color: #f7f8f9!important;
30
+ opacity: 0.6;
31
+ }
32
+
33
+ .u-cell-hover {
34
+ background-color: #f7f8f9!important;
35
+ }
36
+ /* end--各种hover点击反馈相关的类名--end */
37
+
38
+ /* start--文本行数限制--start */
39
+ .u-line-1 {
40
+ overflow: hidden;
41
+ white-space: nowrap;
42
+ text-overflow: ellipsis;
43
+ }
44
+
45
+ .u-line-2 {
46
+ -webkit-line-clamp: 2;
47
+ }
48
+
49
+ .u-line-3 {
50
+ -webkit-line-clamp: 3;
51
+ }
52
+
53
+ .u-line-4 {
54
+ -webkit-line-clamp: 4;
55
+ }
56
+
57
+ .u-line-5 {
58
+ -webkit-line-clamp: 5;
59
+ }
60
+
61
+ .u-line-2, .u-line-3, .u-line-4, .u-line-5 {
62
+ overflow: hidden;
63
+ word-break: break-all;
64
+ text-overflow: ellipsis;
65
+ display: -webkit-box; // 弹性伸缩盒
66
+ -webkit-box-orient: vertical; // 设置伸缩盒子元素排列方式
67
+ }
68
+
69
+ /* end--文本行数限制--end */
70
+
71
+
72
+ /* start--Retina 屏幕下的 1px 边框--start */
73
+ .u-border,
74
+ .u-border-bottom,
75
+ .u-border-left,
76
+ .u-border-right,
77
+ .u-border-top,
78
+ .u-border-top-bottom {
79
+ position: relative
80
+ }
81
+
82
+ .u-border-bottom:after,
83
+ .u-border-left:after,
84
+ .u-border-right:after,
85
+ .u-border-top-bottom:after,
86
+ .u-border-top:after,
87
+ .u-border:after {
88
+ /* #ifndef APP-NVUE */
89
+ content: ' ';
90
+ /* #endif */
91
+ position: absolute;
92
+ left: 0;
93
+ top: 0;
94
+ pointer-events: none;
95
+ box-sizing: border-box;
96
+ -webkit-transform-origin: 0 0;
97
+ transform-origin: 0 0;
98
+ // 多加0.1%,能解决有时候边框缺失的问题
99
+ width: 199.8%;
100
+ height: 199.7%;
101
+ transform: scale(0.5, 0.5);
102
+ border: 0 solid $u-border-color;
103
+ z-index: 2;
104
+ }
105
+
106
+ .u-border-top:after {
107
+ border-top-width: 1px
108
+ }
109
+
110
+ .u-border-left:after {
111
+ border-left-width: 1px
112
+ }
113
+
114
+ .u-border-right:after {
115
+ border-right-width: 1px
116
+ }
117
+
118
+ .u-border-bottom:after {
119
+ border-bottom-width: 1px
120
+ }
121
+
122
+ .u-border-top-bottom:after {
123
+ border-width: 1px 0
124
+ }
125
+
126
+ .u-border:after {
127
+ border-width: 1px
128
+ }
129
+ /* end--Retina 屏幕下的 1px 边框--end */
130
+
131
+
132
+ /* start--clearfix--start */
133
+ .u-clearfix:after,
134
+ .clearfix:after {
135
+ /* #ifndef APP-NVUE */
136
+ content: '';
137
+ /* #endif */
138
+ display: table;
139
+ clear: both
140
+ }
141
+ /* end--clearfix--end */
142
+
143
+ /* start--高斯模糊tabbar底部处理--start */
144
+ .u-blur-effect-inset {
145
+ width: 750rpx;
146
+ height: var(--window-bottom);
147
+ background-color: #FFFFFF;
148
+ }
149
+ /* end--高斯模糊tabbar底部处理--end */
150
+
151
+ /* start--提升H5端uni.toast()的层级,避免被uView的modal等遮盖--start */
152
+ /* #ifdef H5 */
153
+ uni-toast {
154
+ z-index: 10090;
155
+ }
156
+ uni-toast .uni-toast {
157
+ z-index: 10090;
158
+ }
159
+ /* #endif */
160
+ /* end--提升H5端uni.toast()的层级,避免被uView的modal等遮盖--end */
161
+
162
+ /* start--去除button的所有默认样式--start */
163
+ .u-reset-button {
164
+ padding: 0;
165
+ font-size: inherit;
166
+ line-height: inherit;
167
+ background-color: transparent;
168
+ color: inherit;
169
+ }
170
+
171
+ .u-reset-button::after {
172
+ border: none;
173
+ }
174
+ /* end--去除button的所有默认样式--end */
175
+
@@ -0,0 +1,22 @@
1
+ // 获取父组件的参数,因为支付宝小程序不支持provide/inject的写法
2
+ // this.$parent在非H5中,可以准确获取到父组件,但是在H5中,需要多次this.$parent.$parent.xxx
3
+ // 这里默认值等于undefined有它的含义,因为最顶层元素(组件)的$parent就是undefined,意味着不传name
4
+ // 值(默认为undefined),就是查找最顶层的$parent
5
+ import { type ComponentInternalInstance, getCurrentInstance } from 'vue';
6
+
7
+ export default function $parent(componentName?: string, _instance: ComponentInternalInstance | null | undefined = null) {
8
+
9
+ const instance: ComponentInternalInstance | null | undefined = _instance || getCurrentInstance();
10
+ let parent = instance && (instance.parent as ComponentInternalInstance | null | undefined);
11
+
12
+ if(!componentName) return parent;
13
+
14
+ while (parent) {
15
+ const name = (parent.type as any)?.name as string | undefined;
16
+ if (name === componentName) {
17
+ return parent;
18
+ }
19
+ parent = parent.parent;
20
+ }
21
+ return null;
22
+ }
@@ -0,0 +1,13 @@
1
+ import validation from './test';
2
+
3
+ /**
4
+ * 添加单位,如果有rpx,%,px等单位结尾或者值为auto,直接返回,否则加上rpx单位结尾
5
+ * @param value 输入值,可以为字符串或数字,默认'auto'
6
+ * @param unit 单位,默认'rpx'
7
+ * @returns 添加单位后的字符串
8
+ */
9
+ export default function addUnit(value: string | number = 'auto', unit: string = 'rpx'): string {
10
+ const strValue = String(value);
11
+ // 用uView内置验证规则中的number判断是否为数值
12
+ return validation.number(strValue) ? `${strValue}${unit}` : strValue;
13
+ }
@@ -0,0 +1,37 @@
1
+ // 为了让用户能够自定义主题,会逐步弃用此文件,各颜色通过css提供
2
+ // 为了给某些特殊场景使用和向后兼容,无需删除此文件(2020-06-20)
3
+ let color = {
4
+ primary: "#2979ff",
5
+ primaryDark: "#2b85e4",
6
+ primaryDisabled: "#a0cfff",
7
+ primaryLight: "#ecf5ff",
8
+ bgColor: "#f3f4f6",
9
+
10
+ info: "#909399",
11
+ infoDark: "#82848a",
12
+ infoDisabled: "#c8c9cc",
13
+ infoLight: "#f4f4f5",
14
+
15
+ warning: "#ff9900",
16
+ warningDark: "#f29100",
17
+ warningDisabled: "#fcbd71",
18
+ warningLight: "#fdf6ec",
19
+
20
+ error: "#fa3534",
21
+ errorDark: "#dd6161",
22
+ errorDisabled: "#fab6b6",
23
+ errorLight: "#fef0f0",
24
+
25
+ success: "#19be6b",
26
+ successDark: "#18b566",
27
+ successDisabled: "#71d5a1",
28
+ successLight: "#dbf1e1",
29
+
30
+ mainColor: "#303133",
31
+ contentColor: "#606266",
32
+ tipsColor: "#909399",
33
+ lightColor: "#c0c4cc",
34
+ borderColor: "#e4e7ed"
35
+ }
36
+
37
+ export default color;
@@ -0,0 +1,123 @@
1
+ /**
2
+ * 求两个颜色之间的渐变值
3
+ * @param startColor 开始的颜色
4
+ * @param endColor 结束的颜色
5
+ * @param step 颜色等分的份额
6
+ * @returns 渐变色数组
7
+ */
8
+ function colorGradient(startColor: string = 'rgb(0, 0, 0)', endColor: string = 'rgb(255, 255, 255)', step: number = 10): string[] {
9
+ const startRGB = hexToRgb(startColor, false) as [number, number, number]; // 转换为rgb数组模式
10
+ const [startR, startG, startB] = startRGB;
11
+ const endRGB = hexToRgb(endColor, false) as [number, number, number];
12
+ const [endR, endG, endB] = endRGB;
13
+
14
+ const sR = (endR - startR) / step; // 总差值
15
+ const sG = (endG - startG) / step;
16
+ const sB = (endB - startB) / step;
17
+ const colorArr: string[] = [];
18
+ for (let i = 0; i < step; i++) {
19
+ // 计算每一步的hex值
20
+ const hex = rgbToHex(`rgb(${Math.round(sR * i + startR)},${Math.round(sG * i + startG)},${Math.round(sB * i + startB)})`);
21
+ colorArr.push(hex as string);
22
+ }
23
+ return colorArr;
24
+ }
25
+
26
+ /**
27
+ * 将hex表示方式转换为rgb表示方式(返回rgb数组或字符串)
28
+ * @param sColor hex或rgb字符串
29
+ * @param str 是否返回字符串
30
+ * @returns rgb数组或字符串
31
+ */
32
+ function hexToRgb(sColor: string, str: boolean = true): [number, number, number] | string {
33
+ const reg = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/;
34
+ sColor = sColor.toLowerCase();
35
+ if (sColor && reg.test(sColor)) {
36
+ if (sColor.length === 4) {
37
+ let sColorNew = '#';
38
+ for (let i = 1; i < 4; i += 1) {
39
+ sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1));
40
+ }
41
+ sColor = sColorNew;
42
+ }
43
+ // 处理六位的颜色值
44
+ const sColorChange: [number, number, number] = [parseInt('0x' + sColor.slice(1, 3)), parseInt('0x' + sColor.slice(3, 5)), parseInt('0x' + sColor.slice(5, 7))];
45
+ if (!str) {
46
+ return sColorChange;
47
+ } else {
48
+ return `rgb(${sColorChange[0]},${sColorChange[1]},${sColorChange[2]})`;
49
+ }
50
+ } else if (/^(rgb|RGB)/.test(sColor)) {
51
+ const arr = sColor.replace(/(?:\(|\)|rgb|RGB)*/g, '').split(',');
52
+ return arr.map(val => Number(val)) as [number, number, number];
53
+ } else {
54
+ return sColor;
55
+ }
56
+ }
57
+
58
+ /**
59
+ * 将rgb表示方式转换为hex表示方式
60
+ * @param rgb rgb字符串或hex字符串
61
+ * @returns hex字符串
62
+ */
63
+ function rgbToHex(rgb: string): string | undefined {
64
+ const reg = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/;
65
+ if (/^(rgb|RGB)/.test(rgb)) {
66
+ const aColor = rgb.replace(/(?:\(|\)|rgb|RGB)*/g, '').split(',');
67
+ let strHex = '#';
68
+ for (let i = 0; i < aColor.length; i++) {
69
+ let hex = Number(aColor[i]).toString(16);
70
+ hex = hex.length == 1 ? '0' + hex : hex; // 保证每个rgb的值为2位
71
+ strHex += hex;
72
+ }
73
+ if (strHex.length !== 7) {
74
+ strHex = rgb;
75
+ }
76
+ return strHex;
77
+ } else if (reg.test(rgb)) {
78
+ const aNum = rgb.replace(/#/, '').split('');
79
+ if (aNum.length === 6) {
80
+ return rgb;
81
+ } else if (aNum.length === 3) {
82
+ let numHex = '#';
83
+ for (let i = 0; i < aNum.length; i += 1) {
84
+ numHex += aNum[i] + aNum[i];
85
+ }
86
+ return numHex;
87
+ }
88
+ } else {
89
+ return rgb;
90
+ }
91
+ }
92
+
93
+ /**
94
+ * JS颜色十六进制转换为rgb或rgba,返回的格式为 rgba(255,255,255,0.5)字符串
95
+ * @param color 十六进制色值或rgb字符串
96
+ * @param alpha 透明度
97
+ * @returns rgba字符串
98
+ */
99
+ function colorToRgba(color: string, alpha: number = 0.3): string {
100
+ color = rgbToHex(color) as string; // 确保是hex格式
101
+ const reg = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/;
102
+ let sColor = color.toLowerCase();
103
+ if (sColor && reg.test(sColor)) {
104
+ if (sColor.length === 4) {
105
+ let sColorNew = '#';
106
+ for (let i = 1; i < 4; i += 1) {
107
+ sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1));
108
+ }
109
+ sColor = sColorNew;
110
+ }
111
+ const sColorChange: [number, number, number] = [parseInt('0x' + sColor.slice(1, 3)), parseInt('0x' + sColor.slice(3, 5)), parseInt('0x' + sColor.slice(5, 7))];
112
+ return `rgba(${sColorChange.join(',')},${alpha})`;
113
+ } else {
114
+ return sColor;
115
+ }
116
+ }
117
+
118
+ export default {
119
+ colorGradient,
120
+ hexToRgb,
121
+ rgbToHex,
122
+ colorToRgba
123
+ };
@@ -0,0 +1,28 @@
1
+ let timeout: ReturnType<typeof setTimeout> | null = null;
2
+
3
+ /**
4
+ * 防抖原理:一定时间内,只有最后一次操作,再过wait毫秒后才执行函数
5
+ * @param func 要执行的回调函数
6
+ * @param wait 延时的时间,单位ms,默认500
7
+ * @param immediate 是否立即执行,默认false
8
+ * @returns void
9
+ */
10
+ function debounce(func: () => void, wait: number = 500, immediate: boolean = false): void {
11
+ // 清除定时器
12
+ if (timeout !== null) clearTimeout(timeout);
13
+ // 立即执行,此类情况一般用不到
14
+ if (immediate) {
15
+ const callNow = !timeout;
16
+ timeout = setTimeout(() => {
17
+ timeout = null;
18
+ }, wait);
19
+ if (callNow) typeof func === 'function' && func();
20
+ } else {
21
+ // 设置定时器,当最后一次操作后,timeout不会再被清除,所以在延时wait毫秒后执行func回调方法
22
+ timeout = setTimeout(() => {
23
+ typeof func === 'function' && func();
24
+ }, wait);
25
+ }
26
+ }
27
+
28
+ export default debounce;
@@ -0,0 +1,39 @@
1
+ // 判断arr是否为一个数组,返回一个bool值
2
+ function isArray(arr: any): arr is any[] {
3
+ return Object.prototype.toString.call(arr) === '[object Array]';
4
+ }
5
+
6
+ /**
7
+ * 深度克隆
8
+ * @param obj 需要克隆的对象
9
+ * @param cache 用于处理循环引用的 WeakMap
10
+ * @returns 克隆后的对象
11
+ */
12
+ function deepClone<T>(obj: T, cache: WeakMap<any, any> = new WeakMap()): T {
13
+ if (obj === null || typeof obj !== 'object') return obj;
14
+ if (cache.has(obj)) return cache.get(obj);
15
+ let clone: any;
16
+ if (obj instanceof Date) {
17
+ clone = new Date(obj.getTime());
18
+ } else if (obj instanceof RegExp) {
19
+ clone = new RegExp(obj);
20
+ } else if (obj instanceof Map) {
21
+ clone = new Map(Array.from(obj, ([key, value]) => [key, deepClone(value, cache)]));
22
+ } else if (obj instanceof Set) {
23
+ clone = new Set(Array.from(obj, value => deepClone(value, cache)));
24
+ } else if (Array.isArray(obj)) {
25
+ clone = obj.map(value => deepClone(value, cache));
26
+ } else if (Object.prototype.toString.call(obj) === '[object Object]') {
27
+ clone = Object.create(Object.getPrototypeOf(obj));
28
+ cache.set(obj, clone);
29
+ for (const [key, value] of Object.entries(obj)) {
30
+ clone[key] = deepClone(value, cache);
31
+ }
32
+ } else {
33
+ clone = Object.assign({}, obj);
34
+ }
35
+ cache.set(obj, clone);
36
+ return clone;
37
+ }
38
+
39
+ export default deepClone;
@@ -0,0 +1,34 @@
1
+ import deepClone from './deepClone';
2
+
3
+ /**
4
+ * JS对象深度合并
5
+ * @param target 目标对象
6
+ * @param source 源对象
7
+ * @returns 合并后的对象
8
+ */
9
+ function deepMerge<T extends object, S extends object>(target: T = {} as T, source: S = {} as S): T & S {
10
+ target = deepClone(target);
11
+ if (typeof target !== 'object' || target === null || typeof source !== 'object' || source === null) return target as T & S;
12
+ const merged: any = Array.isArray(target) ? target.slice() : Object.assign({}, target);
13
+ for (const prop in source) {
14
+ if (!Object.prototype.hasOwnProperty.call(source, prop)) continue;
15
+ const sourceValue = (source as any)[prop];
16
+ const targetValue = merged[prop];
17
+ if (sourceValue instanceof Date) {
18
+ merged[prop] = new Date(sourceValue);
19
+ } else if (sourceValue instanceof RegExp) {
20
+ merged[prop] = new RegExp(sourceValue);
21
+ } else if (sourceValue instanceof Map) {
22
+ merged[prop] = new Map(sourceValue);
23
+ } else if (sourceValue instanceof Set) {
24
+ merged[prop] = new Set(sourceValue);
25
+ } else if (typeof sourceValue === 'object' && sourceValue !== null) {
26
+ merged[prop] = deepMerge(targetValue, sourceValue);
27
+ } else {
28
+ merged[prop] = sourceValue;
29
+ }
30
+ }
31
+ return merged as T & S;
32
+ }
33
+
34
+ export default deepMerge;
@@ -0,0 +1,59 @@
1
+ // 获取父组件的参数,因为支付宝小程序不支持provide/inject的写法
2
+ // this.$parent在非H5中,可以准确获取到父组件,但是在H5中,需要多次this.$parent.$parent.xxx
3
+ interface VueInstance {
4
+ $parent?: VueInstance;
5
+ $options?: { name?: string };
6
+ [key: string]: any;
7
+ }
8
+
9
+ /**
10
+ * 获取父组件参数
11
+ * @param this 当前组件实例
12
+ * @param name 父组件name
13
+ * @param keys 需要获取的参数名数组或对象
14
+ * @returns 父组件参数对象
15
+ */
16
+ export default function getParent(this: VueInstance, name: string, keys: string[] | Record<string, any>): Record<string, any> {
17
+ let parent = this.$parent;
18
+ // 通过while历遍,这里主要是为了H5需要多层解析的问题
19
+ while (parent) {
20
+ // 父组件
21
+ if (parent.$options?.name !== name) {
22
+ // 如果组件的name不相等,继续上一级寻找
23
+ parent = parent.$parent;
24
+ } else {
25
+ const data: Record<string, any> = {};
26
+ // 判断keys是否数组,如果传过来的是一个数组,那么直接使用数组元素值当做键值去父组件寻找
27
+ if (Array.isArray(keys)) {
28
+ keys.forEach(val => {
29
+ data[val] = parent?.[val] ? parent[val] : '';
30
+ });
31
+ } else {
32
+ // 历遍传过来的对象参数
33
+ for (const i in keys) {
34
+ // 如果子组件有此值则用,无此值则用父组件的值
35
+ // 判断是否空数组,如果是,则用父组件的值,否则用子组件的值
36
+ if (Array.isArray(keys[i])) {
37
+ if (keys[i].length) {
38
+ data[i] = keys[i];
39
+ } else {
40
+ data[i] = parent[i];
41
+ }
42
+ } else if (keys[i] && keys[i].constructor === Object) {
43
+ // 判断是否对象,如果是对象,且有属性,那么使用子组件的值,否则使用父组件的值
44
+ if (Object.keys(keys[i]).length) {
45
+ data[i] = keys[i];
46
+ } else {
47
+ data[i] = parent[i];
48
+ }
49
+ } else {
50
+ // 只要子组件有传值,即使是false值,也是“传值”了,也需要覆盖父组件的同名参数
51
+ data[i] = (keys[i] || keys[i] === false) ? keys[i] : parent[i];
52
+ }
53
+ }
54
+ }
55
+ return data;
56
+ }
57
+ }
58
+ return {};
59
+ }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * 获取元素的位置信息
3
+ * @param {any} selector 选择器
4
+ * @param {boolean} all 是否获取所有匹配元素
5
+ * @returns {Promise<any>} 返回一个 Promise,解析为元素的位置信息
6
+ */
7
+
8
+ import { getCurrentInstance } from 'vue';
9
+
10
+ export default function (selector: any, _instance: any = null, all: boolean = false): Promise<any> {
11
+ const instance = _instance || getCurrentInstance();
12
+ return new Promise(resolve => {
13
+ uni.createSelectorQuery()
14
+ .in(instance?.proxy)
15
+ [all ? 'selectAll' : 'select'](selector)
16
+ .boundingClientRect((rect: any) => {
17
+ if (all && Array.isArray(rect) && rect.length) {
18
+ resolve(rect);
19
+ }
20
+ if (!all && rect) {
21
+ resolve(rect);
22
+ }
23
+ })
24
+ .exec();
25
+ });
26
+ }
@@ -0,0 +1,42 @@
1
+ /**
2
+ * 本算法来源于简书开源代码,详见:https://www.jianshu.com/p/fdbf293d0a85
3
+ * 全局唯一标识符(uuid,Globally Unique Identifier),也称作 uuid(Universally Unique IDentifier)
4
+ * 一般用于多个组件之间,给它一个唯一的标识符,或者v-for循环的时候,如果使用数组的index可能会导致更新列表出现问题
5
+ * 最可能的情况是左滑删除item或者对某条信息流"不喜欢"并去掉它的时候,会导致组件内的数据可能出现错乱
6
+ * v-for的时候,推荐使用后端返回的id而不是循环的index
7
+ * @param len uuid的长度,默认32
8
+ * @param firstU 将返回的首字母置为"u",默认true
9
+ * @param radix 生成uuid的基数(意味着返回的字符串都是这个基数),2-二进制,8-八进制,10-十进制,16-十六进制
10
+ * @returns 生成的uuid字符串
11
+ */
12
+ function guid(len: number = 32, firstU: boolean = true, radix?: number): string {
13
+ const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
14
+ const uuid: string[] = [];
15
+ const base = radix || chars.length;
16
+
17
+ if (len) {
18
+ // 如果指定uuid长度,只是取随机的字符,0|x为位运算,能去掉x的小数位,返回整数位
19
+ for (let i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * base)];
20
+ } else {
21
+ let r: number;
22
+ // rfc4122标准要求返回的uuid中,某些位为固定的字符
23
+ uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
24
+ uuid[14] = '4';
25
+
26
+ for (let i = 0; i < 36; i++) {
27
+ if (!uuid[i]) {
28
+ r = 0 | (Math.random() * 16);
29
+ uuid[i] = chars[i == 19 ? (r & 0x3) | 0x8 : r];
30
+ }
31
+ }
32
+ }
33
+ // 移除第一个字符,并用u替代,因为第一个字符为数值时,该guid不能用作id或者class
34
+ if (firstU) {
35
+ uuid.shift();
36
+ return 'u' + uuid.join('');
37
+ } else {
38
+ return uuid.join('');
39
+ }
40
+ }
41
+
42
+ export default guid;