maooo-skyline-ui-system 0.1.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 (117) hide show
  1. package/README.md +157 -0
  2. package/miniprogram_dist/button/button.js +43 -0
  3. package/miniprogram_dist/button/button.json +5 -0
  4. package/miniprogram_dist/button/button.wxml +12 -0
  5. package/miniprogram_dist/button/button.wxs +23 -0
  6. package/miniprogram_dist/button/button.wxss +133 -0
  7. package/miniprogram_dist/calendar/calendar.js +549 -0
  8. package/miniprogram_dist/calendar/calendar.json +8 -0
  9. package/miniprogram_dist/calendar/calendar.wxml +79 -0
  10. package/miniprogram_dist/calendar/calendar.wxss +265 -0
  11. package/miniprogram_dist/card/card.js +22 -0
  12. package/miniprogram_dist/card/card.json +5 -0
  13. package/miniprogram_dist/card/card.wxml +9 -0
  14. package/miniprogram_dist/card/card.wxss +34 -0
  15. package/miniprogram_dist/countdown/countdown.js +213 -0
  16. package/miniprogram_dist/countdown/countdown.json +3 -0
  17. package/miniprogram_dist/countdown/countdown.wxml +9 -0
  18. package/miniprogram_dist/countdown/countdown.wxss +33 -0
  19. package/miniprogram_dist/form/form.js +12 -0
  20. package/miniprogram_dist/form/form.json +5 -0
  21. package/miniprogram_dist/form/form.wxml +12 -0
  22. package/miniprogram_dist/form/form.wxss +35 -0
  23. package/miniprogram_dist/form-group/form-group.js +53 -0
  24. package/miniprogram_dist/form-group/form-group.json +5 -0
  25. package/miniprogram_dist/form-group/form-group.wxml +11 -0
  26. package/miniprogram_dist/form-group/form-group.wxss +50 -0
  27. package/miniprogram_dist/form-input/form-input.js +48 -0
  28. package/miniprogram_dist/form-input/form-input.json +8 -0
  29. package/miniprogram_dist/form-input/form-input.wxml +28 -0
  30. package/miniprogram_dist/form-input/form-input.wxss +3 -0
  31. package/miniprogram_dist/form-textarea/form-textarea.js +34 -0
  32. package/miniprogram_dist/form-textarea/form-textarea.json +8 -0
  33. package/miniprogram_dist/form-textarea/form-textarea.wxml +20 -0
  34. package/miniprogram_dist/form-textarea/form-textarea.wxss +3 -0
  35. package/miniprogram_dist/icon/_iconfont/iconfont.js +63 -0
  36. package/miniprogram_dist/icon/_iconfont/iconfont.json +4 -0
  37. package/miniprogram_dist/icon/_iconfont/iconfont.wxml +8 -0
  38. package/miniprogram_dist/icon/_iconfont/iconfont.wxss +3 -0
  39. package/miniprogram_dist/icon/icon.js +41 -0
  40. package/miniprogram_dist/icon/icon.json +7 -0
  41. package/miniprogram_dist/icon/icon.wxml +3 -0
  42. package/miniprogram_dist/icon/icon.wxss +13 -0
  43. package/miniprogram_dist/image-cropper/image-cropper.js +703 -0
  44. package/miniprogram_dist/image-cropper/image-cropper.json +3 -0
  45. package/miniprogram_dist/image-cropper/image-cropper.wxml +58 -0
  46. package/miniprogram_dist/image-cropper/image-cropper.wxss +195 -0
  47. package/miniprogram_dist/index.js +4 -0
  48. package/miniprogram_dist/input/input.js +119 -0
  49. package/miniprogram_dist/input/input.json +7 -0
  50. package/miniprogram_dist/input/input.wxml +38 -0
  51. package/miniprogram_dist/input/input.wxss +52 -0
  52. package/miniprogram_dist/media-upload/media-upload.js +1108 -0
  53. package/miniprogram_dist/media-upload/media-upload.json +7 -0
  54. package/miniprogram_dist/media-upload/media-upload.wxml +98 -0
  55. package/miniprogram_dist/media-upload/media-upload.wxss +149 -0
  56. package/miniprogram_dist/navbar/navbar.js +191 -0
  57. package/miniprogram_dist/navbar/navbar.json +6 -0
  58. package/miniprogram_dist/navbar/navbar.wxml +25 -0
  59. package/miniprogram_dist/navbar/navbar.wxss +60 -0
  60. package/miniprogram_dist/notify/notify.js +342 -0
  61. package/miniprogram_dist/notify/notify.json +5 -0
  62. package/miniprogram_dist/notify/notify.wxml +27 -0
  63. package/miniprogram_dist/notify/notify.wxss +246 -0
  64. package/miniprogram_dist/notify/service.js +69 -0
  65. package/miniprogram_dist/oss-upload/oss-upload.js +505 -0
  66. package/miniprogram_dist/oss-upload/oss-upload.json +6 -0
  67. package/miniprogram_dist/oss-upload/oss-upload.wxml +20 -0
  68. package/miniprogram_dist/oss-upload/oss-upload.wxss +5 -0
  69. package/miniprogram_dist/popup/popup.js +36 -0
  70. package/miniprogram_dist/popup/popup.json +5 -0
  71. package/miniprogram_dist/popup/popup.wxml +14 -0
  72. package/miniprogram_dist/popup/popup.wxss +138 -0
  73. package/miniprogram_dist/popup-page/popup-page.js +341 -0
  74. package/miniprogram_dist/popup-page/popup-page.json +8 -0
  75. package/miniprogram_dist/popup-page/popup-page.wxml +65 -0
  76. package/miniprogram_dist/popup-page/popup-page.wxss +92 -0
  77. package/miniprogram_dist/progress/progress.js +161 -0
  78. package/miniprogram_dist/progress/progress.json +5 -0
  79. package/miniprogram_dist/progress/progress.wxml +16 -0
  80. package/miniprogram_dist/progress/progress.wxss +81 -0
  81. package/miniprogram_dist/rate/rate.js +127 -0
  82. package/miniprogram_dist/rate/rate.json +5 -0
  83. package/miniprogram_dist/rate/rate.wxml +32 -0
  84. package/miniprogram_dist/rate/rate.wxs +21 -0
  85. package/miniprogram_dist/rate/rate.wxss +126 -0
  86. package/miniprogram_dist/stepper/stepper.js +153 -0
  87. package/miniprogram_dist/stepper/stepper.json +5 -0
  88. package/miniprogram_dist/stepper/stepper.wxml +19 -0
  89. package/miniprogram_dist/stepper/stepper.wxss +66 -0
  90. package/miniprogram_dist/switch/switch.js +29 -0
  91. package/miniprogram_dist/switch/switch.json +3 -0
  92. package/miniprogram_dist/switch/switch.wxml +10 -0
  93. package/miniprogram_dist/switch/switch.wxss +71 -0
  94. package/miniprogram_dist/textarea/textarea.js +83 -0
  95. package/miniprogram_dist/textarea/textarea.json +5 -0
  96. package/miniprogram_dist/textarea/textarea.wxml +14 -0
  97. package/miniprogram_dist/textarea/textarea.wxss +33 -0
  98. package/miniprogram_dist/theme/theme.wxss +64 -0
  99. package/miniprogram_dist/toast/toast.js +51 -0
  100. package/miniprogram_dist/toast/toast.json +5 -0
  101. package/miniprogram_dist/toast/toast.wxml +13 -0
  102. package/miniprogram_dist/toast/toast.wxss +143 -0
  103. package/miniprogram_dist/upload/upload.js +276 -0
  104. package/miniprogram_dist/upload/upload.json +3 -0
  105. package/miniprogram_dist/upload/upload.wxml +36 -0
  106. package/miniprogram_dist/upload/upload.wxss +136 -0
  107. package/miniprogram_dist/utils/class.js +37 -0
  108. package/miniprogram_dist/utils/device.js +67 -0
  109. package/miniprogram_dist/utils/dom.js +51 -0
  110. package/miniprogram_dist/utils/event.js +28 -0
  111. package/miniprogram_dist/utils/form.js +39 -0
  112. package/miniprogram_dist/utils/function.js +63 -0
  113. package/miniprogram_dist/utils/index.js +21 -0
  114. package/miniprogram_dist/utils/props.js +51 -0
  115. package/miniprogram_dist/utils/style.js +52 -0
  116. package/miniprogram_dist/utils/theme.js +30 -0
  117. package/package.json +31 -0
