feat: 消息渠道

This commit is contained in:
wolter 2024-11-27 18:13:11 +08:00
parent 22dc1bbcc6
commit 9673f52efa
10 changed files with 112 additions and 10 deletions

View File

@ -10,4 +10,7 @@ var (
NotFound = &BusinessErr{code: 404, message: "未找到"} NotFound = &BusinessErr{code: 404, message: "未找到"}
SystemError = &BusinessErr{code: 500, message: "系统错误"} SystemError = &BusinessErr{code: 500, message: "系统错误"}
// 消息渠道
ReportChannelNotfound = &BusinessErr{code: 1000, message: "消息渠道不存在"}
) )

View File

@ -38,3 +38,16 @@ func ReportChannelList(c *gin.Context) {
Data: list, Data: list,
}, nil) }, nil)
} }
func ReportChannelUpdate(c *gin.Context) {
req, _ := controllers.GetRequest(c).(*backend.ReportChannelUpdateRequest)
reportChannel := req.Request2DB()
err := services.ReportChannelUpdate(&reportChannel)
controllers.HandRes(c, req, err)
}
func ReportChannelDelete(c *gin.Context) {
req, _ := controllers.GetRequest(c).(*entities.IdRequest)
err := services.ReportChannelDelete(*req)
controllers.HandRes(c, req, err)
}

View File

