223 lines
5.5 KiB
TypeScript
223 lines
5.5 KiB
TypeScript
import { defineStore } from 'pinia';
|
|
import { ref } from 'vue';
|
|
import { Institution, InstitutionPayload } from './types';
|
|
import { api } from 'src/boot/axios';
|
|
import { PaginationResult } from 'src/types';
|
|
import useFlowStore from '../flow';
|
|
import { Status } from '../types';
|
|
|
|
export const useInstitution = defineStore('institution-store', () => {
|
|
const flowStore = useFlowStore();
|
|
|
|
const data = ref<Institution[]>([]);
|
|
const page = ref<number>(1);
|
|
const pageMax = ref<number>(1);
|
|
const pageSize = ref<number>(30);
|
|
|
|
async function getInstitution(id: string) {
|
|
const res = await api.get<Institution>(`/institution/${id}`);
|
|
if (res.status < 400) {
|
|
return res.data;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
async function getInstitutionList(opts?: {
|
|
page?: number;
|
|
pageSize?: number;
|
|
query?: string;
|
|
group?: string;
|
|
status?: Status;
|
|
payload?: { group?: string[] };
|
|
startDate?: string;
|
|
endDate?: string;
|
|
activeOnly?: boolean;
|
|
}) {
|
|
const { payload, ...params } = opts || {};
|
|
|
|
console.log(params.query);
|
|
|
|
const res = payload
|
|
? await api.post<PaginationResult<Institution>>(
|
|
'/institution/list',
|
|
payload,
|
|
{
|
|
params,
|
|
},
|
|
)
|
|
: await api.get<PaginationResult<Institution>>('/institution', {
|
|
params,
|
|
});
|
|
|
|
if (res.status < 400) {
|
|
return res.data;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
async function createInstitution(
|
|
data: InstitutionPayload,
|
|
imgList: {
|
|
selectedImage: string;
|
|
list: { url: string; imgFile: File | null; name: string }[];
|
|
},
|
|
) {
|
|
const res = await api.post('/institution', {
|
|
...data,
|
|
selectedImage: imgList.selectedImage || '',
|
|
});
|
|
|
|
if (imgList.list.length > 0 && res.data.id) {
|
|
for (let index = 0; index < imgList.list.length; index++) {
|
|
const imgFile = imgList.list[index].imgFile;
|
|
if (imgFile)
|
|
await addImageList(imgFile, res.data.id, imgList.list[index].name);
|
|
}
|
|
}
|
|
|
|
if (res.data.bank && data.bank.length > 0) {
|
|
for (let i = 0; i < data.bank?.length; i++) {
|
|
if (data.bank[i].bankQr) {
|
|
await api
|
|
.put(
|
|
`/institution/${res.data.id}/bank-qr/${res.data.bank[i].id}`,
|
|
data.bank[i].bankQr,
|
|
{
|
|
headers: { 'Content-Type': data.bank[i].bankQr?.type },
|
|
onUploadProgress: (e) => console.log(e),
|
|
},
|
|
)
|
|
.catch((e) => console.error(e));
|
|
}
|
|
}
|
|
}
|
|
|
|
if (res.status < 400) {
|
|
return res.data;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
async function editInstitution(
|
|
data: InstitutionPayload & { id: string },
|
|
opts?: { indexDeleteQrCodeBank?: number[] },
|
|
) {
|
|
const res = await api.put(`/institution/${data.id}`, {
|
|
...data,
|
|
id: undefined,
|
|
group: undefined,
|
|
});
|
|
|
|
if (!!res.data.bank && !!data.bank.length) {
|
|
for (let i = 0; i < data.bank?.length; i++) {
|
|
if (data.bank[i].bankQr) {
|
|
console.log(i);
|
|
console.log(data.bank[i].bankQr);
|
|
await api
|
|
.put(
|
|
`/institution/${res.data.id}/bank-qr/${res.data.bank[i].id}`,
|
|
data.bank[i].bankQr,
|
|
{
|
|
headers: { 'Content-Type': data.bank[i].bankQr?.type },
|
|
onUploadProgress: (e) => console.log(e),
|
|
},
|
|
)
|
|
.catch((e) => console.error(e));
|
|
}
|
|
}
|
|
}
|
|
|
|
if (opts.indexDeleteQrCodeBank && opts.indexDeleteQrCodeBank.length > 0) {
|
|
console.log('delete');
|
|
opts.indexDeleteQrCodeBank.forEach(async (i) => {
|
|
await api
|
|
.delete(`/institution/${res.data.id}/bank-qr/${res.data.bank[i].id}`)
|
|
.catch((e) => console.error(e));
|
|
});
|
|
}
|
|
|
|
if (res.status < 400) {
|
|
return res.data;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
async function deleteInstitution(id: string) {
|
|
const res = await api.delete(`/institution/${id}`);
|
|
if (res.status < 400) {
|
|
return res.data;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
async function fetchImageListById(
|
|
id: string,
|
|
flow?: {
|
|
sessionId?: string;
|
|
refTransactionId?: string;
|
|
transactionId?: string;
|
|
},
|
|
) {
|
|
const res = await api.get(`/institution/${id}/image`, {
|
|
headers: {
|
|
'X-Session-Id': flow?.sessionId,
|
|
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
|
|
'X-Tid': flow?.transactionId,
|
|
},
|
|
});
|
|
|
|
if (!res) return false;
|
|
if (res.status === 200) return res.data;
|
|
if (res.status === 204) return null;
|
|
|
|
return false;
|
|
}
|
|
|
|
async function addImageList(file: File, id: string, name: string) {
|
|
await api
|
|
.put(`/institution/${id}/image/${name}`, file, {
|
|
headers: { 'Content-Type': file.type },
|
|
onUploadProgress: (e) => console.log(e),
|
|
})
|
|
.catch((e) => console.error(e));
|
|
|
|
return name;
|
|
}
|
|
|
|
async function deleteImageByName(
|
|
id: string,
|
|
name: string,
|
|
flow?: {
|
|
sessionId?: string;
|
|
refTransactionId?: string;
|
|
transactionId?: string;
|
|
},
|
|
) {
|
|
const res = await api.delete(`/institution/${id}/image/${name}`, {
|
|
headers: {
|
|
'X-Session-Id': flow?.sessionId,
|
|
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
|
|
'X-Tid': flow?.transactionId,
|
|
},
|
|
});
|
|
|
|
if (!res) return false;
|
|
}
|
|
|
|
return {
|
|
data,
|
|
page,
|
|
pageMax,
|
|
pageSize,
|
|
|
|
getInstitution,
|
|
getInstitutionList,
|
|
createInstitution,
|
|
editInstitution,
|
|
deleteInstitution,
|
|
|
|
fetchImageListById,
|
|
addImageList,
|
|
deleteImageByName,
|
|
};
|
|
});
|