Files
2025-06-18 09:19:27 +07:00

107 lines
3.1 KiB
TypeScript

import { defineStore } from 'pinia'
const runtime = useRuntimeConfig().public;
interface Setting {
listTypeUser: [];
listMenu: [];
roleMenuById: [];
message: string;
}
export const useMySetting2Store = defineStore('mySetting2Store', {
// id: 'mySetting2Store',
state: (): Setting => ({
listTypeUser: [],
listMenu: [],
roleMenuById: [],
message: ''
}),
actions: {
async getMenu() {
try {
const response = await $fetch(`${runtime.APIBASE}/menu/getlist`, {
method: "GET",
headers: { "Content-Type": "application/json" }
})
this.listMenu = response as []
} catch (error) {
if (error instanceof Error) {
this.message = error.message
// console.log(error.message)
}
}
},
async getRoleMenuById(body: Record<string, any>) {
try {
console.log(body)
const response = await $fetch(`${runtime.APIBASE}/menu/role/type/${body}`, {
headers: {
"Content-Type": "application/json",
},
method: "GET",
})
this.roleMenuById = response as []
} catch (error) {
if (error instanceof Error) {
this.message = error.message
// console.log(error.message)
}
}
},
async getTypeUser() {
try {
const response = await $fetch(`${runtime.APIBASE}/menu/type`, {
method: "GET",
headers: { "Content-Type": "application/json" }
})
this.listTypeUser = response as []
} catch (error) {
if (error instanceof Error) {
this.message = error.message
// console.log(error.message)
}
}
},
async postRoleUserMenu(body: Record<string, any>) {
try {
const roleUserMenu = body.menus.map((item) => ({
type_user_id: item.type_user_id,
menu_id: item.menu_id,
access: [
{
add: parseInt(item.access[0].value),
update: parseInt(item.access[1].value),
read: parseInt(item.access[2].value),
delete: parseInt(item.access[3].value),
}
]
}))
console.log("body post:", body.menus)
const response = await $fetch(`${runtime.APIBASE}/menu/update/rolemenu?id=${body.menus[0].type_user_id}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(roleUserMenu)
})
} catch (error) {
if (error instanceof Error) {
this.message = error.message
// console.log(error.message)
}
}
}
// async addTypeUser(data: any) {
// console.log(data)
// try {
// const response = await $fetch('https://',{
// method: 'POST',
// headers: {'Content-Type': 'application/json'},
// body: JSON.stringify(data)
// })
// } catch (error) {
// if(error instanceof Error){
// this.message = error.message
// // console.log(error.message)
// }
// }
// }
}
})