feat(notification): 添加通知方式

This commit is contained in:
naiba
2020-12-19 22:14:36 +08:00
parent b118958f35
commit d19de0edc2
16 changed files with 391 additions and 58 deletions

View File

@@ -1,6 +1,7 @@
package controller
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
@@ -30,8 +31,10 @@ func (ma *memberAPI) serve() {
mr.POST("/logout", ma.logout)
mr.POST("/server", ma.addOrEditServer)
mr.POST("/notification", ma.addOrEditNotification)
mr.POST("/setting", ma.updateSetting)
mr.DELETE("/server/:id", ma.delete)
mr.DELETE("/notification/:id", ma.deleteNotification)
}
func (ma *memberAPI) delete(c *gin.Context) {
@@ -58,6 +61,27 @@ func (ma *memberAPI) delete(c *gin.Context) {
})
}
func (ma *memberAPI) deleteNotification(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
if id < 1 {
c.JSON(http.StatusOK, model.Response{
Code: http.StatusBadRequest,
Message: "错误的 Notification ID",
})
return
}
if err := dao.DB.Delete(&model.Notification{}, "id = ?", id).Error; err != nil {
c.JSON(http.StatusOK, model.Response{
Code: http.StatusBadRequest,
Message: fmt.Sprintf("数据库错误:%s", err),
})
return
}
c.JSON(http.StatusOK, model.Response{
Code: http.StatusOK,
})
}
type serverForm struct {
ID uint64
Name string `binding:"required"`
@@ -96,6 +120,51 @@ func (ma *memberAPI) addOrEditServer(c *gin.Context) {
})
}
type notificationForm struct {
ID uint64
Name string
URL string
RequestMethod int
RequestType int
RequestBody string
VerifySSL string
}
func (ma *memberAPI) addOrEditNotification(c *gin.Context) {
var nf notificationForm
var n model.Notification
err := c.ShouldBindJSON(&nf)
if err == nil {
var data map[string]string
err = json.Unmarshal([]byte(nf.RequestBody), &data)
}
if err == nil {
n.Name = nf.Name
n.RequestMethod = nf.RequestMethod
n.RequestType = nf.RequestType
n.RequestBody = nf.RequestBody
n.URL = nf.URL
verifySSL := nf.VerifySSL == "on"
n.VerifySSL = &verifySSL
n.ID = nf.ID
if n.ID == 0 {
err = dao.DB.Create(&n).Error
} else {
err = dao.DB.Save(&n).Error
}
}
if err != nil {
c.JSON(http.StatusOK, model.Response{
Code: http.StatusBadRequest,
Message: fmt.Sprintf("请求错误:%s", err),
})
return
}
c.JSON(http.StatusOK, model.Response{
Code: http.StatusOK,
})
}
type logoutForm struct {
ID uint64
}

View File

@@ -4,6 +4,7 @@ import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/naiba/nezha/model"
"github.com/naiba/nezha/pkg/mygin"
"github.com/naiba/nezha/service/dao"
)
@@ -22,6 +23,7 @@ func (mp *memberPage) serve() {
Redirect: "/login",
}))
mr.GET("/server", mp.server)
mr.GET("/notification", mp.notification)
mr.GET("/setting", mp.setting)
}
@@ -34,6 +36,15 @@ func (mp *memberPage) server(c *gin.Context) {
}))
}
func (mp *memberPage) notification(c *gin.Context) {
var nf []model.Notification
dao.DB.Find(&nf)
c.HTML(http.StatusOK, "dashboard/notification", mygin.CommonEnvironment(c, gin.H{
"Title": "通知管理",
"Notifications": nf,
}))
}
func (mp *memberPage) setting(c *gin.Context) {
c.HTML(http.StatusOK, "dashboard/setting", mygin.CommonEnvironment(c, gin.H{
"Title": "系统设置",