| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- import type { AntUploadRequestOption, UploadCoInterface } from "@/components/dynamicf/UploadImageFormItem";
- import { RandomUtils, StringUtils } from "@imengyu/imengyu-utils";
- import OSS from 'ali-oss';
- const client = new OSS({
- region: 'oss-cn-shenzhen',
- accessKeyId: 'LTAI5t5e7wAQ1FvUA4LCsNs5',
- accessKeySecret: 'lhF0SimpatPMHNjmjtIKsWsYwTmJhx',
- bucket: 'minnanwenhua',
- });
- export function useAliOssUploadCo(subPath: string) : UploadCoInterface {
- return {
- uploadRequest: async (requestOption: AntUploadRequestOption) => {
- const uploadPath = `${subPath}/${requestOption.file.name.split('.')[0]}-${RandomUtils.genNonDuplicateID(26)}.${StringUtils.path.getFileExt(requestOption.file.name)}`;
- //小于8mb则直接上传,否则分片上传
- if (requestOption.file.size < 8 * 1024 * 1024) {
- client.put(uploadPath, requestOption.file).then((res) => {
- requestOption.onSuccess?.({
- url: res.url,
- key: uploadPath,
- }, null);
- }).catch((err) => {
- requestOption.onError?.(err, {});
- })
- } else {
- client.multipartUpload(uploadPath, requestOption.file, {
- progress(percentage) {
- requestOption.onProgress({ percent: percentage * 100 })
- },
- }).then((res) => {
- requestOption.onSuccess?.({
- url: client.generateObjectUrl(uploadPath),
- key: uploadPath,
- }, null);
- }).catch((err) => {
- requestOption.onError?.(err, {});
- });
- }
- },
- getUrlByUploadResponse: (response: unknown) => {
- return (response as any).url as string;
- },
- }
- }
|