@ -15,6 +15,7 @@ import (
zh_translations "gopkg.in/go-playground/validator.v9/translations/zh" zh_translations "gopkg.in/go-playground/validator.v9/translations/zh"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"reflect"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -44,7 +45,7 @@ func Error500(c *gin.Context) {
*/ */
func GenRequest(c *gin.Context, request interface{}) (msgs []string, err error) { func GenRequest(c *gin.Context, request interface{}) (msgs []string, err error) {
if c.Request.Method == "GET" || c.Request.Method == "DELETE" { if c.Request.Method == "GET" || c.Request.Method == "DELETE" {
err = c.ShouldBindQuery(request) err = c.ShouldBind(request)
} else { } else {
err = c.ShouldBindJSON(request) err = c.ShouldBindJSON(request)
} }
@ -53,6 +54,12 @@ func GenRequest(c *gin.Context, request interface{}) (msgs []string, err error)
validate := validator.New() validate := validator.New()
zh_ch := zh.New() zh_ch := zh.New()
//注册一个函数获取struct tag里自定义的label作为字段名
validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
name := fld.Tag.Get("label")
return name
})
uni := ut.New(zh_ch) uni := ut.New(zh_ch)
trans, _ := uni.GetTranslator("zh") trans, _ := uni.GetTranslator("zh")
//验证器注册翻译器 //验证器注册翻译器

View File

@ -7,8 +7,8 @@ import (
type ReportChannelListRequest struct { type ReportChannelListRequest struct {
entities.PageRequest entities.PageRequest
ReportChannelId int `json:"report_channel_id" validate:"min=0"` ReportChannelId int `json:"report_channel_id" form:"report_channel_id" validate:"min=0"`
Status int `json:"status"` Status int `json:"status" form:"status"`
} }
type ReportChannelListResponse struct { type ReportChannelListResponse struct {
@ -30,10 +30,10 @@ func (this *ReportChannelListResponse) FromDb(in cronreportchannelmodel.CronRepo
} }
type ReportChannelCreateRequest struct { type ReportChannelCreateRequest struct {
ClientKey string `json:"client_key"` ClientKey string `json:"client_key" validate:"required" label:"服务key"`
ClientSecret string `json:"client_secret"` ClientSecret string `json:"client_secret" validate:"required" label:"服务secret"`
Config string `json:"config"` Config string `json:"config" validate:"required" label:"服务配置"`
Status int `json:"status"` Status int `json:"status" validate:"required" label:"状态"`
} }
func (this *ReportChannelCreateRequest) Request2DB() (db cronreportchannelmodel.CronReportChannel) { func (this *ReportChannelCreateRequest) Request2DB() (db cronreportchannelmodel.CronReportChannel) {
@ -43,3 +43,20 @@ func (this *ReportChannelCreateRequest) Request2DB() (db cronreportchannelmodel.
db.Status = this.Status db.Status = this.Status
return db return db
} }
type ReportChannelUpdateRequest struct {
ReportChannelId uint `json:"report_channel_id"`
ClientKey string `json:"client_key" validate:"required" label:"配置key"`
ClientSecret string `json:"client_secret" validate:"required" label:"配置密钥"`
Config string `json:"config" validate:"required" label:"配置"`
Status int `json:"status" validate:"required" label:"状态"`
}
func (this *ReportChannelUpdateRequest) Request2DB() (db cronreportchannelmodel.CronReportChannel) {
db.ReportChannelId = this.ReportChannelId
db.ClientKey = this.ClientKey
db.ClientSecret = this.ClientSecret
db.Config = this.Config
db.Status = this.Status
return db
}

View File

@ -1,7 +1,7 @@
package entities package entities
type IdRequest struct { type IdRequest struct {
Id int64 `json:"id"` Id uint `json:"id"`
} }
type PageRequest struct { type PageRequest struct {

View File

@ -2,6 +2,7 @@ package requestmapping
import ( import (
"cron_admin/app/constants/common" "cron_admin/app/constants/common"
"cron_admin/app/http/entities"
"cron_admin/app/http/entities/backend" "cron_admin/app/http/entities/backend"
) )
@ -11,4 +12,10 @@ var BackendRequestMap = map[string]func() interface{}{
common.ADMIN_OAUTH_V1 + "/sql/add": func() interface{} { return new(backend.DbAddRequest) }, common.ADMIN_OAUTH_V1 + "/sql/add": func() interface{} { return new(backend.DbAddRequest) },
common.ADMIN_OAUTH_V1 + "/sql/edit": func() interface{} { return new(backend.DbEditRequest) }, common.ADMIN_OAUTH_V1 + "/sql/edit": func() interface{} { return new(backend.DbEditRequest) },
common.ADMIN_OAUTH_V1 + "/sql/del": func() interface{} { return new(backend.DbDeleteRequest) }, common.ADMIN_OAUTH_V1 + "/sql/del": func() interface{} { return new(backend.DbDeleteRequest) },
// 消息渠道
common.ADMIN_OAUTH_V1 + "/channel/create": func() interface{} { return new(backend.ReportChannelCreateRequest) },
common.ADMIN_OAUTH_V1 + "/channel/list": func() interface{} { return new(backend.ReportChannelListRequest) },
common.ADMIN_OAUTH_V1 + "/channel/update": func() interface{} { return new(backend.ReportChannelUpdateRequest) },
common.ADMIN_OAUTH_V1 + "/channel/delete": func() interface{} { return new(entities.IdRequest) },
} }

View File

@ -56,6 +56,8 @@ func RegisterAdminRoute(router *gin.Engine) {
{ {
mes.GET("/list", backend.ReportChannelList) mes.GET("/list", backend.ReportChannelList)
mes.POST("/create", backend.ReportChannelCreate) mes.POST("/create", backend.ReportChannelCreate)
mes.POST("/delete", backend.ReportChannelDelete)
mes.POST("/update", backend.ReportChannelUpdate)
} }
//日志 //日志

View File

@ -17,9 +17,10 @@ type CronReportChannel struct {
ClientKey string `xorm:"'client_key' varchar(20)"` ClientKey string `xorm:"'client_key' varchar(20)"`
ClientSecret string `xorm:"'client_secret' varchar(50)"` ClientSecret string `xorm:"'client_secret' varchar(50)"`
Config string `xorm:"'config' JSON"` Config string `xorm:"'config' JSON"`
Status int `xorm:"'status' TINYINT"`
CreateTime time.Time `xorm:"'create_time' created timestamp"` CreateTime time.Time `xorm:"'create_time' created timestamp"`
UpdateTime time.Time `xorm:"'update_time' updated timestamp"` UpdateTime time.Time `xorm:"'update_time' updated timestamp"`
Status int `xorm:"'status' TINYINT"` DeletedTime time.Time `xorm:"'deleted_time' deleted datetime"`
} }
// 表名 // 表名

View File

@ -0,0 +1,22 @@
package repository
import (
"cron_admin/app/models/cronreportchannelmodel"
"xorm.io/xorm"
)
type ReportChannel struct {
ICommonRepo[cronreportchannelmodel.CronReportChannel]
}
func NewReportChannelRepo() *ReportChannel {
return &ReportChannel{
ICommonRepo: NewCommonRepo[cronreportchannelmodel.CronReportChannel](),
}
}
func (c *ReportChannel) WithByID(id uint) DBOption {
return func(g *xorm.Session) *xorm.Session {
return g.Where("report_channel_id = ?", id)
}
}

View File

@ -1,6 +1,8 @@
package services package services
import ( import (
"cron_admin/app/constants/errorcode"
"cron_admin/app/http/entities"
"cron_admin/app/http/entities/backend" "cron_admin/app/http/entities/backend"
"cron_admin/app/models/cronreportchannelmodel" "cron_admin/app/models/cronreportchannelmodel"
"cron_admin/app/repository" "cron_admin/app/repository"
@ -17,7 +19,7 @@ func ReportChannelCreate(param *cronreportchannelmodel.CronReportChannel) (err e
func ReportChannelList(param backend.ReportChannelListRequest) (list []cronreportchannelmodel.CronReportChannel, total int64, err error) { func ReportChannelList(param backend.ReportChannelListRequest) (list []cronreportchannelmodel.CronReportChannel, total int64, err error) {
var ( var (
repo = repository.NewCommonRepo[cronreportchannelmodel.CronReportChannel]() repo = repository.NewReportChannelRepo()
opts = make([]repository.DBOption, 0) opts = make([]repository.DBOption, 0)
) )
@ -34,3 +36,31 @@ func ReportChannelList(param backend.ReportChannelListRequest) (list []cronrepor
total, err = repo.FindAndCount(&list, opts...) total, err = repo.FindAndCount(&list, opts...)
return return
} }
func ReportChannelUpdate(param *cronreportchannelmodel.CronReportChannel) (err error) {
var (
repo = repository.NewCommonRepo[cronreportchannelmodel.CronReportChannel]()
opts = make([]repository.DBOption, 0)
)
if param.ReportChannelId > 0 {
opts = append(opts, repo.WithByID(param.ReportChannelId))
}
_, err = repo.Update(param, opts...)
return
}
func ReportChannelDelete(param entities.IdRequest) (err error) {
var (
repo = repository.NewCommonRepo[cronreportchannelmodel.CronReportChannel]()
opts = make([]repository.DBOption, 0)
)
if param.Id > 0 {
opts = append(opts, repo.WithByID(param.Id))
} else {
return errorcode.ReportChannelNotfound
}
_, err = repo.Delete(new(cronreportchannelmodel.CronReportChannel), opts...)
return
}