riku
2026-03-05 9465dc404f7e7cd56100e4859ee0946a3fef7b09
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
module.exports =
  /******/
  (function (modules) {
    // webpackBootstrap
    /******/ // The module cache
    /******/
    var installedModules = {}; /******/ // The require function
    /******/
    /******/ function __webpack_require__(moduleId) {
      /******/
      /******/ // Check if module is in cache
      /******/
      if (installedModules[moduleId]) {
        /******/
        return installedModules[moduleId].exports;
        /******/
      } /******/ // Create a new module (and put it into the cache)
      /******/ var module = (installedModules[moduleId] = {
        /******/
        i: moduleId,
        /******/
        l: false,
        /******/
        exports: {},
        /******/
      }); /******/ // Execute the module function
      /******/
      /******/ modules[moduleId].call(
        module.exports,
        module,
        module.exports,
        __webpack_require__,
      ); /******/ // Flag the module as loaded
      /******/
      /******/ module.l = true; /******/ // Return the exports of the module
      /******/
      /******/ return module.exports;
      /******/
    } /******/ // expose the modules object (__webpack_modules__)
    /******/
    /******/
    /******/ __webpack_require__.m = modules; /******/ // expose the module cache
    /******/
    /******/ __webpack_require__.c = installedModules; /******/ // define getter function for harmony exports
    /******/
    /******/ __webpack_require__.d = function (exports, name, getter) {
      /******/
      if (!__webpack_require__.o(exports, name)) {
        /******/
        Object.defineProperty(exports, name, {
          enumerable: true,
          get: getter,
        });
        /******/
      }
      /******/
    }; /******/ // define __esModule on exports
    /******/
    /******/ __webpack_require__.r = function (exports) {
      /******/
      if (typeof Symbol !== 'undefined' && Symbol.toStringTag) {
        /******/
        Object.defineProperty(exports, Symbol.toStringTag, {
          value: 'Module',
        });
        /******/
      }
      /******/
      Object.defineProperty(exports, '__esModule', {
        value: true,
      });
      /******/
    }; /******/ /******/ /******/ /******/ /******/ // create a fake namespace object // mode & 1: value is a module id, require it // mode & 2: merge all properties of value into the ns // mode & 4: return value when already ns object // mode & 8|1: behave like require
    /******/
    /******/ __webpack_require__.t = function (value, mode) {
      /******/
      if (mode & 1) value = __webpack_require__(value);
      /******/
      if (mode & 8) return value;
      /******/
      if (mode & 4 && typeof value === 'object' && value && value.__esModule)
        return value;
      /******/
      var ns = Object.create(null);
      /******/
      __webpack_require__.r(ns);
      /******/
      Object.defineProperty(ns, 'default', {
        enumerable: true,
        value: value,
      });
      /******/
      if (mode & 2 && typeof value != 'string')
        for (var key in value)
          __webpack_require__.d(
            ns,
            key,
            function (key) {
              return value[key];
            }.bind(null, key),
          );
      /******/
      return ns;
      /******/
    }; /******/ // getDefaultExport function for compatibility with non-harmony modules
    /******/
    /******/ __webpack_require__.n = function (module) {
      /******/
      var getter =
        module && module.__esModule
          ? /******/
            function getDefault() {
              return module['default'];
            }
          : /******/
            function getModuleExports() {
              return module;
            };
      /******/
      __webpack_require__.d(getter, 'a', getter);
      /******/
      return getter;
      /******/
    }; /******/ // Object.prototype.hasOwnProperty.call
    /******/
    /******/ __webpack_require__.o = function (object, property) {
      return Object.prototype.hasOwnProperty.call(object, property);
    }; /******/ // __webpack_public_path__
    /******/
    /******/ __webpack_require__.p = ''; /******/ // Load entry module and return exports
    /******/
    /******/
    /******/ return __webpack_require__((__webpack_require__.s = 22));
    /******/
  })(
    /************************************************************************/
    /******/
    {
      /***/
      22:
        /***/
        function (module, exports, __webpack_require__) {
          'use strict';
 
          Component({
            options: {
              addGlobalClass: true,
            },
            properties: {
              title: {
                type: String,
                value: '',
              },
              titleClass: {
                type: 'String',
                value: '',
              },
 
              sizeType: {
                type: Array,
                value: ['original', 'compressed'],
              },
              sourceType: {
                type: Array,
                value: ['album', 'camera'],
              },
              maxSize: {
                type: Number,
                value: 5 * 1024 * 1024,
              },
              maxCount: {
                // 最多上传多少个文件
                type: Number,
                value: 1,
              },
              countInfo: {
                type: String,
                value: '',
              },
              files: {
                // 当前的图片列表, {url, error, loading}
                type: Array,
                value: [],
 
                observer(newVal) {
                  this.setData({
                    currentFiles: newVal,
                  });
                },
              },
              select: {
                // 过滤某个文件
                type: null,
                value: null,
              },
              upload: {
                // 返回Promise的一个文件上传的函数
                type: null,
                value: () => {},
              },
              tips: {
                type: String,
                value: '',
              },
              extClass: {
                type: String,
                value: '',
              },
              showDelete: {
                // 是否显示delete按钮
                type: Boolean,
                value: true,
              },
              allowFiles: {
                // 是否允许选择图片之外的其他格式文件
                type: Boolean,
                value: false,
              },
            },
            data: {
              currentFiles: [],
              showPreview: false,
              previewImageUrls: [],
              groups: [
                {
                  text: '选择图片',
                  value: 0,
                },
                {
                  text: '从聊天记录选择文件',
                  value: 1,
                },
              ],
            },
 
            ready() {},
 
            methods: {
              showActionSheet() {
                if (this.data.allowFiles) {
                  this.setData({
                    showSheet: true,
                  });
                } else {
                  this.chooseImage();
                }
              },
              previewImage(e) {
                const { index } = e.currentTarget.dataset;
                const f = this.data.files[index];
                const previewImageUrls = this.data.files
                  .filter(f => f.type == 'image')
                  .map(f => f.url);
                const i = previewImageUrls.indexOf(f.url);
                this.setData({
                  previewImageUrls,
                  previewCurrent: i,
                  showPreview: true,
                });
              },
              previewFile(e) {
                const { index } = e.currentTarget.dataset;
                const file = this.data.files[index];
                wx.openDocument({
                  filePath: file.url,
                });
              },
              actionClick(e) {
                switch (e.detail.value) {
                  case 0:
                    this.chooseImage();
                    break;
                  case 1:
                    this.chooseMessageFile();
                    break;
                  default:
                    this.chooseImage();
                    break;
                }
                this.setData({
                  showSheet: false,
                });
              },
              chooseImage() {
                if (this.uploading) return;
                wx.chooseMedia({
                  count: this.data.maxCount - this.data.files.length,
                  sizeType: ['compressed'],
                  mediaType: ['image'],
                  success: res => {
                    console.log('chooseImage resp', res);
                    // 首先检查文件大小
                    let invalidIndex = -1; // @ts-ignore
                    res.tempFilePaths = res.tempFiles.map(
                      item => item.tempFilePath,
                    );
                    res.tempFiles.forEach((item, index) => {
                      if (item.size > this.data.maxSize) {
                        invalidIndex = index;
                      }
                    });
                    if (typeof this.data.select === 'function') {
                      const ret = this.data.select(res);
                      if (ret === false) {
                        return;
                      }
                    }
                    if (invalidIndex >= 0) {
                      this.triggerEvent(
                        'fail',
                        {
                          type: 1,
                          errMsg: `chooseImage:fail size exceed ${this.data.maxSize}`,
                          total: res.tempFilePaths.length,
                          index: invalidIndex,
                        },
                        {},
                      );
                      return;
                    } // 获取文件内容
 
                    const mgr = wx.getFileSystemManager();
                    const contents = res.tempFilePaths.map(item => {
                      // @ts-ignore
                      const fileContent = mgr.readFileSync(item);
                      return fileContent;
                    });
                    const obj = {
                      tempFilePaths: res.tempFilePaths,
                      tempFiles: res.tempFiles,
                      contents,
                    }; // 触发选中的事件,开发者根据内容来上传文件,上传了把上传的结果反馈到files属性里面
 
                    this.triggerEvent('select', obj, {});
 
                    let files = res.tempFiles.map((item, i) => ({
                      loading: true,
                      url: item.tempFilePath,
                      type: item.fileType,
                    }));
                    //压缩图片
                    wx.compressImage({
                      src: files[0].url,
                      quality: 20,
                      success(data) {
                        console.log('compressImage', data);
                      },
                      fail() {},
                    });
 
                    if (!files || !files.length) return;
                    const newFiles = this.data.files.concat(files);
                    this.setData({
                      files: newFiles,
                      currentFiles: newFiles,
                    });
                    this.triggerEvent(
                      'uploadImg',
                      {
                        newFiles,
                      },
                      {},
                    );
                  },
                  fail: fail => {
                    if (fail.errMsg.indexOf('chooseImage:fail cancel') >= 0) {
                      this.triggerEvent('cancel', {}, {});
                      return;
                    }
                    fail.type = 2;
                    this.triggerEvent('fail', fail, {});
                  },
                });
              },
              chooseMessageFile() {
                if (this.uploading) return;
                wx.chooseMessageFile({
                  count: this.data.maxCount - this.data.files.length,
                  type: 'file',
                  extension: [
                    'xls',
                    'xlsx',
                    'csv',
                    'doc',
                    'docx',
                    'pdf',
                    'ppt',
                    'pptx',
                  ],
                  success: res => {
                    console.log('chooseMessageFile', res);
                    // 首先检查文件大小
                    let invalidIndex = -1; // @ts-ignore
                    res.tempFilePaths = res.tempFiles.map(item => item.path);
                    res.tempFiles.forEach((item, index) => {
                      if (item.size > this.data.maxSize) {
                        invalidIndex = index;
                      }
                    });
                    if (typeof this.data.select === 'function') {
                      const ret = this.data.select(res);
                      if (ret === false) {
                        return;
                      }
                    }
                    if (invalidIndex >= 0) {
                      this.triggerEvent(
                        'fail',
                        {
                          type: 1,
                          errMsg: `chooseMessageFile:fail size exceed ${this.data.maxSize}`,
                          total: res.tempFilePaths.length,
                          index: invalidIndex,
                        },
                        {},
                      );
                      return;
                    } // 获取文件内容
 
                    const mgr = wx.getFileSystemManager();
                    const contents = res.tempFilePaths.map(item => {
                      // @ts-ignore
                      const fileContent = mgr.readFileSync(item);
                      return fileContent;
                    });
                    const obj = {
                      tempFilePaths: res.tempFilePaths,
                      tempFiles: res.tempFiles,
                      contents,
                    }; // 触发选中的事件,开发者根据内容来上传文件,上传了把上传的结果反馈到files属性里面
 
                    this.triggerEvent('select', obj, {});
 
                    let files = res.tempFiles.map((item, i) => {
                      // 判断文件类型,对应不同样式
                      const list = item.name.split('.');
                      const ext = list[list.length - 1];
                      let extensionClass = '';
                      switch (ext) {
                        case 'xls':
                        case 'xlsx':
                        case 'csv':
                          extensionClass = 'file_xlsx';
                          break;
                        case 'doc':
                        case 'docx':
                          extensionClass = 'file_word';
                          break;
                        case 'pdf':
                          extensionClass = 'file_pdf';
                          break;
                        case 'ppt':
                        case 'pptx':
                          extensionClass = 'file_ppt';
                          break;
                        default:
                          break;
                      }
 
                      // 文件大小格式化
                      let size = parseInt(item.size / 1024);
                      if (size > 1024) {
                        size = (size / 1024).toFixed(1) + ' MB';
                      } else {
                        size += ' KB';
                      }
                      return {
                        loading: true,
                        url: item.path,
                        name: item.name,
                        ext: ext ?? '',
                        styleClass: extensionClass,
                        type: item.type,
                        time: item.time,
                        size,
                      };
                    });
                    if (!files || !files.length) return;
                    const newFiles = this.data.files.concat(files);
                    this.setData({
                      files: newFiles,
                      currentFiles: newFiles,
                    });
                    this.triggerEvent(
                      'uploadImg',
                      {
                        newFiles,
                      },
                      {},
                    );
                  },
                });
              },
              deletePic(e) {
                const index = e.detail.index;
                this._delete(index);
              },
              deleteFile(e) {
                const { index } = e.currentTarget.dataset;
                this._delete(index);
              },
              _delete(index) {
                const files = this.data.files;
                const file = files.splice(index, 1);
                this.setData({
                  files,
                  currentFiles: files,
                });
                this.triggerEvent('delete', {
                  index,
                  item: file[0],
                });
              },
            },
          });
 
          /***/
        },
 
      /******/
    },
  );