2025-11-20 13:24:36 +07:00
|
|
|
import config from "@/app.config";
|
|
|
|
|
import { getToken } from "@/plugins/auth";
|
2025-04-01 12:50:30 +07:00
|
|
|
import { defineStore } from "pinia";
|
|
|
|
|
import { Notify } from "quasar";
|
|
|
|
|
import { io, Socket } from "socket.io-client";
|
2026-05-19 10:08:22 +07:00
|
|
|
import { ref } from "vue";
|
2025-04-01 12:50:30 +07:00
|
|
|
interface sockeBackup {
|
|
|
|
|
message: string;
|
|
|
|
|
success?: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const useSocketStore = defineStore("socket", () => {
|
|
|
|
|
let socket: Socket;
|
2026-05-19 10:08:22 +07:00
|
|
|
const notificationCounter = ref(0);
|
2025-04-01 12:50:30 +07:00
|
|
|
|
|
|
|
|
async function init() {
|
|
|
|
|
socket = io(new URL(config.API.socket).origin, {
|
|
|
|
|
auth: { token: await getToken() },
|
|
|
|
|
path: "/api/v1/org-socket",
|
|
|
|
|
});
|
|
|
|
|
socket.on("send-command-notification", (payload) => {
|
|
|
|
|
let body: sockeBackup = JSON.parse(payload);
|
|
|
|
|
notifyStatus(body.message, body.success);
|
|
|
|
|
});
|
2025-07-07 17:43:44 +07:00
|
|
|
|
2025-11-20 13:24:36 +07:00
|
|
|
socket.on("send-notification", (payload) => {
|
2025-11-20 12:51:15 +07:00
|
|
|
let body: sockeBackup = JSON.parse(payload);
|
|
|
|
|
notifyStatus(body.message, body.success);
|
|
|
|
|
});
|
|
|
|
|
|
2025-07-07 17:43:44 +07:00
|
|
|
socket.on("send-create-draft-org", (payload) => {
|
|
|
|
|
let body: sockeBackup = JSON.parse(payload);
|
|
|
|
|
if (body.message == "ระบบกำลังทำการสร้างแบบร่างโครงสร้างหน่วยงาน") {
|
|
|
|
|
notifyStatus(body.message, body.success);
|
|
|
|
|
} else {
|
|
|
|
|
notifyStatusOrg("draft", body.message, body.success);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
socket.on("send-publish-org", (payload) => {
|
|
|
|
|
let body: sockeBackup = JSON.parse(payload);
|
|
|
|
|
if (body.message == "ระบบกำลังทำการเผยแพร่โครงสร้างหน่วยงาน") {
|
|
|
|
|
notifyStatus(body.message, body.success);
|
|
|
|
|
} else {
|
|
|
|
|
notifyStatusOrg("current", body.message, body.success);
|
|
|
|
|
}
|
|
|
|
|
});
|
2026-05-14 13:35:24 +07:00
|
|
|
|
|
|
|
|
socket.on("socket-notification", (payload) => {
|
|
|
|
|
let body: sockeBackup = JSON.parse(payload);
|
2026-05-19 10:08:22 +07:00
|
|
|
notifyStatusWithProgress(body.message, body.success);
|
|
|
|
|
notificationCounter.value++;
|
2026-05-14 13:35:24 +07:00
|
|
|
});
|
2025-04-01 12:50:30 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function notifyStatus(message: string, success?: boolean) {
|
|
|
|
|
Notify.create({
|
|
|
|
|
group: false,
|
|
|
|
|
type: success === undefined || success ? "positive" : "negative",
|
|
|
|
|
message: `${message}`,
|
|
|
|
|
position: "top",
|
|
|
|
|
timeout: 0,
|
|
|
|
|
actions: [
|
|
|
|
|
{
|
|
|
|
|
icon: "close",
|
|
|
|
|
color: "white",
|
|
|
|
|
round: true,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-19 10:08:22 +07:00
|
|
|
function notifyStatusWithProgress(message: string, success?: boolean) {
|
|
|
|
|
Notify.create({
|
|
|
|
|
group: false,
|
|
|
|
|
type: success === undefined || success ? "positive" : "negative",
|
|
|
|
|
message: `${message}`,
|
|
|
|
|
position: "top",
|
|
|
|
|
timeout: success === undefined || success ? 3000 : 0,
|
|
|
|
|
actions:
|
|
|
|
|
success === undefined || success
|
|
|
|
|
? []
|
|
|
|
|
: [
|
|
|
|
|
{
|
|
|
|
|
icon: "close",
|
|
|
|
|
color: "white",
|
|
|
|
|
round: true,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
progress: true,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-07 17:43:44 +07:00
|
|
|
function fnStyleNotiOrg() {
|
|
|
|
|
if (document.getElementById("notify-link-style")) return;
|
|
|
|
|
const style = document.createElement("style");
|
|
|
|
|
style.id = "notify-link-style";
|
|
|
|
|
style.textContent = `
|
|
|
|
|
.notify-link {
|
|
|
|
|
padding: 4px 8px;
|
|
|
|
|
border-radius: 4px;
|
|
|
|
|
text-decoration: none;
|
|
|
|
|
color: #fff;
|
|
|
|
|
border: 1px solid #fff;
|
|
|
|
|
transition: all 0.3s;
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
margin:0 0 0 5px;
|
|
|
|
|
}
|
|
|
|
|
.notify-link:hover {
|
|
|
|
|
background-color: #ffffff;
|
|
|
|
|
color: #21BA45;
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
document.head.appendChild(style);
|
|
|
|
|
}
|
2026-05-19 10:08:22 +07:00
|
|
|
|
2025-07-07 17:43:44 +07:00
|
|
|
(window as any).resetOrgPage = (type: string) => {
|
|
|
|
|
localStorage.setItem("org_type", type);
|
|
|
|
|
window.location.reload();
|
|
|
|
|
};
|
2026-05-19 10:08:22 +07:00
|
|
|
|
2025-07-07 17:43:44 +07:00
|
|
|
function notifyStatusOrg(type: string, message: string, success?: boolean) {
|
|
|
|
|
fnStyleNotiOrg();
|
|
|
|
|
Notify.create({
|
|
|
|
|
message: `${message} <a href="#" onclick="window.resetOrgPage('${type}')" class="notify-link">${
|
2025-07-08 14:02:18 +07:00
|
|
|
type == "draft" ? "ไปยังโครงสร้างแบบร่าง" : "ไปยังโครงสร้างปัจจุบัน"
|
2025-07-07 17:43:44 +07:00
|
|
|
}</a>`,
|
|
|
|
|
html: true,
|
|
|
|
|
group: false,
|
|
|
|
|
type: success === undefined || success ? "positive" : "negative",
|
|
|
|
|
position: "top",
|
|
|
|
|
timeout: 0,
|
|
|
|
|
actions: [
|
|
|
|
|
{
|
|
|
|
|
icon: "close",
|
|
|
|
|
color: "white",
|
|
|
|
|
round: true,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-01 12:50:30 +07:00
|
|
|
init();
|
|
|
|
|
|
2026-05-19 10:08:22 +07:00
|
|
|
return { notificationCounter };
|
2025-04-01 12:50:30 +07:00
|
|
|
});
|