tam chat
This commit is contained in:
181
client/app.go
181
client/app.go
@@ -104,6 +104,8 @@ type App struct {
|
||||
wifiRejected bool
|
||||
quitDialogShown bool
|
||||
monitoringTornDown bool
|
||||
chatUnread int
|
||||
replyStaffID uint
|
||||
}
|
||||
|
||||
type LocalStats struct {
|
||||
@@ -274,10 +276,7 @@ func (a *App) startLocalServer() {
|
||||
blocker.Instance.Start()
|
||||
|
||||
// Kiểm tra điều kiện app được phép và giờ học trước khi tải giao diện
|
||||
go a.fetchAllowedApps(st.SystemID)
|
||||
go a.fetchStudentStatus(st.StudentID)
|
||||
go a.fetchWifiPolicy()
|
||||
go a.refreshNetworkStatus()
|
||||
go a.refreshStudentData()
|
||||
|
||||
// Chuyển hướng WebView về trang dashboard của app bằng cách reload app assets
|
||||
runtime.WindowReloadApp(a.ctx)
|
||||
@@ -315,10 +314,8 @@ func (a *App) loadSession() {
|
||||
log.Printf("[APP] Loaded local session: %s (%s)", s.FullName, s.StudentCode)
|
||||
blocker.Instance.Start()
|
||||
|
||||
go a.fetchAllowedApps(s.SystemID)
|
||||
go a.fetchStudentStatus(s.StudentID)
|
||||
go a.refreshStudentData()
|
||||
go a.fetchWifiPolicy()
|
||||
go a.refreshNetworkStatus()
|
||||
go a.syncProfileToServer(&s)
|
||||
}
|
||||
}
|
||||
@@ -589,6 +586,33 @@ func (a *App) runMonitorTick() {
|
||||
}
|
||||
}
|
||||
|
||||
// refreshNetworkStatus — đọc WiFi + ping server ngay (không cộng thời gian online/offline).
|
||||
func (a *App) resolveClassRkID() int64 {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
if a.dashboard.ClassRkID > 0 {
|
||||
return a.dashboard.ClassRkID
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (a *App) refreshStudentData() {
|
||||
a.mu.Lock()
|
||||
student := a.student
|
||||
a.mu.Unlock()
|
||||
if student == nil {
|
||||
return
|
||||
}
|
||||
a.fetchStudentStatus(student.StudentID)
|
||||
classID := a.resolveClassRkID()
|
||||
if classID > 0 {
|
||||
a.fetchAllowedApps(classID)
|
||||
} else {
|
||||
a.fetchAllowedApps(0)
|
||||
}
|
||||
a.refreshNetworkStatus()
|
||||
}
|
||||
|
||||
// refreshNetworkStatus — đọc WiFi + ping server ngay (không cộng thời gian online/offline).
|
||||
func (a *App) refreshNetworkStatus() {
|
||||
if !a.isMonitoringActive() || !a.CheckLoginStatus() {
|
||||
@@ -601,11 +625,14 @@ func (a *App) refreshNetworkStatus() {
|
||||
a.wifiBSSID = wifi.BSSID
|
||||
a.backendOnline = backendOnline
|
||||
a.mu.Unlock()
|
||||
if backendOnline {
|
||||
a.connectWS()
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) pingBackend() bool {
|
||||
client := http.Client{Timeout: 3 * time.Second}
|
||||
resp, err := client.Get("http://127.0.0.1:8080/api/stats")
|
||||
resp, err := client.Get("http://127.0.0.1:8080/api/health")
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
@@ -628,12 +655,18 @@ func (a *App) syncLogsToServer() {
|
||||
return
|
||||
}
|
||||
|
||||
go a.fetchStudentStatus(student.StudentID)
|
||||
go a.fetchAllowedApps(student.SystemID)
|
||||
go func() {
|
||||
a.fetchStudentStatus(student.StudentID)
|
||||
classID := a.resolveClassRkID()
|
||||
if classID <= 0 {
|
||||
classID = student.SystemID
|
||||
}
|
||||
a.fetchAllowedApps(classID)
|
||||
}()
|
||||
|
||||
payload := map[string]any{
|
||||
"studentRkId": student.StudentID,
|
||||
"classRkId": student.SystemID,
|
||||
"classRkId": a.resolveClassRkID(),
|
||||
"sessionDate": time.Now().Format("2006-01-02"),
|
||||
"addOnlineSeconds": unsyncedOn,
|
||||
"addOfflineSeconds": unsyncedOff,
|
||||
@@ -809,8 +842,9 @@ func (a *App) fetchAllowedApps(classId int64) {
|
||||
}
|
||||
a.mu.Unlock()
|
||||
|
||||
url := fmt.Sprintf("http://127.0.0.1:8080/api/classes/%d/allowed-apps?studentId=%d", classId, studentID)
|
||||
client := http.Client{Timeout: 4 * time.Second}
|
||||
resp, err := client.Get(fmt.Sprintf("http://127.0.0.1:8080/api/classes/%d/allowed-apps?studentId=%d", classId, studentID))
|
||||
resp, err := client.Get(url)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -888,7 +922,7 @@ func (a *App) fetchStudentStatus(studentID int64) {
|
||||
}
|
||||
|
||||
func (a *App) connectWS() {
|
||||
if !a.isMonitoringActive() {
|
||||
if !a.CheckLoginStatus() {
|
||||
return
|
||||
}
|
||||
a.mu.Lock()
|
||||
@@ -897,9 +931,13 @@ func (a *App) connectWS() {
|
||||
return
|
||||
}
|
||||
student := a.student
|
||||
classID := a.dashboard.ClassRkID
|
||||
if classID <= 0 {
|
||||
classID = student.SystemID
|
||||
}
|
||||
a.mu.Unlock()
|
||||
|
||||
wsUrl := fmt.Sprintf("ws://127.0.0.1:8080/ws?role=student&studentId=%d&classId=%d", student.StudentID, student.SystemID)
|
||||
wsUrl := fmt.Sprintf("ws://127.0.0.1:8080/ws?role=student&studentId=%d&classId=%d", student.StudentID, classID)
|
||||
dialer := websocket.Dialer{HandshakeTimeout: 4 * time.Second}
|
||||
conn, _, err := dialer.Dial(wsUrl, nil)
|
||||
if err != nil {
|
||||
@@ -934,6 +972,20 @@ func (a *App) connectWS() {
|
||||
runtime.EventsEmit(a.ctx, "start_webcam_stream")
|
||||
case "stop_webcam_stream":
|
||||
runtime.EventsEmit(a.ctx, "stop_webcam_stream")
|
||||
case "chat:message":
|
||||
if staffVal, ok := msg.Data["staffId"].(float64); ok {
|
||||
a.mu.Lock()
|
||||
a.replyStaffID = uint(staffVal)
|
||||
a.chatUnread++
|
||||
unread := a.chatUnread
|
||||
a.mu.Unlock()
|
||||
runtime.EventsEmit(a.ctx, "chat:notify", map[string]any{
|
||||
"unread": unread,
|
||||
"preview": msg.Data["body"],
|
||||
"from": msg.Data["staffName"],
|
||||
})
|
||||
}
|
||||
runtime.EventsEmit(a.ctx, "chat:message", msg.Data)
|
||||
}
|
||||
}
|
||||
}()
|
||||
@@ -1003,3 +1055,104 @@ func (a *App) stopScreenshotStream() {
|
||||
close(a.streamScStop)
|
||||
log.Println("[WS] Screenshot screen-streaming stopped.")
|
||||
}
|
||||
|
||||
func (a *App) GetChatUnread() int {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
return a.chatUnread
|
||||
}
|
||||
|
||||
func (a *App) ClearChatUnread() {
|
||||
a.mu.Lock()
|
||||
a.chatUnread = 0
|
||||
a.mu.Unlock()
|
||||
}
|
||||
|
||||
func (a *App) GetChatConversations() ([]map[string]any, error) {
|
||||
a.mu.Lock()
|
||||
student := a.student
|
||||
a.mu.Unlock()
|
||||
if student == nil {
|
||||
return nil, errors.New("chưa đăng nhập")
|
||||
}
|
||||
url := fmt.Sprintf("http://127.0.0.1:8080/api/student/chat/conversations?studentRkId=%d", student.StudentID)
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var payload struct {
|
||||
Data []map[string]any `json:"data"`
|
||||
}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&payload); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return payload.Data, nil
|
||||
}
|
||||
|
||||
func (a *App) GetChatMessages(staffID uint) ([]map[string]any, error) {
|
||||
a.mu.Lock()
|
||||
student := a.student
|
||||
a.mu.Unlock()
|
||||
if student == nil {
|
||||
return nil, errors.New("chưa đăng nhập")
|
||||
}
|
||||
url := fmt.Sprintf("http://127.0.0.1:8080/api/student/chat/messages?studentRkId=%d&staffId=%d", student.StudentID, staffID)
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var payload struct {
|
||||
Data []map[string]any `json:"data"`
|
||||
ReplyStaffID uint `json:"replyStaffId"`
|
||||
}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&payload); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if staffID > 0 {
|
||||
a.mu.Lock()
|
||||
a.replyStaffID = staffID
|
||||
a.mu.Unlock()
|
||||
} else if payload.ReplyStaffID > 0 {
|
||||
a.mu.Lock()
|
||||
a.replyStaffID = payload.ReplyStaffID
|
||||
a.mu.Unlock()
|
||||
}
|
||||
a.ClearChatUnread()
|
||||
return payload.Data, nil
|
||||
}
|
||||
|
||||
func (a *App) SendChatMessage(body string) error {
|
||||
body = strings.TrimSpace(body)
|
||||
if body == "" {
|
||||
return errors.New("tin nhắn trống")
|
||||
}
|
||||
a.mu.Lock()
|
||||
student := a.student
|
||||
staffID := a.replyStaffID
|
||||
a.mu.Unlock()
|
||||
if student == nil {
|
||||
return errors.New("chưa đăng nhập")
|
||||
}
|
||||
payload := map[string]any{
|
||||
"studentRkId": student.StudentID,
|
||||
"staffId": staffID,
|
||||
"body": body,
|
||||
}
|
||||
b, _ := json.Marshal(payload)
|
||||
resp, err := http.Post("http://127.0.0.1:8080/api/student/chat/messages", "application/json", bytes.NewReader(b))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode >= 300 {
|
||||
var errBody map[string]any
|
||||
_ = json.NewDecoder(resp.Body).Decode(&errBody)
|
||||
if msg, ok := errBody["error"].(string); ok {
|
||||
return errors.New(msg)
|
||||
}
|
||||
return fmt.Errorf("gửi tin thất bại (%d)", resp.StatusCode)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -437,45 +437,291 @@ body {
|
||||
font-family: monospace;
|
||||
}
|
||||
|
||||
.clock-display-summary {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 0.45rem;
|
||||
margin-bottom: 0.45rem;
|
||||
padding-bottom: 0.45rem;
|
||||
border-bottom: 1px dashed var(--border-color);
|
||||
flex: 0 0 auto;
|
||||
.card-title-row--shifts {
|
||||
margin-bottom: 0.2rem;
|
||||
flex-wrap: nowrap;
|
||||
gap: 0.35rem 0.5rem;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.clock-chip {
|
||||
.clock-inline {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 0.35rem;
|
||||
padding: 0.2rem 0.5rem;
|
||||
border-radius: 6px;
|
||||
border: 1px solid var(--border-color);
|
||||
background: var(--bg-subtle);
|
||||
font-size: 0.72rem;
|
||||
}
|
||||
|
||||
.clock-chip em {
|
||||
font-style: normal;
|
||||
flex-wrap: wrap;
|
||||
margin-left: auto;
|
||||
font-size: 0.68rem;
|
||||
color: var(--text-muted);
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.clock-chip strong {
|
||||
.clock-inline-item {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 0.2rem;
|
||||
}
|
||||
|
||||
.clock-inline-item strong {
|
||||
font-family: 'Share Tech Mono', monospace;
|
||||
font-size: 0.78rem;
|
||||
font-size: 0.7rem;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.clock-chip--online strong {
|
||||
color: var(--online-color);
|
||||
.clock-inline-item strong.online { color: var(--online-color); }
|
||||
.clock-inline-item strong.offline { color: var(--offline-color); }
|
||||
|
||||
.clock-inline-dot {
|
||||
width: 5px;
|
||||
height: 5px;
|
||||
border-radius: 50%;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.clock-chip--offline strong {
|
||||
color: var(--offline-color);
|
||||
.clock-inline-dot.online { background: var(--online-color); }
|
||||
.clock-inline-dot.offline { background: var(--offline-color); }
|
||||
|
||||
.clock-inline-sep {
|
||||
opacity: 0.35;
|
||||
font-size: 0.65rem;
|
||||
}
|
||||
|
||||
.card-title-row--shifts .card-title-sub {
|
||||
font-size: 0.62rem;
|
||||
margin-left: 0.35rem;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
/* Student chat messenger */
|
||||
.student-chat-dock {
|
||||
position: fixed;
|
||||
right: 12px;
|
||||
bottom: 12px;
|
||||
z-index: 1000;
|
||||
}
|
||||
|
||||
.student-chat-toast {
|
||||
position: absolute;
|
||||
right: 0;
|
||||
bottom: 64px;
|
||||
width: 260px;
|
||||
padding: 0.65rem 0.75rem;
|
||||
border-radius: 10px;
|
||||
background: #1e293b;
|
||||
color: #fff;
|
||||
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.28);
|
||||
font-size: 0.78rem;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.2rem;
|
||||
}
|
||||
|
||||
.student-chat-fab {
|
||||
position: relative;
|
||||
width: 48px;
|
||||
height: 48px;
|
||||
border-radius: 50%;
|
||||
border: none;
|
||||
background: var(--accent);
|
||||
color: #fff;
|
||||
font-size: 1.15rem;
|
||||
cursor: pointer;
|
||||
box-shadow: 0 6px 18px rgba(0, 0, 0, 0.25);
|
||||
}
|
||||
|
||||
.student-chat-badge {
|
||||
position: absolute;
|
||||
top: -2px;
|
||||
right: -2px;
|
||||
min-width: 16px;
|
||||
height: 16px;
|
||||
padding: 0 4px;
|
||||
border-radius: 8px;
|
||||
background: #ef4444;
|
||||
color: #fff;
|
||||
font-size: 0.62rem;
|
||||
font-weight: 700;
|
||||
line-height: 16px;
|
||||
}
|
||||
|
||||
.student-chat-messenger {
|
||||
position: absolute;
|
||||
right: 0;
|
||||
bottom: 58px;
|
||||
width: min(520px, calc(100vw - 24px));
|
||||
height: min(400px, calc(100vh - 80px));
|
||||
background: var(--bg-card);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: 12px;
|
||||
box-shadow: 0 12px 36px rgba(0, 0, 0, 0.3);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.student-chat-messenger-head {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: 0.55rem 0.7rem;
|
||||
border-bottom: 1px solid var(--border-color);
|
||||
font-size: 0.82rem;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.student-chat-close {
|
||||
border: none;
|
||||
background: none;
|
||||
color: var(--text-muted);
|
||||
font-size: 1.1rem;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.student-chat-messenger-body {
|
||||
display: flex;
|
||||
flex: 1;
|
||||
min-height: 0;
|
||||
}
|
||||
|
||||
.student-chat-sidebar {
|
||||
width: 150px;
|
||||
flex-shrink: 0;
|
||||
border-right: 1px solid var(--border-color);
|
||||
overflow-y: auto;
|
||||
background: rgba(255, 255, 255, 0.02);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.student-chat-conv-btn {
|
||||
width: 100%;
|
||||
text-align: left;
|
||||
border: none;
|
||||
background: transparent;
|
||||
padding: 0.55rem 0.5rem;
|
||||
cursor: pointer;
|
||||
border-bottom: 1px solid var(--border-color);
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.student-chat-conv-btn.active {
|
||||
background: rgba(187, 33, 38, 0.12);
|
||||
border-left: 2px solid var(--accent);
|
||||
}
|
||||
|
||||
.student-chat-conv-name {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
font-size: 0.72rem;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.student-chat-conv-unread {
|
||||
font-style: normal;
|
||||
background: var(--accent);
|
||||
color: #fff;
|
||||
border-radius: 8px;
|
||||
padding: 0 5px;
|
||||
font-size: 0.6rem;
|
||||
}
|
||||
|
||||
.student-chat-conv-preview {
|
||||
display: block;
|
||||
font-size: 0.65rem;
|
||||
color: var(--text-muted);
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
margin-top: 0.15rem;
|
||||
}
|
||||
|
||||
.student-chat-thread {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.student-chat-thread-head {
|
||||
padding: 0.45rem 0.65rem;
|
||||
border-bottom: 1px solid var(--border-color);
|
||||
font-size: 0.75rem;
|
||||
font-weight: 600;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.student-chat-messages {
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
padding: 0.55rem;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.4rem;
|
||||
background: rgba(0, 0, 0, 0.12);
|
||||
}
|
||||
|
||||
.student-chat-empty {
|
||||
font-size: 0.72rem;
|
||||
color: var(--text-muted);
|
||||
text-align: center;
|
||||
padding: 1rem 0.5rem;
|
||||
}
|
||||
|
||||
.student-chat-bubble {
|
||||
max-width: 88%;
|
||||
font-size: 0.76rem;
|
||||
}
|
||||
|
||||
.student-chat-bubble--staff { align-self: flex-start; }
|
||||
.student-chat-bubble--student { align-self: flex-end; }
|
||||
|
||||
.student-chat-label {
|
||||
font-size: 0.6rem;
|
||||
color: var(--text-muted);
|
||||
margin-bottom: 0.1rem;
|
||||
}
|
||||
|
||||
.student-chat-body {
|
||||
padding: 0.4rem 0.55rem;
|
||||
border-radius: 10px;
|
||||
line-height: 1.35;
|
||||
word-break: break-word;
|
||||
}
|
||||
|
||||
.student-chat-bubble--staff .student-chat-body {
|
||||
background: rgba(255, 255, 255, 0.06);
|
||||
border: 1px solid var(--border-color);
|
||||
}
|
||||
|
||||
.student-chat-bubble--student .student-chat-body {
|
||||
background: var(--accent);
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.student-chat-time {
|
||||
font-size: 0.58rem;
|
||||
color: var(--text-muted);
|
||||
margin-top: 0.1rem;
|
||||
}
|
||||
|
||||
.student-chat-compose {
|
||||
display: flex;
|
||||
gap: 0.35rem;
|
||||
padding: 0.45rem 0.55rem;
|
||||
border-top: 1px solid var(--border-color);
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.student-chat-compose input {
|
||||
flex: 1;
|
||||
font-size: 0.74rem;
|
||||
padding: 0.4rem 0.5rem;
|
||||
border-radius: 8px;
|
||||
border: 1px solid var(--border-color);
|
||||
background: var(--bg-base);
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.student-chat-compose input:disabled {
|
||||
opacity: 0.5;
|
||||
}
|
||||
|
||||
.shifts-wrap {
|
||||
@@ -622,15 +868,15 @@ body {
|
||||
.clocks-card {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.4rem;
|
||||
gap: 0.3rem;
|
||||
flex: 1;
|
||||
min-height: 0;
|
||||
overflow: hidden;
|
||||
padding: 0.75rem 0.85rem;
|
||||
padding: 0.5rem 0.65rem;
|
||||
}
|
||||
|
||||
.card-title {
|
||||
font-size: 0.85rem;
|
||||
font-size: 0.78rem;
|
||||
font-weight: 700;
|
||||
color: var(--text-primary);
|
||||
letter-spacing: -0.2px;
|
||||
|
||||
@@ -23,6 +23,11 @@ let stats = {
|
||||
shifts: []
|
||||
};
|
||||
|
||||
let chatOpen = false;
|
||||
let chatMessages = [];
|
||||
let chatConversations = [];
|
||||
let activeStaffId = 0;
|
||||
|
||||
// Quản lý webcam
|
||||
let webcamStream = null;
|
||||
let webcamInterval = null;
|
||||
@@ -46,6 +51,15 @@ function init() {
|
||||
|
||||
window.runtime.EventsOn('start_webcam_stream', startWebcam);
|
||||
window.runtime.EventsOn('stop_webcam_stream', stopWebcam);
|
||||
window.runtime.EventsOn('chat:message', () => {
|
||||
updateChatBadge();
|
||||
loadChatConversations().catch(console.error);
|
||||
if (chatOpen && activeStaffId) loadChatMessages(activeStaffId).catch(console.error);
|
||||
});
|
||||
window.runtime.EventsOn('chat:notify', (data) => {
|
||||
updateChatBadge();
|
||||
showChatToast(data);
|
||||
});
|
||||
checkLogin();
|
||||
}
|
||||
|
||||
@@ -57,6 +71,8 @@ async function checkLogin() {
|
||||
studentInfo = await window.go.main.App.GetStudentInfo();
|
||||
renderDashboard();
|
||||
startStatsTicker();
|
||||
ensureChatWidget();
|
||||
updateChatBadge();
|
||||
} else {
|
||||
loggedIn = false;
|
||||
renderLoginPrompt();
|
||||
@@ -226,19 +242,14 @@ function renderDashboard() {
|
||||
</div>
|
||||
|
||||
<div class="clocks-card card">
|
||||
<div class="card-title-row">
|
||||
<div class="card-title-row card-title-row--shifts">
|
||||
<div class="card-title">Hôm nay — theo ca</div>
|
||||
<div class="card-title-sub" id="session-date">${stats.sessionDate || ''}</div>
|
||||
</div>
|
||||
<div class="clock-display clock-display-summary">
|
||||
<span class="clock-chip clock-chip--online">
|
||||
<em>Trực tuyến</em>
|
||||
<strong id="clock-online">00:00:00</strong>
|
||||
</span>
|
||||
<span class="clock-chip clock-chip--offline">
|
||||
<em>Ngoại tuyến</em>
|
||||
<strong id="clock-offline">00:00:00</strong>
|
||||
</span>
|
||||
<div class="clock-inline">
|
||||
<span class="clock-inline-item" title="Tổng trực tuyến"><span class="clock-inline-dot online"></span><strong id="clock-online" class="online">00:00:00</strong></span>
|
||||
<span class="clock-inline-sep">·</span>
|
||||
<span class="clock-inline-item" title="Tổng ngoại tuyến"><span class="clock-inline-dot offline"></span><strong id="clock-offline" class="offline">00:00:00</strong></span>
|
||||
<span class="card-title-sub" id="session-date">${stats.sessionDate || ''}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="shifts-wrap" id="shifts-wrap">
|
||||
${renderShiftsTable(stats.shifts)}
|
||||
@@ -254,11 +265,177 @@ function renderDashboard() {
|
||||
loggedIn = false;
|
||||
studentInfo = null;
|
||||
stopWebcam();
|
||||
const w = document.getElementById('student-chat-widget');
|
||||
if (w) w.remove();
|
||||
await window.go.main.App.Logout();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function ensureChatWidget() {
|
||||
if (document.getElementById('student-chat-widget')) return;
|
||||
const wrap = document.createElement('div');
|
||||
wrap.id = 'student-chat-widget';
|
||||
wrap.className = 'student-chat-dock';
|
||||
wrap.innerHTML = `
|
||||
<div class="student-chat-toast" id="student-chat-toast" hidden></div>
|
||||
<button type="button" class="student-chat-fab" id="student-chat-fab" title="Tin nhắn">
|
||||
💬<span class="student-chat-badge" id="student-chat-badge" hidden>0</span>
|
||||
</button>
|
||||
<div class="student-chat-messenger" id="student-chat-messenger" hidden>
|
||||
<header class="student-chat-messenger-head">
|
||||
<strong>Tin nhắn</strong>
|
||||
<button type="button" class="student-chat-close" id="student-chat-close">×</button>
|
||||
</header>
|
||||
<div class="student-chat-messenger-body">
|
||||
<aside class="student-chat-sidebar" id="student-chat-conv-list"></aside>
|
||||
<section class="student-chat-thread">
|
||||
<div class="student-chat-thread-head" id="student-chat-thread-head">Chọn hội thoại</div>
|
||||
<div class="student-chat-messages" id="student-chat-messages"></div>
|
||||
<div class="student-chat-compose">
|
||||
<input id="student-chat-input" placeholder="Nhập tin nhắn..." disabled />
|
||||
<button type="button" class="btn btn-primary btn-sm" id="student-chat-send" disabled>Gửi</button>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
document.body.appendChild(wrap);
|
||||
document.getElementById('student-chat-fab').addEventListener('click', toggleChat);
|
||||
document.getElementById('student-chat-close').addEventListener('click', () => setChatOpen(false));
|
||||
document.getElementById('student-chat-send').addEventListener('click', sendStudentChat);
|
||||
document.getElementById('student-chat-input').addEventListener('keydown', (e) => {
|
||||
if (e.key === 'Enter') { e.preventDefault(); sendStudentChat(); }
|
||||
});
|
||||
}
|
||||
|
||||
function showChatToast(data) {
|
||||
const toast = document.getElementById('student-chat-toast');
|
||||
if (!toast) return;
|
||||
const from = data?.from || 'Giảng viên';
|
||||
const preview = data?.preview || 'Bạn có tin nhắn mới';
|
||||
toast.innerHTML = `<strong>${escapeHtml(from)}</strong><span>${escapeHtml(preview)}</span>`;
|
||||
toast.hidden = false;
|
||||
clearTimeout(showChatToast._t);
|
||||
showChatToast._t = setTimeout(() => { toast.hidden = true; }, 5000);
|
||||
}
|
||||
|
||||
function setChatOpen(open) {
|
||||
chatOpen = open;
|
||||
const panel = document.getElementById('student-chat-messenger');
|
||||
if (panel) panel.hidden = !open;
|
||||
if (open) {
|
||||
loadChatConversations().catch(console.error);
|
||||
if (activeStaffId) loadChatMessages(activeStaffId).catch(console.error);
|
||||
}
|
||||
}
|
||||
|
||||
function toggleChat() {
|
||||
setChatOpen(!chatOpen);
|
||||
}
|
||||
|
||||
async function loadChatConversations() {
|
||||
const rows = await window.go.main.App.GetChatConversations();
|
||||
chatConversations = Array.isArray(rows) ? rows : [];
|
||||
renderChatConversations();
|
||||
updateChatBadge();
|
||||
}
|
||||
|
||||
function renderChatConversations() {
|
||||
const list = document.getElementById('student-chat-conv-list');
|
||||
if (!list) return;
|
||||
if (!chatConversations.length) {
|
||||
list.innerHTML = '<div class="student-chat-empty">Chưa có tin nhắn</div>';
|
||||
return;
|
||||
}
|
||||
list.innerHTML = chatConversations.map((c) => {
|
||||
const sid = Number(c.staffId || 0);
|
||||
const unread = Number(c.unread || 0);
|
||||
const active = sid === activeStaffId ? ' active' : '';
|
||||
const name = escapeHtml(c.staffName || c.staffEmail || 'Giảng viên');
|
||||
const preview = escapeHtml(c.lastMessage || '');
|
||||
return `<button type="button" class="student-chat-conv-btn${active}" data-staff-id="${sid}">
|
||||
<span class="student-chat-conv-name">${name}${unread > 0 ? `<em class="student-chat-conv-unread">${unread}</em>` : ''}</span>
|
||||
<span class="student-chat-conv-preview">${preview}</span>
|
||||
</button>`;
|
||||
}).join('');
|
||||
list.querySelectorAll('.student-chat-conv-btn').forEach((btn) => {
|
||||
btn.addEventListener('click', () => {
|
||||
const sid = Number(btn.getAttribute('data-staff-id'));
|
||||
pickStaffChat(sid);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function pickStaffChat(staffId) {
|
||||
activeStaffId = staffId;
|
||||
const head = document.getElementById('student-chat-thread-head');
|
||||
const conv = chatConversations.find((c) => Number(c.staffId) === staffId);
|
||||
if (head) head.textContent = conv?.staffName || conv?.staffEmail || 'Giảng viên';
|
||||
const input = document.getElementById('student-chat-input');
|
||||
const sendBtn = document.getElementById('student-chat-send');
|
||||
if (input) input.disabled = !staffId;
|
||||
if (sendBtn) sendBtn.disabled = !staffId;
|
||||
renderChatConversations();
|
||||
if (staffId) await loadChatMessages(staffId);
|
||||
}
|
||||
|
||||
async function loadChatMessages(staffId) {
|
||||
const msgs = await window.go.main.App.GetChatMessages(staffId);
|
||||
chatMessages = Array.isArray(msgs) ? msgs : [];
|
||||
renderChatMessages();
|
||||
updateChatBadge();
|
||||
loadChatConversations().catch(console.error);
|
||||
}
|
||||
|
||||
function renderChatMessages() {
|
||||
const box = document.getElementById('student-chat-messages');
|
||||
if (!box) return;
|
||||
if (!chatMessages.length) {
|
||||
box.innerHTML = '<div class="student-chat-empty">Chưa có tin nhắn</div>';
|
||||
return;
|
||||
}
|
||||
box.innerHTML = chatMessages.map((m) => {
|
||||
const role = m.senderRole === 'student' ? 'student' : 'staff';
|
||||
const time = m.createdAt ? new Date(m.createdAt).toLocaleTimeString('vi-VN', { hour: '2-digit', minute: '2-digit' }) : '';
|
||||
const label = role === 'staff' ? (m.staffName || 'Giảng viên') : 'Bạn';
|
||||
return `<div class="student-chat-bubble student-chat-bubble--${role}"><div class="student-chat-label">${label}</div><div class="student-chat-body">${escapeHtml(m.body || '')}</div><div class="student-chat-time">${time}</div></div>`;
|
||||
}).join('');
|
||||
box.scrollTop = box.scrollHeight;
|
||||
}
|
||||
|
||||
function escapeHtml(s) {
|
||||
return String(s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
||||
}
|
||||
|
||||
async function sendStudentChat() {
|
||||
const input = document.getElementById('student-chat-input');
|
||||
if (!input || !input.value.trim() || !activeStaffId) return;
|
||||
try {
|
||||
await window.go.main.App.SendChatMessage(input.value.trim());
|
||||
input.value = '';
|
||||
await loadChatMessages(activeStaffId);
|
||||
} catch (err) {
|
||||
alert(err?.message || err || 'Gửi tin thất bại');
|
||||
}
|
||||
}
|
||||
|
||||
async function updateChatBadge() {
|
||||
const badge = document.getElementById('student-chat-badge');
|
||||
if (!badge || !window.go?.main?.App?.GetChatUnread) return;
|
||||
try {
|
||||
const n = await window.go.main.App.GetChatUnread();
|
||||
if (n > 0) {
|
||||
badge.hidden = false;
|
||||
badge.textContent = n > 9 ? '9+' : String(n);
|
||||
} else {
|
||||
badge.hidden = true;
|
||||
}
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
|
||||
function formatDuration(totalSeconds) {
|
||||
const hrs = Math.floor(totalSeconds / 3600);
|
||||
const mins = Math.floor((totalSeconds % 3600) / 60);
|
||||
@@ -330,6 +507,8 @@ function startStatsTicker() {
|
||||
if (indicator) {
|
||||
indicator.className = `status-indicator ${stats.serverReachable ? 'online' : 'offline'}`;
|
||||
}
|
||||
|
||||
updateChatBadge();
|
||||
} catch (err) {
|
||||
console.error('Error fetching stats:', err);
|
||||
}
|
||||
|
||||
10
client/frontend/wailsjs/go/main/App.d.ts
vendored
10
client/frontend/wailsjs/go/main/App.d.ts
vendored
@@ -3,6 +3,14 @@
|
||||
|
||||
export function CheckLoginStatus():Promise<boolean>;
|
||||
|
||||
export function ClearChatUnread():Promise<void>;
|
||||
|
||||
export function GetChatConversations():Promise<Array<Record<string, any>>>;
|
||||
|
||||
export function GetChatMessages(arg1:number):Promise<Array<Record<string, any>>>;
|
||||
|
||||
export function GetChatUnread():Promise<number>;
|
||||
|
||||
export function GetStats():Promise<Record<string, any>>;
|
||||
|
||||
export function GetStudentInfo():Promise<Record<string, any>>;
|
||||
@@ -11,4 +19,6 @@ export function Logout():Promise<void>;
|
||||
|
||||
export function NavigateToLogin():Promise<void>;
|
||||
|
||||
export function SendChatMessage(arg1:string):Promise<void>;
|
||||
|
||||
export function SendWebcamFrame(arg1:string):Promise<void>;
|
||||
|
||||
@@ -6,6 +6,22 @@ export function CheckLoginStatus() {
|
||||
return window['go']['main']['App']['CheckLoginStatus']();
|
||||
}
|
||||
|
||||
export function ClearChatUnread() {
|
||||
return window['go']['main']['App']['ClearChatUnread']();
|
||||
}
|
||||
|
||||
export function GetChatConversations() {
|
||||
return window['go']['main']['App']['GetChatConversations']();
|
||||
}
|
||||
|
||||
export function GetChatMessages(arg1) {
|
||||
return window['go']['main']['App']['GetChatMessages'](arg1);
|
||||
}
|
||||
|
||||
export function GetChatUnread() {
|
||||
return window['go']['main']['App']['GetChatUnread']();
|
||||
}
|
||||
|
||||
export function GetStats() {
|
||||
return window['go']['main']['App']['GetStats']();
|
||||
}
|
||||
@@ -22,6 +38,10 @@ export function NavigateToLogin() {
|
||||
return window['go']['main']['App']['NavigateToLogin']();
|
||||
}
|
||||
|
||||
export function SendChatMessage(arg1) {
|
||||
return window['go']['main']['App']['SendChatMessage'](arg1);
|
||||
}
|
||||
|
||||
export function SendWebcamFrame(arg1) {
|
||||
return window['go']['main']['App']['SendWebcamFrame'](arg1);
|
||||
}
|
||||
|
||||
@@ -182,19 +182,63 @@ func EnumerateGUIWindows() ([]WindowInfo, error) {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func parseKeywordList(keywords string) []string {
|
||||
keywords = strings.ReplaceAll(keywords, "\r\n", "\n")
|
||||
parts := strings.FieldsFunc(keywords, func(r rune) bool {
|
||||
return r == ',' || r == '\n' || r == ';' || r == '|'
|
||||
})
|
||||
seen := map[string]bool{}
|
||||
var list []string
|
||||
for _, p := range parts {
|
||||
trimmed := strings.TrimSpace(strings.ToLower(p))
|
||||
trimmed = strings.TrimSuffix(trimmed, ".exe")
|
||||
if trimmed == "" || seen[trimmed] {
|
||||
continue
|
||||
}
|
||||
seen[trimmed] = true
|
||||
list = append(list, trimmed)
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
var keywordAliases = map[string][]string{
|
||||
"lark": {"lark", "feishu", "larkshell", "larkhelper"},
|
||||
"feishu": {"lark", "feishu", "larkshell", "larkhelper"},
|
||||
"teams": {"teams", "ms-teams", "msteams"},
|
||||
"zalo": {"zalo", "zalopcb"},
|
||||
}
|
||||
|
||||
func expandKeyword(kw string) []string {
|
||||
base := []string{kw}
|
||||
if aliases, ok := keywordAliases[kw]; ok {
|
||||
base = append(base, aliases...)
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
var out []string
|
||||
for _, a := range base {
|
||||
a = strings.TrimSpace(strings.ToLower(a))
|
||||
if a != "" && !seen[a] {
|
||||
seen[a] = true
|
||||
out = append(out, a)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func matchesAllowedKeyword(kw, pNameLower, wTitleLower string) bool {
|
||||
for _, variant := range expandKeyword(kw) {
|
||||
if strings.Contains(pNameLower, variant) || strings.Contains(wTitleLower, variant) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (b *Blocker) SetKeywords(keywords string) {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
|
||||
parts := strings.Split(keywords, ",")
|
||||
var list []string
|
||||
for _, p := range parts {
|
||||
trimmed := strings.TrimSpace(strings.ToLower(p))
|
||||
if trimmed != "" {
|
||||
list = append(list, trimmed)
|
||||
}
|
||||
}
|
||||
b.allowedKeywords = list
|
||||
b.allowedKeywords = parseKeywordList(keywords)
|
||||
log.Printf("[BLOCKER] Keywords updated: %v", b.allowedKeywords)
|
||||
}
|
||||
|
||||
@@ -254,7 +298,7 @@ func (b *Blocker) checkAndKill() {
|
||||
// 2. Kiểm tra xem có chứa bất kỳ từ khóa nào được cho phép không
|
||||
allowed := false
|
||||
for _, kw := range keywords {
|
||||
if strings.Contains(pNameLower, kw) || strings.Contains(wTitleLower, kw) {
|
||||
if matchesAllowedKeyword(kw, pNameLower, wTitleLower) {
|
||||
allowed = true
|
||||
break
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user