This commit is contained in:
wenjiarui 2023-09-20 17:52:19 +08:00
parent 5164024000
commit 7431214099
30 changed files with 301 additions and 119 deletions

View File

@ -81,7 +81,7 @@ export default OakComponent({
// return result; // return result;
// }, // },
async uploadFile(extraFile) { async uploadFile(extraFile) {
const result = await this.features.extraFile.createAndUpload(extraFile); const result = await this.features.extraFile.createAndUpload(extraFile, extraFile.extra1);
return result; return result;
}, },
setEditor(editor) { setEditor(editor) {

View File

@ -105,9 +105,7 @@ export default OakComponent({
bucket: '', bucket: '',
id: generateNewId(), id: generateNewId(),
}; };
const { url, bucket } = await this.features.extraFile.createAndUpload(extraFile); const { url } = await this.features.extraFile.createAndUpload(extraFile, extraFile.extra1);
extraFile.bucket = bucket;
extraFile.extra1 = null;
// await this.addExtraFile(extraFile); // await this.addExtraFile(extraFile);
this.editorCtx.insertImage({ this.editorCtx.insertImage({
src: 'http://' + url, src: 'http://' + url,

View File

@ -20,6 +20,7 @@ export default OakComponent({
fileType: 1, fileType: 1,
sort: 1, sort: 1,
isBridge: 1, isBridge: 1,
uploadState: 1,
}, },
formData({ data: originalFiles, features }) { formData({ data: originalFiles, features }) {
console.log(originalFiles); console.log(originalFiles);
@ -96,7 +97,9 @@ export default OakComponent({
getUrl(extraFile) { getUrl(extraFile) {
const { fileList } = this.state; const { fileList } = this.state;
if (fileList[extraFile?.id]) { if (fileList[extraFile?.id]) {
console.log(fileList[extraFile?.id]);
const url = this.features.extraFile.getUrl(Object.assign({}, extraFile, { extra1: fileList[extraFile?.id] })); const url = this.features.extraFile.getUrl(Object.assign({}, extraFile, { extra1: fileList[extraFile?.id] }));
console.log(url);
return url; return url;
} }
return this.features.extraFile.getUrl(extraFile); return this.features.extraFile.getUrl(extraFile);
@ -238,7 +241,6 @@ export default OakComponent({
assert(origin === 'qiniu', '目前只支持七牛上传'); // 目前只支持七牛上传 assert(origin === 'qiniu', '目前只支持七牛上传'); // 目前只支持七牛上传
const id = generateNewId(); const id = generateNewId();
const updateData = { const updateData = {
extra1,
origin, origin,
type: type || 'file', type: type || 'file',
tag1, tag1,
@ -259,9 +261,7 @@ export default OakComponent({
callback(updateData, 'uploading'); callback(updateData, 'uploading');
} }
try { try {
this.addItem(Object.assign({}, updateData, { this.addItem(updateData, undefined, async () => {
extra1: null,
}), undefined, async () => {
await this.features.extraFile.upload(updateData, extra1); await this.features.extraFile.upload(updateData, extra1);
}); });
await this.execute(); await this.execute();
@ -278,9 +278,7 @@ export default OakComponent({
} }
} }
else { else {
this.addItem(Object.assign({}, updateData, { this.addItem(updateData, undefined, async () => {
extra1: null,
}), undefined, async () => {
await this.features.extraFile.upload(updateData, extra1); await this.features.extraFile.upload(updateData, extra1);
}); });
this.setState({ this.setState({
@ -313,10 +311,17 @@ export default OakComponent({
}, },
async onDeleteByMp(event) { async onDeleteByMp(event) {
const { value } = event.currentTarget.dataset; const { value } = event.currentTarget.dataset;
const { id, bucket, origin } = value; const { id, bucket, origin, uploadState } = value;
const { removeLater } = this.props; const { removeLater } = this.props;
if (removeLater || (origin !== 'unknown' && !bucket)) { const { fileList } = this.state;
if (removeLater || !uploadState) {
this.removeItem(id); this.removeItem(id);
Object.assign(fileList, {
[id]: null,
});
this.setState({
fileList
});
} }
else { else {
const result = await wx.showModal({ const result = await wx.showModal({
@ -326,16 +331,29 @@ export default OakComponent({
const { confirm } = result; const { confirm } = result;
if (confirm) { if (confirm) {
this.removeItem(id); this.removeItem(id);
Object.assign(fileList, {
id: null,
});
this.setState({
fileList
});
await this.execute(); await this.execute();
} }
} }
}, },
async onDeleteByWeb(value) { async onDeleteByWeb(value) {
const { id, bucket, origin } = value; const { id, bucket, origin, uploadState } = value;
const { removeLater = true } = this.props; const { removeLater = true } = this.props;
const { fileList } = this.state;
// 如果 removeLater为true 或 origin === 'qiniu' 且 bucket不存在 // 如果 removeLater为true 或 origin === 'qiniu' 且 bucket不存在
if (removeLater || (origin !== 'unknown' && !bucket)) { if (removeLater || !uploadState) {
this.removeItem(id); this.removeItem(id);
Object.assign(fileList, {
id: null,
});
this.setState({
fileList
});
} }
else { else {
const confirm = Dialog.confirm({ const confirm = Dialog.confirm({
@ -345,6 +363,12 @@ export default OakComponent({
okText: '确定', okText: '确定',
onOk: async (e) => { onOk: async (e) => {
this.removeItem(id); this.removeItem(id);
Object.assign(fileList, {
id: null,
});
this.setState({
fileList
});
await this.execute(); await this.execute();
confirm.destroy(); confirm.destroy();
}, },

View File

@ -3,6 +3,7 @@ import { WebComponentProps } from "oak-frontend-base";
import { EntityDict } from "../../../oak-app-domain"; import { EntityDict } from "../../../oak-app-domain";
interface NewUploadFile extends UploadFile { interface NewUploadFile extends UploadFile {
id?: string; id?: string;
status?: 'done' | 'uploading' | 'error' | 'removed';
} }
type Theme = "file" | "image" | "image-flow" | "custom"; type Theme = "file" | "image" | "image-flow" | "custom";
export default function render(props: WebComponentProps<EntityDict, "extraFile", true, { export default function render(props: WebComponentProps<EntityDict, "extraFile", true, {

View File

@ -63,6 +63,21 @@ export default function render(props) {
} }
}, [files]); }, [files]);
const extraFileToUploadFile = (extraFile) => { const extraFileToUploadFile = (extraFile) => {
let status = undefined;
switch (extraFile.uploadState) {
case 'uploading': {
status = 'uploading';
break;
}
case 'failed': {
status = 'error';
break;
}
case 'success': {
status = 'done';
break;
}
}
return Object.assign({}, extraFile, { return Object.assign({}, extraFile, {
id: extraFile.id, id: extraFile.id,
url: getUrl(extraFile), url: getUrl(extraFile),
@ -71,8 +86,9 @@ export default function render(props) {
fileName: getFileName(extraFile), fileName: getFileName(extraFile),
size: extraFile.size, size: extraFile.size,
type: extraFile.fileType, type: extraFile.fileType,
uid: extraFile.id, //upload 组件需要uid来维护fileList uid: extraFile.id,
// status: 'done', status,
percent: status === 'uploading' ? 50 : undefined,
}); });
}; };
const setNewUploadFilesByStatus = (file, status) => { const setNewUploadFilesByStatus = (file, status) => {

View File

@ -12,13 +12,10 @@ export declare class ExtraFile<ED extends EntityDict, Cxt extends BackendRuntime
private application; private application;
private locales; private locales;
constructor(cache: Cache<ED, Cxt, FrontCxt, AD & CommonAspectDict<ED, Cxt>>, application: Application<ED, Cxt, FrontCxt, AD>, locales: Locales<ED, Cxt, FrontCxt, AD>); constructor(cache: Cache<ED, Cxt, FrontCxt, AD & CommonAspectDict<ED, Cxt>>, application: Application<ED, Cxt, FrontCxt, AD>, locales: Locales<ED, Cxt, FrontCxt, AD>);
createAndUpload(extraFile: EntityDict['extraFile']['CreateSingle']['data']): Promise<{ createAndUpload(extraFile: EntityDict['extraFile']['CreateSingle']['data'], file: string | File): Promise<{
url: any; url: any;
bucket: ED["extraFile"]["Schema"]["bucket"] | undefined;
}>;
upload(extraFile: EntityDict['extraFile']['CreateSingle']['data'], file: string | File): Promise<Partial<ED["extraFile"]["Schema"]> & {
uploadState: string;
}>; }>;
upload(extraFile: EntityDict['extraFile']['CreateSingle']['data'], file: string | File): Promise<void>;
getUrl(extraFile?: EntityDict['extraFile']['OpSchema'] | EntityDict['extraFile']['Schema'] | null, style?: string): any; getUrl(extraFile?: EntityDict['extraFile']['OpSchema'] | EntityDict['extraFile']['Schema'] | null, style?: string): any;
/** /**
* 使使url时URL.revokeObjectURL释放缓存 * 使使url时URL.revokeObjectURL释放缓存

View File

@ -14,22 +14,18 @@ export class ExtraFile extends Feature {
this.application = application; this.application = application;
this.locales = locales; this.locales = locales;
} }
async createAndUpload(extraFile) { async createAndUpload(extraFile, file) {
await this.cache.operate('extraFile', { await this.cache.operate('extraFile', {
action: 'create', action: 'create',
data: Object.assign({}, extraFile, { extra1: null }), data: extraFile,
id: generateNewId(), id: generateNewId(),
}); });
const result = await this.upload(Object.assign({}, extraFile, { extra1: null }), extraFile.extra1); await this.upload(extraFile, file);
const application = this.application.getApplication(); const application = this.application.getApplication();
const config = application?.system?.config || const config = application?.system?.config ||
application?.system?.platform?.config; application?.system?.platform?.config;
const { bucket } = result;
return { return {
url: this.getUrl(Object.assign({}, extraFile, { url: this.getUrl(extraFile),
extra1: null,
})),
bucket,
}; };
} }
async upload(extraFile, file) { async upload(extraFile, file) {
@ -73,7 +69,7 @@ export class ExtraFile extends Feature {
}, },
id: generateNewId(), id: generateNewId(),
}); });
return Object.assign(extraFileData, { uploadState: 'success' }); this.publish();
} }
catch (err) { catch (err) {
await this.cache.operate('extraFile', { await this.cache.operate('extraFile', {
@ -86,6 +82,7 @@ export class ExtraFile extends Feature {
}, },
id: generateNewId(), id: generateNewId(),
}); });
this.publish();
throw err; throw err;
} }
} }

View File

@ -81,7 +81,7 @@ export default OakComponent({
} }
}, },
uploadFile(extraFile) { uploadFile(extraFile) {
return this.features.extraFile.createAndUpload(extraFile); return this.features.extraFile.createAndUpload(extraFile, extraFile.extra1);
}, },
setEditor(editor) { setEditor(editor) {
this.setState({ this.setState({

View File

@ -40,6 +40,9 @@ export declare class OakMpHaveToSubscribeMessage extends Error {
export declare class OakUserInfoLoadingException<ED extends EntityDict & BaseEntityDict> extends OakUserException<ED> { export declare class OakUserInfoLoadingException<ED extends EntityDict & BaseEntityDict> extends OakUserException<ED> {
constructor(message?: string); constructor(message?: string);
} }
export declare class OakUploadException<ED extends EntityDict & BaseEntityDict> extends OakUserException<ED> {
constructor(message?: string);
}
export declare function makeException<ED extends EntityDict & BaseEntityDict>(data: { export declare function makeException<ED extends EntityDict & BaseEntityDict>(data: {
name: string; name: string;
message?: string; message?: string;

View File

@ -88,6 +88,12 @@ export class OakUserInfoLoadingException extends OakUserException {
} }
} }
; ;
export class OakUploadException extends OakUserException {
constructor(message) {
super(message || '上传文件失败');
}
}
;
export function makeException(data) { export function makeException(data) {
const exception = makeException2(data); const exception = makeException2(data);
if (exception) { if (exception) {
@ -130,6 +136,11 @@ export function makeException(data) {
e.setOpRecords(opRecords); e.setOpRecords(opRecords);
return e; return e;
} }
case 'OakUploadException': {
const e = new OakUploadException(message);
e.setOpRecords(opRecords);
return e;
}
default: { default: {
return; return;
} }

View File

@ -1,5 +1,6 @@
import { getConfig } from '../../utils/getContextConfig'; import { getConfig } from '../../utils/getContextConfig';
import { urlSafeBase64Encode } from '../sign'; import { urlSafeBase64Encode } from '../sign';
import { OakUploadException } from '../../types/Exception';
const QiniuSearchUrl = 'https://rs.qiniuapi.com/stat/EncodedEntryURI'; const QiniuSearchUrl = 'https://rs.qiniuapi.com/stat/EncodedEntryURI';
export default class Qiniu { export default class Qiniu {
name = 'qiniu'; name = 'qiniu';
@ -16,14 +17,23 @@ export default class Qiniu {
} }
async upload(extraFile, uploadFn, file) { async upload(extraFile, uploadFn, file) {
const uploadMeta = extraFile.uploadMeta; const uploadMeta = extraFile.uploadMeta;
const result = await uploadFn(file, 'file', uploadMeta.uploadHost, { try {
key: uploadMeta.key, const result = await uploadFn(file, 'file', uploadMeta.uploadHost, {
token: uploadMeta.uploadToken, key: uploadMeta.key,
}, true); token: uploadMeta.uploadToken,
if (result.success === true || result.key) { }, true);
return; console.log(result);
// await new Promise(
// () => setTimeout(() => { return Promise.resolve() }, 10000)
// )
if (result.success === true || result.key) {
return;
}
} }
throw new Error('图片上传失败'); catch (err) {
throw new OakUploadException('图片上传失败');
}
throw new OakUploadException('图片上传失败');
} }
composeFileUrl(extraFile, config, style) { composeFileUrl(extraFile, config, style) {
const { objectId, extension, entity, } = extraFile || {}; const { objectId, extension, entity, } = extraFile || {};

View File

@ -83,7 +83,7 @@ exports.default = OakComponent({
// return result; // return result;
// }, // },
async uploadFile(extraFile) { async uploadFile(extraFile) {
const result = await this.features.extraFile.createAndUpload(extraFile); const result = await this.features.extraFile.createAndUpload(extraFile, extraFile.extra1);
return result; return result;
}, },
setEditor(editor) { setEditor(editor) {

View File

@ -107,9 +107,7 @@ exports.default = OakComponent({
bucket: '', bucket: '',
id: (0, uuid_1.generateNewId)(), id: (0, uuid_1.generateNewId)(),
}; };
const { url, bucket } = await this.features.extraFile.createAndUpload(extraFile); const { url } = await this.features.extraFile.createAndUpload(extraFile, extraFile.extra1);
extraFile.bucket = bucket;
extraFile.extra1 = null;
// await this.addExtraFile(extraFile); // await this.addExtraFile(extraFile);
this.editorCtx.insertImage({ this.editorCtx.insertImage({
src: 'http://' + url, src: 'http://' + url,

View File

@ -23,6 +23,7 @@ exports.default = OakComponent({
fileType: 1, fileType: 1,
sort: 1, sort: 1,
isBridge: 1, isBridge: 1,
uploadState: 1,
}, },
formData({ data: originalFiles, features }) { formData({ data: originalFiles, features }) {
console.log(originalFiles); console.log(originalFiles);
@ -99,7 +100,9 @@ exports.default = OakComponent({
getUrl(extraFile) { getUrl(extraFile) {
const { fileList } = this.state; const { fileList } = this.state;
if (fileList[extraFile?.id]) { if (fileList[extraFile?.id]) {
console.log(fileList[extraFile?.id]);
const url = this.features.extraFile.getUrl(Object.assign({}, extraFile, { extra1: fileList[extraFile?.id] })); const url = this.features.extraFile.getUrl(Object.assign({}, extraFile, { extra1: fileList[extraFile?.id] }));
console.log(url);
return url; return url;
} }
return this.features.extraFile.getUrl(extraFile); return this.features.extraFile.getUrl(extraFile);
@ -241,7 +244,6 @@ exports.default = OakComponent({
(0, assert_1.default)(origin === 'qiniu', '目前只支持七牛上传'); // 目前只支持七牛上传 (0, assert_1.default)(origin === 'qiniu', '目前只支持七牛上传'); // 目前只支持七牛上传
const id = (0, uuid_1.generateNewId)(); const id = (0, uuid_1.generateNewId)();
const updateData = { const updateData = {
extra1,
origin, origin,
type: type || 'file', type: type || 'file',
tag1, tag1,
@ -262,9 +264,7 @@ exports.default = OakComponent({
callback(updateData, 'uploading'); callback(updateData, 'uploading');
} }
try { try {
this.addItem(Object.assign({}, updateData, { this.addItem(updateData, undefined, async () => {
extra1: null,
}), undefined, async () => {
await this.features.extraFile.upload(updateData, extra1); await this.features.extraFile.upload(updateData, extra1);
}); });
await this.execute(); await this.execute();
@ -281,9 +281,7 @@ exports.default = OakComponent({
} }
} }
else { else {
this.addItem(Object.assign({}, updateData, { this.addItem(updateData, undefined, async () => {
extra1: null,
}), undefined, async () => {
await this.features.extraFile.upload(updateData, extra1); await this.features.extraFile.upload(updateData, extra1);
}); });
this.setState({ this.setState({
@ -316,10 +314,17 @@ exports.default = OakComponent({
}, },
async onDeleteByMp(event) { async onDeleteByMp(event) {
const { value } = event.currentTarget.dataset; const { value } = event.currentTarget.dataset;
const { id, bucket, origin } = value; const { id, bucket, origin, uploadState } = value;
const { removeLater } = this.props; const { removeLater } = this.props;
if (removeLater || (origin !== 'unknown' && !bucket)) { const { fileList } = this.state;
if (removeLater || !uploadState) {
this.removeItem(id); this.removeItem(id);
Object.assign(fileList, {
[id]: null,
});
this.setState({
fileList
});
} }
else { else {
const result = await wx.showModal({ const result = await wx.showModal({
@ -329,16 +334,29 @@ exports.default = OakComponent({
const { confirm } = result; const { confirm } = result;
if (confirm) { if (confirm) {
this.removeItem(id); this.removeItem(id);
Object.assign(fileList, {
id: null,
});
this.setState({
fileList
});
await this.execute(); await this.execute();
} }
} }
}, },
async onDeleteByWeb(value) { async onDeleteByWeb(value) {
const { id, bucket, origin } = value; const { id, bucket, origin, uploadState } = value;
const { removeLater = true } = this.props; const { removeLater = true } = this.props;
const { fileList } = this.state;
// 如果 removeLater为true 或 origin === 'qiniu' 且 bucket不存在 // 如果 removeLater为true 或 origin === 'qiniu' 且 bucket不存在
if (removeLater || (origin !== 'unknown' && !bucket)) { if (removeLater || !uploadState) {
this.removeItem(id); this.removeItem(id);
Object.assign(fileList, {
id: null,
});
this.setState({
fileList
});
} }
else { else {
const confirm = index_1.default.confirm({ const confirm = index_1.default.confirm({
@ -348,6 +366,12 @@ exports.default = OakComponent({
okText: '确定', okText: '确定',
onOk: async (e) => { onOk: async (e) => {
this.removeItem(id); this.removeItem(id);
Object.assign(fileList, {
id: null,
});
this.setState({
fileList
});
await this.execute(); await this.execute();
confirm.destroy(); confirm.destroy();
}, },

View File

@ -3,6 +3,7 @@ import { WebComponentProps } from "oak-frontend-base";
import { EntityDict } from "../../../oak-app-domain"; import { EntityDict } from "../../../oak-app-domain";
interface NewUploadFile extends UploadFile { interface NewUploadFile extends UploadFile {
id?: string; id?: string;
status?: 'done' | 'uploading' | 'error' | 'removed';
} }
type Theme = "file" | "image" | "image-flow" | "custom"; type Theme = "file" | "image" | "image-flow" | "custom";
export default function render(props: WebComponentProps<EntityDict, "extraFile", true, { export default function render(props: WebComponentProps<EntityDict, "extraFile", true, {

View File

@ -66,6 +66,21 @@ function render(props) {
} }
}, [files]); }, [files]);
const extraFileToUploadFile = (extraFile) => { const extraFileToUploadFile = (extraFile) => {
let status = undefined;
switch (extraFile.uploadState) {
case 'uploading': {
status = 'uploading';
break;
}
case 'failed': {
status = 'error';
break;
}
case 'success': {
status = 'done';
break;
}
}
return Object.assign({}, extraFile, { return Object.assign({}, extraFile, {
id: extraFile.id, id: extraFile.id,
url: getUrl(extraFile), url: getUrl(extraFile),
@ -74,8 +89,9 @@ function render(props) {
fileName: getFileName(extraFile), fileName: getFileName(extraFile),
size: extraFile.size, size: extraFile.size,
type: extraFile.fileType, type: extraFile.fileType,
uid: extraFile.id, //upload 组件需要uid来维护fileList uid: extraFile.id,
// status: 'done', status,
percent: status === 'uploading' ? 50 : undefined,
}); });
}; };
const setNewUploadFilesByStatus = (file, status) => { const setNewUploadFilesByStatus = (file, status) => {

View File

@ -12,13 +12,10 @@ export declare class ExtraFile<ED extends EntityDict, Cxt extends BackendRuntime
private application; private application;
private locales; private locales;
constructor(cache: Cache<ED, Cxt, FrontCxt, AD & CommonAspectDict<ED, Cxt>>, application: Application<ED, Cxt, FrontCxt, AD>, locales: Locales<ED, Cxt, FrontCxt, AD>); constructor(cache: Cache<ED, Cxt, FrontCxt, AD & CommonAspectDict<ED, Cxt>>, application: Application<ED, Cxt, FrontCxt, AD>, locales: Locales<ED, Cxt, FrontCxt, AD>);
createAndUpload(extraFile: EntityDict['extraFile']['CreateSingle']['data']): Promise<{ createAndUpload(extraFile: EntityDict['extraFile']['CreateSingle']['data'], file: string | File): Promise<{
url: any; url: any;
bucket: ED["extraFile"]["Schema"]["bucket"] | undefined;
}>;
upload(extraFile: EntityDict['extraFile']['CreateSingle']['data'], file: string | File): Promise<Partial<ED["extraFile"]["Schema"]> & {
uploadState: string;
}>; }>;
upload(extraFile: EntityDict['extraFile']['CreateSingle']['data'], file: string | File): Promise<void>;
getUrl(extraFile?: EntityDict['extraFile']['OpSchema'] | EntityDict['extraFile']['Schema'] | null, style?: string): any; getUrl(extraFile?: EntityDict['extraFile']['OpSchema'] | EntityDict['extraFile']['Schema'] | null, style?: string): any;
/** /**
* 使使url时URL.revokeObjectURL释放缓存 * 使使url时URL.revokeObjectURL释放缓存

View File

@ -18,22 +18,18 @@ class ExtraFile extends oak_frontend_base_1.Feature {
this.application = application; this.application = application;
this.locales = locales; this.locales = locales;
} }
async createAndUpload(extraFile) { async createAndUpload(extraFile, file) {
await this.cache.operate('extraFile', { await this.cache.operate('extraFile', {
action: 'create', action: 'create',
data: Object.assign({}, extraFile, { extra1: null }), data: extraFile,
id: (0, uuid_1.generateNewId)(), id: (0, uuid_1.generateNewId)(),
}); });
const result = await this.upload(Object.assign({}, extraFile, { extra1: null }), extraFile.extra1); await this.upload(extraFile, file);
const application = this.application.getApplication(); const application = this.application.getApplication();
const config = application?.system?.config || const config = application?.system?.config ||
application?.system?.platform?.config; application?.system?.platform?.config;
const { bucket } = result;
return { return {
url: this.getUrl(Object.assign({}, extraFile, { url: this.getUrl(extraFile),
extra1: null,
})),
bucket,
}; };
} }
async upload(extraFile, file) { async upload(extraFile, file) {
@ -77,7 +73,7 @@ class ExtraFile extends oak_frontend_base_1.Feature {
}, },
id: (0, uuid_1.generateNewId)(), id: (0, uuid_1.generateNewId)(),
}); });
return Object.assign(extraFileData, { uploadState: 'success' }); this.publish();
} }
catch (err) { catch (err) {
await this.cache.operate('extraFile', { await this.cache.operate('extraFile', {
@ -90,6 +86,7 @@ class ExtraFile extends oak_frontend_base_1.Feature {
}, },
id: (0, uuid_1.generateNewId)(), id: (0, uuid_1.generateNewId)(),
}); });
this.publish();
throw err; throw err;
} }
} }

View File

@ -83,7 +83,7 @@ exports.default = OakComponent({
} }
}, },
uploadFile(extraFile) { uploadFile(extraFile) {
return this.features.extraFile.createAndUpload(extraFile); return this.features.extraFile.createAndUpload(extraFile, extraFile.extra1);
}, },
setEditor(editor) { setEditor(editor) {
this.setState({ this.setState({

View File

@ -40,6 +40,9 @@ export declare class OakMpHaveToSubscribeMessage extends Error {
export declare class OakUserInfoLoadingException<ED extends EntityDict & BaseEntityDict> extends OakUserException<ED> { export declare class OakUserInfoLoadingException<ED extends EntityDict & BaseEntityDict> extends OakUserException<ED> {
constructor(message?: string); constructor(message?: string);
} }
export declare class OakUploadException<ED extends EntityDict & BaseEntityDict> extends OakUserException<ED> {
constructor(message?: string);
}
export declare function makeException<ED extends EntityDict & BaseEntityDict>(data: { export declare function makeException<ED extends EntityDict & BaseEntityDict>(data: {
name: string; name: string;
message?: string; message?: string;

View File

@ -1,6 +1,6 @@
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.makeException = exports.OakUserInfoLoadingException = exports.OakMpHaveToSubscribeMessage = exports.OakTokenExpiredException = exports.OakUserDisabledException = exports.OakUserInfoUncompletedException = exports.OakMobileUnsetException = exports.OakChangeLoginWayException = exports.OakDistinguishUserException = exports.OakNotEnoughMoneyException = void 0; exports.makeException = exports.OakUploadException = exports.OakUserInfoLoadingException = exports.OakMpHaveToSubscribeMessage = exports.OakTokenExpiredException = exports.OakUserDisabledException = exports.OakUserInfoUncompletedException = exports.OakMobileUnsetException = exports.OakChangeLoginWayException = exports.OakDistinguishUserException = exports.OakNotEnoughMoneyException = void 0;
const types_1 = require("oak-domain/lib/types"); const types_1 = require("oak-domain/lib/types");
class OakNotEnoughMoneyException extends types_1.OakUserException { class OakNotEnoughMoneyException extends types_1.OakUserException {
constructor(message) { constructor(message) {
@ -100,6 +100,13 @@ class OakUserInfoLoadingException extends types_1.OakUserException {
} }
exports.OakUserInfoLoadingException = OakUserInfoLoadingException; exports.OakUserInfoLoadingException = OakUserInfoLoadingException;
; ;
class OakUploadException extends types_1.OakUserException {
constructor(message) {
super(message || '上传文件失败');
}
}
exports.OakUploadException = OakUploadException;
;
function makeException(data) { function makeException(data) {
const exception = (0, types_1.makeException)(data); const exception = (0, types_1.makeException)(data);
if (exception) { if (exception) {
@ -142,6 +149,11 @@ function makeException(data) {
e.setOpRecords(opRecords); e.setOpRecords(opRecords);
return e; return e;
} }
case 'OakUploadException': {
const e = new OakUploadException(message);
e.setOpRecords(opRecords);
return e;
}
default: { default: {
return; return;
} }

View File

@ -2,6 +2,7 @@
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const getContextConfig_1 = require("../../utils/getContextConfig"); const getContextConfig_1 = require("../../utils/getContextConfig");
const sign_1 = require("../sign"); const sign_1 = require("../sign");
const Exception_1 = require("../../types/Exception");
const QiniuSearchUrl = 'https://rs.qiniuapi.com/stat/EncodedEntryURI'; const QiniuSearchUrl = 'https://rs.qiniuapi.com/stat/EncodedEntryURI';
class Qiniu { class Qiniu {
name = 'qiniu'; name = 'qiniu';
@ -18,14 +19,23 @@ class Qiniu {
} }
async upload(extraFile, uploadFn, file) { async upload(extraFile, uploadFn, file) {
const uploadMeta = extraFile.uploadMeta; const uploadMeta = extraFile.uploadMeta;
const result = await uploadFn(file, 'file', uploadMeta.uploadHost, { try {
key: uploadMeta.key, const result = await uploadFn(file, 'file', uploadMeta.uploadHost, {
token: uploadMeta.uploadToken, key: uploadMeta.key,
}, true); token: uploadMeta.uploadToken,
if (result.success === true || result.key) { }, true);
return; console.log(result);
// await new Promise(
// () => setTimeout(() => { return Promise.resolve() }, 10000)
// )
if (result.success === true || result.key) {
return;
}
} }
throw new Error('图片上传失败'); catch (err) {
throw new Exception_1.OakUploadException('图片上传失败');
}
throw new Exception_1.OakUploadException('图片上传失败');
} }
composeFileUrl(extraFile, config, style) { composeFileUrl(extraFile, config, style) {
const { objectId, extension, entity, } = extraFile || {}; const { objectId, extension, entity, } = extraFile || {};

View File

@ -90,7 +90,7 @@ export default OakComponent({
async uploadFile( async uploadFile(
extraFile: EntityDict['extraFile']['CreateSingle']['data'] extraFile: EntityDict['extraFile']['CreateSingle']['data']
) { ) {
const result = await this.features.extraFile.createAndUpload(extraFile); const result = await this.features.extraFile.createAndUpload(extraFile, extraFile.extra1!);
return result; return result;
}, },

View File

@ -114,10 +114,9 @@ export default OakComponent({
bucket: '', bucket: '',
id: generateNewId(), id: generateNewId(),
} as EntityDict['extraFile']['CreateSingle']['data']; } as EntityDict['extraFile']['CreateSingle']['data'];
const { url, bucket } = const { url } =
await this.features.extraFile.createAndUpload(extraFile); await this.features.extraFile.createAndUpload(extraFile, extraFile.extra1!);
extraFile.bucket = bucket;
extraFile.extra1 = null;
// await this.addExtraFile(extraFile); // await this.addExtraFile(extraFile);
(this as any).editorCtx.insertImage({ (this as any).editorCtx.insertImage({
src: 'http://' + url, src: 'http://' + url,

View File

@ -29,6 +29,7 @@ export default OakComponent({
fileType: 1, fileType: 1,
sort: 1, sort: 1,
isBridge: 1, isBridge: 1,
uploadState: 1,
}, },
formData({ data: originalFiles, features }) { formData({ data: originalFiles, features }) {
console.log(originalFiles); console.log(originalFiles);
@ -111,7 +112,9 @@ export default OakComponent({
getUrl(extraFile: EntityDict['extraFile']['OpSchema']) { getUrl(extraFile: EntityDict['extraFile']['OpSchema']) {
const { fileList } = this.state; const { fileList } = this.state;
if (fileList[extraFile?.id]) { if (fileList[extraFile?.id]) {
console.log(fileList[extraFile?.id]);
const url = this.features.extraFile.getUrl(Object.assign({}, extraFile, { extra1: fileList[extraFile?.id] })); const url = this.features.extraFile.getUrl(Object.assign({}, extraFile, { extra1: fileList[extraFile?.id] }));
console.log(url);
return url; return url;
} }
return this.features.extraFile.getUrl(extraFile); return this.features.extraFile.getUrl(extraFile);
@ -278,7 +281,6 @@ export default OakComponent({
assert(origin === 'qiniu', '目前只支持七牛上传'); // 目前只支持七牛上传 assert(origin === 'qiniu', '目前只支持七牛上传'); // 目前只支持七牛上传
const id = generateNewId(); const id = generateNewId();
const updateData = { const updateData = {
extra1,
origin, origin,
type: type || 'file', type: type || 'file',
tag1, tag1,
@ -299,9 +301,7 @@ export default OakComponent({
callback(updateData, 'uploading'); callback(updateData, 'uploading');
} }
try { try {
this.addItem(Object.assign({}, updateData, { this.addItem(updateData, undefined, async () => {
extra1: null,
}), undefined, async () => {
await this.features.extraFile.upload( await this.features.extraFile.upload(
updateData, extra1 updateData, extra1
) )
@ -319,9 +319,7 @@ export default OakComponent({
throw error; throw error;
} }
} else { } else {
this.addItem(Object.assign({}, updateData, { this.addItem(updateData, undefined, async () => {
extra1: null,
}), undefined, async () => {
await this.features.extraFile.upload( await this.features.extraFile.upload(
updateData, extra1 updateData, extra1
) )
@ -359,10 +357,19 @@ export default OakComponent({
}, },
async onDeleteByMp(event: WechatMiniprogram.Touch) { async onDeleteByMp(event: WechatMiniprogram.Touch) {
const { value } = event.currentTarget.dataset; const { value } = event.currentTarget.dataset;
const { id, bucket, origin } = value; const { id, bucket, origin, uploadState } = value;
const { removeLater } = this.props; const { removeLater } = this.props;
if (removeLater || (origin !== 'unknown' && !bucket)) { const { fileList } = this.state;
if (removeLater || !uploadState) {
this.removeItem(id); this.removeItem(id);
Object.assign(
fileList, {
[id]: null,
}
);
this.setState({
fileList
});
} else { } else {
const result = await wx.showModal({ const result = await wx.showModal({
title: '确认删除吗', title: '确认删除吗',
@ -371,16 +378,33 @@ export default OakComponent({
const { confirm } = result; const { confirm } = result;
if (confirm) { if (confirm) {
this.removeItem(id); this.removeItem(id);
Object.assign(
fileList, {
id: null,
}
);
this.setState({
fileList
});
await this.execute(); await this.execute();
} }
} }
}, },
async onDeleteByWeb(value: any) { async onDeleteByWeb(value: any) {
const { id, bucket, origin } = value; const { id, bucket, origin, uploadState } = value;
const { removeLater = true } = this.props; const { removeLater = true } = this.props;
const { fileList } = this.state;
// 如果 removeLater为true 或 origin === 'qiniu' 且 bucket不存在 // 如果 removeLater为true 或 origin === 'qiniu' 且 bucket不存在
if (removeLater || (origin !== 'unknown' && !bucket)) { if (removeLater || !uploadState) {
this.removeItem(id); this.removeItem(id);
Object.assign(
fileList, {
id: null,
}
);
this.setState({
fileList
});
} else { } else {
const confirm = Dialog.confirm({ const confirm = Dialog.confirm({
title: '确认删除当前文件?', title: '确认删除当前文件?',
@ -389,6 +413,14 @@ export default OakComponent({
okText: '确定', okText: '确定',
onOk: async (e: any) => { onOk: async (e: any) => {
this.removeItem(id); this.removeItem(id);
Object.assign(
fileList, {
id: null,
}
);
this.setState({
fileList
});
await this.execute(); await this.execute();
confirm.destroy(); confirm.destroy();
}, },

View File

@ -15,6 +15,7 @@ import { isPc } from "oak-frontend-base/es/utils/utils";
interface NewUploadFile extends UploadFile { interface NewUploadFile extends UploadFile {
id?: string; id?: string;
status?: 'done' | 'uploading' | 'error' | 'removed';
} }
type Theme = "file" | "image" | "image-flow" | "custom"; type Theme = "file" | "image" | "image-flow" | "custom";
@ -158,6 +159,21 @@ export default function render(
const extraFileToUploadFile = ( const extraFileToUploadFile = (
extraFile: EntityDict["extraFile"]["OpSchema"] extraFile: EntityDict["extraFile"]["OpSchema"]
): NewUploadFile => { ): NewUploadFile => {
let status = undefined as NewUploadFile['status'];
switch (extraFile.uploadState) {
case 'uploading': {
status = 'uploading';
break;
}
case 'failed': {
status = 'error';
break;
}
case 'success': {
status = 'done';
break;
}
}
return Object.assign({}, extraFile, { return Object.assign({}, extraFile, {
id: extraFile.id, id: extraFile.id,
url: getUrl(extraFile), url: getUrl(extraFile),
@ -167,7 +183,8 @@ export default function render(
size: extraFile.size!, size: extraFile.size!,
type: extraFile.fileType!, type: extraFile.fileType!,
uid: extraFile.id, //upload 组件需要uid来维护fileList uid: extraFile.id, //upload 组件需要uid来维护fileList
// status: 'done', status,
percent: status === 'uploading' ? 50 : undefined,
}); });
}; };

View File

@ -36,29 +36,26 @@ export class ExtraFile<
} }
async createAndUpload( async createAndUpload(
extraFile: EntityDict['extraFile']['CreateSingle']['data'] extraFile: EntityDict['extraFile']['CreateSingle']['data'],
file: string | File,
) { ) {
await this.cache.operate('extraFile', { await this.cache.operate('extraFile', {
action: 'create', action: 'create',
data: Object.assign({}, extraFile, { extra1: null }), data: extraFile,
id: generateNewId(), id: generateNewId(),
} as EntityDict['extraFile']['Operation']); } as EntityDict['extraFile']['Operation']);
const result = await this.upload( await this.upload(
Object.assign({}, extraFile, { extra1: null }), extraFile,
extraFile.extra1! file
); );
const application = this.application.getApplication(); const application = this.application.getApplication();
const config = const config =
application?.system?.config || application?.system?.config ||
application?.system?.platform?.config; application?.system?.platform?.config;
const { bucket } = result;
return { return {
url: this.getUrl( url: this.getUrl(
Object.assign({}, extraFile, { extraFile as EntityDict['extraFile']['OpSchema']
extra1: null,
}) as EntityDict['extraFile']['OpSchema']
), ),
bucket,
}; };
} }
@ -110,7 +107,7 @@ export class ExtraFile<
}, },
id: generateNewId(), id: generateNewId(),
} as EntityDict['extraFile']['Operation']); } as EntityDict['extraFile']['Operation']);
return Object.assign(extraFileData, { uploadState: 'success' }); this.publish();
} catch (err) { } catch (err) {
await this.cache.operate('extraFile', { await this.cache.operate('extraFile', {
action: 'update', action: 'update',
@ -122,6 +119,7 @@ export class ExtraFile<
}, },
id: generateNewId(), id: generateNewId(),
} as EntityDict['extraFile']['Operation']); } as EntityDict['extraFile']['Operation']);
this.publish();
throw err; throw err;
} }
} }

View File

@ -90,7 +90,7 @@ export default OakComponent({
}, },
uploadFile(extraFile: EntityDict['extraFile']['CreateSingle']['data']) { uploadFile(extraFile: EntityDict['extraFile']['CreateSingle']['data']) {
return this.features.extraFile.createAndUpload(extraFile); return this.features.extraFile.createAndUpload(extraFile, extraFile.extra1!);
}, },
setEditor(editor: IDomEditor | null) { setEditor(editor: IDomEditor | null) {

View File

@ -101,6 +101,12 @@ export class OakUserInfoLoadingException<ED extends EntityDict & BaseEntityDict>
} }
}; };
export class OakUploadException<ED extends EntityDict & BaseEntityDict> extends OakUserException<ED> {
constructor(message?: string) {
super(message || '上传文件失败');
}
};
export function makeException<ED extends EntityDict & BaseEntityDict>(data: { export function makeException<ED extends EntityDict & BaseEntityDict>(data: {
name: string; name: string;
message?: string; message?: string;
@ -149,6 +155,11 @@ export function makeException<ED extends EntityDict & BaseEntityDict>(data: {
e.setOpRecords(opRecords); e.setOpRecords(opRecords);
return e; return e;
} }
case 'OakUploadException': {
const e = new OakUploadException(message);
e.setOpRecords(opRecords);
return e;
}
default: { default: {
return; return;
} }

View File

@ -12,6 +12,7 @@ import { QiniuCloudInstance } from 'oak-external-sdk';
import { get } from 'oak-domain/lib/utils/lodash'; import { get } from 'oak-domain/lib/utils/lodash';
import { Config } from '../../types/Config'; import { Config } from '../../types/Config';
import { urlSafeBase64Encode } from '../sign'; import { urlSafeBase64Encode } from '../sign';
import { OakUploadException } from '../../types/Exception';
const QiniuSearchUrl = 'https://rs.qiniuapi.com/stat/EncodedEntryURI'; const QiniuSearchUrl = 'https://rs.qiniuapi.com/stat/EncodedEntryURI';
@ -54,20 +55,28 @@ export default class Qiniu<ED extends EntityDict & BaseEntityDict> implements Up
file: string | File file: string | File
) { ) {
const uploadMeta = extraFile.uploadMeta! as QiniuUploadInfo; const uploadMeta = extraFile.uploadMeta! as QiniuUploadInfo;
const result = await uploadFn( let result;
file, try {
'file', result = await uploadFn(
uploadMeta.uploadHost, file,
{ 'file',
key: uploadMeta.key, uploadMeta.uploadHost,
token: uploadMeta.uploadToken, {
}, key: uploadMeta.key,
true token: uploadMeta.uploadToken,
); },
true
);
} catch (err) {
// 网络错误
throw new OakUploadException('图片上传失败');
}
// 解析回调
if (result.success === true || result.key) { if (result.success === true || result.key) {
return; return;
} else {
throw new OakUploadException('图片上传失败');
} }
throw new Error('图片上传失败');
} }
composeFileUrl( composeFileUrl(
@ -113,6 +122,7 @@ export default class Qiniu<ED extends EntityDict & BaseEntityDict> implements Up
return false; return false;
} }
async removeFile(extraFile: OpSchema, context: BackendRuntimeContext<ED>) { async removeFile(extraFile: OpSchema, context: BackendRuntimeContext<ED>) {
const { bucket, uploadMeta } = extraFile; const { bucket, uploadMeta } = extraFile;
} }