sadais-piui 1.2.7-beta113 → 1.2.7-beta115

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 (160) hide show
  1. package/.prettierrc +7 -7
  2. package/CHANGELOG.md +545 -545
  3. package/LICENSE +20 -20
  4. package/README.md +166 -166
  5. package/components/pi-action-sheet/index.vue +247 -247
  6. package/components/pi-badge/index.vue +146 -146
  7. package/components/pi-bottom-bar/index.vue +139 -139
  8. package/components/pi-bubble/index.vue +174 -174
  9. package/components/pi-button/index.vue +492 -492
  10. package/components/pi-calendar-input/index.vue +230 -230
  11. package/components/pi-calendar-panel/components/calendar-days/index.vue +410 -410
  12. package/components/pi-calendar-panel/index.vue +473 -473
  13. package/components/pi-canvas/index.vue +407 -407
  14. package/components/pi-card/index.vue +127 -127
  15. package/components/pi-cascader/index.vue +601 -601
  16. package/components/pi-checkbox/index.vue +363 -363
  17. package/components/pi-checkbox-group/index.vue +222 -222
  18. package/components/pi-circle-progress/index.vue +311 -311
  19. package/components/pi-code-input/index.vue +270 -270
  20. package/components/pi-collapse/index.vue +124 -124
  21. package/components/pi-collapse-item/index.vue +352 -352
  22. package/components/pi-count-down/index.vue +245 -245
  23. package/components/pi-count-up/index.vue +209 -209
  24. package/components/pi-date-picker-input/index.vue +219 -219
  25. package/components/pi-divider/index.vue +112 -112
  26. package/components/pi-dropdown/components/dropdown-tab.vue +71 -71
  27. package/components/pi-dropdown/index.vue +356 -356
  28. package/components/pi-dropdown-item/index.vue +252 -252
  29. package/components/pi-form/index.vue +273 -273
  30. package/components/pi-form-item/index.vue +603 -590
  31. package/components/pi-gap/index.vue +72 -72
  32. package/components/pi-grid/index.vue +92 -92
  33. package/components/pi-grid-item/index.vue +220 -220
  34. package/components/pi-icon/index.vue +147 -147
  35. package/components/pi-img/index.vue +266 -266
  36. package/components/pi-img-cropper/canvas.js +106 -106
  37. package/components/pi-img-cropper/index.vue +357 -357
  38. package/components/pi-img-cropper/touch.js +163 -163
  39. package/components/pi-img-cropper/vector.js +34 -34
  40. package/components/pi-input/index.vue +409 -401
  41. package/components/pi-line/index.vue +78 -78
  42. package/components/pi-line-progress/index.vue +198 -198
  43. package/components/pi-list/index.vue +115 -115
  44. package/components/pi-list-item/index.vue +335 -335
  45. package/components/pi-loading/index.vue +188 -188
  46. package/components/pi-marquee/index.vue +310 -310
  47. package/components/pi-marquee-item/index.vue +47 -47
  48. package/components/pi-mask/index.vue +198 -198
  49. package/components/pi-modal/index.vue +303 -303
  50. package/components/pi-navbar/index.vue +421 -421
  51. package/components/pi-notify/index.vue +239 -239
  52. package/components/pi-number-keyboard/index.vue +189 -189
  53. package/components/pi-opentype-button/index.vue +501 -501
  54. package/components/pi-pagination/index.vue +290 -290
  55. package/components/pi-picker/index.vue +317 -317
  56. package/components/pi-popup/index.vue +356 -356
  57. package/components/pi-popup-select/index.vue +300 -300
  58. package/components/pi-radio/index.vue +403 -403
  59. package/components/pi-radio-group/index.vue +300 -300
  60. package/components/pi-rate/index.vue +212 -212
  61. package/components/pi-regions-select/index.vue +349 -349
  62. package/components/pi-regions-select-input/index.vue +230 -230
  63. package/components/pi-search/index.vue +386 -386
  64. package/components/pi-section/index.vue +218 -218
  65. package/components/pi-select/index.vue +678 -678
  66. package/components/pi-select-input/index.vue +273 -273
  67. package/components/pi-skeleton/index.vue +280 -280
  68. package/components/pi-status-bar/index.vue +56 -56
  69. package/components/pi-stepper/index.vue +408 -408
  70. package/components/pi-steps/index.vue +378 -378
  71. package/components/pi-subsection/index.vue +403 -403
  72. package/components/pi-swiper/index.vue +344 -344
  73. package/components/pi-switch/index.vue +231 -231
  74. package/components/pi-table/index.vue +351 -351
  75. package/components/pi-tabs/index.vue +435 -435
  76. package/components/pi-tag/index.vue +488 -488
  77. package/components/pi-upload-img/index.vue +980 -980
  78. package/components/pi-waterfall/index.vue +130 -130
  79. package/config/actionSheet.js +24 -24
  80. package/config/badge.js +9 -9
  81. package/config/bottomBar.js +11 -11
  82. package/config/bubble.js +6 -6
  83. package/config/button.js +29 -29
  84. package/config/calendar.js +19 -19
  85. package/config/calendarInput.js +19 -19
  86. package/config/calendarPanel.js +27 -27
  87. package/config/card.js +8 -8
  88. package/config/cascader.js +22 -22
  89. package/config/checkbox.js +15 -15
  90. package/config/checkboxGroup.js +15 -15
  91. package/config/circleProgress.js +20 -20
  92. package/config/codeInput.js +13 -13
  93. package/config/collapse.js +7 -7
  94. package/config/collapseItem.js +11 -11
  95. package/config/countDown.js +12 -12
  96. package/config/countUp.js +13 -13
  97. package/config/datePickerInput.js +18 -18
  98. package/config/divider.js +10 -10
  99. package/config/dropdown.js +25 -25
  100. package/config/dropdownItem.js +26 -26
  101. package/config/form.js +29 -29
  102. package/config/formItem.js +27 -27
  103. package/config/gap.js +8 -8
  104. package/config/grid.js +9 -9
  105. package/config/gridItem.js +12 -12
  106. package/config/icon.js +11 -11
  107. package/config/img.js +21 -21
  108. package/config/imgCropper.js +26 -26
  109. package/config/index.js +28 -28
  110. package/config/input.js +37 -36
  111. package/config/line.js +8 -8
  112. package/config/lineProgress.js +14 -14
  113. package/config/list.js +12 -12
  114. package/config/listItem.js +25 -25
  115. package/config/loading.js +10 -10
  116. package/config/marquee.js +10 -10
  117. package/config/mask.js +12 -12
  118. package/config/modal.js +36 -36
  119. package/config/navbar.js +32 -32
  120. package/config/notify.js +13 -13
  121. package/config/numberKeyboard.js +21 -21
  122. package/config/pagination.js +10 -10
  123. package/config/picker.js +19 -19
  124. package/config/popup.js +23 -23
  125. package/config/popupSelect.js +35 -35
  126. package/config/radio.js +16 -16
  127. package/config/radioGroup.js +17 -17
  128. package/config/rate.js +15 -15
  129. package/config/regionsData.js +58 -58
  130. package/config/regionsSelect.js +19 -19
  131. package/config/regionsSelectInput.js +29 -29
  132. package/config/search.js +27 -27
  133. package/config/section.js +23 -23
  134. package/config/select.js +41 -41
  135. package/config/selectInput.js +29 -29
  136. package/config/skeleton.js +13 -13
  137. package/config/statusBar.js +5 -5
  138. package/config/stepper.js +19 -19
  139. package/config/steps.js +19 -19
  140. package/config/subsection.js +14 -14
  141. package/config/swiper.js +22 -22
  142. package/config/switch.js +13 -13
  143. package/config/table.js +8 -8
  144. package/config/tabs.js +23 -23
  145. package/config/tag.js +21 -21
  146. package/config/uploadImg.js +81 -81
  147. package/config/waterfall.js +6 -6
  148. package/index.js +41 -41
  149. package/mixin/component-custom.js +24 -24
  150. package/mixin/emitter.js +45 -45
  151. package/mixin/props-sync.js +65 -65
  152. package/mixin/value-sync.js +32 -32
  153. package/mixin/wx-code.js +30 -30
  154. package/package.json +28 -28
  155. package/plugins/async-validator.js +1511 -1511
  156. package/scss/animation.scss +260 -260
  157. package/scss/icon/icon.scss +595 -595
  158. package/scss/index.scss +5 -5
  159. package/scss/mixin.scss +47 -47
  160. package/tools/index.js +19 -19