@@ -0,0 +1,703 @@
1
+ const MAX_SCALE = 4;
2
+ const DEFAULT_OUTPUT_WIDTH = 750;
3
+ const PREVIEW_CANVAS_ID = "moImageCropperPreview";
4
+ const OUTPUT_CANVAS_ID = "moImageCropperOutput";
5
+ const SINGLE_CROP_RATIO = 0.86;
6
+
7
+ function getWindowInfo() {
8
+ if (wx.getWindowInfo) return wx.getWindowInfo();
9
+ return wx.getSystemInfoSync();
10
+ }
11
+
12
+ function getMenuButtonRect() {
13
+ if (!wx.getMenuButtonBoundingClientRect) return null;
14
+
15
+ try {
16
+ return wx.getMenuButtonBoundingClientRect();
17
+ } catch (error) {
18
+ return null;
19
+ }
20
+ }
21
+
22
+ function getHeaderStyle() {
23
+ const windowInfo = getWindowInfo();
24
+ const menuButton = getMenuButtonRect();
25
+ const statusBarHeight = windowInfo.statusBarHeight || (windowInfo.safeArea && windowInfo.safeArea.top) || 0;
26
+ const navHeight = menuButton && menuButton.top > statusBarHeight
27
+ ? (menuButton.top - statusBarHeight) * 2 + menuButton.height
28
+ : 48;
29
+ const headerHeight = statusBarHeight + navHeight;
30
+
31
+ return [
32
+ `height: ${Math.round(headerHeight)}px`,
33
+ `padding-top: ${Math.round(statusBarHeight)}px`
34
+ ].join("; ");
35
+ }
36
+
37
+ function normalizeArray(value) {
38
+ return Array.isArray(value) ? value : [];
39
+ }
40
+
41
+ function normalizeAspectRatio(value) {
42
+ const numberValue = Number(value);
43
+ return Number.isFinite(numberValue) && numberValue > 0 ? numberValue : 1;
44
+ }
45
+
46
+ function normalizeQuality(value) {
47
+ const numberValue = Number(value);
48
+ return Number.isFinite(numberValue) && numberValue > 0 && numberValue <= 1 ? numberValue : 1;
49
+ }
50
+
51
+ function normalizeOutputWidth(value) {
52
+ const numberValue = Number(value);
53
+ return Number.isFinite(numberValue) && numberValue > 0 ? Math.round(numberValue) : DEFAULT_OUTPUT_WIDTH;
54
+ }
55
+
56
+ function getTouchDistance(touches) {
57
+ if (!touches || touches.length < 2) return 0;
58
+
59
+ const dx = touches[0].x - touches[1].x;
60
+ const dy = touches[0].y - touches[1].y;
61
+
62
+ return Math.sqrt(dx * dx + dy * dy);
63
+ }
64
+
65
+ function getTouchCenter(touches) {
66
+ if (!touches || !touches.length) return { x: 0, y: 0 };
67
+
68
+ if (touches.length === 1) {
69
+ return {
70
+ x: touches[0].x,
71
+ y: touches[0].y
72
+ };
73
+ }
74
+
75
+ return {
76
+ x: (touches[0].x + touches[1].x) / 2,
77
+ y: (touches[0].y + touches[1].y) / 2
78
+ };
79
+ }
80
+
81
+ function styleSize(width, height) {
82
+ return `width: ${Math.round(width)}px; height: ${Math.round(height)}px;`;
83
+ }
84
+
85
+ function cloneTransform(transform) {
86
+ return {
87
+ imageX: transform.imageX,
88
+ imageY: transform.imageY,
89
+ scale: transform.scale,
90
+ minScale: transform.minScale
91
+ };
92
+ }
93
+
94
+ function getCoverTransform(imageInfo, box) {
95
+ const scale = Math.max(box.width / imageInfo.width, box.height / imageInfo.height);
96
+
97
+ return {
98
+ imageX: box.x + (box.width - imageInfo.width * scale) / 2,
99
+ imageY: box.y + (box.height - imageInfo.height * scale) / 2,
100
+ scale,
101
+ minScale: scale
102
+ };
103
+ }
104
+
105
+ function getFileUrl(file) {
106
+ return file.url || file.tempFilePath || file.thumb || "";
107
+ }
108
+
109
+ function normalizeInputFile(file, index) {
110
+ const safeFile = file && typeof file === "object" ? file : {};
111
+ const url = getFileUrl(safeFile);
112
+
113
+ return {
114
+ ...safeFile,
115
+ id: safeFile.id || `image_cropper_${Date.now()}_${index}`,
116
+ url,
117
+ tempFilePath: safeFile.tempFilePath || url,
118
+ type: "image"
119
+ };
120
+ }
121
+
122
+ function runAfterNextTick(callback) {
123
+ if (wx.nextTick) {
124
+ wx.nextTick(callback);
125
+ return;
126
+ }
127
+
128
+ setTimeout(callback, 0);
129
+ }
130
+
131
+ function getImageInfo(src) {
132
+ return new Promise((resolve, reject) => {
133
+ wx.getImageInfo({
134
+ src,
135
+ success: resolve,
136
+ fail: reject
137
+ });
138
+ });
139
+ }
140
+
141
+ Component({
142
+ properties: {
143
+ show: {
144
+ type: Boolean,
145
+ value: false
146
+ },
147
+ src: {
148
+ type: String,
149
+ value: ""
150
+ },
151
+ fileList: {
152
+ type: Array,
153
+ value: []
154
+ },
155
+ cropIconName: {
156
+ type: String,
157
+ value: ""
158
+ },
159
+ aspectRatio: {
160
+ type: Number,
161
+ value: 1
162
+ },
163
+ outputWidth: {
164
+ type: Number,
165
+ value: DEFAULT_OUTPUT_WIDTH
166
+ },
167
+ quality: {
168
+ type: Number,
169
+ value: 1
170
+ },
171
+ zIndex: {
172
+ type: Number,
173
+ value: 999
174
+ },
175
+ customClass: {
176
+ type: String,
177
+ value: ""
178
+ }
179
+ },
180
+ data: {
181
+ confirming: false,
182
+ currentIndex: 0,
183
+ cropperModeClass: "",
184
+ displayList: [],
185
+ headerStyle: "",
186
+ stageStyle: "",
187
+ outputCanvasStyle: ""
188
+ },
189
+ observers: {
190
+ "show, src, fileList, aspectRatio, outputWidth"(show) {
191
+ if (!show) {
192
+ this.resetCropperState();
193
+ return;
194
+ }
195
+
196
+ this.initCropper();
197
+ }
198
+ },
199
+ lifetimes: {
200
+ attached() {
201
+ this.resetInternalState();
202
+ },
203
+ detached() {
204
+ this.resetInternalState();
205
+ }
206
+ },
207
+ methods: {
208
+ resetInternalState() {
209
+ this.items = [];
210
+ this.cropBox = null;
211
+ this.stage = null;
212
+ this.touchState = null;
213
+ this.isSingleMode = false;
214
+ this.drawToken = (this.drawToken || 0) + 1;
215
+ this.initToken = (this.initToken || 0) + 1;
216
+ },
217
+ resetCropperState() {
218
+ this.resetInternalState();
219
+ this.setData({
220
+ confirming: false,
221
+ currentIndex: 0,
222
+ cropperModeClass: "",
223
+ displayList: [],
224
+ headerStyle: "",
225
+ stageStyle: "",
226
+ outputCanvasStyle: ""
227
+ });
228
+ },
229
+ emitError(source, message, error, extra) {
230
+ this.triggerEvent("error", {
231
+ source,
232
+ message,
233
+ error,
234
+ ...(extra || {})
235
+ });
236
+ },
237
+ getInputFiles() {
238
+ const fileList = normalizeArray(this.data.fileList).map((file, index) => normalizeInputFile(file, index));
239
+
240
+ if (fileList.length) return fileList;
241
+ if (!this.data.src) return [];
242
+
243
+ return [
244
+ normalizeInputFile(
245
+ {
246
+ id: "image_cropper_single",
247
+ url: this.data.src,
248
+ tempFilePath: this.data.src
249
+ },
250
+ 0
251
+ )
252
+ ];
253
+ },
254
+ initCropper() {
255
+ const inputFiles = this.getInputFiles();
256
+ const token = (this.initToken || 0) + 1;
257
+
258
+ this.initToken = token;
259
+
260
+ if (!inputFiles.length) {
261
+ this.emitError("load", "图片地址不能为空");
262
+ return;
263
+ }
264
+
265
+ this.setData({
266
+ confirming: false,
267
+ currentIndex: 0,
268
+ cropperModeClass: inputFiles.length <= 1 ? "is-single" : "is-multi"
269
+ });
270
+ this.setupLayout(inputFiles.length <= 1);
271
+
272
+ Promise.all(
273
+ inputFiles.map((file, index) =>
274
+ getImageInfo(file.tempFilePath || file.url).then(
275
+ (imageInfo) => this.createCropperItem(file, imageInfo, index),
276
+ (error) => {
277
+ throw {
278
+ source: "load",
279
+ message: "图片加载失败",
280
+ error,
281
+ index,
282
+ file
283
+ };
284
+ }
285
+ )
286
+ )
287
+ )
288
+ .then((items) => {
289
+ if (!this.data.show || this.initToken !== token) return;
290
+
291
+ this.items = items;
292
+ this.syncLayoutStyles(() => {
293
+ this.syncDisplayList();
294
+ this.queuePreviewDraw();
295
+ });
296
+ })
297
+ .catch((errorDetail) => {
298
+ if (!this.data.show || this.initToken !== token) return;
299
+
300
+ this.emitError(errorDetail.source || "load", errorDetail.message || "图片加载失败", errorDetail.error || errorDetail, {
301
+ index: errorDetail.index,
302
+ file: errorDetail.file
303
+ });
304
+ });
305
+ },
306
+ setupLayout(isSingleMode) {
307
+ const windowInfo = getWindowInfo();
308
+ const stageWidth = Math.max(240, windowInfo.windowWidth);
309
+ const stageHeight = isSingleMode ? stageWidth : Math.round(stageWidth / normalizeAspectRatio(this.data.aspectRatio));
310
+ const cropSize = Math.round(stageWidth * SINGLE_CROP_RATIO);
311
+ const outputWidth = normalizeOutputWidth(this.data.outputWidth);
312
+ const outputHeight = isSingleMode ? outputWidth : Math.round(outputWidth / normalizeAspectRatio(this.data.aspectRatio));
313
+
314
+ this.isSingleMode = Boolean(isSingleMode);
315
+ this.stage = {
316
+ width: stageWidth,
317
+ height: stageHeight
318
+ };
319
+ this.cropBox = {
320
+ x: isSingleMode ? (stageWidth - cropSize) / 2 : 0,
321
+ y: isSingleMode ? (stageHeight - cropSize) / 2 : 0,
322
+ width: isSingleMode ? cropSize : stageWidth,
323
+ height: isSingleMode ? cropSize : stageHeight
324
+ };
325
+ this.outputSize = {
326
+ width: outputWidth,
327
+ height: outputHeight
328
+ };
329
+ },
330
+ createInitialTransform(imageInfo) {
331
+ const transform = getCoverTransform(imageInfo, this.cropBox);
332
+
333
+ return this.limitTransform(transform, imageInfo);
334
+ },
335
+ createCropperItem(file, imageInfo) {
336
+ const initialTransform = this.createInitialTransform(imageInfo);
337
+
338
+ return {
339
+ file,
340
+ imageInfo,
341
+ originalUrl: file.url,
342
+ originalTempFilePath: file.tempFilePath || file.url,
343
+ initialTransform: cloneTransform(initialTransform),
344
+ transform: cloneTransform(initialTransform),
345
+ cropped: Boolean(file.cropped),
346
+ cropInfo: file.cropInfo || null,
347
+ croppedTempFilePath: file.cropped && file.tempFilePath !== file.url ? file.tempFilePath : ""
348
+ };
349
+ },
350
+ syncLayoutStyles(callback) {
351
+ if (!this.stage || !this.outputSize) return;
352
+
353
+ this.setData(
354
+ {
355
+ headerStyle: getHeaderStyle(),
356
+ stageStyle: styleSize(this.stage.width, this.stage.height),
357
+ outputCanvasStyle: styleSize(this.outputSize.width, this.outputSize.height)
358
+ },
359
+ () => {
360
+ if (typeof callback === "function") callback();
361
+ }
362
+ );
363
+ },
364
+ syncDisplayList() {
365
+ this.setData({
366
+ displayList: this.items.map((item, index) => ({
367
+ id: item.file.id || `image_cropper_display_${index}`,
368
+ url: item.croppedTempFilePath || item.file.url || item.file.tempFilePath,
369
+ active: index === this.data.currentIndex,
370
+ cropped: item.cropped,
371
+ rowEnd: index % 3 === 2
372
+ }))
373
+ });
374
+ },
375
+ queuePreviewDraw() {
376
+ const token = this.drawToken + 1;
377
+
378
+ this.drawToken = token;
379
+ runAfterNextTick(() => {
380
+ if (this.drawToken !== token || !this.data.show) return;
381
+
382
+ this.drawPreview();
383
+ });
384
+ },
385
+ getCurrentItem() {
386
+ return this.items[this.data.currentIndex];
387
+ },
388
+ limitTransform(transform, imageInfo) {
389
+ const imageWidth = imageInfo.width * transform.scale;
390
+ const imageHeight = imageInfo.height * transform.scale;
391
+ const minX = this.cropBox.x + this.cropBox.width - imageWidth;
392
+ const maxX = this.cropBox.x;
393
+ const minY = this.cropBox.y + this.cropBox.height - imageHeight;
394
+ const maxY = this.cropBox.y;
395
+ const nextTransform = cloneTransform(transform);
396
+
397
+ if (imageWidth <= this.cropBox.width) {
398
+ nextTransform.imageX = this.cropBox.x + (this.cropBox.width - imageWidth) / 2;
399
+ } else {
400
+ nextTransform.imageX = Math.min(maxX, Math.max(minX, nextTransform.imageX));
401
+ }
402
+
403
+ if (imageHeight <= this.cropBox.height) {
404
+ nextTransform.imageY = this.cropBox.y + (this.cropBox.height - imageHeight) / 2;
405
+ } else {
406
+ nextTransform.imageY = Math.min(maxY, Math.max(minY, nextTransform.imageY));
407
+ }
408
+
409
+ return nextTransform;
410
+ },
411
+ drawPreview() {
412
+ const item = this.getCurrentItem();
413
+
414
+ if (!this.data.show || !item || !this.stage || !this.cropBox) return;
415
+
416
+ const context = wx.createCanvasContext(PREVIEW_CANVAS_ID, this);
417
+ const transform = item.transform;
418
+
419
+ context.setFillStyle("#000000");
420
+ context.fillRect(0, 0, this.stage.width, this.stage.height);
421
+
422
+ if (this.isSingleMode) {
423
+ const backgroundTransform = getCoverTransform(item.imageInfo, {
424
+ x: 0,
425
+ y: 0,
426
+ width: this.stage.width,
427
+ height: this.stage.height
428
+ });
429
+ const centerX = this.cropBox.x + this.cropBox.width / 2;
430
+ const centerY = this.cropBox.y + this.cropBox.height / 2;
431
+ const radius = this.cropBox.width / 2;
432
+
433
+ context.drawImage(
434
+ item.imageInfo.path,
435
+ backgroundTransform.imageX,
436
+ backgroundTransform.imageY,
437
+ item.imageInfo.width * backgroundTransform.scale,
438
+ item.imageInfo.height * backgroundTransform.scale
439
+ );
440
+ context.setFillStyle("rgba(0, 0, 0, 0.62)");
441
+ context.fillRect(0, 0, this.stage.width, this.stage.height);
442
+ context.save();
443
+ context.beginPath();
444
+ context.arc(centerX, centerY, radius, 0, Math.PI * 2);
445
+ context.clip();
446
+ context.drawImage(
447
+ item.imageInfo.path,
448
+ transform.imageX,
449
+ transform.imageY,
450
+ item.imageInfo.width * transform.scale,
451
+ item.imageInfo.height * transform.scale
452
+ );
453
+ context.restore();
454
+ context.beginPath();
455
+ context.arc(centerX, centerY, radius - 2, 0, Math.PI * 2);
456
+ context.setStrokeStyle("#ffffff");
457
+ context.setLineWidth(4);
458
+ context.stroke();
459
+ context.draw(false, () => {});
460
+ return;
461
+ }
462
+
463
+ context.drawImage(
464
+ item.imageInfo.path,
465
+ transform.imageX,
466
+ transform.imageY,
467
+ item.imageInfo.width * transform.scale,
468
+ item.imageInfo.height * transform.scale
469
+ );
470
+ context.draw(false, () => {});
471
+ },
472
+ markCurrentCropped() {
473
+ const item = this.getCurrentItem();
474
+
475
+ if (!item || item.cropped) return;
476
+
477
+ item.cropped = true;
478
+ item.cropInfo = null;
479
+ item.croppedTempFilePath = "";
480
+ this.syncDisplayList();
481
+ },
482
+ handleTouchStart(event) {
483
+ const item = this.getCurrentItem();
484
+
485
+ if (!item || this.data.confirming) return;
486
+
487
+ const touches = event.touches || [];
488
+ const transform = item.transform;
489
+
490
+ this.touchState = {
491
+ startCenter: getTouchCenter(touches),
492
+ startDistance: getTouchDistance(touches),
493
+ startImageX: transform.imageX,
494
+ startImageY: transform.imageY,
495
+ startScale: transform.scale
496
+ };
497
+ },
498
+ handleTouchMove(event) {
499
+ const item = this.getCurrentItem();
500
+
501
+ if (!this.touchState || !item || this.data.confirming) return;
502
+
503
+ const touches = event.touches || [];
504
+ const center = getTouchCenter(touches);
505
+ let nextTransform = cloneTransform(item.transform);
506
+
507
+ if (touches.length >= 2 && this.touchState.startDistance > 0) {
508
+ const distance = getTouchDistance(touches);
509
+ const nextScale = Math.max(
510
+ item.initialTransform.minScale,
511
+ Math.min(MAX_SCALE, this.touchState.startScale * (distance / this.touchState.startDistance))
512
+ );
513
+ const imagePointX = (this.touchState.startCenter.x - this.touchState.startImageX) / this.touchState.startScale;
514
+ const imagePointY = (this.touchState.startCenter.y - this.touchState.startImageY) / this.touchState.startScale;
515
+
516
+ nextTransform.scale = nextScale;
517
+ nextTransform.imageX = center.x - imagePointX * nextScale;
518
+ nextTransform.imageY = center.y - imagePointY * nextScale;
519
+ } else {
520
+ nextTransform.imageX = this.touchState.startImageX + (center.x - this.touchState.startCenter.x);
521
+ nextTransform.imageY = this.touchState.startImageY + (center.y - this.touchState.startCenter.y);
522
+ }
523
+
524
+ item.transform = this.limitTransform(nextTransform, item.imageInfo);
525
+ this.markCurrentCropped();
526
+ this.drawPreview();
527
+ },
528
+ handleTouchEnd() {
529
+ this.touchState = null;
530
+ },
531
+ handleRootTouchMove() {},
532
+ handleSelectImage(event) {
533
+ if (this.data.confirming) return;
534
+
535
+ const index = Number(event.currentTarget.dataset.index);
536
+
537
+ if (!this.items[index] || index === this.data.currentIndex) return;
538
+
539
+ this.touchState = null;
540
+ this.setData(
541
+ {
542
+ currentIndex: index
543
+ },
544
+ () => {
545
+ this.syncDisplayList();
546
+ this.queuePreviewDraw();
547
+ }
548
+ );
549
+ },
550
+ handleRestore() {
551
+ const item = this.getCurrentItem();
552
+
553
+ if (!item || this.data.confirming) return;
554
+
555
+ item.transform = cloneTransform(item.initialTransform);
556
+ item.cropped = false;
557
+ item.cropInfo = null;
558
+ item.croppedTempFilePath = "";
559
+ this.touchState = null;
560
+ this.syncDisplayList();
561
+ this.drawPreview();
562
+ },
563
+ getCropInfo(item, outputWidth, outputHeight) {
564
+ const crop = this.cropBox;
565
+ const transform = item.transform;
566
+ const sourceX = (crop.x - transform.imageX) / transform.scale;
567
+ const sourceY = (crop.y - transform.imageY) / transform.scale;
568
+ const sourceWidth = crop.width / transform.scale;
569
+ const sourceHeight = crop.height / transform.scale;
570
+
571
+ return {
572
+ x: Math.max(0, Math.round(sourceX)),
573
+ y: Math.max(0, Math.round(sourceY)),
574
+ width: Math.round(Math.min(item.imageInfo.width - sourceX, sourceWidth)),
575
+ height: Math.round(Math.min(item.imageInfo.height - sourceY, sourceHeight)),
576
+ scale: transform.scale,
577
+ outputWidth,
578
+ outputHeight
579
+ };
580
+ },
581
+ exportItem(item, index) {
582
+ const outputWidth = this.outputSize.width;
583
+ const outputHeight = this.outputSize.height;
584
+ const quality = normalizeQuality(this.data.quality);
585
+ const cropInfo = this.getCropInfo(item, outputWidth, outputHeight);
586
+ const context = wx.createCanvasContext(OUTPUT_CANVAS_ID, this);
587
+
588
+ try {
589
+ context.clearRect(0, 0, outputWidth, outputHeight);
590
+ context.drawImage(
591
+ item.imageInfo.path,
592
+ cropInfo.x,
593
+ cropInfo.y,
594
+ cropInfo.width,
595
+ cropInfo.height,
596
+ 0,
597
+ 0,
598
+ outputWidth,
599
+ outputHeight
600
+ );
601
+ } catch (error) {
602
+ return Promise.reject({
603
+ source: "draw",
604
+ message: "绘制裁剪图片失败",
605
+ error,
606
+ index,
607
+ file: item.file
608
+ });
609
+ }
610
+
611
+ return new Promise((resolve, reject) => {
612
+ context.draw(false, () => {
613
+ wx.canvasToTempFilePath(
614
+ {
615
+ canvasId: OUTPUT_CANVAS_ID,
616
+ x: 0,
617
+ y: 0,
618
+ width: outputWidth,
619
+ height: outputHeight,
620
+ destWidth: outputWidth,
621
+ destHeight: outputHeight,
622
+ quality,
623
+ fileType: "jpg",
624
+ success: (res) => {
625
+ resolve({
626
+ ...item.file,
627
+ url: res.tempFilePath,
628
+ tempFilePath: res.tempFilePath,
629
+ originalUrl: item.originalUrl,
630
+ originalTempFilePath: item.originalTempFilePath,
631
+ cropInfo,
632
+ cropped: item.cropped
633
+ });
634
+ },
635
+ fail: (error) => {
636
+ reject({
637
+ source: "export",
638
+ message: "导出裁剪图片失败",
639
+ error,
640
+ index,
641
+ file: item.file
642
+ });
643
+ }
644
+ },
645
+ this
646
+ );
647
+ });
648
+ });
649
+ },
650
+ exportAllItems() {
651
+ return this.items.reduce(
652
+ (promise, item, index) =>
653
+ promise.then((files) =>
654
+ this.exportItem(item, index).then((file) => {
655
+ files.push(file);
656
+ return files;
657
+ })
658
+ ),
659
+ Promise.resolve([])
660
+ );
661
+ },
662
+ handleConfirm() {
663
+ if (this.data.confirming) return;
664
+
665
+ if (!this.items.length || !this.cropBox || !this.outputSize) {
666
+ this.emitError("draw", "裁剪图片未初始化");
667
+ return;
668
+ }
669
+
670
+ this.setData({
671
+ confirming: true
672
+ });
673
+
674
+ this.exportAllItems()
675
+ .then((files) => {
676
+ this.setData({
677
+ confirming: false
678
+ });
679
+ this.triggerEvent("confirm", {
680
+ tempFilePath: files[0] ? files[0].tempFilePath : "",
681
+ cropInfo: files[0] ? files[0].cropInfo : null,
682
+ fileList: files,
683
+ files,
684
+ currentIndex: this.data.currentIndex
685
+ });
686
+ })
687
+ .catch((errorDetail) => {
688
+ this.setData({
689
+ confirming: false
690
+ });
691
+ this.emitError(errorDetail.source || "export", errorDetail.message || "导出裁剪图片失败", errorDetail.error || errorDetail, {
692
+ index: errorDetail.index,
693
+ file: errorDetail.file
694
+ });
695
+ });
696
+ },
697
+ handleCancel() {
698
+ if (this.data.confirming) return;
699
+
700
+ this.triggerEvent("cancel");
701
+ }
702
+ }
703
+ });
@@ -0,0 +1,3 @@
1
+ {
2
+ "component": true
3
+ }