ke_junjie
2025-06-04 4bf0783db564f0d446c74d7b42242aff879b894c
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
import { pickExclude } from '../common/utils';
import { isImageUrl, isVideoUrl } from '../common/validator';
export function isImageFile(item) {
  if (item.isImage != null) {
    return item.isImage;
  }
  if (item.type) {
    return item.type === 'image';
  }
  if (item.url) {
    return isImageUrl(item.url);
  }
  return false;
}
export function isVideoFile(item) {
  if (item.isVideo != null) {
    return item.isVideo;
  }
  if (item.type) {
    return item.type === 'video';
  }
  if (item.url) {
    return isVideoUrl(item.url);
  }
  return false;
}
function formatImage(res) {
  return res.tempFiles.map((item) =>
    Object.assign(Object.assign({}, pickExclude(item, ['path'])), {
      type: 'image',
      url: item.path,
      thumb: item.path,
    })
  );
}
function formatVideo(res) {
  return [
    Object.assign(
      Object.assign(
        {},
        pickExclude(res, ['tempFilePath', 'thumbTempFilePath', 'errMsg'])
      ),
      { type: 'video', url: res.tempFilePath, thumb: res.thumbTempFilePath }
    ),
  ];
}
function formatMedia(res) {
  return res.tempFiles.map((item) =>
    Object.assign(
      Object.assign(
        {},
        pickExclude(item, ['fileType', 'thumbTempFilePath', 'tempFilePath'])
      ),
      {
        type: res.type,
        url: item.tempFilePath,
        thumb:
          res.type === 'video' ? item.thumbTempFilePath : item.tempFilePath,
      }
    )
  );
}
function formatFile(res) {
  return res.tempFiles.map((item) =>
    Object.assign(Object.assign({}, pickExclude(item, ['path'])), {
      url: item.path,
    })
  );
}
export function chooseFile({
  accept,
  multiple,
  capture,
  compressed,
  maxDuration,
  sizeType,
  camera,
  maxCount,
}) {
  return new Promise((resolve, reject) => {
    switch (accept) {
      case 'image':
        wx.chooseImage({
          count: multiple ? Math.min(maxCount, 9) : 1,
          sourceType: capture,
          sizeType,
          success: (res) => resolve(formatImage(res)),
          fail: reject,
        });
        break;
      case 'media':
        wx.chooseMedia({
          count: multiple ? Math.min(maxCount, 9) : 1,
          sourceType: capture,
          maxDuration,
          sizeType,
          camera,
          success: (res) => resolve(formatMedia(res)),
          fail: reject,
        });
        break;
      case 'video':
        wx.chooseVideo({
          sourceType: capture,
          compressed,
          maxDuration,
          camera,
          success: (res) => resolve(formatVideo(res)),
          fail: reject,
        });
        break;
      default:
        wx.chooseMessageFile({
          count: multiple ? maxCount : 1,
          type: accept,
          success: (res) => resolve(formatFile(res)),
          fail: reject,
        });
        break;
    }
  });
}