|
|
|
|
@@ -1,8 +1,4 @@
|
|
|
|
|
// nuxt 3 - plugins/my-plugin.ts
|
|
|
|
|
import { useStore } from "~/stores/index";
|
|
|
|
|
import axios from "axios";
|
|
|
|
|
|
|
|
|
|
export default defineNuxtPlugin(() => {
|
|
|
|
|
export default defineNuxtPlugin((nuxtApp) => {
|
|
|
|
|
const module = "application";
|
|
|
|
|
const mode = "dev";
|
|
|
|
|
const paths = [
|
|
|
|
|
@@ -1001,13 +997,12 @@ export default defineNuxtPlugin(() => {
|
|
|
|
|
params: {},
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
const store = useStore();
|
|
|
|
|
const { $copy, $clone, $updateSeriesFields, $snackbar, $remove, $dialog } = useNuxtApp();
|
|
|
|
|
const { $copy, $clone, $updateSeriesFields, $snackbar, $store, $remove, $dialog } = nuxtApp;
|
|
|
|
|
|
|
|
|
|
const requestLogin = function () {
|
|
|
|
|
store.commit("login", undefined);
|
|
|
|
|
store.commit("layersetting", undefined);
|
|
|
|
|
store.commit("lastlegendfiltertab", "Giỏ hàng");
|
|
|
|
|
$store.commit("login", undefined);
|
|
|
|
|
$store.commit("layersetting", undefined);
|
|
|
|
|
$store.commit("lastlegendfiltertab", "Giỏ hàng");
|
|
|
|
|
window.location.href = `https://${mode === "dev" ? "dev." : ""}login.utopia.com.vn/signin?module=${module}&link=${window.location.origin}`;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
@@ -1022,17 +1017,19 @@ export default defineNuxtPlugin(() => {
|
|
|
|
|
return $copy(result);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const readyapi = function (list) {
|
|
|
|
|
var array = [];
|
|
|
|
|
list.forEach((element) => {
|
|
|
|
|
let found = apis.find((v) => v.name === element);
|
|
|
|
|
const readyapi = function (apiNames) {
|
|
|
|
|
const apisWithReady = [];
|
|
|
|
|
apiNames.forEach((apiName) => {
|
|
|
|
|
const found = apis.find((v) => v.name === apiName);
|
|
|
|
|
if (found) {
|
|
|
|
|
let ele = JSON.parse(JSON.stringify(found));
|
|
|
|
|
ele.ready = store[element] ? true : false;
|
|
|
|
|
array.push(ele);
|
|
|
|
|
const apiWithReady = {
|
|
|
|
|
...$copy(found),
|
|
|
|
|
ready: Boolean($store[apiName]),
|
|
|
|
|
};
|
|
|
|
|
apisWithReady.push(apiWithReady);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return array;
|
|
|
|
|
return apisWithReady;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// get data
|
|
|
|
|
@@ -1042,16 +1039,15 @@ export default defineNuxtPlugin(() => {
|
|
|
|
|
let found = apis.find((api) => api.name === v.name);
|
|
|
|
|
let url = (v.path ? paths.find((x) => x.name === v.path).url : path) + (v.url ? v.url : found.url);
|
|
|
|
|
let params = v.params ? v.params : found.params === undefined ? {} : found.params;
|
|
|
|
|
params.login = store.login ? store.login.id : undefined;
|
|
|
|
|
return { url: url, params: params };
|
|
|
|
|
params.login = $store.login ? $store.login.id : undefined;
|
|
|
|
|
return { url, params };
|
|
|
|
|
});
|
|
|
|
|
//let data = await Promise.all(arr.map(v=>axios.get(v.url, {params: v.params})))
|
|
|
|
|
let data = await Promise.all(arr.map((v) => $fetch(v.url, { params: v.params })));
|
|
|
|
|
data.map((v, i) => {
|
|
|
|
|
list[i].data = v;
|
|
|
|
|
if (list[i].commit) {
|
|
|
|
|
let data = v.rows ? v.rows : v;
|
|
|
|
|
store.commit(list[i].commit, data);
|
|
|
|
|
$store.commit(list[i].commit, data);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return list;
|
|
|
|
|
@@ -1067,20 +1063,24 @@ export default defineNuxtPlugin(() => {
|
|
|
|
|
let found = findapi(name);
|
|
|
|
|
let curpath = found.path ? paths.find((x) => x.name === found.path).url : path;
|
|
|
|
|
var rs;
|
|
|
|
|
if (!Array.isArray(data))
|
|
|
|
|
rs = await axios.post(`${curpath}${found.url}`, data, {
|
|
|
|
|
params: { values: values },
|
|
|
|
|
if (!Array.isArray(data)) {
|
|
|
|
|
rs = await $fetch(`${curpath}${found.url}`, {
|
|
|
|
|
method: "POST",
|
|
|
|
|
body: data,
|
|
|
|
|
params: { values },
|
|
|
|
|
});
|
|
|
|
|
else {
|
|
|
|
|
let params = { action: "import", values: values };
|
|
|
|
|
rs = await axios.post(`${curpath}import-data/${found.url.substring(5, found.url.length - 1)}/`, data, {
|
|
|
|
|
params: params,
|
|
|
|
|
} else {
|
|
|
|
|
let params = { action: "import", values };
|
|
|
|
|
rs = await $fetch(`${curpath}import-data/${found.url.substring(5, found.url.length - 1)}/`, {
|
|
|
|
|
method: "POST",
|
|
|
|
|
body: data,
|
|
|
|
|
params,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// update store
|
|
|
|
|
if (found.commit) {
|
|
|
|
|
if (store[found.commit]) {
|
|
|
|
|
let copy = JSON.parse(JSON.stringify(store[found.commit]));
|
|
|
|
|
if ($store[found.commit]) {
|
|
|
|
|
let copy = JSON.parse(JSON.stringify($store[found.commit]));
|
|
|
|
|
let rows = Array.isArray(rs.data) ? rs.data : [rs.data];
|
|
|
|
|
rows.map((v) => {
|
|
|
|
|
if (v.id && !v.error) {
|
|
|
|
|
@@ -1089,11 +1089,11 @@ export default defineNuxtPlugin(() => {
|
|
|
|
|
else copy.push(v);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
store.commit(found.commit, copy);
|
|
|
|
|
$store.commit(found.commit, copy);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (notify !== false) {
|
|
|
|
|
store.lang === "en"
|
|
|
|
|
$store.lang === "en"
|
|
|
|
|
? $snackbar("Data has been successfully saved to the system.", "Success", "Success")
|
|
|
|
|
: $snackbar("Dữ liệu đã được lưu vào hệ thống", "Thành công", "Success");
|
|
|
|
|
}
|
|
|
|
|
@@ -1110,25 +1110,27 @@ export default defineNuxtPlugin(() => {
|
|
|
|
|
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 axios.put(`${curpath}${updateUrl}${data.id}/`, data, {
|
|
|
|
|
params: { values: values ? values : found.params.values },
|
|
|
|
|
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.data.id) : -1;
|
|
|
|
|
let index = $store[found.commit] ? $store[found.commit].findIndex((v) => v.id === rs.id) : -1;
|
|
|
|
|
if (index >= 0) {
|
|
|
|
|
var copy = JSON.parse(JSON.stringify(store[found.commit]));
|
|
|
|
|
if (Array.isArray(rs.data) === false) copy[index] = rs.data;
|
|
|
|
|
var copy = $copy($store[found.commit]);
|
|
|
|
|
if (Array.isArray(rs) === false) copy[index] = rs;
|
|
|
|
|
else {
|
|
|
|
|
rs.data.forEach((v) => {
|
|
|
|
|
rs.forEach((v) => {
|
|
|
|
|
let index = copy.findIndex((v) => v.id === v.id);
|
|
|
|
|
if (index >= 0) copy[index] = v;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
store.commit(found.commit, copy);
|
|
|
|
|
$store.commit(found.commit, copy);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (notify !== false) {
|
|
|
|
|
store.lang === "en"
|
|
|
|
|
$store.lang === "en"
|
|
|
|
|
? $snackbar("Data has been successfully saved to the system.", "Success", "Success")
|
|
|
|
|
: $snackbar("Dữ liệu đã được lưu vào hệ thống", "Thành công", "Success");
|
|
|
|
|
}
|
|
|
|
|
@@ -1145,15 +1147,19 @@ export default defineNuxtPlugin(() => {
|
|
|
|
|
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 axios.patch(`${curpath}${updateUrl}${data.id}/`, data, {
|
|
|
|
|
params: { values: values ? values : found.params?.values },
|
|
|
|
|
|
|
|
|
|
const rs = await $fetch(`${curpath}${updateUrl}${data.id}/`, {
|
|
|
|
|
method: "PATCH",
|
|
|
|
|
body: data,
|
|
|
|
|
params: { values: values || found.params?.values },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (notify !== false) {
|
|
|
|
|
store.lang === "en"
|
|
|
|
|
$store.lang === "en"
|
|
|
|
|
? $snackbar("Data has been successfully saved to the system.", "Success", "Success")
|
|
|
|
|
: $snackbar("Dữ liệu đã được lưu vào hệ thống", "Thành công", "Success");
|
|
|
|
|
}
|
|
|
|
|
return rs.data;
|
|
|
|
|
return rs;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.log(err);
|
|
|
|
|
return "error";
|
|
|
|
|
@@ -1161,7 +1167,7 @@ export default defineNuxtPlugin(() => {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const findpage = function (arr) {
|
|
|
|
|
var copy = $copy(store.pagetrack);
|
|
|
|
|
var copy = $copy($store.pagetrack);
|
|
|
|
|
var doFind = function () {
|
|
|
|
|
let found = undefined;
|
|
|
|
|
for (let i = 1; i <= 30; i++) {
|
|
|
|
|
@@ -1184,7 +1190,7 @@ export default defineNuxtPlugin(() => {
|
|
|
|
|
} else {
|
|
|
|
|
result = doFind(copy);
|
|
|
|
|
}
|
|
|
|
|
store.commit("pagetrack", copy);
|
|
|
|
|
$store.commit("pagetrack", copy);
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
@@ -1209,7 +1215,7 @@ export default defineNuxtPlugin(() => {
|
|
|
|
|
const setpage = function (pagename, row, api) {
|
|
|
|
|
let json = row.detail;
|
|
|
|
|
let fields = $updateSeriesFields(json.fields);
|
|
|
|
|
let copy = store[pagename] || getpage();
|
|
|
|
|
let copy = $store[pagename] || getpage();
|
|
|
|
|
copy.fields = fields;
|
|
|
|
|
copy.setting = $copy(row);
|
|
|
|
|
if (json.filters) copy.filters = $copy(json.filters);
|
|
|
|
|
@@ -1222,7 +1228,7 @@ export default defineNuxtPlugin(() => {
|
|
|
|
|
copy.api = copyApi;
|
|
|
|
|
copy.origin_api = copy;
|
|
|
|
|
}
|
|
|
|
|
store.commit(pagename, copy);
|
|
|
|
|
$store.commit(pagename, copy);
|
|
|
|
|
return copy;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
@@ -1289,30 +1295,30 @@ export default defineNuxtPlugin(() => {
|
|
|
|
|
// insert row
|
|
|
|
|
var insertrow = async function (name, data, values, pagename, notify) {
|
|
|
|
|
let result = await insertapi(name, data, values, notify);
|
|
|
|
|
if (result === "error" || !pagename || !store[pagename]) return result;
|
|
|
|
|
let copy = $clone(store[pagename]);
|
|
|
|
|
if (result === "error" || !pagename || !$store[pagename]) return result;
|
|
|
|
|
let copy = $clone($store[pagename]);
|
|
|
|
|
copy.update = { refresh: true };
|
|
|
|
|
store.commit(pagename, copy);
|
|
|
|
|
$store.commit(pagename, copy);
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// update row
|
|
|
|
|
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]);
|
|
|
|
|
if (result === "error" || !pagename || !$store[pagename]) return result;
|
|
|
|
|
let copy = $clone($store[pagename]);
|
|
|
|
|
copy.update = { refresh: true };
|
|
|
|
|
store.commit(pagename, copy);
|
|
|
|
|
$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);
|
|
|
|
|
if (result === "error" || !pagename || !store[pagename]) return result;
|
|
|
|
|
let copy = $clone(store[pagename]);
|
|
|
|
|
if (result === "error" || !pagename || !$store[pagename]) return result;
|
|
|
|
|
let copy = $clone($store[pagename]);
|
|
|
|
|
copy.update = { refresh: true };
|
|
|
|
|
store.commit(pagename, copy);
|
|
|
|
|
$store.commit(pagename, copy);
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
@@ -1328,11 +1334,11 @@ export default defineNuxtPlugin(() => {
|
|
|
|
|
else {
|
|
|
|
|
let params = { action: "delete" };
|
|
|
|
|
rs = await $fetch(`${path}import-data/${found.url.substring(5, found.url.length - 1)}/`, id, {
|
|
|
|
|
params: params,
|
|
|
|
|
params,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (found.commit) {
|
|
|
|
|
let copy = JSON.parse(JSON.stringify(store[found.commit]));
|
|
|
|
|
let copy = JSON.parse(JSON.stringify($store[found.commit]));
|
|
|
|
|
if (!Array.isArray(id)) {
|
|
|
|
|
let index = copy.findIndex((v) => v.id === id);
|
|
|
|
|
if (index >= 0) $remove(copy, index);
|
|
|
|
|
@@ -1342,7 +1348,7 @@ export default defineNuxtPlugin(() => {
|
|
|
|
|
if (index >= 0) $remove(copy, index);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
store.commit(found.name, copy);
|
|
|
|
|
$store.commit(found.name, copy);
|
|
|
|
|
console.log("copy", copy);
|
|
|
|
|
}
|
|
|
|
|
return id;
|
|
|
|
|
@@ -1363,16 +1369,16 @@ export default defineNuxtPlugin(() => {
|
|
|
|
|
// delete row
|
|
|
|
|
const deleterow = async function (name, id, pagename) {
|
|
|
|
|
let result = await deleteapi(name, id);
|
|
|
|
|
if (result === "error" || !pagename || !store[pagename]) return result;
|
|
|
|
|
let copy = $clone(store[pagename]);
|
|
|
|
|
if (result === "error" || !pagename || !$store[pagename]) return result;
|
|
|
|
|
let copy = $clone($store[pagename]);
|
|
|
|
|
copy.update = { refresh: true };
|
|
|
|
|
store.commit(pagename, copy);
|
|
|
|
|
$store.commit(pagename, copy);
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// update page
|
|
|
|
|
const updatepage = function (pagename, row, action) {
|
|
|
|
|
let copy = $clone(store[pagename]);
|
|
|
|
|
let copy = $clone($store[pagename]);
|
|
|
|
|
let rows = Array.isArray(row) ? row : [row];
|
|
|
|
|
rows.map((x) => {
|
|
|
|
|
let idx = copy.data.findIndex((v) => v.id === x.id);
|
|
|
|
|
@@ -1383,7 +1389,7 @@ export default defineNuxtPlugin(() => {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
copy.update = { data: copy.data };
|
|
|
|
|
store.commit(pagename, copy);
|
|
|
|
|
$store.commit(pagename, copy);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const buildFileUrl = (file) => {
|
|
|
|
|
@@ -1472,25 +1478,25 @@ export default defineNuxtPlugin(() => {
|
|
|
|
|
return right === "edit" ? rightObj && rightObj.is_edit : Boolean(rightObj);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (store.rights.length === 0) return true; // full rights
|
|
|
|
|
if ($store.rights.length === 0) return true; // full rights
|
|
|
|
|
|
|
|
|
|
if (code && category) {
|
|
|
|
|
// if passed, must pass both
|
|
|
|
|
const foundRight = store.rights.find(
|
|
|
|
|
const foundRight = $store.rights.find(
|
|
|
|
|
({ setting__category, setting__code }) => setting__category === category && setting__code === code,
|
|
|
|
|
);
|
|
|
|
|
return getRight(foundRight);
|
|
|
|
|
} else {
|
|
|
|
|
const { tab, subtab } = store.tabinfo;
|
|
|
|
|
const { tab, subtab } = $store.tabinfo;
|
|
|
|
|
let isTabEdit;
|
|
|
|
|
let isSubTabEdit;
|
|
|
|
|
|
|
|
|
|
const tabRight = store.rights.find((rights) => rights.setting === tab.id);
|
|
|
|
|
const tabRight = $store.rights.find((rights) => rights.setting === tab.id);
|
|
|
|
|
isTabEdit = getRight(tabRight);
|
|
|
|
|
|
|
|
|
|
if (!subtab) isSubTabEdit = false;
|
|
|
|
|
else {
|
|
|
|
|
const subTabRight = store.rights.find((rights) => rights.setting === subtab.id);
|
|
|
|
|
const subTabRight = $store.rights.find((rights) => rights.setting === subtab.id);
|
|
|
|
|
isSubTabEdit = getRight(subTabRight);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -1589,7 +1595,6 @@ export default defineNuxtPlugin(() => {
|
|
|
|
|
deleteapi,
|
|
|
|
|
deleterow,
|
|
|
|
|
updatepage,
|
|
|
|
|
store,
|
|
|
|
|
requestLogin,
|
|
|
|
|
buildFileUrl,
|
|
|
|
|
generateDocument,
|
|
|
|
|
|