Skip to content

Commit b3a2e8a

Browse files
authored
chore(Upload): compatible with WxWork and PC (#4087)
* chore(Upload): compatible with WxWork and PC * fix(Avatar): fix ts error * fix(Upload): when request-method returns Promise, uploading cannot be done
1 parent f95b0bb commit b3a2e8a

File tree

5 files changed

+108
-61
lines changed

5 files changed

+108
-61
lines changed

packages/components/avatar/avatar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default class Avatar extends SuperComponent {
2727
classPrefix: name,
2828
isShow: true,
2929
zIndex: 0,
30-
systemInfo,
30+
windowWidth: systemInfo.windowWidth,
3131
};
3232

3333
relations: RelationsOptions = {

packages/components/avatar/avatar.wxml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
>
2323
<view
2424
class="{{_this.getClass(classPrefix, size || 'medium', shape, bordered)}} {{prefix}}-class-image"
25-
style="{{_this.getSize(size, systemInfo)}}"
25+
style="{{_this.getSize(size, windowWidth)}}"
2626
aria-label="{{ ariaLabel || alt ||'头像'}}"
2727
aria-role="{{ ariaRole || 'img'}}"
2828
aria-hidden="{{ ariaHidden }}"

packages/components/avatar/avatar.wxs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ module.exports = {
1111
return classNames.join(' ');
1212
},
1313

14-
getSize: function (size = 'medium', systemInfo) {
14+
getSize: function (size = 'medium', windowWidth) {
1515
var res = getRegExp('^([0-9]+)(px|rpx)$').exec(size);
1616

1717
if (res && res.length >= 3) {
1818
var px = res[1];
1919
if (res[2] === 'rpx') {
20-
px = Math.floor((systemInfo.windowWidth * res[1]) / 750);
20+
px = Math.floor((windowWidth * res[1]) / 750);
2121
}
2222

2323
return 'width:' + size + ';height:' + size + ';font-size:' + ((px / 8) * 3 + 2) + 'px';

packages/components/common/utils.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@ import { prefix } from './config';
22
import { isString, isNumber, isDef, isBoolean, isObject } from './validator';
33
import { getWindowInfo, getAppBaseInfo, getDeviceInfo } from './wechat';
44

5-
export const systemInfo: WechatMiniprogram.WindowInfo | WechatMiniprogram.SystemInfo = getWindowInfo();
5+
interface WxWorkSystemInfo extends WechatMiniprogram.SystemInfo {
6+
environment?: 'wxwork';
7+
}
8+
interface SystemInfo extends WxWorkSystemInfo {}
9+
10+
export const systemInfo: WechatMiniprogram.WindowInfo | SystemInfo = getWindowInfo();
611

7-
export const appBaseInfo: WechatMiniprogram.AppBaseInfo | WechatMiniprogram.SystemInfo = getAppBaseInfo();
12+
export const appBaseInfo: WechatMiniprogram.AppBaseInfo | SystemInfo = getAppBaseInfo();
813

9-
export const deviceInfo: WechatMiniprogram.DeviceInfo | WechatMiniprogram.SystemInfo = getDeviceInfo();
14+
export const deviceInfo: WechatMiniprogram.DeviceInfo | SystemInfo = getDeviceInfo();
1015

1116
type Context = WechatMiniprogram.Page.TrivialInstance | WechatMiniprogram.Component.TrivialInstance;
1217

@@ -133,6 +138,15 @@ export const isIOS = function (): boolean {
133138
return !!(deviceInfo?.system?.toLowerCase().search('ios') + 1);
134139
};
135140

141+
/**
142+
* 判断是否是为企微环境
143+
* 企微环境 wx.getSystemInfoSync() 接口会额外返回 environment 字段(微信中不返回)
144+
* https://developer.work.weixin.qq.com/document/path/91511
145+
*/
146+
export const isWxWork = (deviceInfo as SystemInfo)?.environment === 'wxwork';
147+
148+
export const isPC = ['mac', 'windows'].includes(deviceInfo?.platform);
149+
136150
export const addUnit = function (value?: string | number): string | undefined {
137151
if (!isDef(value)) {
138152
return undefined;

packages/components/upload/upload.ts

Lines changed: 87 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { SuperComponent, wxComponent } from '../common/src/index';
22
import props from './props';
3-
import { UploadFile } from './type';
3+
import { UploadFile, SizeLimitObj } from './type';
44
import config from '../common/config';
5-
import { isOverSize } from '../common/utils';
5+
import { isOverSize, isWxWork, isPC } from '../common/utils';
66
import { isObject } from '../common/validator';
77

88
const { prefix } = config;
@@ -111,6 +111,20 @@ export default class Upload extends SuperComponent {
111111
return parseInt(`${Date.now()}${Math.floor(Math.random() * 900 + 100)}`, 10).toString(36) + extName;
112112
}
113113

114+
checkFileSize(size: number, sizeLimit: SizeLimitObj | number, fileType?: string): boolean {
115+
if (isOverSize(size, sizeLimit)) {
116+
let title = `${fileType === 'video' ? '视频' : '图片'}大小超过限制`;
117+
118+
if (isObject(sizeLimit)) {
119+
const { size: limitSize, message: limitMessage } = sizeLimit as SizeLimitObj;
120+
title = limitMessage?.replace('{sizeLimit}', String(limitSize));
121+
}
122+
wx.showToast({ icon: 'none', title });
123+
return true;
124+
}
125+
return false;
126+
}
127+
114128
onDelete(e: any) {
115129
const { index } = e.currentTarget.dataset;
116130
this.deleteHandle(index);
@@ -278,13 +292,13 @@ export default class Upload extends SuperComponent {
278292
},
279293

280294
uploadFiles(files: UploadFile[]) {
281-
return new Promise((resolve) => {
295+
return Promise.resolve().then(() => {
282296
// 开始调用上传函数
283297
const task = this.data.requestMethod(files);
284298
if (task instanceof Promise) {
285299
return task;
286300
}
287-
resolve({});
301+
return Promise.resolve({});
288302
});
289303
},
290304

@@ -323,50 +337,77 @@ export default class Upload extends SuperComponent {
323337
chooseMedia(mediaType) {
324338
const { customLimit } = this.data;
325339
const { config, sizeLimit } = this.properties;
326-
wx.chooseMedia({
327-
count: Math.min(20, customLimit),
328-
mediaType,
329-
...config,
330-
success: (res) => {
331-
const files = [];
332-
333-
// 支持单/多文件
334-
res.tempFiles.forEach((temp) => {
335-
const { size, fileType, tempFilePath, width, height, duration, thumbTempFilePath, ...res } = temp;
336-
337-
if (isOverSize(size, sizeLimit)) {
338-
let title = `${fileType === 'image' ? '图片' : '视频'}大小超过限制`;
339340

340-
if (typeof sizeLimit !== 'number') {
341-
title = sizeLimit.message.replace('{sizeLimit}', sizeLimit?.size);
342-
}
343-
wx.showToast({ icon: 'none', title });
344-
return;
345-
}
341+
if (isWxWork || isPC) {
342+
wx.chooseImage({
343+
count: Math.min(20, customLimit),
344+
...config,
345+
success: (res) => {
346+
const files = [];
347+
348+
res.tempFiles.forEach((temp) => {
349+
const { path, size } = temp;
350+
351+
if (this.checkFileSize(size, sizeLimit, 'image')) return;
352+
353+
const name = this.getRandFileName(path);
354+
files.push({
355+
name,
356+
type: 'image',
357+
url: path,
358+
size: size,
359+
percent: 0,
360+
});
361+
});
346362

347-
const name = this.getRandFileName(tempFilePath);
348-
files.push({
349-
name,
350-
type: this.getFileType(mediaType, tempFilePath, fileType),
351-
url: tempFilePath,
352-
size: size,
353-
width: width,
354-
height: height,
355-
duration: duration,
356-
thumb: thumbTempFilePath,
357-
percent: 0,
358-
...res,
363+
this.afterSelect(files);
364+
},
365+
fail: (err) => {
366+
this.triggerFailEvent(err);
367+
},
368+
complete: (res) => {
369+
this.triggerEvent('complete', res);
370+
},
371+
});
372+
} else {
373+
wx.chooseMedia({
374+
count: Math.min(20, customLimit),
375+
mediaType,
376+
...config,
377+
success: (res) => {
378+
const files = [];
379+
380+
// 支持单/多文件
381+
res.tempFiles.forEach((temp) => {
382+
const { size, fileType, tempFilePath, width, height, duration, thumbTempFilePath, ...res } = temp;
383+
384+
if (this.checkFileSize(size, sizeLimit, fileType)) return;
385+
386+
const name = this.getRandFileName(tempFilePath);
387+
files.push({
388+
name,
389+
type: this.getFileType(mediaType, tempFilePath, fileType),
390+
url: tempFilePath,
391+
size: size,
392+
width: width,
393+
height: height,
394+
duration: duration,
395+
thumb: thumbTempFilePath,
396+
percent: 0,
397+
...res,
398+
});
359399
});
360-
});
361-
this.afterSelect(files);
362-
},
363-
fail: (err) => {
364-
this.triggerFailEvent(err);
365-
},
366-
complete: (res) => {
367-
this.triggerEvent('complete', res);
368-
},
369-
});
400+
401+
this.afterSelect(files);
402+
},
403+
fail: (err) => {
404+
this.triggerFailEvent(err);
405+
},
406+
complete: (res) => {
407+
this.triggerEvent('complete', res);
408+
},
409+
});
410+
}
370411
},
371412

372413
chooseMessageFile(mediaType) {
@@ -383,15 +424,7 @@ export default class Upload extends SuperComponent {
383424
res.tempFiles.forEach((temp) => {
384425
const { size, type: fileType, path: tempFilePath, ...res } = temp;
385426

386-
if (isOverSize(size, sizeLimit)) {
387-
let title = `${fileType === 'image' ? '图片' : '视频'}大小超过限制`;
388-
389-
if (typeof sizeLimit !== 'number') {
390-
title = sizeLimit.message.replace('{sizeLimit}', sizeLimit?.size);
391-
}
392-
wx.showToast({ icon: 'none', title });
393-
return;
394-
}
427+
if (this.checkFileSize(size, sizeLimit, fileType)) return;
395428

396429
const name = this.getRandFileName(tempFilePath);
397430
files.push({

0 commit comments

Comments
 (0)