เพิ่ม socket แจ้งเตือนออกคำสั่ง

This commit is contained in:
Thanaphon Frappet 2025-04-01 12:50:30 +07:00
parent e6748cfa57
commit 0c315eed8a
4 changed files with 69 additions and 1 deletions

View file

@ -1,4 +1,11 @@
<script setup lang="ts"></script>
<script setup lang="ts">
import { useSocketStore } from "@/stores/socket";
import { onMounted } from "vue";
onMounted(() => {
useSocketStore();
});
</script>
<template>
<div id="azay-admin-app">

7
src/api/socket.ts Normal file
View file

@ -0,0 +1,7 @@
import env from "./index";
const socket = `${env.API_URI}/org-socket`;
export default {
socket,
};

View file

@ -70,6 +70,9 @@ import development from "./api/15_development/api.development";
/** ออกคำสั่งใหม่ */
import command from "./api/18_command/api.command";
/** socket */
import socket from "./api/socket";
import file from "./api/file/api.file";
// environment variables
@ -140,6 +143,9 @@ const API = {
/*file*/
...file,
/*socket*/
...socket,
};
const path = import.meta.env.VITE_MANUAL_URL ?? "";

48
src/stores/socket.ts Normal file
View file

@ -0,0 +1,48 @@
import { defineStore } from "pinia";
import { Notify } from "quasar";
import { io, Socket } from "socket.io-client";
import config from "@/app.config";
import { getToken } from "@/plugins/auth";
interface sockeBackup {
message: string;
success?: boolean;
}
export const useSocketStore = defineStore("socket", () => {
let socket: Socket;
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);
});
}
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,
},
],
});
}
init();
return {};
});