mirror of
https://github.com/fumiama/paper-manager.git
synced 2026-06-12 20:40:33 +08:00
add /api/getMessageList
This commit is contained in:
@@ -177,6 +177,16 @@ func init() {
|
|||||||
}
|
}
|
||||||
writeresult(w, codeSuccess, &message{M: "成功"}, messageOk, typeSuccess)
|
writeresult(w, codeSuccess, &message{M: "成功"}, messageOk, typeSuccess)
|
||||||
}}
|
}}
|
||||||
|
|
||||||
|
apimap["/api/getMessageList"] = &apihandler{"GET", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
token := r.Header.Get("Authorization")
|
||||||
|
ret, err := getMessageList(token)
|
||||||
|
if err != nil {
|
||||||
|
writeresult(w, codeError, nil, err.Error(), typeError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
writeresult(w, codeSuccess, ret, messageOk, typeSuccess)
|
||||||
|
}}
|
||||||
}
|
}
|
||||||
|
|
||||||
// APIHandler serves all backend /api call
|
// APIHandler serves all backend /api call
|
||||||
|
|||||||
@@ -43,6 +43,17 @@ func (r UserRole) Nick() string {
|
|||||||
return "nil"
|
return "nil"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
MessageNormal MessageType = iota
|
||||||
|
MessageRegister
|
||||||
|
MessageUserAdded
|
||||||
|
MessageContactChange
|
||||||
|
MessagePasswordChange
|
||||||
|
MessageResetPassword
|
||||||
|
)
|
||||||
|
|
||||||
|
type MessageType uint8
|
||||||
|
|
||||||
const (
|
const (
|
||||||
UserTableUser = "user"
|
UserTableUser = "user"
|
||||||
UserTableMessage = "msg"
|
UserTableMessage = "msg"
|
||||||
@@ -315,6 +326,24 @@ type Message struct {
|
|||||||
Pswd string // Pswd is the user's password to add in register message
|
Pswd string // Pswd is the user's password to add in register message
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Type decide message type by fields Name, Cont and Pswd.
|
||||||
|
func (m *Message) Type() MessageType {
|
||||||
|
switch {
|
||||||
|
case m.Name != "" && m.Cont != "" && m.Pswd != "":
|
||||||
|
return MessageRegister
|
||||||
|
case m.Name == "" && m.Cont != "" && m.Pswd == "":
|
||||||
|
return MessageUserAdded
|
||||||
|
case m.Name != "" && m.Cont != "" && m.Pswd == "":
|
||||||
|
return MessageContactChange
|
||||||
|
case m.Name != "" && m.Cont == "" && m.Pswd != "":
|
||||||
|
return MessagePasswordChange
|
||||||
|
case m.Name != "" && m.Cont == "" && m.Pswd == "":
|
||||||
|
return MessageResetPassword
|
||||||
|
default:
|
||||||
|
return MessageNormal
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// SendMessage will send a message
|
// SendMessage will send a message
|
||||||
func (u *UserDatabase) SendMessage(m *Message) error {
|
func (u *UserDatabase) SendMessage(m *Message) error {
|
||||||
m.ID = nil
|
m.ID = nil
|
||||||
@@ -376,7 +405,6 @@ func (u *UserDatabase) notifyUserAdded(opname, name string) error {
|
|||||||
m := Message{
|
m := Message{
|
||||||
Date: time.Now().Unix(),
|
Date: time.Now().Unix(),
|
||||||
Text: opname + "添加了用户 " + name,
|
Text: opname + "添加了用户 " + name,
|
||||||
Name: name,
|
|
||||||
Cont: opname,
|
Cont: opname,
|
||||||
}
|
}
|
||||||
u.mu.Lock()
|
u.mu.Lock()
|
||||||
@@ -449,7 +477,7 @@ func (u *UserDatabase) notifyPasswordChange(name, npwd string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetMessagesOfUser set Pswd field to empty
|
// GetMessagesOfUser will change non-empty Pswd field to "-"
|
||||||
func (u *UserDatabase) GetMessagesOfUser(to int) (ms []Message, err error) {
|
func (u *UserDatabase) GetMessagesOfUser(to int) (ms []Message, err error) {
|
||||||
u.mu.RLock()
|
u.mu.RLock()
|
||||||
defer u.mu.RUnlock()
|
defer u.mu.RUnlock()
|
||||||
@@ -459,8 +487,10 @@ func (u *UserDatabase) GetMessagesOfUser(to int) (ms []Message, err error) {
|
|||||||
}
|
}
|
||||||
ms = make([]Message, 0, n)
|
ms = make([]Message, 0, n)
|
||||||
m := Message{}
|
m := Message{}
|
||||||
err = u.db.FindFor(UserTableMessage, &m, "WHERE ToID="+strconv.Itoa(to), func() error {
|
err = u.db.FindFor(UserTableMessage, &m, "WHERE ToID="+strconv.Itoa(to)+" ORDER BY Date DESC", func() error {
|
||||||
m.Pswd = ""
|
if m.Pswd != "" {
|
||||||
|
m.Pswd = "-"
|
||||||
|
}
|
||||||
ms = append(ms, m)
|
ms = append(ms, m)
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|||||||
43
backend/message.go
Normal file
43
backend/message.go
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
package backend
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/fumiama/paper-manager/backend/global"
|
||||||
|
)
|
||||||
|
|
||||||
|
type messageList struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
Avatar string `json:"avatar"`
|
||||||
|
Date string `json:"date"`
|
||||||
|
Text string `json:"text"`
|
||||||
|
Type global.MessageType `json:"type"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func getMessageList(token string) ([]messageList, error) {
|
||||||
|
user := usertokens.Get(token)
|
||||||
|
if user == nil {
|
||||||
|
return nil, errInvalidToken
|
||||||
|
}
|
||||||
|
ms, err := global.UserDB.GetMessagesOfUser(*user.ID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
if len(ms) == 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
ml := make([]messageList, len(ms))
|
||||||
|
for i, m := range ms {
|
||||||
|
avtr := ""
|
||||||
|
u, err := global.UserDB.GetUserByName(m.Name)
|
||||||
|
if err == nil {
|
||||||
|
avtr = u.Avtr
|
||||||
|
}
|
||||||
|
ml[i].ID = *m.ID
|
||||||
|
ml[i].Avatar = avtr
|
||||||
|
ml[i].Date = time.Unix(m.Date, 0).Format(chineseDateLayout)
|
||||||
|
ml[i].Text = m.Text
|
||||||
|
ml[i].Type = m.Type()
|
||||||
|
}
|
||||||
|
return ml, nil
|
||||||
|
}
|
||||||
10
frontend/vben/src/api/dashboard/index.ts
Normal file
10
frontend/vben/src/api/dashboard/index.ts
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
import { defHttp } from '/@/utils/http/axios'
|
||||||
|
import { MessageItem } from './model/workbenchModel'
|
||||||
|
|
||||||
|
enum Api {
|
||||||
|
GetMessageList = '/getMessageList',
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getMessageList = () => {
|
||||||
|
return defHttp.get<MessageItem[]>({ url: Api.GetMessageList })
|
||||||
|
}
|
||||||
16
frontend/vben/src/api/dashboard/model/workbenchModel.ts
Normal file
16
frontend/vben/src/api/dashboard/model/workbenchModel.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
export enum MessageTypeEnum {
|
||||||
|
MessageNormal = 0,
|
||||||
|
MessageRegister = 1,
|
||||||
|
MessageUserAdded = 2,
|
||||||
|
MessageContactChange = 3,
|
||||||
|
MessagePasswordChange = 4,
|
||||||
|
MessageResetPassword = 5,
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface MessageItem {
|
||||||
|
id: number
|
||||||
|
avatar: string
|
||||||
|
date: string
|
||||||
|
text: string
|
||||||
|
type: MessageTypeEnum
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<template>
|
<template>
|
||||||
<Card title="我的消息" v-bind="$attrs">
|
<Card title="我的消息" v-bind="$attrs">
|
||||||
<List item-layout="horizontal" :data-source="dynamicInfoItems">
|
<List item-layout="horizontal" :data-source="dynamicInfoItemsRef">
|
||||||
<template #renderItem="{ item }">
|
<template #renderItem="{ item }">
|
||||||
<ListItem>
|
<ListItem>
|
||||||
<ListItemMeta>
|
<ListItemMeta>
|
||||||
@@ -8,11 +8,20 @@
|
|||||||
{{ item.date }}
|
{{ item.date }}
|
||||||
</template>
|
</template>
|
||||||
<!-- eslint-disable-next-line -->
|
<!-- eslint-disable-next-line -->
|
||||||
<template #title> {{ item.name }} <span v-html="item.desc"> </span> </template>
|
<template #title> <span v-html="item.text"> </span> </template>
|
||||||
<template #avatar>
|
<template #avatar>
|
||||||
<Icon :icon="item.avatar" :size="30" />
|
<Avatar :src="item.avatar || headerImg" :size="36" />
|
||||||
</template>
|
</template>
|
||||||
</ListItemMeta>
|
</ListItemMeta>
|
||||||
|
<a-button
|
||||||
|
ghost
|
||||||
|
color="success"
|
||||||
|
v-if="
|
||||||
|
item.type in [MessageTypeEnum.MessageRegister, MessageTypeEnum.MessageResetPassword]
|
||||||
|
"
|
||||||
|
>接受</a-button
|
||||||
|
>
|
||||||
|
|
||||||
<a-button ghost color="error">删除</a-button>
|
<a-button ghost color="error">删除</a-button>
|
||||||
</ListItem>
|
</ListItem>
|
||||||
</template>
|
</template>
|
||||||
@@ -20,10 +29,17 @@
|
|||||||
</Card>
|
</Card>
|
||||||
</template>
|
</template>
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
|
import { ref } from 'vue'
|
||||||
import { Card, List } from 'ant-design-vue'
|
import { Card, List } from 'ant-design-vue'
|
||||||
import { dynamicInfoItems } from './data'
|
import { getMessageList } from '/@/api/dashboard/index'
|
||||||
import { Icon } from '/@/components/Icon'
|
import { MessageTypeEnum, MessageItem } from '/@/api/dashboard/model/workbenchModel'
|
||||||
|
import { Avatar } from 'ant-design-vue'
|
||||||
|
import headerImg from '/@/assets/images/header.jpg'
|
||||||
|
|
||||||
const ListItem = List.Item
|
const ListItem = List.Item
|
||||||
const ListItemMeta = List.Item.Meta
|
const ListItemMeta = List.Item.Meta
|
||||||
|
const dynamicInfoItemsRef = ref([] as MessageItem[])
|
||||||
|
getMessageList().then((value) => {
|
||||||
|
dynamicInfoItemsRef.value = value
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -1,96 +0,0 @@
|
|||||||
interface NavItem {
|
|
||||||
title: string
|
|
||||||
icon: string
|
|
||||||
color: string
|
|
||||||
}
|
|
||||||
|
|
||||||
interface DynamicInfoItem {
|
|
||||||
avatar: string
|
|
||||||
name: string
|
|
||||||
date: string
|
|
||||||
desc: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export const navItems: NavItem[] = [
|
|
||||||
{
|
|
||||||
title: '首页',
|
|
||||||
icon: 'ion:home-outline',
|
|
||||||
color: '#1fdaca',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: '仪表盘',
|
|
||||||
icon: 'ion:grid-outline',
|
|
||||||
color: '#bf0c2c',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: '组件',
|
|
||||||
icon: 'ion:layers-outline',
|
|
||||||
color: '#e18525',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: '系统管理',
|
|
||||||
icon: 'ion:settings-outline',
|
|
||||||
color: '#3fb27f',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: '权限管理',
|
|
||||||
icon: 'ion:key-outline',
|
|
||||||
color: '#4daf1bc9',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: '图表',
|
|
||||||
icon: 'ion:bar-chart-outline',
|
|
||||||
color: '#00d8ff',
|
|
||||||
},
|
|
||||||
]
|
|
||||||
|
|
||||||
export const dynamicInfoItems: DynamicInfoItem[] = [
|
|
||||||
{
|
|
||||||
avatar: 'dynamic-avatar-1|svg',
|
|
||||||
name: '威廉',
|
|
||||||
date: '刚刚',
|
|
||||||
desc: `在 <a>开源组</a> 创建了项目 <a>Vue</a>`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
avatar: 'dynamic-avatar-2|svg',
|
|
||||||
name: '艾文',
|
|
||||||
date: '1个小时前',
|
|
||||||
desc: `关注了 <a>威廉</a> `,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
avatar: 'dynamic-avatar-3|svg',
|
|
||||||
name: '克里斯',
|
|
||||||
date: '1天前',
|
|
||||||
desc: `发布了 <a>个人动态</a> `,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
avatar: 'dynamic-avatar-4|svg',
|
|
||||||
name: 'Vben',
|
|
||||||
date: '2天前',
|
|
||||||
desc: `发表文章 <a>如何编写一个Vite插件</a> `,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
avatar: 'dynamic-avatar-5|svg',
|
|
||||||
name: '皮特',
|
|
||||||
date: '3天前',
|
|
||||||
desc: `回复了 <a>杰克</a> 的问题 <a>如何进行项目优化?</a>`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
avatar: 'dynamic-avatar-6|svg',
|
|
||||||
name: '杰克',
|
|
||||||
date: '1周前',
|
|
||||||
desc: `关闭了问题 <a>如何运行项目</a> `,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
avatar: 'dynamic-avatar-1|svg',
|
|
||||||
name: '威廉',
|
|
||||||
date: '1周前',
|
|
||||||
desc: `发布了 <a>个人动态</a> `,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
avatar: 'dynamic-avatar-1|svg',
|
|
||||||
name: '威廉',
|
|
||||||
date: '2021-04-01 20:00',
|
|
||||||
desc: `推送了代码到 <a>Github</a>`,
|
|
||||||
},
|
|
||||||
]
|
|
||||||
Reference in New Issue
Block a user