feat: user attachment
This commit is contained in:
parent
ee1f8e0bce
commit
542eb78ea5
2 changed files with 127 additions and 1 deletions
|
|
@ -2,12 +2,121 @@ import { ref } from 'vue';
|
||||||
import { defineStore } from 'pinia';
|
import { defineStore } from 'pinia';
|
||||||
import { Pagination } from '../types';
|
import { Pagination } from '../types';
|
||||||
import { api } from 'src/boot/axios';
|
import { api } from 'src/boot/axios';
|
||||||
import { User, UserCreate } from './types';
|
import {
|
||||||
|
User,
|
||||||
|
UserAttachment,
|
||||||
|
UserAttachmentCreate,
|
||||||
|
UserAttachmentDelete,
|
||||||
|
UserCreate,
|
||||||
|
} from './types';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
|
||||||
const useUserStore = defineStore('api-user', () => {
|
const useUserStore = defineStore('api-user', () => {
|
||||||
const data = ref<Pagination<User[]>>();
|
const data = ref<Pagination<User[]>>();
|
||||||
|
|
||||||
|
async function fetchAttachment(
|
||||||
|
userId: string,
|
||||||
|
flow?: {
|
||||||
|
sessionId: string;
|
||||||
|
refTransactionId: string;
|
||||||
|
transactionId: string;
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
const res = await api.get<UserAttachment[]>(`/user/${userId}/attachment`, {
|
||||||
|
headers: {
|
||||||
|
'X-Session-Id': flow?.sessionId,
|
||||||
|
'X-Rtid': flow?.refTransactionId,
|
||||||
|
'X-Tid': flow?.transactionId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (res && res.status === 200) {
|
||||||
|
return res.data;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function addAttachment(
|
||||||
|
userId: string,
|
||||||
|
payload: UserAttachmentCreate,
|
||||||
|
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<(UserAttachment & { uploadUrl: string })[]>(
|
||||||
|
`/user/${userId}/attachment`,
|
||||||
|
{ file: list.map((v) => v.name) },
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
'X-Session-Id': flow?.sessionId,
|
||||||
|
'X-Rtid': flow?.refTransactionId,
|
||||||
|
'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 deleteAttachment(
|
||||||
|
userId: string,
|
||||||
|
payload: UserAttachmentDelete,
|
||||||
|
flow?: {
|
||||||
|
sessionId: string;
|
||||||
|
refTransactionId: string;
|
||||||
|
transactionId: string;
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
await api.delete(`/user/${userId}/attachment`, {
|
||||||
|
data: payload,
|
||||||
|
headers: {
|
||||||
|
'X-Session-Id': flow?.sessionId,
|
||||||
|
'X-Rtid': flow?.refTransactionId,
|
||||||
|
'X-Tid': flow?.transactionId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async function fetchList(
|
async function fetchList(
|
||||||
opts?: {
|
opts?: {
|
||||||
page?: number;
|
page?: number;
|
||||||
|
|
@ -247,6 +356,10 @@ const useUserStore = defineStore('api-user', () => {
|
||||||
getBranch,
|
getBranch,
|
||||||
addBranch,
|
addBranch,
|
||||||
removeBranch,
|
removeBranch,
|
||||||
|
|
||||||
|
fetchAttachment,
|
||||||
|
addAttachment,
|
||||||
|
deleteAttachment,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -70,3 +70,16 @@ export type UserCreate = {
|
||||||
keycloakId: string;
|
keycloakId: string;
|
||||||
profileImage: File;
|
profileImage: File;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type UserAttachment = {
|
||||||
|
name: string;
|
||||||
|
url: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type UserAttachmentCreate = {
|
||||||
|
file: File[];
|
||||||
|
};
|
||||||
|
|
||||||
|
export type UserAttachmentDelete = {
|
||||||
|
file: string[];
|
||||||
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue