im-ui-mobile 0.1.15 → 0.1.16

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.
@@ -155,6 +155,7 @@ import ImButton from '../im-button/im-button.vue'
155
155
  import { chooseFile } from './utils/file-chooser'
156
156
  import { isFileTypeAccepted } from './utils/file-validator'
157
157
  import { createFileFromUniFile } from './utils/file-adapter'
158
+ import { universalUploadFile } from './utils/upload'
158
159
 
159
160
  // 定义文件类型
160
161
  interface UploadFile {
@@ -621,59 +622,51 @@ const defaultUpload = (uploadFile: UploadFile): Promise<any> => {
621
622
  'AccessToken': props.accessToken,
622
623
  }
623
624
 
624
- return new Promise((resolve, reject) => {
625
- uni.uploadFile({
626
- url: props.action!,
627
- filePath: uploadFile.rawFile.path,
628
- name: props.name,
629
- formData: {
630
- ...props.data,
631
- filename: uploadFile.name,
632
- size: uploadFile.size,
633
- type: uploadFile.type
634
- },
635
- header: headers,
636
- timeout: props.timeout,
637
- withCredentials: props.withCredentials,
638
- // onProgressUpdate: (res: any) => {
639
- // console.log('res.progress',res.progress)
640
- // uploadFile.progress = res.progress
641
- // emit('progress', res.progress, uploadFile)
642
- // updateFileList()
643
- // },
644
- success: (res: any) => {
645
- try {
646
- const data = typeof res.data === 'string' ? JSON.parse(res.data) : res.data
647
- resolve(data)
648
- } catch (e: any) {
649
- reject(e)
650
- }
651
- },
652
- complete: () => {
653
- // 上传完成
654
- },
655
- fail: (e: any) => {
656
- reject(e)
657
- }
658
- })
659
-
660
- // // 监听上传进度
661
- // task.onProgressUpdate = (res:any) => {
662
- // uploadFile.progress = res.progress
663
- // emit('progress', res.progress, uploadFile)
664
- // updateFileList()
665
- // }
666
-
667
- // 保存任务用于取消
668
- // uploadTasks.set(uploadFile.uid, task)
669
-
670
- // // 上传完成后清理
671
- // task.then(() => {
672
- // uploadTasks.delete(uploadFile.uid)
673
- // }).catch(() => {
674
- // uploadTasks.delete(uploadFile.uid)
675
- // })
625
+ return universalUploadFile({
626
+ url: props.action!,
627
+ filePath: uploadFile.rawFile.path,
628
+ name: props.name,
629
+ formData: {
630
+ ...props.data,
631
+ filename: uploadFile.name,
632
+ size: uploadFile.size,
633
+ type: uploadFile.type
634
+ },
635
+ header: headers,
636
+ timeout: props.timeout,
637
+ withCredentials: props.withCredentials
676
638
  })
639
+
640
+ // return new Promise((resolve, reject) => {
641
+ // uni.uploadFile({
642
+ // url: props.action!,
643
+ // filePath: uploadFile.rawFile.path,
644
+ // name: props.name,
645
+ // formData: {
646
+ // ...props.data,
647
+ // filename: uploadFile.name,
648
+ // size: uploadFile.size,
649
+ // type: uploadFile.type
650
+ // },
651
+ // header: headers,
652
+ // timeout: props.timeout,
653
+ // withCredentials: props.withCredentials,
654
+ // success: (res: any) => {
655
+ // try {
656
+ // const data = typeof res.data === 'string' ? JSON.parse(res.data) : res.data
657
+ // resolve(data)
658
+ // } catch (e: any) {
659
+ // reject(e)
660
+ // }
661
+ // },
662
+ // complete: () => {
663
+ // // 上传完成
664
+ // },
665
+ // fail: (e: any) => {
666
+ // reject(e)
667
+ // }
668
+ // })
669
+ // })
677
670
  }
678
671
 
679
672
  // 更新文件列表
@@ -0,0 +1,227 @@
1
+ // utils/upload.js
2
+ /**
3
+ * 获取平台特定的上传方法
4
+ */
5
+ const getPlatformUploadAPI = () => {
6
+ // #ifdef H5 || APP-PLUS
7
+ return uni.uploadFile
8
+ // #endif
9
+
10
+ // #ifdef MP-WEIXIN
11
+ return wx.uploadFile
12
+ // #endif
13
+
14
+ // #ifdef MP-ALIPAY
15
+ return my.uploadFile || my.httpRequest
16
+ // #endif
17
+
18
+ // #ifdef MP-BAIDU
19
+ return swan.uploadFile
20
+ // #endif
21
+
22
+ // #ifdef MP-TOUTIAO
23
+ return tt.uploadFile
24
+ // #endif
25
+
26
+ // #ifdef MP-KUAISHOU
27
+ return ks.uploadFile
28
+ // #endif
29
+
30
+ // 默认返回 uni.uploadFile
31
+ return uni.uploadFile
32
+ }
33
+
34
+ /**
35
+ * 获取平台特定的请求头处理
36
+ */
37
+ const getPlatformHeaders = (headers, platform) => {
38
+ // 微信小程序需要特殊处理
39
+ if (platform === 'wx') {
40
+ // 微信小程序中,header 字段名是小写的
41
+ const wxHeaders = {}
42
+ Object.keys(headers).forEach(key => {
43
+ wxHeaders[key.toLowerCase()] = headers[key]
44
+ })
45
+ return wxHeaders
46
+ }
47
+
48
+ return headers
49
+ }
50
+
51
+ /**
52
+ * 获取平台特定的请求配置
53
+ */
54
+ const getPlatformConfig = (config, platform) => {
55
+ const baseConfig = {
56
+ url: config.url,
57
+ filePath: config.filePath,
58
+ name: config.name || 'file',
59
+ formData: config.formData || {},
60
+ timeout: config.timeout || 60000,
61
+ complete: config.complete,
62
+ success: config.success,
63
+ fail: config.fail
64
+ }
65
+
66
+ // 平台特定的配置调整
67
+ switch (platform) {
68
+ case 'wx':
69
+ // 微信小程序
70
+ return {
71
+ ...baseConfig,
72
+ header: getPlatformHeaders(config.header || {}, 'wx')
73
+ }
74
+
75
+ case 'alipay':
76
+ // 支付宝小程序
77
+ return {
78
+ ...baseConfig,
79
+ headers: config.header || {}, // 支付宝使用 headers
80
+ fileType: 'image' // 支付宝可能需要指定文件类型
81
+ }
82
+
83
+ case 'baidu':
84
+ // 百度小程序
85
+ return {
86
+ ...baseConfig,
87
+ header: config.header || {}
88
+ }
89
+
90
+ case 'tt':
91
+ // 字节跳动小程序
92
+ return {
93
+ ...baseConfig,
94
+ header: config.header || {}
95
+ }
96
+
97
+ default:
98
+ // uni-app 和其他平台
99
+ return {
100
+ ...baseConfig,
101
+ header: config.header || {},
102
+ withCredentials: config.withCredentials || false
103
+ }
104
+ }
105
+ }
106
+
107
+ /**
108
+ * 获取当前平台标识
109
+ */
110
+ const getCurrentPlatform = () => {
111
+ // #ifdef MP-WEIXIN
112
+ return 'wx'
113
+ // #endif
114
+
115
+ // #ifdef MP-ALIPAY
116
+ return 'alipay'
117
+ // #endif
118
+
119
+ // #ifdef MP-BAIDU
120
+ return 'baidu'
121
+ // #endif
122
+
123
+ // #ifdef MP-TOUTIAO
124
+ return 'tt'
125
+ // #endif
126
+
127
+ // #ifdef MP-KUAISHOU
128
+ return 'ks'
129
+ // #endif
130
+
131
+ // #ifdef H5
132
+ return 'h5'
133
+ // #endif
134
+
135
+ // #ifdef APP-PLUS
136
+ return 'app'
137
+ // #endif
138
+
139
+ return 'uni'
140
+ }
141
+
142
+ /**
143
+ * 多平台兼容的文件上传方法
144
+ */
145
+ export const universalUploadFile = (config) => {
146
+ const platform = getCurrentPlatform()
147
+ const platformConfig = getPlatformConfig(config, platform)
148
+ const uploadAPI = getPlatformUploadAPI()
149
+
150
+ return new Promise((resolve, reject) => {
151
+ // 平台特定的成功回调处理
152
+ const successCallback = (res) => {
153
+ try {
154
+ // 统一处理返回数据格式
155
+ let data = res.data || res
156
+
157
+ if (typeof data === 'string') {
158
+ try {
159
+ data = JSON.parse(data)
160
+ } catch (e) {
161
+ // 如果不是 JSON,保持原样
162
+ }
163
+ }
164
+
165
+ // 构建统一的响应格式
166
+ const unifiedResponse = {
167
+ statusCode: res.statusCode || 200,
168
+ data,
169
+ errMsg: res.errMsg || 'uploadFile:ok'
170
+ }
171
+
172
+ // 调用原始 success 回调
173
+ if (config.success) {
174
+ config.success(unifiedResponse)
175
+ }
176
+
177
+ resolve(unifiedResponse)
178
+ } catch (error) {
179
+ reject(error)
180
+ }
181
+ }
182
+
183
+ // 平台特定的失败回调处理
184
+ const failCallback = (err) => {
185
+ const error = {
186
+ errMsg: err.errMsg || 'uploadFile:fail',
187
+ detail: err
188
+ }
189
+
190
+ if (config.fail) {
191
+ config.fail(error)
192
+ }
193
+
194
+ reject(error)
195
+ }
196
+
197
+ // 执行上传
198
+ try {
199
+ if (platform === 'alipay' && my.uploadFile) {
200
+ // 支付宝小程序特殊处理
201
+ my.uploadFile({
202
+ url: platformConfig.url,
203
+ fileType: 'image',
204
+ fileName: 'file',
205
+ filePath: platformConfig.filePath,
206
+ formData: platformConfig.formData,
207
+ headers: platformConfig.headers,
208
+ success: successCallback,
209
+ fail: failCallback,
210
+ complete: platformConfig.complete
211
+ })
212
+ } else {
213
+ // 其他平台
214
+ uploadAPI({
215
+ ...platformConfig,
216
+ success: successCallback,
217
+ fail: failCallback
218
+ })
219
+ }
220
+ } catch (error) {
221
+ reject({
222
+ errMsg: 'uploadFile:fail',
223
+ detail: error
224
+ })
225
+ }
226
+ })
227
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "im-ui-mobile",
3
- "version": "0.1.15",
3
+ "version": "0.1.16",
4
4
  "description": "A Vue3.0 + Typescript instant messaging component library for Uniapp",
5
5
  "type": "module",
6
6
  "main": "index.js",