refactor: add Attachment
This commit is contained in:
parent
8ac40234ce
commit
6882f95122
1 changed files with 57 additions and 64 deletions
|
|
@ -16,6 +16,7 @@ import {
|
|||
import axios from 'axios';
|
||||
import useFlowStore from '../flow';
|
||||
import { Employee } from '../employee/types';
|
||||
import { baseUrl } from '../utils';
|
||||
|
||||
const useCustomerStore = defineStore('api-customer', () => {
|
||||
const flowStore = useFlowStore();
|
||||
|
|
@ -208,6 +209,55 @@ const useCustomerStore = defineStore('api-customer', () => {
|
|||
|
||||
return res.data;
|
||||
}
|
||||
|
||||
async function putAttachment(opts: {
|
||||
branchId: string;
|
||||
file: File;
|
||||
filename?: string;
|
||||
}) {
|
||||
await api.put(
|
||||
`/customer-branch/${opts.branchId}/attachment/${opts.filename || opts.file.name}`,
|
||||
opts.file,
|
||||
{
|
||||
headers: { 'X-Rtid': flowStore.rtid },
|
||||
onUploadProgress: (e) => console.log(e),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
async function deleteAttachment(id: string, filename: string) {
|
||||
await api.delete(`/customer-branch/${id}/attachment/{${filename}}`);
|
||||
}
|
||||
|
||||
async function listAttachment(id: string) {
|
||||
const res = await api.get<string[]>(`/customer-branch/${id}/attachment`, {
|
||||
headers: { 'X-Rtid': flowStore.rtid },
|
||||
});
|
||||
|
||||
if (res.status >= 400) return false;
|
||||
|
||||
return res.data;
|
||||
}
|
||||
|
||||
async function getAttachment(id: string, filename: string, download = false) {
|
||||
const url = `${baseUrl}/customer-branch/${id}/attachment/${filename}`;
|
||||
const res = await api.get<string>(url);
|
||||
|
||||
if (download) {
|
||||
fetch(res.data)
|
||||
.then(async (res) => await res.blob())
|
||||
.then((blob) => {
|
||||
let a = document.createElement('a');
|
||||
a.download = filename;
|
||||
a.href = window.URL.createObjectURL(blob);
|
||||
a.click();
|
||||
a.remove();
|
||||
});
|
||||
}
|
||||
|
||||
return res.data;
|
||||
}
|
||||
|
||||
async function create(
|
||||
data: CustomerCreate,
|
||||
flow?: {
|
||||
|
|
@ -395,7 +445,7 @@ const useCustomerStore = defineStore('api-customer', () => {
|
|||
});
|
||||
if (!res) return false;
|
||||
|
||||
if (file) await addBranchAttachment(res.data.id, { file });
|
||||
// if (file) await addBranchAttachment(res.data.id, { file });
|
||||
|
||||
return res.data;
|
||||
}
|
||||
|
|
@ -443,7 +493,7 @@ const useCustomerStore = defineStore('api-customer', () => {
|
|||
},
|
||||
);
|
||||
|
||||
if (file) await addBranchAttachment(id, { file });
|
||||
// if (file) await addBranchAttachment(id, { file });
|
||||
|
||||
if (!res) return false;
|
||||
|
||||
|
|
@ -472,68 +522,6 @@ const useCustomerStore = defineStore('api-customer', () => {
|
|||
return false;
|
||||
}
|
||||
|
||||
async function addBranchAttachment(
|
||||
branchId: string,
|
||||
payload: BranchAttachmentCreate,
|
||||
flow?: {
|
||||
sessionId?: string;
|
||||
refTransactionId?: string;
|
||||
transactionId?: string;
|
||||
},
|
||||
) {
|
||||
const list: { name: string; file: File }[] = [];
|
||||
|
||||
payload.file.forEach((v) => {
|
||||
let filename = v.name;
|
||||
|
||||
if (list.some((v) => v.name === filename)) {
|
||||
const dotIndex = filename.lastIndexOf('.');
|
||||
const originalName =
|
||||
dotIndex !== -1 && !filename.startsWith('.')
|
||||
? filename.slice(0, dotIndex)
|
||||
: filename;
|
||||
const extension =
|
||||
dotIndex !== -1 && !filename.startsWith('.')
|
||||
? filename.slice(dotIndex)
|
||||
: '';
|
||||
|
||||
let i = 0;
|
||||
|
||||
while (list.some((v) => v.name === filename)) {
|
||||
filename = `${originalName} (${++i})`;
|
||||
if (dotIndex !== -1) filename += extension;
|
||||
}
|
||||
}
|
||||
|
||||
list.push({ name: filename, file: v });
|
||||
});
|
||||
|
||||
const res = await api.post<(BranchAttachment & { uploadUrl: string })[]>(
|
||||
`/customer-branch/${branchId}/attachment`,
|
||||
{ file: list.map((v) => v.name) },
|
||||
{
|
||||
headers: {
|
||||
'X-Session-Id': flow?.sessionId,
|
||||
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
|
||||
'X-Tid': flow?.transactionId,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
await Promise.all(
|
||||
res.data.map(async (a) => {
|
||||
const found = list.find((b) => b.name === a.name)!;
|
||||
|
||||
await axios
|
||||
.put(a.uploadUrl, found.file, {
|
||||
headers: { 'Content-Type': found.file.type },
|
||||
onUploadProgress: (e) => console.log(e),
|
||||
})
|
||||
.catch((e) => console.error(e));
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
async function fetchListCustomeBranchById(
|
||||
branchId: string,
|
||||
flow?: {
|
||||
|
|
@ -576,6 +564,11 @@ const useCustomerStore = defineStore('api-customer', () => {
|
|||
fetchListCustomeBranchById,
|
||||
|
||||
fetchBranchEmployee,
|
||||
|
||||
listAttachment,
|
||||
getAttachment,
|
||||
putAttachment,
|
||||
deleteAttachment,
|
||||
};
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue