132 lines
4.3 KiB
JavaScript
132 lines
4.3 KiB
JavaScript
import { generateNewId } from 'oak-domain/lib/utils/uuid';
|
|
export default OakComponent({
|
|
entity: 'extraFile',
|
|
projection: {
|
|
id: 1,
|
|
tag1: 1,
|
|
tag2: 1,
|
|
origin: 1,
|
|
bucket: 1,
|
|
objectId: 1,
|
|
filename: 1,
|
|
extra1: 1,
|
|
extension: 1,
|
|
type: 1,
|
|
entity: 1,
|
|
entityId: 1,
|
|
fileType: 1,
|
|
sort: 1,
|
|
isBridge: 1,
|
|
uploadState: 1,
|
|
},
|
|
isList: true,
|
|
formData({ data: rows, features }) {
|
|
let file1;
|
|
let file1Url;
|
|
let file2;
|
|
let file2Url;
|
|
const { idCardType } = this.props;
|
|
if (idCardType === 'ID-Card') {
|
|
file1 = rows?.find((ele) => !ele.$$deleteAt$$ && ele.tag1 === idCardType && ele.tag2 === 'file1');
|
|
file1Url = features.extraFile.getUrl(file1);
|
|
file2 = rows?.find((ele) => !ele.$$deleteAt$$ && ele.tag1 === idCardType && ele.tag2 === 'file2');
|
|
file2Url = features.extraFile.getUrl(file2);
|
|
}
|
|
return {
|
|
file1,
|
|
file2,
|
|
file1Url,
|
|
file2Url
|
|
};
|
|
},
|
|
data: {},
|
|
properties: {
|
|
origin: 'qiniu',
|
|
idCardType: '',
|
|
entityId: '',
|
|
entity: 'user',
|
|
autoUpload: false
|
|
},
|
|
methods: {
|
|
async onPickMp(e) {
|
|
const tag2 = e.currentTarget.dataset.tag2;
|
|
try {
|
|
const { errMsg, tempFiles } = await wx.chooseMedia({
|
|
count: 1,
|
|
mediaType: ['image'],
|
|
sourceType: ['album', 'camera'],
|
|
});
|
|
if (errMsg !== 'chooseMedia:ok') {
|
|
this.setMessage({
|
|
type: 'warning',
|
|
content: errMsg,
|
|
});
|
|
}
|
|
else {
|
|
await Promise.all(tempFiles.map(async (tempExtraFile) => {
|
|
const { tempFilePath, thumbTempFilePath, fileType, size, } = tempExtraFile;
|
|
const filePath = tempFilePath || thumbTempFilePath;
|
|
const fileFullName = filePath.match(/[^/]+(?!.*\/)/g)[0];
|
|
this.pushExtraFile({
|
|
name: fileFullName,
|
|
fileType,
|
|
size,
|
|
extra1: filePath,
|
|
}, tag2);
|
|
}));
|
|
}
|
|
}
|
|
catch (err) {
|
|
console.error(err);
|
|
if (err.errMsg !== 'chooseMedia:fail cancel') {
|
|
this.setMessage({
|
|
type: 'error',
|
|
content: err.errMsg,
|
|
});
|
|
}
|
|
}
|
|
},
|
|
async pushExtraFile(options, tag2) {
|
|
const { entity, entityId, autoUpload = false, origin = 'qiniu', idCardType } = this.props;
|
|
const { name, extra1, fileType, size } = options;
|
|
const extension = name.substring(name.lastIndexOf('.') + 1);
|
|
const filename = name.substring(0, name.lastIndexOf('.'));
|
|
const applicationId = this.features.application.getApplicationId();
|
|
const id = generateNewId();
|
|
const updateData = {
|
|
applicationId,
|
|
origin,
|
|
type: 'image',
|
|
tag1: idCardType,
|
|
tag2,
|
|
objectId: generateNewId(),
|
|
filename,
|
|
size,
|
|
extension,
|
|
fileType,
|
|
id,
|
|
entityId,
|
|
entity,
|
|
sort: 1000,
|
|
uploadState: 'uploading'
|
|
};
|
|
const _file = this.state[tag2]; //取出file1、file2
|
|
// 如果autoUpload
|
|
if (autoUpload) {
|
|
await this.features.extraFile.autoUpload(updateData, extra1);
|
|
if (_file) {
|
|
this.removeItem(_file.id);
|
|
await this.execute();
|
|
}
|
|
}
|
|
else {
|
|
this.addItem(updateData);
|
|
if (_file) {
|
|
this.removeItem(_file.id);
|
|
}
|
|
this.features.extraFile.addLocalFile(id, extra1);
|
|
}
|
|
},
|
|
},
|
|
});
|