hrms-checkin/src/views/MainView.vue

495 lines
15 KiB
Vue
Raw Normal View History

2024-01-19 15:34:09 +07:00
<script setup lang="ts">
2024-01-19 17:22:07 +07:00
import { ref, onMounted, watch } from 'vue'
2024-01-19 15:34:09 +07:00
import { useQuasar } from 'quasar'
2024-09-02 13:40:15 +07:00
import { useRouter } from 'vue-router'
2024-01-19 15:34:09 +07:00
import http from '@/plugins/http'
import config from '@/app.config'
2024-11-19 11:53:07 +07:00
import avatar from '@/assets/avatar_user.jpg'
2024-08-28 13:43:21 +07:00
import { logout, tokenParsed } from '@/plugins/auth'
2024-09-02 13:40:15 +07:00
import { useCounterMixin } from '@/stores/mixin'
2024-01-19 15:34:09 +07:00
import type { notiType } from '@/interface/index/Main'
import type { Noti } from '@/interface/response/Main'
2024-01-19 15:34:09 +07:00
const mixin = useCounterMixin()
2024-07-16 21:27:39 +07:00
const { date2Thai, hideLoader, messageError, dialogRemove, success } = mixin
2024-01-19 15:34:09 +07:00
const router = useRouter()
const $q = useQuasar()
// landing page config url
const configParam = {
landingPageUrl: import.meta.env.VITE_URL_SSO,
}
2024-09-02 13:40:15 +07:00
const fullName = ref<string>('') //ชื่อผู้ใช้งาน
const notiTrigger = ref<boolean>(false) // เปิด,ปิดรายการ
const notiList = ref<notiType[]>([]) // รายการแจ้งเตือน
const totalNotiList = ref<number>(0) //จำนวนการรายการแจ้งเตือน
const totalNoti = ref<number>(0) //จำนวนการแจ้งเตือน
const statusLoad = ref<boolean>(false) // สถานะการโหลด
//รูปแบบการแสดงผลของวันที่และเวลา
const thaiOptions: Intl.DateTimeFormatOptions = {
hour: '2-digit',
minute: '2-digit',
}
/**
* งกนดงขอมลจำนวนการแจงเตอน
*/
2024-01-19 17:22:07 +07:00
async function fetchTotolNotificate() {
2024-09-02 13:40:15 +07:00
await http
.get(config.API.msgNotificateTotal)
.then(async (res) => {
totalNoti.value = await res.data.result
})
.catch((err) => {
messageError($q, err)
})
2024-01-19 17:22:07 +07:00
}
/**
* งกนดงขอมลรายการแจงเตอน
* @param index page องการโหลดขอม
* @param type DEL อโหลดหลงลบขอความ, NOMAL อกรณโหลดขอมลปกต
*/
2024-01-19 15:34:09 +07:00
async function fetchNotifications(index: number, type: string) {
await http
.get(config.API.msgNotificate + `?page=${index}&pageSize=${20}`)
.then((res) => {
const response = res.data.result.data
2024-01-19 17:22:07 +07:00
totalNotiList.value = res.data.result.total
2024-01-19 15:34:09 +07:00
const list: notiType[] = []
if (type === 'DEL') {
2024-01-19 17:22:07 +07:00
notiList.value = []
2024-01-19 15:34:09 +07:00
}
2024-08-28 13:43:21 +07:00
response.map((e: Noti) => {
list.push({
id: e.id,
sender:
e.createdFullName == '' || e.createdFullName == null
? 'เจ้าหน้าที่'[0]
: e.createdFullName[0],
body: e.body ?? '',
timereceive: e.receiveDate,
isOpen: e.isOpen,
2024-01-19 17:22:07 +07:00
})
2024-08-28 13:43:21 +07:00
})
notiList.value.push(...list)
statusLoad.value = totalNotiList.value === 0 ? true : false
2024-01-19 15:34:09 +07:00
})
2024-09-02 13:40:15 +07:00
.catch((err) => {
messageError($q, err)
})
2024-01-19 15:34:09 +07:00
}
/**
* function ลบรายการแจงเตอน
* @param id noti
*/
async function onClickDelete(id: string, index: number) {
dialogRemove($q, async () => {
await http
.delete(config.API.msgId(id))
2024-09-02 13:40:15 +07:00
.then(async () => {
2024-01-19 15:34:09 +07:00
notiList.value.splice(index, 1)
2024-01-19 17:22:07 +07:00
totalNotiList.value--
2024-09-02 13:40:15 +07:00
notiList.value.length === 14 && (await fetchNotifications(1, 'DEL'))
2024-01-19 15:34:09 +07:00
success($q, 'ลบข้อมูลสำเร็จ')
})
.catch((e) => {
messageError($q, e)
})
.finally(async () => {
hideLoader()
})
})
}
2024-09-02 13:40:15 +07:00
/**
* function logout
*/
2024-01-19 15:34:09 +07:00
function onClickLogout() {
$q.dialog({
title: 'ยืนยันการออกจากระบบ',
2024-07-25 12:11:41 +07:00
message: `ต้องการออกจากระบบใช่หรือไม่?`,
2024-01-19 15:34:09 +07:00
cancel: 'ยกเลิก',
ok: 'ยืนยัน',
persistent: true,
2024-07-16 21:27:39 +07:00
}).onOk(async () => {
2024-11-13 15:42:42 +07:00
await http.post(config.API.keycloakLogSSO, { text: 'ออกจากระบบ' })
await logout()
2024-01-19 15:34:09 +07:00
})
}
2024-01-19 17:22:07 +07:00
const page = ref<number>(0)
2024-09-02 13:40:15 +07:00
/**
* โหลดรายการแจงเตอนเพมเม scroll
2024-07-16 21:27:39 +07:00
* @param index
* @param done
*/
2024-09-02 13:40:15 +07:00
function onLoad(index: number, done: Function) {
2024-01-19 17:22:07 +07:00
if (
notiList.value.length < totalNotiList.value ||
(notiList.value.length == 0 && totalNotiList.value === 0)
) {
page.value++
setTimeout(async () => {
await fetchNotifications(page.value, 'NOMAL')
await done()
2024-01-19 17:22:07 +07:00
}, 1500)
2024-01-19 15:34:09 +07:00
}
}
// landing page redirect
2024-11-18 15:34:38 +07:00
const landingPageUrl = ref<string>(getLandingUrl())
function getLandingUrl() {
if (window.location.hostname === 'bmasso.bma.go.th') {
return `${configParam.landingPageUrl}/landing?mode=dev`
} else if (window.location.hostname === 'bma-ehr.frappet.com') {
return `${configParam.landingPageUrl}/landing?mode=prod`
} else {
return `${configParam.landingPageUrl}/landing?mode=dev`
}
}
2024-11-19 11:53:07 +07:00
/** ฟังก์ชันเรียกข้อมูลผู้ใช่งาน*/
async function fetchKeycloakPosition() {
await http
.get(config.API.keycloakPosition())
.then(async (res) => {
const data = await res.data.result
//เช็คว่ามีรูปไหม ถ้ามีรูปเรียกข้อมูลรูป
if (data.avatarName) {
await getImg(data.profileId, data.avatarName)
} else {
profileImg.value = avatar
}
})
.catch((err) => {
messageError($q, err)
})
}
const profileImg = ref<string>('')
async function getImg(id: string, pathName: string) {
await http
.get(config.API.fileByFile('ทะเบียนประวัติ', 'โปรไฟล์', id, pathName))
.then((res) => {
profileImg.value = res.data.downloadUrl
})
}
2024-01-19 17:22:07 +07:00
watch(
() => notiTrigger.value,
() => {
if (!notiTrigger.value) {
const updatedNotifications = notiList.value.map((item) => ({
...item,
isOpen: true,
}))
notiList.value = updatedNotifications
fetchTotolNotificate()
}
2024-01-19 15:34:09 +07:00
}
2024-01-19 17:22:07 +07:00
)
2024-01-19 15:34:09 +07:00
onMounted(async () => {
2024-01-19 17:22:07 +07:00
fetchTotolNotificate()
2024-11-19 11:53:07 +07:00
fetchKeycloakPosition()
2024-08-28 13:43:21 +07:00
const checkTokenParsed = await tokenParsed()
if (checkTokenParsed != null) {
fullName.value = checkTokenParsed.name
2024-01-19 15:34:09 +07:00
}
})
</script>
2024-01-19 15:34:09 +07:00
<template>
<q-layout view="hHh LpR fFr">
<!-- header -->
<q-header flat class="text-dark col-12 bg-top header-br" height-hint="7">
<q-toolbar
class="q-my-xs items-center"
:style="$q.screen.gt.xs ? 'padding: 1% 2%;' : 'padding: 1% 4%;'"
>
<div class="row items-center">
2024-01-19 17:22:07 +07:00
<q-avatar style="background: linear-gradient(#4ce2d3, #02a998)">
2024-01-19 15:34:09 +07:00
<q-img
src="@/assets/logo1.png"
spinner-color="white"
style="height: 35px; width: 35px"
/>
</q-avatar>
<div class="row q-ml-md text-left items-center gt-xs">
<div
style="color: #ffffff; line-height: 10px"
class="text-body2 text-weight-bolder col-12"
>
ระบบ<span class="text-primary">ทรพยากรบคคล</span>
</div>
<div class="text-caption text-white">กรงเทพมหานคร</div>
</div>
</div>
<q-space />
<q-btn
icon="history"
unelevated
rounded
dense
flat
color="white"
@click="router.push('/history')"
/>
2024-01-19 17:22:07 +07:00
<q-btn round dense flat size="13px" class="q-mx-md">
2024-01-19 15:34:09 +07:00
<q-icon name="notifications" size="24px" color="white" />
<q-badge
rounded
2024-01-19 17:22:07 +07:00
v-show="totalNoti !== 0"
2024-01-19 15:34:09 +07:00
color="negative"
text-color="white"
floating
2024-01-19 17:22:07 +07:00
>{{ totalNoti }}</q-badge
>
<q-menu
v-model="notiTrigger"
anchor="bottom middle"
self="top middle"
class="q-mx-lg q-mt-xl"
style="width: 480px"
2024-01-19 15:34:09 +07:00
>
<div class="q-px-md q-py-sm row col-12 items-center">
2024-01-19 17:22:07 +07:00
<div class="text-subtitle1 text-weight-medium">การแจงเตอน</div>
<q-space />
<div class="text-grey-5" style="font-size: 12px">
งหมด {{ totalNotiList }} อความ
2024-01-19 15:34:09 +07:00
</div>
</div>
<q-infinite-scroll @load="onLoad" v-if="statusLoad === false">
2024-01-19 15:34:09 +07:00
<div
v-for="(item, index) in notiList"
:key="index"
class="caption"
>
<q-item v-ripple class="mytry q-py-sm" dense>
<q-item-section avatar top style="min-width: 10px">
<q-avatar
rounded
color="primary"
size="25px"
text-color="white"
>
<span class="text-weight-medium text-uppercase">{{
2024-01-19 17:22:07 +07:00
item.body[0]
2024-01-19 15:34:09 +07:00
}}</span>
</q-avatar>
</q-item-section>
<q-item-section>
<q-item-label
2024-01-19 17:22:07 +07:00
caption
:class="
item.isOpen
? 'text-grey-7'
: 'text-black text-weight-medium'
"
>{{ item.body }}</q-item-label
>
<q-item-label caption class="text-weight-light">
{{ date2Thai(item.timereceive) }}
{{
new Date(item.timereceive).toLocaleTimeString(
'th-TH',
thaiOptions
)
}}
. <q-space />
</q-item-label>
2024-01-19 15:34:09 +07:00
</q-item-section>
<div>
<q-btn
size="sm"
unelevated
dense
icon="mdi-close"
class="mybtn q-mx-xs"
@click="onClickDelete(item.id, index)"
></q-btn>
</div>
</q-item>
</div>
2024-01-19 17:22:07 +07:00
<template
v-slot:loading
v-if="
notiList.length < totalNotiList ||
(notiList.length === 0, totalNotiList === 0)
"
>
2024-01-19 15:34:09 +07:00
<div class="row justify-center q-my-md">
<q-spinner-dots color="primary" size="40px" />
</div>
</template>
2024-01-19 17:22:07 +07:00
</q-infinite-scroll>
<div class="q-pa-md" v-else>
<q-banner rounded class="bg-amber-1 text-center">
<div class="text-yellow-10">
<q-icon
name="mdi-alert-box"
class="q-mx-xs"
size="sm"
color="yellow-10"
/>
2024-07-11 09:16:37 +07:00
ไมอม
</div>
</q-banner>
</div>
2024-01-19 17:22:07 +07:00
</q-menu>
</q-btn>
2024-11-19 11:53:07 +07:00
<q-btn round dense color="white" flat style="font-size: 16px">
<q-avatar size="30px">
<q-img :src="profileImg" spinner-color="white" />
</q-avatar>
2024-11-18 23:32:26 +07:00
<q-menu style="width: 250px">
<div class="column items-center col-12 q-py-md" color="grey-3">
2024-11-19 11:53:07 +07:00
<q-avatar size="72px" color="grey-4">
<!-- <q-icon color="primary" name="account_circle" size="32px" /> -->
<q-img :src="profileImg" spinner-color="black" />
<!-- <img :src="require('@/assets/logo.png')" /> -->
</q-avatar>
2024-11-18 23:32:26 +07:00
<div class="text-subtitle2 q-mt-md q-mb-xs text-center">
{{ fullName }}
</div>
2024-11-18 23:32:26 +07:00
</div>
<!-- <q-list>
2024-01-19 17:22:07 +07:00
<q-item>
<q-item-section avatar>
<q-icon color="primary" name="account_circle" />
</q-item-section>
2024-01-19 15:34:09 +07:00
2024-01-19 17:22:07 +07:00
<q-item-section>{{ fullName }}</q-item-section>
</q-item>
</q-list> -->
2024-01-19 15:34:09 +07:00
2024-11-18 23:32:26 +07:00
<q-separator />
<q-list dense class="q-py-sm">
<q-item clickable :href="landingPageUrl">
<q-item-section avatar>
<q-avatar
color="blue"
text-color="white"
icon="home"
size="24px"
font-size="14px"
/>
</q-item-section>
<q-item-section class="q-py-sm"> Landing Page </q-item-section>
</q-item>
2024-11-18 23:32:26 +07:00
<q-item clickable @click="onClickLogout">
<q-item-section avatar>
<q-avatar
color="red"
text-color="white"
icon="logout"
size="24px"
font-size="14px"
/>
</q-item-section>
<q-item-section class="q-py-sm"> ออกจากระบบ </q-item-section>
</q-item>
</q-list>
</q-menu>
</q-btn>
2024-01-19 15:34:09 +07:00
</q-toolbar>
</q-header>
<div
class="bg-top"
:style="$q.screen.gt.xs ? 'height: 200px;' : 'height: 200px;'"
/>
<q-page-container class="bg-grey-2 q-pb-md">
<q-page
:style="
$q.screen.gt.xs
? 'padding: 1.8% 2%; margin-top: -200px;'
: 'padding: 5% 4%; margin-top: -200px;'
"
>
<router-view :key="$route.fullPath" />
</q-page>
</q-page-container>
</q-layout>
</template>
2024-01-19 15:34:09 +07:00
<style>
.bg-drawer {
background: #242a3d;
}
.menu {
padding-bottom: 5px;
padding-top: 5px;
}
.tabsHome .q-tab__icon {
font-size: 18px;
}
.border-100 {
border-radius: 100px;
}
.bg-top {
background: #273238 !important;
}
.header-br {
border-bottom: 1px solid #fdfdfd31;
}
.menuActive {
background: #1e2234;
border-radius: 0 100px 100px 0;
margin-right: 4%;
}
.q-card {
box-shadow: 3px 3px 20px -10px rgba(151, 150, 150, 0.261) !important;
/* border: 1px solid #eeeded; */
border-radius: 8px;
}
.q-menu {
box-shadow: 3px 3px 10px 1px rgba(95, 95, 95, 0.15) !important;
}
.toptitle {
font-size: 1.2rem;
font-weight: bold;
margin-bottom: 1.2%;
}
.q-field--outlined .q-field__control {
border-radius: 5px;
}
.q-field--outlined .q-field__control:before {
border-color: #c8d3db;
}
/* .q-field--outlined .q-icon {
color: #7474747f;
} */
.shadow-0 {
box-shadow: none !important;
}
.btnBlue {
background-color: #016987;
color: #fff;
}
</style>