This commit is contained in:
Viet An
2026-06-05 20:12:34 +07:00
parent 3254c64f7d
commit 541f99f82a
13 changed files with 47 additions and 83 deletions

View File

@@ -223,7 +223,7 @@ const {
$resetNull, $resetNull,
$findapi, $findapi,
$insertapi, $insertapi,
$updateapi, $patchapi,
$remove, $remove,
$deleteapi, $deleteapi,
$empty, $empty,
@@ -315,7 +315,7 @@ async function update() {
data.link = arr1.length === 0 ? null : arr1; data.link = arr1.length === 0 ? null : arr1;
let api = $findapi("useraction"); let api = $findapi("useraction");
record = data.id record = data.id
? await $updateapi("useraction", { data, values: api.params.values }) ? await $patchapi("useraction", { data, values: api.params.values })
: await $insertapi("useraction", { data, values: api.params.values }); : await $insertapi("useraction", { data, values: api.params.values });
getValue(); getValue();
} }

View File

@@ -21,7 +21,7 @@
</template> </template>
<script setup> <script setup>
const emit = defineEmits(["close"]); const emit = defineEmits(["close"]);
const { $store, $getdata, $updateapi, $updatepage } = useNuxtApp(); const { $store, $getdata, $patchapi, $updatepage } = useNuxtApp();
const props = defineProps({ const props = defineProps({
row: Object, row: Object,
pagename: String, pagename: String,
@@ -31,7 +31,7 @@ let record = await $getdata("application", {
first: true, first: true,
}); });
async function save() { async function save() {
await $updateapi("application", record); await $patchapi("application", record);
record = await $getdata("application", { record = await $getdata("application", {
filter: { id: props.row.id }, filter: { id: props.row.id },
first: true, first: true,

View File

@@ -119,7 +119,7 @@ export default {
data = this.$copy(this.current); data = this.$copy(this.current);
data.detail = this.detail; data.detail = this.detail;
} }
let rs = data.id ? await this.$updateapi(this.api, data) : await this.$insertapi(this.api, { data }); let rs = data.id ? await this.$patchapi(this.api, data) : await this.$insertapi(this.api, { data });
if (!rs) return; if (!rs) return;
this.detail = undefined; this.detail = undefined;
if (this.current) { if (this.current) {

View File

@@ -205,7 +205,7 @@ var props = defineProps({
row: Object, row: Object,
}); });
const store = useStore(); const store = useStore();
const { $find, $getdata, $updateapi, $insertapi, $findapi, $getapi, $empty, $errPhone, $resetNull, $snackbar } = const { $find, $getdata, $patchapi, $insertapi, $findapi, $getapi, $empty, $errPhone, $resetNull, $snackbar } =
useNuxtApp(); useNuxtApp();
const emit = defineEmits(["update", "dataevent"]); const emit = defineEmits(["update", "dataevent"]);
var viewport = store.viewport; var viewport = store.viewport;
@@ -271,7 +271,7 @@ async function update() {
record.value.updater = store.login.id; record.value.updater = store.login.id;
record.update_time = new Date(); record.update_time = new Date();
let rs = record.value.id let rs = record.value.id
? await $updateapi("company", record.value) ? await $patchapi("company", record.value)
: await $insertapi("company", { data: record.value }); : await $insertapi("company", { data: record.value });
if (rs === "error") return; if (rs === "error") return;
if (!record.value.id) $snackbar(`Khách hàng đã được khởi tạo với mã <b>${rs.code}</b>`, "Success"); if (!record.value.id) $snackbar(`Khách hàng đã được khởi tạo với mã <b>${rs.code}</b>`, "Success");

View File

@@ -120,7 +120,7 @@ const props = defineProps({
data: Object, data: Object,
focus: Boolean, focus: Boolean,
}); });
const { $empty, $copy, $copyToClipboard, $stripHtml, $updateapi, $insertapi, $findIndex, $snackbar } = useNuxtApp(); const { $empty, $copy, $copyToClipboard, $stripHtml, $patchapi, $insertapi, $findIndex, $snackbar } = useNuxtApp();
const radioOption = ref(); const radioOption = ref();
const login = { id: 1 }; const login = { id: 1 };
const errors = ref([]); const errors = ref([]);
@@ -180,7 +180,7 @@ async function saveSetting() {
detail, detail,
update_time: new Date(), update_time: new Date(),
}; };
result = await $updateapi("usersetting", updatedSetting); result = await $patchapi("usersetting", updatedSetting);
} }
isLoading.value = false; isLoading.value = false;
if (radioSave.value === "new") { if (radioSave.value === "new") {

View File

@@ -45,7 +45,7 @@ export default {
if (this.setdeleted) { if (this.setdeleted) {
let record = await this.$getdata(name, { first: true, filter: { id } }); let record = await this.$getdata(name, { first: true, filter: { id } });
record.deleted = 1; record.deleted = 1;
result = await this.$updateapi(name, record); result = await this.$patchapi(name, record);
} else result = await this.$deleteapi(name, id); } else result = await this.$deleteapi(name, id);
if (result === "error") return this.$dialog("Đã xảy ra lỗi, xóa dữ liệu thất bại", "Lỗi", "Error"); if (result === "error") return this.$dialog("Đã xảy ra lỗi, xóa dữ liệu thất bại", "Lỗi", "Error");
this.$snackbar("Dữ liệu đã được xoá khỏi hệ thống", "Success"); this.$snackbar("Dữ liệu đã được xoá khỏi hệ thống", "Success");

View File

@@ -45,7 +45,7 @@ export default {
}, },
methods: { methods: {
async save() { async save() {
let rs = await this.$updateapi("file", this.record); let rs = await this.$patchapi("file", this.record);
}, },
}, },
}; };

View File

@@ -168,7 +168,7 @@ export default {
}, },
async saveData() { async saveData() {
if (!this.row) return this.insertData(); if (!this.row) return this.insertData();
await this.$updateapi("importsetting", this.current, undefined, this.pagename); await this.$patchapi("importsetting", this.current, undefined, this.pagename);
}, },
updateFromModel() { updateFromModel() {
let filter = this.$filter(this.pagedata2.data, { let filter = this.$filter(this.pagedata2.data, {

View File

@@ -266,7 +266,7 @@ const store = useStore();
const { const {
$find, $find,
$getdata, $getdata,
$updateapi, $patchapi,
$findapi, $findapi,
$getapi, $getapi,
$empty, $empty,
@@ -361,7 +361,7 @@ async function update() {
record.value.updater = store.login.id; record.value.updater = store.login.id;
record.value.updater_time = new Date(); record.value.updater_time = new Date();
let rs = record.value.id let rs = record.value.id
? await $updateapi("people", record.value) ? await $patchapi("people", record.value)
: await $insertapi("people", { data: record.value, notify: false }); : await $insertapi("people", { data: record.value, notify: false });
if (rs === "error") return; if (rs === "error") return;
if (!record.value.id) $snackbar(`Người liên quan đã được khởi tạo với mã <b>${rs.code}</b>`, "Success"); if (!record.value.id) $snackbar(`Người liên quan đã được khởi tạo với mã <b>${rs.code}</b>`, "Success");

View File

@@ -160,7 +160,7 @@ export default {
}); });
let rs0 = await this.$insertapi("gethash", { data: { text: this.password }, notify: false }); let rs0 = await this.$insertapi("gethash", { data: { text: this.password }, notify: false });
user.password = rs0.rows[0]; user.password = rs0.rows[0];
let rs = await this.$updateapi("user", user, undefined, false); let rs = await this.$patchapi("user", user, undefined, false);
if (rs !== "error") { if (rs !== "error") {
this.currpass = undefined; this.currpass = undefined;
this.password = undefined; this.password = undefined;

View File

@@ -53,7 +53,7 @@ export default {
if (this.checkError()) return this.$snackbar("Mã phê duyệt gồm 4 số từ 0-9"); if (this.checkError()) return this.$snackbar("Mã phê duyệt gồm 4 số từ 0-9");
let user = await this.$getdata("user", { first: true, filter: { id: this.$store.state.login.id } }); let user = await this.$getdata("user", { first: true, filter: { id: this.$store.state.login.id } });
user.approval_code = this.code; user.approval_code = this.code;
await this.$updateapi("user", user); await this.$patchapi("user", user);
}, },
checkError() { checkError() {
if (Object.keys(this.data).length < 4) return true; if (Object.keys(this.data).length < 4) return true;

View File

@@ -110,7 +110,7 @@ export default {
}); });
let rs = await this.$insertapi("gethash", { data: { text: this.password } }); let rs = await this.$insertapi("gethash", { data: { text: this.password } });
user.password = rs.rows[0]; user.password = rs.rows[0];
let rs1 = await this.$updateapi("user", user); let rs1 = await this.$patchapi("user", user);
if (rs1 !== "error") this.$emit("close"); if (rs1 !== "error") this.$emit("close");
else { else {
this.$snackbar("Có lỗi xảy ra. Hãy thử lại một lần nữa", "Error"); this.$snackbar("Có lỗi xảy ra. Hãy thử lại một lần nữa", "Error");

View File

@@ -1134,7 +1134,6 @@ export default defineNuxtPlugin((nuxtApp) => {
return apisWithReady; return apisWithReady;
}; };
// get data
const getapi = async function (list) { const getapi = async function (list) {
try { try {
const arr = list.map((v) => { const arr = list.map((v) => {
@@ -1159,7 +1158,6 @@ export default defineNuxtPlugin((nuxtApp) => {
} }
}; };
// insert data
const insertapi = async function (name, { data, values, notify = true } = {}) { const insertapi = async function (name, { data, values, notify = true } = {}) {
try { try {
const api = findapi(name); const api = findapi(name);
@@ -1206,64 +1204,43 @@ export default defineNuxtPlugin((nuxtApp) => {
} }
}; };
// update api
const updateapi = async function (name, data, values, notify) {
try {
let found = findapi(name);
let curpath = found.path ? paths.find((x) => x.name === found.path).url : path;
let updateUrl = found.url_detail ? found.url_detail : found.url;
let rs = await $fetch(`${curpath}${updateUrl}${data.id}/`, {
method: "PUT",
body: data,
params: { values: values || found.params.values },
});
if (found.commit) {
let index = $store[found.commit] ? $store[found.commit].findIndex((v) => v.id === rs.id) : -1;
if (index >= 0) {
var copy = $copy($store[found.commit]);
if (Array.isArray(rs) === false) copy[index] = rs;
else {
rs.forEach((v) => {
let index = copy.findIndex((v) => v.id === v.id);
if (index >= 0) copy[index] = v;
});
}
$store.commit(found.commit, copy);
}
}
if (notify !== false) {
$store.lang === "en"
? $snackbar("Data has been successfully saved to the system.", "Success")
: $snackbar("Dữ liệu đã được lưu vào hệ thống", "Success");
}
return rs;
} catch (err) {
console.log(err);
return "error";
}
};
// patch api
const patchapi = async function (name, data, values, notify) { const patchapi = async function (name, data, values, notify) {
try { try {
let found = findapi(name); const api = findapi(name);
let curpath = found.path ? paths.find((x) => x.name === found.path).url : path; const curpath = api.path ? paths.find((x) => x.name === api.path).url : path;
let updateUrl = found.url_detail ? found.url_detail : found.url; const updateUrl = api.url_detail || api.url;
const rs = await $fetch(`${curpath}${updateUrl}${data.id}/`, { const rs = await $fetch(`${curpath}${updateUrl}${data.id}/`, {
method: "PATCH", method: "PATCH",
body: data, body: data,
params: { values: values || found.params?.values }, params: { values: values || api.params?.values },
}); });
if (notify !== false) { if (api.commit) {
$store.lang === "en" const index = $store[api.commit] ? $store[api.commit].findIndex((v) => v.id === rs.id) : -1;
? $snackbar("Data has been successfully saved to the system.", "Success") if (index >= 0) {
: $snackbar("Dữ liệu đã được lưu vào hệ thống", "Success"); const copy = $copy($store[api.commit]);
if (Array.isArray(rs)) {
rs.forEach((r) => {
const index = copy.findIndex((v) => v.id === r.id);
if (index >= 0) copy[index] = r;
});
} else {
copy[index] = rs;
}
$store.commit(api.commit, copy);
}
}
if (notify) {
$snackbar(
$store.lang === "en" ? "Data has been successfully saved to the system." : "Dữ liệu đã được lưu vào hệ thống",
"Success",
);
} }
return rs; return rs;
} catch (err) { } catch (err) {
console.log(err); console.error(err);
return "error"; return "error";
} }
}; };
@@ -1335,10 +1312,10 @@ export default defineNuxtPlugin((nuxtApp) => {
}; };
const getdata = async function (name, { filter, params, first = false } = {}) { const getdata = async function (name, { filter, params, first = false } = {}) {
const found = findapi(name); const api = findapi(name);
if (params) found.params = params; if (params) api.params = params;
else if (filter) found.params.filter = filter; else if (filter) api.params.filter = filter;
const rs = await getapi([found]); const rs = await getapi([api]);
const { data } = rs[0]; const { data } = rs[0];
if (data) { if (data) {
if (data.rows) { if (data.rows) {
@@ -1394,7 +1371,6 @@ export default defineNuxtPlugin((nuxtApp) => {
subscribeToData(payload, callback); subscribeToData(payload, callback);
}; };
// insert row
const insertrow = async function (name, data, values, pagename, notify) { const insertrow = async function (name, data, values, pagename, notify) {
let result = await insertapi(name, { data, values, notify }); let result = await insertapi(name, { data, values, notify });
if (result === "error" || !pagename || !$store[pagename]) return result; if (result === "error" || !pagename || !$store[pagename]) return result;
@@ -1404,18 +1380,7 @@ export default defineNuxtPlugin((nuxtApp) => {
return result; return result;
}; };
// update row
const updaterow = async function (name, data, values, pagename, notify) { const updaterow = async function (name, data, values, pagename, notify) {
let result = await updateapi(name, data, values, notify);
if (result === "error" || !pagename || !$store[pagename]) return result;
let copy = $clone($store[pagename]);
copy.update = { refresh: true };
$store.commit(pagename, copy);
return result;
};
// patch row
const patchrow = async function (name, data, values, pagename, notify) {
let result = await patchapi(name, data, values, notify); let result = await patchapi(name, data, values, notify);
if (result === "error" || !pagename || !$store[pagename]) return result; if (result === "error" || !pagename || !$store[pagename]) return result;
let copy = $clone($store[pagename]); let copy = $clone($store[pagename]);
@@ -1683,7 +1648,6 @@ export default defineNuxtPlugin((nuxtApp) => {
getdata, getdata,
subscribe, subscribe,
insertapi, insertapi,
updateapi,
patchapi, patchapi,
updaterow, updaterow,
findpage, findpage,