fix: fix push setting page (#68)

* fix: unknown channel on message page

* feat: add get all chanels short api

* style: temporaryly change label color

* fix: update query for new api

* fix: fix PushSetting page

* fix: fix PushSetting page

---------

Co-authored-by: JustSong <quanpengsong@gmail.com>
This commit is contained in:
Edward
2023-05-06 21:19:49 +08:00
committed by GitHub
co-authored by JustSong
parent 01c6af0005
commit c70cc3a793
3 changed files with 64 additions and 21 deletions
+22
View File
@@ -11,6 +11,10 @@ import (
) )
func GetAllChannels(c *gin.Context) { func GetAllChannels(c *gin.Context) {
if c.Query("brief") != "" {
GetBriefChannels(c)
return
}
userId := c.GetInt("id") userId := c.GetInt("id")
p, _ := strconv.Atoi(c.Query("p")) p, _ := strconv.Atoi(c.Query("p"))
if p < 0 { if p < 0 {
@@ -32,6 +36,24 @@ func GetAllChannels(c *gin.Context) {
return return
} }
func GetBriefChannels(c *gin.Context) {
userId := c.GetInt("id")
channels, err := model.GetBriefChannelsByUserId(userId)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
"data": channels,
})
return
}
func SearchChannels(c *gin.Context) { func SearchChannels(c *gin.Context) {
userId := c.GetInt("id") userId := c.GetInt("id")
keyword := c.Query("keyword") keyword := c.Query("keyword")
+13 -1
View File
@@ -2,6 +2,7 @@ package model
import ( import (
"errors" "errors"
"message-pusher/common"
) )
const ( const (
@@ -22,7 +23,7 @@ const (
type Channel struct { type Channel struct {
Id int `json:"id"` Id int `json:"id"`
Type string `json:"type" gorm:"type:varchar(32)"` Type string `json:"type" gorm:"type:varchar(32)"`
UserId int `json:"user_id" gorm:"uniqueIndex:name_user_id"` UserId int `json:"user_id" gorm:"uniqueIndex:name_user_id;index"`
Name string `json:"name" gorm:"type:varchar(32);uniqueIndex:name_user_id"` Name string `json:"name" gorm:"type:varchar(32);uniqueIndex:name_user_id"`
Description string `json:"description"` Description string `json:"description"`
Status int `json:"status" gorm:"default:1"` // enabled, disabled Status int `json:"status" gorm:"default:1"` // enabled, disabled
@@ -34,6 +35,12 @@ type Channel struct {
CreatedTime int64 `json:"created_time" gorm:"bigint"` CreatedTime int64 `json:"created_time" gorm:"bigint"`
} }
type BriefChannel struct {
Id int `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
}
func GetChannelById(id int, userId int) (*Channel, error) { func GetChannelById(id int, userId int) (*Channel, error) {
if id == 0 || userId == 0 { if id == 0 || userId == 0 {
return nil, errors.New("id 或 userId 为空!") return nil, errors.New("id 或 userId 为空!")
@@ -67,6 +74,11 @@ func GetChannelsByUserId(userId int, startIdx int, num int) (channels []*Channel
return channels, err return channels, err
} }
func GetBriefChannelsByUserId(userId int) (channels []*BriefChannel, err error) {
err = DB.Model(&Channel{}).Select("id", "name", "description").Where("user_id = ? and status = ?", userId, common.ChannelStatusEnabled).Find(&channels).Error
return channels, err
}
func SearchChannels(userId int, keyword string) (channels []*Channel, err error) { func SearchChannels(userId int, keyword string) (channels []*Channel, err error) {
err = DB.Omit("secret").Where("user_id = ?", userId).Where("id = ? or name LIKE ?", keyword, keyword+"%").Find(&channels).Error err = DB.Omit("secret").Where("user_id = ?", userId).Where("id = ? or name LIKE ?", keyword, keyword+"%").Find(&channels).Error
return channels, err return channels, err
+29 -20
View File
@@ -3,16 +3,17 @@ import { Button, Form, Grid, Header, Message } from 'semantic-ui-react';
import { API, showError, showSuccess } from '../helpers'; import { API, showError, showSuccess } from '../helpers';
const PushSetting = () => { const PushSetting = () => {
let [inputs, setInputs] = useState({ let [user, setUser] = useState({
id: '', id: '',
username: '', username: '',
channel: '', channel: '',
token: '', token: '',
}); });
let [channels, setChannels] = useState([]);
let [loading, setLoading] = useState(false); let [loading, setLoading] = useState(false);
const handleInputChange = (e, { name, value }) => { const handleInputChange = (e, { name, value }) => {
setInputs((inputs) => ({ ...inputs, [name]: value })); setUser((inputs) => ({ ...inputs, [name]: value }));
}; };
const loadUser = async () => { const loadUser = async () => {
@@ -25,23 +26,42 @@ const PushSetting = () => {
if (data.token === ' ') { if (data.token === ' ') {
data.token = ''; data.token = '';
} }
setInputs(data); setUser(data);
} else { } else {
showError(message); showError(message);
} }
setLoading(false); setLoading(false);
}; };
const loadUserChannels = async () => {
let res = await API.get(`/api/channel?brief=true`);
const { success, message, data } = res.data;
if (success) {
data.forEach((channel) => {
channel.key = channel.name;
channel.text = channel.name;
channel.value = channel.name;
if (channel.description === '') {
channel.description = '无备注信息';
}
});
setChannels(data);
} else {
showError(message);
}
};
useEffect(() => { useEffect(() => {
loadUser().then(); loadUser().then();
loadUserChannels().then();
}, []); }, []);
const submit = async (which) => { const submit = async (which) => {
let data = {}; let data = {};
switch (which) { switch (which) {
case 'general': case 'general':
data.channel = inputs.channel; data.channel = user.channel;
data.token = inputs.token; data.token = user.token;
if (data.token === '') { if (data.token === '') {
data.token = ' '; data.token = ' ';
} }
@@ -61,7 +81,7 @@ const PushSetting = () => {
const test = async () => { const test = async () => {
let res = await API.get( let res = await API.get(
`/push/${inputs.username}?token=${inputs.token}&channel=${inputs.channel}&title=消息推送服务&description=配置成功!` `/push/${user.username}?token=${user.token}&channel=${user.channel}&title=消息推送服务&description=配置成功!`
); );
const { success, message } = res.data; const { success, message } = res.data;
if (success) { if (success) {
@@ -83,25 +103,14 @@ const PushSetting = () => {
<Form.Select <Form.Select
label='默认推送方式' label='默认推送方式'
name='channel' name='channel'
options={[ options={channels}
{ key: 'email', text: '邮件', value: 'email' }, value={user.channel}
{ key: 'test', text: '微信测试号', value: 'test' },
{ key: 'corp_app', text: '企业微信应用号', value: 'corp_app' },
{ key: 'corp', text: '企业微信群机器人', value: 'corp' },
{ key: 'lark', text: '飞书群机器人', value: 'lark' },
{ key: 'ding', text: '钉钉群机器人', value: 'ding' },
{ key: 'bark', text: 'Bark App', value: 'bark' },
{ key: 'client', text: 'WebSocket 客户端', value: 'client' },
{ key: 'telegram', text: 'Telegram 机器人', value: 'telegram' },
{ key: 'discord', text: 'Discord 群机器人', value: 'discord' },
]}
value={inputs.channel}
onChange={handleInputChange} onChange={handleInputChange}
/> />
<Form.Input <Form.Input
label='推送 token' label='推送 token'
placeholder='未设置则不检查 token' placeholder='未设置则不检查 token'
value={inputs.token} value={user.token}
name='token' name='token'
onChange={handleInputChange} onChange={handleInputChange}
/> />