hrms-admin/src/modules/02_users/views/01_user.vue

839 lines
28 KiB
Vue

<script setup lang="ts">
import { ref, onMounted, watch } from "vue";
import { useQuasar } from "quasar";
import { storeToRefs } from "pinia";
import http from "@/plugins/http";
import config from "@/app.config";
import { useRouter } from "vue-router";
import { useCounterMixin } from "@/stores/mixin";
import { updateCurrentPage } from "@/utils/functions";
import { tokenParsed } from "@/plugins/auth";
import { usekeycloakPosition } from "@/stores/keycloakPosition";
/** importType*/
import type { QTableProps } from "quasar";
import type {
DataOption,
ItemsMenu,
Pagination,
} from "@/modules/02_users/interface/index/Main";
import type { NodeTree } from "@/modules/02_users/interface/response/Main";
import type { Users, Roles } from "@/modules/02_users/interface/response/Main";
/** importComponents*/
import DialogAddUser from "@/modules/02_users/components/Users/DialogFormUser.vue";
import DialogManagementRole from "@/modules/02_users/components/Users/DialogManagementRole.vue";
/** use*/
const $q = useQuasar();
const router = useRouter();
const { dataPosition } = storeToRefs(usekeycloakPosition());
const {
dialogRemove,
messageError,
showLoader,
hideLoader,
success,
dialogConfirm,
dialogMessage,
} = useCounterMixin();
const NODE_ENV = ref<string>(process.env.NODE_ENV || "development");
/** Tree*/
const activeId = ref<string>(""); // id ของโครงสร้างที่ใช้งานอยู่
const orgId = ref<string | null>(null); // id ของ node ที่เลือก
const orgLevel = ref<number | null>(null); // ระดับของ node ที่เลือก
const expanded = ref<Array<string | number>>([]); // ข้อมูล node ที่ขยาย
const filter = ref<string>(""); // คำค้นหาใน tree
const nodeTree = ref<Array<NodeTree>>([
{
labelName: "",
orgCode: "",
orgLevel: 0,
orgName: "",
orgRevisionId: "",
orgRootName: "",
orgTreeCode: "",
orgTreeFax: "",
orgTreeId: "",
orgTreeName: "หน่วยงานทั้งหมด",
orgTreeOrder: 0,
orgTreePhoneEx: "",
orgTreePhoneIn: "",
orgTreeRank: "",
orgTreeRankSub: "",
orgTreeShortName: "",
responsibility: "",
totalPosition: 0,
totalPositionCurrentUse: 0,
totalPositionCurrentVacant: 0,
totalPositionNextUse: 0,
totalPositionNextVacant: 0,
totalRootPosition: 0,
totalRootPositionCurrentUse: 0,
totalRootPositionCurrentVacant: 0,
totalRootPositionNextUse: 0,
totalRootPositionNextVacant: 0,
children: [] as NodeTree[],
isOfficer: false,
orgRootDnaId: "",
orgChild1DnaId: "",
orgChild2DnaId: "",
orgChild3DnaId: "",
orgChild4DnaId: "",
},
]);
const lazy = ref(nodeTree);
/** Table*/
const keyword = ref<string>(""); // คำค้นหา
const isAll = ref<boolean>(true); // แสดงตำแหน่งทั้งหมด
const rows = ref<Users[]>([]); // ข้อมูลรายาการผู้ใช้งาน
const currentPage = ref<number>(1); // หน้า
const total = ref<number>(0); // จำนวนรายการ
const maxPage = ref<number>(0); // จำนวนหน้า
const pageSize = ref<number>(10); // จำนวนข้อมูลต่อหน้า
const employeeClass = ref<string>("officer");
const columns = ref<QTableProps["columns"]>([
{
name: "no",
align: "left",
label: "ลำดับ",
sortable: false,
field: (row) =>
(currentPage.value - 1) * pageSize.value + rows.value.indexOf(row) + 1,
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "username",
align: "left",
label: "ชื่อผู้ใช้งาน",
sortable: false,
field: "username",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "email",
align: "left",
label: "อีเมล",
sortable: false,
field: "email",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "firstname",
align: "left",
label: "ชื่อ",
sortable: false,
field: "firstname",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "lastname",
align: "left",
label: "นามสกุล",
sortable: false,
field: "lastname",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "role",
align: "left",
label: "สิทธิ์",
sortable: false,
headerStyle: "font-size: 14px",
style: "font-size: 14px",
field: (row) => {
const names = row.roles.map((role: Roles) => role.name);
return names.join(", ");
},
},
{
name: "enabled",
align: "left",
label: "สถานะการใช้งาน",
sortable: false,
field: "enabled",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
]);
const visibleColumns = ref<string[]>([
"no",
"username",
"email",
"firstname",
"lastname",
"role",
"enabled",
]);
/** เพิ่มข้อมูลผู้ใช้งาน*/
const modalDialogAdd = ref<boolean>(false); // เปิด ปิด popup
const isStatusEdit = ref<boolean>(false); // สถานะการแก้ไข
const userId = ref<string>(""); // id ผู้ใช้งานที่ต้องการแก้ไข
// รายการตัวเลือก
const itemMenu = ref<ItemsMenu[]>([
{
label: "ระงับการใช้งาน",
icon: "mdi-lock-outline",
color: "orange-10",
type: "close",
},
{
label: "เปิดใช้งาน",
icon: "mdi-lock-open-outline",
color: "primary",
type: "open",
},
{
label: "จัดการสิทธิ์",
icon: "mdi-account-group",
color: "blue-9",
type: "managementRole",
},
{
label: "รีเซ็ตรหัสผ่าน",
icon: "mdi-lock-reset",
color: "teal-4",
type: "resetPassword",
},
{
label: "ลบ",
icon: "delete",
color: "red",
type: "delete",
},
]);
const employeeClassMain = ref<DataOption[]>([
{ id: "officer", name: "ข้าราชการ กทม.สามัญ" },
{ id: "employee", name: "ลูกจ้างประจำ" },
]);
const employeeClassOption = ref<DataOption[]>(employeeClassMain.value);
const modalDialogManagementRole = ref<boolean>(false); // จัดการสิทธิ์ผู้ใช้งาน
/** ฟังก์ชันเรียกข้อมูลโครงสร้าง แบบปัจุบัน*/
async function fetchOrganizationActive() {
showLoader();
await http
.get(config.API.activeOrganization)
.then(async (res) => {
const data = res.data.result;
if (data) {
activeId.value = data.activeId;
await fetchDataTree(data.activeId);
}
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
hideLoader();
});
}
/**
* ฟังก์ชัน fetch ข้อมูลของโครงสร้าง
* @param id id โครงสร้าง
*/
async function fetchDataTree(id: string) {
const isSuperAdmin = tokenParsedData.value.includes("SUPER_ADMIN");
if (!isSuperAdmin) {
nodeTree.value = [];
}
showLoader();
await http
.get(config.API.orgByid(id.toString()))
.then(async (res) => {
const data = await res.data.result;
nodeTree.value.push(...data);
orgId.value = nodeTree.value[0].orgTreeId || "";
await selectedOrg(nodeTree.value[0]);
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
hideLoader();
});
}
/** ฟังก์ชันดึงข้อมูลรายชื่อผู้ใช้งาน*/
async function fetchListUsers() {
showLoader();
await http
.post(config.API.managementUser + `/admin`, {
pageSize: pageSize.value,
page: currentPage.value,
keyword: keyword.value.trim(),
type: employeeClass.value,
isAll: isAll.value,
nodeId: orgId.value,
node: orgLevel.value,
})
.then(async (res) => {
const data = await res.data.result;
total.value = data.total;
maxPage.value = Math.ceil(total.value / pageSize.value);
rows.value = data.data.map((e: Users) => ({
...e,
roles: Array.isArray(e.roles)
? e.roles.filter(
(e: Roles) =>
e.name === "STAFF" ||
e.name === "SUPER_ADMIN" ||
e.name === "ADMIN" ||
e.name === "USER",
)
: [],
}));
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
hideLoader();
});
}
/**
* ฟังก์ชันจัดการการกระทำต่าง ๆ กับข้อมูลผู้ใช้งาน
* @param type ประเภทตัวเลือก
* @param data ข้อมูลรายการผู้ใช้งาน
*/
function onClickAction(type: string, data: Users) {
// ตรวจสอบประเภทของการกระทำ
if (type === "edit") {
// แก้ไข
modalDialogAdd.value = true;
isStatusEdit.value = true;
userId.value = data.id;
} else if (type === "delete") {
// เรียกใช้ฟังก์ชันสำหรับลบข้อมูลผู้ใช้งาน
onDeleteUser(data.id);
} else if (type === "managementRole") {
// ไปยังหน้าจัดการสิทธิ์ของผู้ใช้งาน
// data && router.push(`/users/roles/${data.id}`);
// data && (userId.value = data.id);
userId.value = data.id;
modalDialogManagementRole.value = true;
} else if (type === "open" || type === "close") {
// เปลี่ยนสถานะเปิด/ปิดของผู้ใช้งาน
const status = type === "open" ? true : false;
onLockUser(data.id, status);
} else if (type === "resetPassword") {
onReSetPassword(data.id);
}
}
/**
* ฟังก์ชันเปิด popup สำหรับเพิ่มข้อมูลผู้ใช้งาน
* กำหนดสถานะการแก้ไขเป็น false และ id ผู้ใช้งานที่ต้องการแก้ไขเป็นค่าว่าง
*/
function openDialog() {
modalDialogAdd.value = true;
isStatusEdit.value = false;
userId.value = "";
}
/**
* function ยืนยันการลบข้อมูลรายการผู้ใช้งาน
* @param id รายการผู้ใช้งาน
* ลบข้อมูลรายชื่อเสร็จแล้วทำการดึงข้อมูลรายชื่อผู้ใช้งานใหม่
*/
async function onDeleteUser(id: string) {
dialogRemove($q, () => {
showLoader();
http
.delete(config.API.managementUser + `/${id}`)
.then(async () => {
currentPage.value = await updateCurrentPage(
currentPage.value,
maxPage.value,
rows.value.length,
);
await fetchListUsers();
success($q, "ลบข้อมูลสำเร็จ");
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
hideLoader();
});
});
}
/**
* ฟังก์ชันระงับการใช้งานผู้ใช้งาน
* @param id ผู้ใช้งาน
* @param type เปิดใช้งาน,ระงับการใช้งาน
* เสร็จแล้วทำการดึงข้อมูลรายชื่อผู้ใช้งานใหม่
*/
function onLockUser(id: string, type: boolean) {
dialogConfirm(
$q,
() => {
showLoader();
http
.put(config.API.managementUser + `/${id}/enableStatus/${type}`)
.then(async () => {
await fetchListUsers();
success($q, type ? "เปิดใช้งานสำเร็จ" : "ระงับการใช้งานสำเร็จ");
})
.catch((err) => {
messageError($q, err);
hideLoader();
});
},
type ? "ยืนยันการเปิดใช้" : "ยืนยันการระงับใช้งาน",
type
? "ต้องการยืนยันการเปิดใช้นี้หรือไม่ ?"
: "ต้องการยืนยันระงับการใช้งานนี้หรือไม่ ?",
);
}
/**
* function อัปเดท paging
* @param initialPagination ข้อมูล pagination
* กำหนดหน้าไปยังหน้าแรก
*/
function updatePagination(initialPagination: Pagination) {
currentPage.value = 1;
pageSize.value = initialPagination.rowsPerPage;
}
/**
* function ค้นหาข้อมูล Option
* @param val คำค้นหา
* @param update function
* @param type ประเภท option
*/
function filterFnOptions(val: string, update: Function, type: string) {
switch (type) {
case "employeeClass":
update(() => {
employeeClassOption.value = employeeClassMain.value.filter(
(v: DataOption) => v.name.indexOf(val) > -1,
);
});
break;
}
}
async function selectedOrg(data: NodeTree) {
orgId.value = data.orgTreeId;
orgLevel.value = data.orgLevel;
currentPage.value = 1;
// ดึงข้อมูลผู้ใช้งานตามหน่วยงานที่เลือก
await fetchListUsers();
}
async function onSearchData() {
currentPage.value = 1;
await fetchListUsers();
}
function onReSetPassword(id: string) {
dialogConfirm(
$q,
async () => {
try {
showLoader();
const messsage =
NODE_ENV.value === "production"
? "รหัสผ่านจะถูกเปลี่ยนเป็น วันเดือนปีพุทธศักราชเกิด ของผู้ใช้งาน (รูปแบบ ววดดปปปป เช่น 14042533)"
: "ระบบจะตั้งรหัสผ่านใหม่เป็น P@ssw0rd";
await http.post(config.API.resetPassword, {
keycloak: id,
});
// success($q, "รีเซ็ตรหัสผ่านสำเร็จ");
dialogMessage(
$q,
"รีเซ็ตรหัสผ่านสำเร็จ",
messsage,
"mdi-check-circle",
"ตกลง",
"green",
() => {},
() => {},
true,
);
} catch (error) {
messageError($q, error);
} finally {
hideLoader();
}
},
"ยืนยันการรีเซ็ตรหัสผ่าน",
"ต้องการยืนยันการรีเซ็ตรหัสผ่านนี้หรือไม่ ?",
);
}
/**
* ดูการเปลี่ยนแปลงของจำนวนข้อมูลต่อหน้า
* ดึงข้อมูลรายชื่อผู้ใช้งานตามจำนวนข้อมูลต่อหน้า
*/
watch(
() => pageSize.value,
() => {
fetchListUsers();
},
);
const tokenParsedData = ref<string[]>([]);
/**
* ตรวจสอบการแสดงปุ่ม action
* @param keycloakID keycloakID รหัสผู้ใช้งาน
* @returns true หากผู้ใช้งานมีสิทธิ์ในการดำเนินการ
*/
function checkhideBtnAction(keycloakID: string) {
if (
dataPosition.value?.keycloak === keycloakID &&
tokenParsedData.value.includes("ADMIN") &&
!tokenParsedData.value.includes("SUPER_ADMIN")
) {
return false;
} else {
return true;
}
}
/**
* hook ทำงานเมื่อมีการเรียกใช้งาน Components
* ดึงข้อมูลรายชื่อผู้ใช้งาน
*/
onMounted(async () => {
const token = await tokenParsed();
tokenParsedData.value = token.role || [];
await fetchOrganizationActive();
});
</script>
<template>
<div class="row items-center">
<div class="toptitle text-dark row items-center q-py-xs">
ดการผใชงาน {{ NODE_ENV }}
</div>
</div>
<q-card flat bordered>
<q-card-section :horizontal="$q.screen.gt.xs">
<!-- Tree -->
<q-card-section
class="col-lg-3 col-md-4 col-sm-5 col-xs-12 q-col-gutter-sm"
>
<div class="col-12">
<div class="q-gutter-sm">
<div class="row q-col-gutter-sm q-pl-sm">
<div class="col-12">
<q-input dense outlined v-model="filter" label="ค้นหา">
<template v-slot:append>
<q-icon name="search" />
</template>
</q-input>
</div>
</div>
<div class="bg-white tree-container q-pa-xs">
<q-tree
class="q-pa-sm q-gutter-sm"
dense
default-expand-all
:nodes="lazy"
node-key="orgTreeId"
label-key="labelName"
:filter="filter?.trim()"
no-results-label="ไม่พบข้อมูลที่ค้นหา"
no-nodes-label="ไม่มีข้อมูล"
v-model:expanded="expanded"
>
<template v-slot:default-header="prop">
<q-item
@click.stop="selectedOrg(prop.node)"
:active="orgId == prop.node.orgTreeId"
clickable
active-class="my-list-link text-primary text-weight-medium"
class="row col-12 items-center text-dark q-py-xs q-pl-sm rounded-borders my-list"
>
<div>
<div
:class="
prop.node.isOfficer
? 'text-weight-medium text-blue'
: 'text-weight-medium'
"
>
<div
v-if="
prop.node.isDeputy == true &&
prop.node.orgLevel == 0
"
class="text-info"
>
{{ prop.node.orgTreeName }}
</div>
<div v-else>
{{ prop.node.orgTreeName }}
</div>
{{ prop.node.isOfficer ? "(สกจ.)" : "" }}
</div>
<div class="text-weight-light text-grey-8">
{{
prop.node.orgCode == null ? null : prop.node.orgCode
}}
{{
prop.node.orgTreeShortName == null
? null
: prop.node.orgTreeShortName
}}
</div>
</div>
</q-item>
</template>
</q-tree>
</div>
</div>
</div>
</q-card-section>
<q-separator :vertical="$q.screen.gt.xs" />
<!-- Table -->
<q-card-section
class="col-lg-9 col-md-8 col-sm-7 col-xs-12"
style="height: 85vh"
>
<div class="col-12 q-col-gutter-sm scroll" style="height: 100%">
<div class="col-12">
<div class="col-12 row q-col-gutter-sm items-end">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-3">
<div class="row q-col-gutter-sm items-center">
<div class="col-11">
<q-select
outlined
dense
v-model="employeeClass"
:options="employeeClassOption"
label="สถานภาพ"
emit-value
map-options
option-label="name"
option-value="id"
use-input
hide-selected
fill-input
@update:model-value="onSearchData"
@filter="(inputValue: string,
doneFn: Function) => filterFnOptions(inputValue, doneFn,'employeeClass')"
><template v-slot:no-option>
<q-item>
<q-item-section class="text-grey">
ไม่มีข้อมูล
</q-item-section>
</q-item>
</template>
</q-select>
</div>
<div class="col-1">
<q-btn
flat
round
dense
color="primary"
icon="add"
@click="openDialog()"
>
<q-tooltip>เพิ่มผู้ใช้งาน </q-tooltip>
</q-btn>
</div>
</div>
</div>
<div class="col-xs-12 col-sm-0 col-md-0 col-lg-3"></div>
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-6">
<div class="row q-col-gutter-sm items-center">
<div class="col-4">
<q-checkbox
keep-color
v-model="isAll"
label="แสดงตำแหน่งทั้งหมด"
color="primary"
@update:model-value="onSearchData"
>
<q-tooltip
>แสดงตำแหน่งทั้งหมดภายใต้หน่วยงาน/ส่วนราชการที่เลือก</q-tooltip
>
</q-checkbox>
</div>
<div class="col-5">
<q-input
borderless
dense
outlined
v-model="keyword"
placeholder="ค้นหา"
@keydown.enter.prevent="onSearchData"
>
<template v-slot:append>
<q-icon name="search" />
</template>
</q-input>
</div>
<div class="col-3">
<q-select
v-model="visibleColumns"
multiple
outlined
dense
options-dense
:display-value="$q.lang.table.columns"
emit-value
map-options
:options="columns"
option-value="name"
/>
</div>
</div>
</div>
</div>
</div>
<div class="col-12">
<d-table
ref="table"
:columns="columns"
:rows="rows"
row-key="id"
flat
bordered
:paging="true"
dense
:rows-per-page-options="[10, 25, 50, 100]"
:visible-columns="visibleColumns"
@update:pagination="updatePagination"
>
<template v-slot:header="props">
<q-tr :props="props">
<q-th
v-for="col in props.cols"
:key="col.name"
:props="props"
>
<span class="text-weight-medium">{{ col.label }}</span>
</q-th>
<q-th auto-width></q-th>
</q-tr>
</template>
<template v-slot:body="props">
<q-tr :props="props" class="cursor-pointer">
<q-td
v-for="col in props.cols"
:key="col.name"
:props="props"
>
<div v-if="col.name === 'enabled'">
<q-icon
:name="col.value ? 'check' : 'close'"
:color="col.value ? 'primary' : 'red'"
size="24px"
/>
</div>
<div v-else>
{{ col.value ? col.value : "-" }}
</div>
</q-td>
<q-td auto-width>
<q-btn
v-if="checkhideBtnAction(props.row.id)"
flat
dense
icon="mdi-dots-vertical"
class="q-pa-none q-ml-xs"
color="grey-13"
size="12px"
>
<q-menu>
<q-list dense style="min-width: 200px">
<q-item
v-for="(item, index) in props.row.enabled
? itemMenu.filter((e) => e.type !== 'open')
: itemMenu.filter((e) => e.type !== 'close')"
:key="index"
clickable
v-close-popup
@click.stop="onClickAction(item.type, props.row)"
>
<q-item-section>
<div class="row items-center">
<q-icon
:color="item.color"
size="17px"
:name="item.icon"
/>
<div class="q-pl-md">{{ item.label }}</div>
</div>
</q-item-section>
</q-item>
</q-list>
</q-menu>
</q-btn>
</q-td>
</q-tr>
</template>
<template v-slot:pagination="scope">
ทั้งหมด {{ total }} รายการ
<q-pagination
v-model="currentPage"
active-color="primary"
color="dark"
:max="Number(maxPage)"
size="sm"
boundary-links
direction-links
:max-pages="5"
@update:model-value="fetchListUsers"
></q-pagination>
</template>
</d-table>
</div>
</div>
</q-card-section>
</q-card-section>
</q-card>
<DialogAddUser
:fetchlist="fetchListUsers"
v-model:modal="modalDialogAdd"
v-model:is-status-edit="isStatusEdit"
:user-id="userId"
:employee-class="employeeClass"
/>
<DialogManagementRole
v-model:model-value="modalDialogManagementRole"
v-model:user-id="userId"
:fetch-list="fetchListUsers"
/>
</template>
<style scoped>
.tree-container {
overflow: auto;
height: 76vh;
border: 1px solid #e6e6e7;
border-radius: 10px;
}
.my-list-link {
color: rgb(118, 168, 222);
border-radius: 5px;
background: #a3d3fb48 !important;
font-weight: 600;
border: 1px solid rgba(175, 185, 196, 0.217);
}
</style>