diff --git a/controller/channel.go b/controller/channel.go index b49f4d0..6e60384 100644 --- a/controller/channel.go +++ b/controller/channel.go @@ -11,6 +11,10 @@ import ( ) func GetAllChannels(c *gin.Context) { + if c.Query("brief") != "" { + GetBriefChannels(c) + return + } userId := c.GetInt("id") p, _ := strconv.Atoi(c.Query("p")) if p < 0 { @@ -32,6 +36,24 @@ func GetAllChannels(c *gin.Context) { 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) { userId := c.GetInt("id") keyword := c.Query("keyword") diff --git a/model/channel.go b/model/channel.go index 77d2cbe..f381170 100644 --- a/model/channel.go +++ b/model/channel.go @@ -2,6 +2,7 @@ package model import ( "errors" + "message-pusher/common" ) const ( @@ -22,7 +23,7 @@ const ( type Channel struct { Id int `json:"id"` 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"` Description string `json:"description"` Status int `json:"status" gorm:"default:1"` // enabled, disabled @@ -34,6 +35,12 @@ type Channel struct { 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) { if id == 0 || userId == 0 { return nil, errors.New("id 或 userId 为空!") @@ -67,6 +74,11 @@ func GetChannelsByUserId(userId int, startIdx int, num int) (channels []*Channel 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) { err = DB.Omit("secret").Where("user_id = ?", userId).Where("id = ? or name LIKE ?", keyword, keyword+"%").Find(&channels).Error return channels, err diff --git a/web/src/components/PushSetting.js b/web/src/components/PushSetting.js index e8f36ec..a4b98b8 100644 --- a/web/src/components/PushSetting.js +++ b/web/src/components/PushSetting.js @@ -3,16 +3,17 @@ import { Button, Form, Grid, Header, Message } from 'semantic-ui-react'; import { API, showError, showSuccess } from '../helpers'; const PushSetting = () => { - let [inputs, setInputs] = useState({ + let [user, setUser] = useState({ id: '', username: '', channel: '', token: '', }); + let [channels, setChannels] = useState([]); let [loading, setLoading] = useState(false); const handleInputChange = (e, { name, value }) => { - setInputs((inputs) => ({ ...inputs, [name]: value })); + setUser((inputs) => ({ ...inputs, [name]: value })); }; const loadUser = async () => { @@ -25,23 +26,42 @@ const PushSetting = () => { if (data.token === ' ') { data.token = ''; } - setInputs(data); + setUser(data); } else { showError(message); } 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(() => { loadUser().then(); + loadUserChannels().then(); }, []); const submit = async (which) => { let data = {}; switch (which) { case 'general': - data.channel = inputs.channel; - data.token = inputs.token; + data.channel = user.channel; + data.token = user.token; if (data.token === '') { data.token = ' '; } @@ -61,7 +81,7 @@ const PushSetting = () => { const test = async () => { 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; if (success) { @@ -83,25 +103,14 @@ const PushSetting = () => {