ขออัปเดตข้อมูลจากกรมการปกครอง

This commit is contained in:
setthawutttty 2024-10-11 15:42:38 +07:00
parent 189347832c
commit c49d222a3f
10 changed files with 2155 additions and 14 deletions

View file

@ -0,0 +1,352 @@
import { ref } from "vue";
import { defineStore } from "pinia";
import { useCounterMixin } from "@/stores/mixin";
import { useQuasar } from "quasar";
import http from "@/plugins/http";
import config from "@/app.config";
import type { RequestObject } from "@/modules/04_registryPerson/interface/request/Address";
import type { ResponseObject } from "@/modules/04_registryPerson/interface/response/Address";
import type {
DataOption,
AddressOps,
InformationOps,
zipCodeOption,
} from "@/modules/04_registryPerson/interface/index/Main";
const $q = useQuasar();
const mixin = useCounterMixin();
const {
showLoader,
hideLoader,
messageError,
} = mixin;
export const useDataLinkCenter = defineStore("DataLinkCenter", () => {
const retireDate = ref<Date>();
const optionRelationshipMain = ref<DataOption[]>([]);
const optionRelationship = ref<DataOption[]>([]);
//รายการตัวเลือก
const OpsPerson = ref<InformationOps>({
prefixOps: [],
rankOps: [],
genderOps: [],
bloodOps: [],
statusOps: [],
religionOps: [],
employeeClassOps: [
{ id: "perm", name: "ลูกจ้างประจำ" },
{ id: "temp", name: "ลูกจ้างชั่วคราว" },
],
employeeTypeOps: [
{ id: "gov", name: "งบประมาณเงินอุดหนุนรัฐบาล" },
{ id: "bkk", name: "งบประมาณกรุงเทพมหานคร" },
],
});
//ข้อมูลตัวเลือก
const OpsPersonFilter = ref<InformationOps>({
prefixOps: [],
rankOps: [],
genderOps: [],
bloodOps: [],
statusOps: [],
religionOps: [],
employeeClassOps: [
{ id: "perm", name: "ลูกจ้างประจำ" },
{ id: "temp", name: "ลูกจ้างชั่วคราว" },
],
employeeTypeOps: [
{ id: "gov", name: "งบประมาณเงินอุดหนุนรัฐบาล" },
{ id: "bkk", name: "งบประมาณกรุงเทพมหานคร" },
],
});
const OpsAddress = ref<AddressOps>({
provinceOps: [],
districtOps: [],
districtCOps: [],
subdistrictOps: [],
subdistrictCOps: [],
});
const OpsAddressFilter = ref<AddressOps>({
provinceOps: [],
districtOps: [],
districtCOps: [],
subdistrictOps: [],
subdistrictCOps: [],
});
/**
*
* @param birthDate
* @returns
*/
function calculateAge(birthDate: Date | null) {
if (!birthDate) return null;
const birthDateTimeStamp = new Date(birthDate).getTime();
const now = new Date();
const diff = now.getTime() - birthDateTimeStamp;
const ageDate = new Date(diff);
const years = ageDate.getUTCFullYear() - 1970;
const months = ageDate.getUTCMonth();
const days = ageDate.getUTCDate() - 1;
const retire = new Date(birthDate);
retire.setFullYear(retire.getFullYear() + 60);
retireDate.value = retire;
if (years > 60) {
return "อายุเกิน 60 ปี";
}
return `${years} ปี ${months} เดือน ${days} วัน`;
}
/**
*
*/
async function fetchPerson() {
await http
.get(config.API.profileNewMetaMain)
.then((res) => {
const data = res.data.result;
let optionbloodGroups: DataOption[] = [];
data.bloodGroups.map((r: any) => {
optionbloodGroups.push({
id: r.id.toString(),
name: r.name.toString(),
});
});
OpsPerson.value.bloodOps = optionbloodGroups;
OpsPersonFilter.value.bloodOps = optionbloodGroups;
let optiongenders: DataOption[] = [];
data.genders.map((r: any) => {
optiongenders.push({
id: r.id.toString(),
name: r.name.toString(),
});
});
OpsPerson.value.genderOps = optiongenders;
OpsPersonFilter.value.genderOps = optiongenders;
let optionprefixs: DataOption[] = [];
data.prefixs.map((r: any) => {
optionprefixs.push({
id: r.id.toString(),
name: r.name.toString(),
});
});
OpsPerson.value.prefixOps = optionprefixs;
OpsPersonFilter.value.prefixOps = optionprefixs;
let optionrank: DataOption[] = [];
data.rank.map((r: any) => {
optionrank.push({
id: r.id.toString(),
name: r.name.toString(),
});
});
OpsPerson.value.rankOps = optionrank;
OpsPersonFilter.value.rankOps = optionrank;
let optionrelationships: DataOption[] = [];
data.relationships.map((r: any) => {
optionrelationships.push({
id: r.id.toString(),
name: r.name.toString(),
});
});
OpsPerson.value.statusOps = optionrelationships;
OpsPersonFilter.value.statusOps = optionrelationships;
let optionreligions: DataOption[] = [];
data.religions.map((r: any) => {
optionreligions.push({
id: r.id.toString(),
name: r.name.toString(),
});
});
OpsPerson.value.religionOps = optionreligions;
OpsPersonFilter.value.religionOps = optionreligions;
})
.catch((e) => {
messageError($q, e);
hideLoader();
})
.finally(() => {});
}
/** -----(จังหวัด-อำเภอ-ตำบล)----- */
/**
*
*/
async function fetchProvince(isLoad?: boolean) {
isLoad && showLoader();
await http
.get(config.API.profileNewProvince)
.then(async (res) => {
const data = res.data.result;
let option: DataOption[] = [];
data.map((r: any) => {
option.push({ id: r.id.toString(), name: r.name.toString() });
});
OpsAddress.value.provinceOps = option;
OpsAddressFilter.value.provinceOps = option;
})
.catch((e) => {
messageError($q, e);
})
.finally(() => {
isLoad && hideLoader();
});
}
/**
* /
* @param id
* @param position ,
*/
async function fetchDistrict(
id: string | null,
position: string,
isLoad?: boolean
) {
if (!id) return;
isLoad && showLoader();
await http
.get(config.API.profileNewDistrictByPId(id))
.then(async (res) => {
const data = res.data.result;
let option: DataOption[] = [];
data.districts.map((r: any) => {
option.push({ id: r.id, name: r.name });
});
if (position == "1") {
OpsAddress.value.districtOps = option;
OpsAddressFilter.value.districtOps = option;
} else {
OpsAddress.value.districtCOps = option;
OpsAddressFilter.value.districtCOps = option;
}
return option;
})
.catch((e) => {
messageError($q, e);
})
.finally(() => {
isLoad && hideLoader();
});
}
/**
* /
* @param id /
* @param position ,
*/
async function fetchSubDistrict(
id: string | null,
position: string,
isLoad: boolean
) {
if (!id) return;
isLoad && showLoader();
await http
.get(config.API.profileNewSubDistrictByDId(id))
.then(async (res) => {
const data = res.data.result;
let option: zipCodeOption[] = [];
data.subDistricts.map((r: any) => {
option.push({
id: r.id,
name: r.name,
zipCode: r.zipCode,
});
});
if (position == "1") {
OpsAddressFilter.value.subdistrictOps = option;
OpsAddress.value.subdistrictOps = option;
} else {
OpsAddressFilter.value.subdistrictCOps = option;
OpsAddress.value.subdistrictCOps = option;
}
})
.catch((e) => {
messageError($q, e);
})
.finally(() => {
isLoad && hideLoader();
});
}
/**
* select
* @param val
* @param update function
* @param refData
*/
function filterSelector(val: any, update: Function, refData: string) {
switch (refData) {
case "provinceOps":
update(() => {
OpsAddress.value.provinceOps =
OpsAddressFilter.value.provinceOps.filter(
(v: DataOption) => v.name.indexOf(val) > -1
);
});
break;
case "districtOps":
update(() => {
OpsAddress.value.districtOps =
OpsAddressFilter.value.districtOps.filter(
(v: DataOption) => v.name.indexOf(val) > -1
);
});
break;
case "districtCOps":
update(() => {
OpsAddress.value.districtCOps =
OpsAddressFilter.value.districtCOps.filter(
(v: DataOption) => v.name.indexOf(val) > -1
);
});
break;
case "subdistrictOps":
update(() => {
OpsAddress.value.subdistrictOps =
OpsAddressFilter.value.subdistrictOps.filter(
(v: DataOption) => v.name.indexOf(val) > -1
);
});
break;
case "subdistrictCOps":
update(() => {
OpsAddress.value.subdistrictCOps =
OpsAddressFilter.value.subdistrictCOps.filter(
(v: DataOption) => v.name.indexOf(val) > -1
);
});
break;
default:
break;
}
}
return {
fetchPerson,
OpsPersonFilter,
OpsPerson,
OpsAddress,
OpsAddressFilter,
calculateAge,
fetchProvince,
filterSelector,
fetchDistrict,
fetchSubDistrict,
optionRelationshipMain,
optionRelationship,
};
});