@@ -1,357 +1,357 @@
1
- <template>
2
- <view
3
- :class="[customClass, 'pi-img-cropper']"
4
- :style="[customStyle]"
5
- @touchstart.stop.prevent="handleStart"
6
- @touchmove.stop.prevent="handleMove"
7
- @touchcancel.stop.prevent="handleEnd"
8
- @touchend.stop.prevent="handleEnd"
9
- >
10
- <image class="img" mode="aspectFit" :style="[imgStyle]" :src="src" />
11
- <view class="crop" :style="[cropStyle]">
12
- <view class="corner lt" @touchstart="setCutMode('lt')" />
13
- <view class="corner rt" @touchstart="setCutMode('rt')" />
14
- <view class="corner rb" @touchstart="setCutMode('rb')" />
15
- <view class="corner lb" @touchstart="setCutMode('lb')" />
16
- </view>
17
- <canvas
18
- v-if="canvasId"
19
- :id="canvasId"
20
- :canvas-id="canvasId"
21
- style="position: absolute; left: -500000px; top: -500000px"
22
- :style="canvasStyle"
23
- />
24
- </view>
25
- </template>
26
- <script>
27
- import Touch from "./touch";
28
- import Vector from "./vector";
29
- import Canvas from "./canvas";
30
- import { getConfig } from "../../config";
31
-
32
- const { imgCropper } = getConfig();
33
- export default {
34
- name: "PiImgCropper",
35
- mixins: [Touch, Vector, Canvas],
36
- props: {
37
- // 自定义样式
38
- customStyle: {
39
- type: Object,
40
- default() {
41
- // {}
42
- return imgCropper.customStyle;
43
- },
44
- },
45
- // 自定义样式类
46
- customClass: {
47
- type: String,
48
- default() {
49
- // ''
50
- return imgCropper.customClass;
51
- },
52
- },
53
- // 图片src
54
- src: {
55
- type: String,
56
- // `''`
57
- default: imgCropper.src,
58
- },
59
- // 图片格式
60
- fileType: {
61
- type: String,
62
- // `png`
63
- deault: imgCropper.fileType,
64
- },
65
- // 图片质量(0,1]
66
- quality: {
67
- type: Number,
68
- // `1`
69
- default: imgCropper.quality,
70
- },
71
- // 裁剪窗口大小
72
- cropSize: {
73
- // {`width`: `百分比 相对于容器宽度` or `px` or `rpx`, `height`: `百分比` or `px` or `rpx` }
74
- type: Object,
75
- // { width: `'50%'`, height: `'50%'`}
76
- default: () => imgCropper.cropSize,
77
- },
78
- // 最小的裁剪窗口
79
- minCropSize: {
80
- // {`width`: `百分比 相对于容器宽度` or `px` or `rpx`, `height`: `百分比` or `px` or `rpx` }
81
- type: Object,
82
- // { width: `'50px'`, height: `'50px'`}
83
- default: () => imgCropper.minCropSize,
84
- },
85
- // true: 开启图片边界检测 保证移动图片过程,当图片的边与裁剪框的边重叠时 就无法继续移动
86
- boundaryDetect: {
87
- type: Boolean,
88
- // `true`
89
- default: imgCropper.boundaryDetect,
90
- },
91
- // 是否保持裁剪框的高宽比不变
92
- keepCropRatio: {
93
- type: Boolean,
94
- // `false`
95
- default: imgCropper.keepCropRatio,
96
- },
97
- // 是否禁止旋转
98
- disableRotate: {
99
- type: Boolean,
100
- // `false`
101
- default: imgCropper.disableRotate,
102
- },
103
- // 放大裁剪窗口的倍数
104
- canvasZoom: {
105
- type: Number,
106
- // `1`
107
- default: imgCropper.canvasZoom,
108
- },
109
- // 遮罩层背景颜色
110
- maskBackground: {
111
- type: String,
112
- // 'rgba(0, 0, 0, .5)'
113
- default: imgCropper.maskBackground,
114
- },
115
- },
116
- data() {
117
- return {
118
- ratio: 1,
119
- root: {
120
- width: 0,
121
- height: 0,
122
- },
123
- img: {
124
- path: "",
125
- transform: {
126
- x: 0,
127
- y: 0,
128
- rotate: 0,
129
- scale: 1,
130
- },
131
- width: 0,
132
- height: 0,
133
- },
134
- crop: {
135
- top: 0,
136
- right: 0,
137
- bottom: 0,
138
- left: 0,
139
- ratio: 0,
140
- width: 0,
141
- height: 0,
142
- },
143
- focus: true,
144
- };
145
- },
146
- computed: {
147
- imgStyle() {
148
- const {
149
- width,
150
- height,
151
- transform: { x, y, rotate, scale },
152
- } = this.img;
153
- // 指定图片的大小、位置、旋转角度、缩放系数
154
- const style = {
155
- width: `${width}px`,
156
- height: `${height}px`,
157
- transform: `translate3d(${x}px, ${y}px, 0) rotate(${rotate}deg) scale(${scale})`,
158
- };
159
- return style;
160
- },
161
- cropStyle() {
162
- const { top: t, right: r, bottom: b, left: l } = this.crop;
163
- // 指定裁剪窗口的位置、大小、周边的遮罩层效果
164
- return {
165
- top: `${t}px`,
166
- right: `${r}px`,
167
- bottom: `${b}px`,
168
- left: `${l}px`,
169
- outline: `50000px solid ${this.maskBackground}`,
170
- };
171
- },
172
- },
173
- watch: {
174
- src() {
175
- this.initImage();
176
- },
177
- },
178
- mounted() {
179
- uni.getSystemInfo({
180
- success: (result) => {
181
- this.ratio = result.windowWidth / 750;
182
- this.$nextTick(() => {
183
- this.initUI();
184
- });
185
- },
186
- });
187
- },
188
- methods: {
189
- async initUI() {
190
- const { width, height } = await this.getContainerSize();
191
- this.root = {
192
- width,
193
- height,
194
- };
195
- this.initImage();
196
- this.initCropWindow();
197
- },
198
- getContainerSize() {
199
- return new Promise((resolve) => {
200
- let query = uni.createSelectorQuery();
201
- // #ifndef MP-ALIPAY
202
- query = query.in(this);
203
- // #endif
204
- const node = query.select(".pi-img-cropper");
205
- node
206
- .boundingClientRect((result) => {
207
- resolve(result);
208
- })
209
- .exec();
210
- });
211
- },
212
- async initImage() {
213
- if (!this.src) {
214
- return;
215
- }
216
- const img = await this.loadImageInfo(this.src);
217
- let w = 0,
218
- h = 0;
219
- const root = this.root;
220
- if (img.height > img.width) {
221
- // 竖向图片:高度铺满屏幕高度、宽度等比计算
222
- h = root.height;
223
- w = (h / img.height) * img.width;
224
- if (w > root.width) {
225
- // 若计算后的宽度超出屏幕宽度 则等比缩小
226
- w = root.width;
227
- h = (w / img.width) * img.height;
228
- }
229
- } else {
230
- // 横向图片
231
- w = root.width;
232
- h = (w / img.width) * img.height;
233
- if (h > root.height) {
234
- h = root.height;
235
- w = (h / img.height) * img.width;
236
- }
237
- }
238
- const x = (root.width - img.width) / 2;
239
- const y = (root.height - img.height) / 2;
240
- const scale = w / img.width;
241
- this.img = {
242
- path: img.path,
243
- width: img.width,
244
- height: img.height,
245
- transform: {
246
- x,
247
- y,
248
- rotate: 0,
249
- scale,
250
- },
251
- };
252
- },
253
- async loadImageInfo(src) {
254
- return new Promise((resolve) => {
255
- uni.getImageInfo({
256
- src: this.src,
257
- success: (result) => {
258
- resolve(result);
259
- },
260
- fail: (error) => {
261
- resolve(null);
262
- },
263
- });
264
- });
265
- },
266
- initCropWindow() {
267
- // 初始化裁剪窗口
268
- const root = this.root;
269
- let w = this.toPx(this.cropSize.width);
270
- let h = this.toPx(this.cropSize.height);
271
- const minW = this.toPx(this.minCropSize.width);
272
- const minH = this.toPx(this.minCropSize.height);
273
- w = w < minW ? minW : w > root.width ? root.width : w;
274
- h = h < minH ? minH : h > root.height ? root.height : h;
275
- const ratio = h / w;
276
- const left = (root.width - w) / 2;
277
- const top = (root.height - h) / 2;
278
- this.crop = {
279
- top,
280
- left,
281
- right: left,
282
- bottom: top,
283
- width: w,
284
- height: h,
285
- ratio,
286
- };
287
- },
288
- toPx(unit) {
289
- let value = 0;
290
- if (unit) {
291
- if (/%$/.test(unit)) {
292
- value = Number(unit.replace(/%$/, ""));
293
- value = !value ? 0 : value;
294
- value = value * 0.01 * this.root.width;
295
- } else if (/rpx$/i.test(unit)) {
296
- value = Number(unit.replace(/rpx$/i, ""));
297
- value = !value ? 0 : value;
298
- value = value * this.ratio;
299
- } else {
300
- value = Number(unit.replace(/px$/i, ""));
301
- }
302
- }
303
- return value;
304
- },
305
- },
306
- };
307
- </script>
308
-
309
- <style lang="scss" scoped>
310
- .pi-img-cropper {
311
- position: relative;
312
- width: 100%;
313
- height: 100%;
314
- overflow: hidden;
315
- }
316
-
317
- .img {
318
- position: absolute;
319
- transform-origin: 50% 50%;
320
- }
321
-
322
- .crop {
323
- position: absolute;
324
- background: transparent;
325
- }
326
-
327
- .corner {
328
- position: absolute;
329
- width: 40rpx;
330
- height: 40rpx;
331
- border: 8rpx solid #ffffff;
332
- &.lt {
333
- top: 0;
334
- left: 0;
335
- border-right: none;
336
- border-bottom: none;
337
- }
338
- &.rt {
339
- top: 0;
340
- right: 0;
341
- border-left: none;
342
- border-bottom: none;
343
- }
344
- &.rb {
345
- right: 0;
346
- bottom: 0;
347
- border-top: none;
348
- border-left: none;
349
- }
350
- &.lb {
351
- left: 0;
352
- bottom: 0;
353
- border-top: none;
354
- border-right: none;
355
- }
356
- }
357
- </style>
1
+ <template>
2
+ <view
3
+ :class="[customClass, 'pi-img-cropper']"
4
+ :style="[customStyle]"
5
+ @touchstart.stop.prevent="handleStart"
6
+ @touchmove.stop.prevent="handleMove"
7
+ @touchcancel.stop.prevent="handleEnd"
8
+ @touchend.stop.prevent="handleEnd"
9
+ >
10
+ <image class="img" mode="aspectFit" :style="[imgStyle]" :src="src" />
11
+ <view class="crop" :style="[cropStyle]">
12
+ <view class="corner lt" @touchstart="setCutMode('lt')" />
13
+ <view class="corner rt" @touchstart="setCutMode('rt')" />
14
+ <view class="corner rb" @touchstart="setCutMode('rb')" />
15
+ <view class="corner lb" @touchstart="setCutMode('lb')" />
16
+ </view>
17
+ <canvas
18
+ v-if="canvasId"
19
+ :id="canvasId"
20
+ :canvas-id="canvasId"
21
+ style="position: absolute; left: -500000px; top: -500000px"
22
+ :style="canvasStyle"
23
+ />
24
+ </view>
25
+ </template>
26
+ <script>
27
+ import Touch from "./touch";
28
+ import Vector from "./vector";
29
+ import Canvas from "./canvas";
30
+ import { getConfig } from "../../config";
31
+
32
+ const { imgCropper } = getConfig();
33
+ export default {
34
+ name: "PiImgCropper",
35
+ mixins: [Touch, Vector, Canvas],
36
+ props: {
37
+ // 自定义样式
38
+ customStyle: {
39
+ type: Object,
40
+ default() {
41
+ // {}
42
+ return imgCropper.customStyle;
43
+ },
44
+ },
45
+ // 自定义样式类
46
+ customClass: {
47
+ type: String,
48
+ default() {
49
+ // ''
50
+ return imgCropper.customClass;
51
+ },
52
+ },
53
+ // 图片src
54
+ src: {
55
+ type: String,
56
+ // `''`
57
+ default: imgCropper.src,
58
+ },
59
+ // 图片格式
60
+ fileType: {
61
+ type: String,
62
+ // `png`
63
+ deault: imgCropper.fileType,
64
+ },
65
+ // 图片质量(0,1]
66
+ quality: {
67
+ type: Number,
68
+ // `1`
69
+ default: imgCropper.quality,
70
+ },
71
+ // 裁剪窗口大小
72
+ cropSize: {
73
+ // {`width`: `百分比 相对于容器宽度` or `px` or `rpx`, `height`: `百分比` or `px` or `rpx` }
74
+ type: Object,
75
+ // { width: `'50%'`, height: `'50%'`}
76
+ default: () => imgCropper.cropSize,
77
+ },
78
+ // 最小的裁剪窗口
79
+ minCropSize: {
80
+ // {`width`: `百分比 相对于容器宽度` or `px` or `rpx`, `height`: `百分比` or `px` or `rpx` }
81
+ type: Object,
82
+ // { width: `'50px'`, height: `'50px'`}
83
+ default: () => imgCropper.minCropSize,
84
+ },
85
+ // true: 开启图片边界检测 保证移动图片过程,当图片的边与裁剪框的边重叠时 就无法继续移动
86
+ boundaryDetect: {
87
+ type: Boolean,
88
+ // `true`
89
+ default: imgCropper.boundaryDetect,
90
+ },
91
+ // 是否保持裁剪框的高宽比不变
92
+ keepCropRatio: {
93
+ type: Boolean,
94
+ // `false`
95
+ default: imgCropper.keepCropRatio,
96
+ },
97
+ // 是否禁止旋转
98
+ disableRotate: {
99
+ type: Boolean,
100
+ // `false`
101
+ default: imgCropper.disableRotate,
102
+ },
103
+ // 放大裁剪窗口的倍数
104
+ canvasZoom: {
105
+ type: Number,
106
+ // `1`
107
+ default: imgCropper.canvasZoom,
108
+ },
109
+ // 遮罩层背景颜色
110
+ maskBackground: {
111
+ type: String,
112
+ // 'rgba(0, 0, 0, .5)'
113
+ default: imgCropper.maskBackground,
114
+ },
115
+ },
116
+ data() {
117
+ return {
118
+ ratio: 1,
119
+ root: {
120
+ width: 0,
121
+ height: 0,
122
+ },
123
+ img: {
124
+ path: "",
125
+ transform: {
126
+ x: 0,
127
+ y: 0,
128
+ rotate: 0,
129
+ scale: 1,
130
+ },
131
+ width: 0,
132
+ height: 0,
133
+ },
134
+ crop: {
135
+ top: 0,
136
+ right: 0,
137
+ bottom: 0,
138
+ left: 0,
139
+ ratio: 0,
140
+ width: 0,
141
+ height: 0,
142
+ },
143
+ focus: true,
144
+ };
145
+ },
146
+ computed: {
147
+ imgStyle() {
148
+ const {
149
+ width,
150
+ height,
151
+ transform: { x, y, rotate, scale },
152
+ } = this.img;
153
+ // 指定图片的大小、位置、旋转角度、缩放系数
154
+ const style = {
155
+ width: `${width}px`,
156
+ height: `${height}px`,
157
+ transform: `translate3d(${x}px, ${y}px, 0) rotate(${rotate}deg) scale(${scale})`,
158
+ };
159
+ return style;
160
+ },
161
+ cropStyle() {
162
+ const { top: t, right: r, bottom: b, left: l } = this.crop;
163
+ // 指定裁剪窗口的位置、大小、周边的遮罩层效果
164
+ return {
165
+ top: `${t}px`,
166
+ right: `${r}px`,
167
+ bottom: `${b}px`,
168
+ left: `${l}px`,
169
+ outline: `50000px solid ${this.maskBackground}`,
170
+ };
171
+ },
172
+ },
173
+ watch: {
174
+ src() {
175
+ this.initImage();
176
+ },
177
+ },
178
+ mounted() {
179
+ uni.getSystemInfo({
180
+ success: (result) => {
181
+ this.ratio = result.windowWidth / 750;
182
+ this.$nextTick(() => {
183
+ this.initUI();
184
+ });
185
+ },
186
+ });
187
+ },
188
+ methods: {
189
+ async initUI() {
190
+ const { width, height } = await this.getContainerSize();
191
+ this.root = {
192
+ width,
193
+ height,
194
+ };
195
+ this.initImage();
196
+ this.initCropWindow();
197
+ },
198
+ getContainerSize() {
199
+ return new Promise((resolve) => {
200
+ let query = uni.createSelectorQuery();
201
+ // #ifndef MP-ALIPAY
202
+ query = query.in(this);
203
+ // #endif
204
+ const node = query.select(".pi-img-cropper");
205
+ node
206
+ .boundingClientRect((result) => {
207
+ resolve(result);
208
+ })
209
+ .exec();
210
+ });
211
+ },
212
+ async initImage() {
213
+ if (!this.src) {
214
+ return;
215
+ }
216
+ const img = await this.loadImageInfo(this.src);
217
+ let w = 0,
218
+ h = 0;
219
+ const root = this.root;
220
+ if (img.height > img.width) {
221
+ // 竖向图片:高度铺满屏幕高度、宽度等比计算
222
+ h = root.height;
223
+ w = (h / img.height) * img.width;
224
+ if (w > root.width) {
225
+ // 若计算后的宽度超出屏幕宽度 则等比缩小
226
+ w = root.width;
227
+ h = (w / img.width) * img.height;
228
+ }
229
+ } else {
230
+ // 横向图片
231
+ w = root.width;
232
+ h = (w / img.width) * img.height;
233
+ if (h > root.height) {
234
+ h = root.height;
235
+ w = (h / img.height) * img.width;
236
+ }
237
+ }
238
+ const x = (root.width - img.width) / 2;
239
+ const y = (root.height - img.height) / 2;
240
+ const scale = w / img.width;
241
+ this.img = {
242
+ path: img.path,
243
+ width: img.width,
244
+ height: img.height,
245
+ transform: {
246
+ x,
247
+ y,
248
+ rotate: 0,
249
+ scale,
250
+ },
251
+ };
252
+ },
253
+ async loadImageInfo(src) {
254
+ return new Promise((resolve) => {
255
+ uni.getImageInfo({
256
+ src: this.src,
257
+ success: (result) => {
258
+ resolve(result);
259
+ },
260
+ fail: (error) => {
261
+ resolve(null);
262
+ },
263
+ });
264
+ });
265
+ },
266
+ initCropWindow() {
267
+ // 初始化裁剪窗口
268
+ const root = this.root;
269
+ let w = this.toPx(this.cropSize.width);
270
+ let h = this.toPx(this.cropSize.height);
271
+ const minW = this.toPx(this.minCropSize.width);
272
+ const minH = this.toPx(this.minCropSize.height);
273
+ w = w < minW ? minW : w > root.width ? root.width : w;
274
+ h = h < minH ? minH : h > root.height ? root.height : h;
275
+ const ratio = h / w;
276
+ const left = (root.width - w) / 2;
277
+ const top = (root.height - h) / 2;
278
+ this.crop = {
279
+ top,
280
+ left,
281
+ right: left,
282
+ bottom: top,
283
+ width: w,
284
+ height: h,
285
+ ratio,
286
+ };
287
+ },
288
+ toPx(unit) {
289
+ let value = 0;
290
+ if (unit) {
291
+ if (/%$/.test(unit)) {
292
+ value = Number(unit.replace(/%$/, ""));
293
+ value = !value ? 0 : value;
294
+ value = value * 0.01 * this.root.width;
295
+ } else if (/rpx$/i.test(unit)) {
296
+ value = Number(unit.replace(/rpx$/i, ""));
297
+ value = !value ? 0 : value;
298
+ value = value * this.ratio;
299
+ } else {
300
+ value = Number(unit.replace(/px$/i, ""));
301
+ }
302
+ }
303
+ return value;
304
+ },
305
+ },
306
+ };
307
+ </script>
308
+
309
+ <style lang="scss" scoped>
310
+ .pi-img-cropper {
311
+ position: relative;
312
+ width: 100%;
313
+ height: 100%;
314
+ overflow: hidden;
315
+ }
316
+
317
+ .img {
318
+ position: absolute;
319
+ transform-origin: 50% 50%;
320
+ }
321
+
322
+ .crop {
323
+ position: absolute;
324
+ background: transparent;
325
+ }
326
+
327
+ .corner {
328
+ position: absolute;
329
+ width: 40rpx;
330
+ height: 40rpx;
331
+ border: 8rpx solid #ffffff;
332
+ &.lt {
333
+ top: 0;
334
+ left: 0;
335
+ border-right: none;
336
+ border-bottom: none;
337
+ }
338
+ &.rt {
339
+ top: 0;
340
+ right: 0;
341
+ border-left: none;
342
+ border-bottom: none;
343
+ }
344
+ &.rb {
345
+ right: 0;
346
+ bottom: 0;
347
+ border-top: none;
348
+ border-left: none;
349
+ }
350
+ &.lb {
351
+ left: 0;
352
+ bottom: 0;
353
+ border-top: none;
354
+ border-right: none;
355
+ }
356
+ }
357
+ </style>