æ·feat: commplete init process

This commit is contained in:
engigu
2023-12-30 17:40:20 +08:00
parent 9307bec4ab
commit 77af985b76
92 changed files with 12054 additions and 2 deletions
+69
View File
@@ -0,0 +1,69 @@
package api
import (
"net/http"
"github.com/astaxie/beego/validation"
"github.com/gin-gonic/gin"
"message-nest/pkg/app"
"message-nest/pkg/e"
"message-nest/pkg/util"
"message-nest/service/auth_service"
)
type auth struct {
Username string `valid:"Required; MaxSize(50)"`
Password string `valid:"Required; MaxSize(50)"`
}
type ReqAuth struct {
Username string `json:"username"`
Password string `json:"passwd"`
}
func GetAuth(c *gin.Context) {
appG := app.Gin{C: c}
valid := validation.Validation{}
var user ReqAuth
err := c.ShouldBindJSON(&user)
if err != nil {
app.MarkErrors(valid.Errors)
appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
return
}
username := user.Username
password := user.Password
a := auth{Username: username, Password: password}
ok, _ := valid.Valid(&a)
if !ok {
app.MarkErrors(valid.Errors)
appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
return
}
authService := auth_service.Auth{Username: username, Password: password}
isExist, err := authService.Check()
if err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR_AUTH_CHECK_TOKEN_FAIL, nil)
return
}
if !isExist {
appG.Response(http.StatusUnauthorized, e.ERROR_AUTH, nil)
return
}
token, err := util.GenerateToken(username, password)
if err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR_AUTH_TOKEN, nil)
return
}
appG.Response(http.StatusOK, e.SUCCESS, map[string]string{
"token": token,
})
}
+179
View File
@@ -0,0 +1,179 @@
package v1
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"message-nest/models"
"message-nest/pkg/app"
"message-nest/pkg/e"
"message-nest/service/send_ins_service"
"message-nest/service/send_task_service"
"net/http"
)
type DeleteMsgTaskInsReq struct {
ID string `json:"id" validate:"required,len=36" label:"实例id"`
}
// DeleteMsgSendWay 删除消息渠道
func DeleteMsgTaskIns(c *gin.Context) {
var (
appG = app.Gin{C: c}
req DeleteMsgTaskInsReq
)
errCode, errMsg := app.BindJsonAndPlayValid(c, &req)
if errCode != e.SUCCESS {
appG.CResponse(errCode, errMsg, nil)
return
}
InsService := send_ins_service.SendTaskInsService{
ID: req.ID,
}
err := InsService.Delete()
if err != nil {
appG.CResponse(http.StatusBadRequest, "删除实例失败!", nil)
return
}
appG.CResponse(http.StatusOK, "删除实例成功!", nil)
}
// GetMsgSendWayIns 获取消息任务实例
func GetMsgSendWayIns(c *gin.Context) {
appG := app.Gin{C: c}
id := c.Query("id")
if id == "" {
appG.CResponse(http.StatusBadRequest, "任务id为空!", nil)
return
}
sendTaskService := send_task_service.SendTaskService{
ID: id,
}
task, err := sendTaskService.GetTaskWithIns()
if err != nil {
appG.CResponse(http.StatusBadRequest, "获取到的任务信息为空!", nil)
return
}
appG.CResponse(http.StatusOK, "获取任务信息成功", task)
}
type SendTasksInsReq struct {
ID string `json:"id" validate:"required,len=36" label:"实例id"`
TaskId string `json:"task_id" validate:"required,len=36" label:"ins任务id"`
WayID string `json:"way_id" validate:"required,len=36" label:"渠道id"`
ContentType string `json:"content_type" validate:"required,max=100" label:"实例内容类型"`
Config string `json:"config" validate:"" label:"任务配置"`
Extra string `json:"extra" validate:"" label:"任务额外信息"`
//WayName string `json:"way_name" validate:"required,max=100" label:"渠道名"`
WayType string `json:"way_type" validate:"required,max=100" label:"渠道类型"`
}
type AddManyTasksInsReq struct {
TaskId string `json:"id" validate:"required,len=36" label:"任务id"`
TaskName string `json:"name" validate:"required,max=100" label:"任务名"`
InsData []SendTasksInsReq `json:"ins_data"`
}
// AddManyTasksIns 添加发送任务关联的实例id
func AddManyTasksIns(c *gin.Context) {
var (
appG = app.Gin{C: c}
req AddManyTasksInsReq
)
currentUser := app.GetCurrentUserName(c)
errCode, errStr := app.BindJsonAndPlayValid(c, &req)
if errCode != e.SUCCESS {
appG.CResponse(errCode, errStr, nil)
return
}
// 校验提交的各个实例信息
var taskIns []models.SendTasksIns
if len(req.InsData) > 0 {
for _, data := range req.InsData {
code, dataErrStr := app.CommonPlaygroundValid(data)
if code != http.StatusOK {
appG.CResponse(code, dataErrStr, nil)
return
}
uuidObj, _ := uuid.Parse(data.ID)
taskIns = append(taskIns, models.SendTasksIns{
UUIDModel: models.UUIDModel{
ID: uuidObj,
CreatedBy: currentUser,
ModifiedBy: currentUser,
},
TaskID: data.TaskId,
WayID: data.WayID,
WayType: data.WayType,
ContentType: data.ContentType,
Config: data.Config,
Extra: data.Extra,
})
}
}
sendTaskInsService := send_ins_service.SendTaskInsService{}
err := sendTaskInsService.ManyAdd(taskIns)
if err != "" {
appG.CResponse(http.StatusBadRequest, fmt.Sprintf("添加实例失败!错误原因:%s", err), nil)
return
}
sendTaskService := send_task_service.SendTaskService{
ID: req.TaskId,
Name: req.TaskName,
CreatedBy: currentUser,
}
errAdd := sendTaskService.AddWithID()
if errAdd != nil {
appG.CResponse(http.StatusBadRequest, fmt.Sprintf("添加任务失败!错误原因:%s", err), nil)
return
}
appG.CResponse(http.StatusOK, "添加实例成功!", nil)
}
// AddTasksIns 添加发送任务关联的实例id
func AddTasksIns(c *gin.Context) {
var (
appG = app.Gin{C: c}
req SendTasksInsReq
)
//currentUser := app.GetCurrentUserName(c)
errCode, errStr := app.BindJsonAndPlayValid(c, &req)
if errCode != e.SUCCESS {
appG.CResponse(errCode, errStr, nil)
return
}
sendTaskInsService := send_ins_service.SendTaskInsService{}
uuidObj, _ := uuid.Parse(req.ID)
err := sendTaskInsService.AddOne(models.SendTasksIns{
UUIDModel: models.UUIDModel{ID: uuidObj},
TaskID: req.TaskId,
WayID: req.WayID,
WayType: req.WayType,
ContentType: req.ContentType,
Config: req.Config,
Extra: req.Extra,
//Cre: req.Extra,
})
if err != "" {
appG.CResponse(http.StatusBadRequest, fmt.Sprintf("添加实例失败!错误原因:%s", err), nil)
return
}
appG.CResponse(http.StatusOK, "添加实例成功!", nil)
}
+44
View File
@@ -0,0 +1,44 @@
package v1
import (
"github.com/gin-gonic/gin"
"message-nest/pkg/app"
"message-nest/pkg/e"
"message-nest/service/send_message_service"
"net/http"
)
type SendMessageReq struct {
TaskID string `json:"task_id" validate:"required,len=36" label:"任务id"`
Text string `json:"text" validate:"required" label:"文本内容"`
HTML string `json:"html" label:"html内容"`
MarkDown string `json:"markdown" label:"markdown内容"`
}
// DoSendMassage 外部调用发信接口
func DoSendMassage(c *gin.Context) {
var (
appG = app.Gin{C: c}
req SendMessageReq
)
errCode, errMsg := app.BindJsonAndPlayValid(c, &req)
if errCode != e.SUCCESS {
appG.CResponse(errCode, errMsg, nil)
return
}
msgService := send_message_service.SendMessageService{
TaskID: req.TaskID,
Text: req.Text,
HTML: req.HTML,
MarkDown: req.MarkDown,
}
err := msgService.Send()
if err != "" {
appG.CResponse(http.StatusBadRequest, "发送失败!", nil)
return
}
appG.CResponse(http.StatusOK, "发送成功!", nil)
}
+103
View File
@@ -0,0 +1,103 @@
package v1
import (
"message-nest/pkg/e"
"net/http"
"github.com/gin-gonic/gin"
"message-nest/pkg/app"
"message-nest/pkg/util"
"message-nest/service/send_task_service"
)
type DeleteMsgSendTaskReq struct {
ID string `json:"id" validate:"required,len=36" label:"任务id"`
}
// DeleteMsgSendTask 删除消息任务
func DeleteMsgSendTask(c *gin.Context) {
var (
appG = app.Gin{C: c}
req DeleteMsgSendTaskReq
)
errCode, errMsg := app.BindJsonAndPlayValid(c, &req)
if errCode != e.SUCCESS {
appG.CResponse(errCode, errMsg, nil)
return
}
MsgSendTaskService := send_task_service.SendTaskService{
ID: req.ID,
}
err := MsgSendTaskService.Delete()
if err != nil {
appG.CResponse(http.StatusBadRequest, "删除发信任务失败!", nil)
return
}
appG.CResponse(http.StatusOK, "删除发信任务成功!", nil)
}
// GetMsgSendTaskList 获取消息任务列表
func GetMsgSendTaskList(c *gin.Context) {
appG := app.Gin{C: c}
name := c.Query("name")
offset, limit := util.GetPageSize(c)
MsgSendTaskService := send_task_service.SendTaskService{
Name: name,
PageNum: offset,
PageSize: limit,
}
tasks, err := MsgSendTaskService.GetAll()
if err != nil {
appG.CResponse(http.StatusInternalServerError, "获取任务任务失败!", nil)
return
}
count, err := MsgSendTaskService.Count()
if err != nil {
appG.CResponse(http.StatusInternalServerError, "获取任务任务总数失败!", nil)
return
}
appG.CResponse(http.StatusOK, "获取任务任务成功", map[string]interface{}{
"lists": tasks,
"total": count,
})
}
type AddMsgSendTaskReq struct {
Name string `json:"name" validate:"required,max=100,min=1" label:"任务任务名"`
}
// AddMsgSendTask 添加发送任务
func AddMsgSendTask(c *gin.Context) {
var (
appG = app.Gin{C: c}
req AddMsgSendTaskReq
)
currentUser := app.GetCurrentUserName(c)
errCode, errStr := app.BindJsonAndPlayValid(c, &req)
if errCode != e.SUCCESS {
appG.CResponse(errCode, errStr, nil)
return
}
MsgSendTaskService := send_task_service.SendTaskService{
Name: req.Name,
CreatedBy: currentUser,
ModifiedBy: currentUser,
}
err := MsgSendTaskService.Add()
if err != nil {
appG.CResponse(http.StatusBadRequest, "添加任务任务失败!", nil)
return
}
appG.CResponse(http.StatusOK, "添加任务任务成功!", nil)
}
+40
View File
@@ -0,0 +1,40 @@
package v1
import (
"github.com/gin-gonic/gin"
"message-nest/pkg/app"
"message-nest/pkg/util"
"message-nest/service/send_logs_service"
"net/http"
)
// GetMsgSendWayList 获取消息渠道列表
func GetTaskSendLogsList(c *gin.Context) {
appG := app.Gin{C: c}
name := c.Query("name")
taskId := c.Query("taskid")
offset, limit := util.GetPageSize(c)
logsService := send_logs_service.SendTaskLogsService{
TaskId: taskId,
Name: name,
PageNum: offset,
PageSize: limit,
}
ways, err := logsService.GetAll()
if err != nil {
appG.CResponse(http.StatusInternalServerError, "获取日志失败!", nil)
return
}
count, err := logsService.Count()
if err != nil {
appG.CResponse(http.StatusInternalServerError, "获取日志总数失败!", nil)
return
}
appG.CResponse(http.StatusOK, "获取日志成功", map[string]interface{}{
"lists": ways,
"total": count,
})
}
+222
View File
@@ -0,0 +1,222 @@
package v1
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
"message-nest/pkg/app"
"message-nest/pkg/e"
"message-nest/pkg/util"
"message-nest/service/send_way_service"
)
type DeleteMsgSendWayReq struct {
ID string `json:"id" validate:"required,len=36" label:"渠道id"`
}
// DeleteMsgSendWay 删除消息渠道
func DeleteMsgSendWay(c *gin.Context) {
var (
appG = app.Gin{C: c}
req DeleteMsgSendWayReq
)
errCode, errMsg := app.BindJsonAndPlayValid(c, &req)
if errCode != e.SUCCESS {
appG.CResponse(errCode, errMsg, nil)
return
}
sendWayService := send_way_service.SendWay{
ID: req.ID,
}
err := sendWayService.Delete()
if err != nil {
appG.CResponse(http.StatusBadRequest, fmt.Sprintf("删除发信渠道失败!%s", err), nil)
return
}
appG.CResponse(http.StatusOK, "删除发信渠道成功!", nil)
}
// GetMsgSendWay 获取消息渠道
func GetMsgSendWay(c *gin.Context) {
appG := app.Gin{C: c}
id := c.Query("id")
if id == "" {
appG.CResponse(http.StatusBadRequest, "渠道id为空!", nil)
return
}
sendWayService := send_way_service.SendWay{
ID: id,
}
way, err := sendWayService.GetByID()
if err != nil {
appG.CResponse(http.StatusBadRequest, "获取到的渠道信息为空!", nil)
return
}
appG.CResponse(http.StatusOK, "获取渠道信息成功", way)
}
// GetMsgSendWayList 获取消息渠道列表
func GetMsgSendWayList(c *gin.Context) {
appG := app.Gin{C: c}
name := c.Query("name")
type_ := c.Query("type")
offset, limit := util.GetPageSize(c)
sendWayService := send_way_service.SendWay{
Name: name,
Type: type_,
PageNum: offset,
PageSize: limit,
}
ways, err := sendWayService.GetAll()
if err != nil {
appG.CResponse(http.StatusInternalServerError, "获取渠道信息失败!", nil)
return
}
count, err := sendWayService.Count()
if err != nil {
appG.CResponse(http.StatusInternalServerError, "获取渠道信息总数失败!", nil)
return
}
appG.CResponse(http.StatusOK, "获取渠道信息成功", map[string]interface{}{
"lists": ways,
"total": count,
})
}
type AddMsgSendWayReq struct {
Name string `json:"name" validate:"required,max=100,min=1" label:"渠道名"`
Type string `json:"type" validate:"required,max=100,min=1" label:"渠道类型"`
Auth string `json:"auth" validate:"required,max=2048,min=6" label:"渠道认证方式"`
}
// AddMsgSendWay 添加发送渠道
func AddMsgSendWay(c *gin.Context) {
var (
appG = app.Gin{C: c}
req AddMsgSendWayReq
)
currentUser := app.GetCurrentUserName(c)
errCode, errStr := app.BindJsonAndPlayValid(c, &req)
if errCode != e.SUCCESS {
appG.CResponse(errCode, errStr, nil)
return
}
sendWayService := send_way_service.SendWay{
Name: req.Name,
Type: req.Type,
Auth: req.Auth,
CreatedBy: currentUser,
ModifiedBy: currentUser,
}
diffMsg, _ := sendWayService.ValidateDiffWay()
if diffMsg != "" {
appG.CResponse(http.StatusBadRequest, diffMsg, nil)
return
}
err := sendWayService.Add()
if err != nil {
appG.CResponse(http.StatusBadRequest, "添加渠道失败!", nil)
return
}
appG.CResponse(http.StatusOK, "添加渠道成功!", nil)
}
type EditSendWayReq struct {
ID string `json:"id" validate:"required,len=36" label:"渠道id"`
Name string `json:"name" validate:"required,max=100,min=1" label:"渠道名"`
Type string `json:"type" validate:"required,max=100,min=1" label:"渠道类型"`
Auth string `json:"auth" validate:"required" label:"渠道认证信息"`
}
// EditSendWay 编辑发送渠道
func EditSendWay(c *gin.Context) {
var (
appG = app.Gin{C: c}
req = EditSendWayReq{}
)
currentUser := app.GetCurrentUserName(c)
errCode, errMsg := app.BindJsonAndPlayValid(c, &req)
if errCode != e.SUCCESS {
appG.CResponse(errCode, errMsg, nil)
return
}
sendWayService := send_way_service.SendWay{
ID: req.ID,
Name: req.Name,
Type: req.Type,
Auth: req.Auth,
ModifiedBy: currentUser,
}
diffMsg, _ := sendWayService.ValidateDiffWay()
if diffMsg != "" {
appG.CResponse(http.StatusBadRequest, diffMsg, nil)
return
}
err := sendWayService.Edit()
if err != nil {
appG.CResponse(http.StatusInternalServerError, "编辑渠道信息失败", nil)
return
}
appG.CResponse(http.StatusOK, "编辑渠道信息成功", nil)
}
type TestSendWayReq struct {
Name string `json:"name" validate:"required,max=100,min=1" label:"渠道名"`
Type string `json:"type" validate:"required,max=100,min=1" label:"渠道类型"`
Auth string `json:"auth" validate:"required" label:"渠道认证信息"`
}
// TestSendWay 测试发送渠道
func TestSendWay(c *gin.Context) {
var (
appG = app.Gin{C: c}
req = TestSendWayReq{}
)
errCode, errMsg := app.BindJsonAndPlayValid(c, &req)
if errCode != e.SUCCESS {
appG.CResponse(errCode, errMsg, nil)
return
}
sendWayService := send_way_service.SendWay{
Name: req.Name,
Type: req.Type,
Auth: req.Auth,
}
diffMsg, msgObj := sendWayService.ValidateDiffWay()
if diffMsg != "" {
appG.CResponse(http.StatusBadRequest, diffMsg, nil)
return
}
errTestMsg := sendWayService.TestSendWay(msgObj)
if errTestMsg != "" {
appG.CResponse(http.StatusInternalServerError, errTestMsg, nil)
return
}
appG.CResponse(http.StatusOK, "测试渠道信息成功", nil)
}
+44
View File
@@ -0,0 +1,44 @@
package v1
import (
"fmt"
"github.com/gin-gonic/gin"
"message-nest/pkg/app"
"message-nest/pkg/e"
"message-nest/service/settings_service"
"net/http"
)
type EditUserPasswd struct {
OldPassword string `json:"old_passwd" validate:"required,max=50" label:"旧密码"`
NewPassword string `json:"new_passwd" validate:"required,max=50" label:"新密码"`
}
// EditPasswd 用户重设密码
func EditPasswd(c *gin.Context) {
var (
appG = app.Gin{C: c}
req EditUserPasswd
)
currentUser := app.GetCurrentUserName(c)
errCode, errStr := app.BindJsonAndPlayValid(c, &req)
if errCode != e.SUCCESS {
appG.CResponse(errCode, errStr, nil)
return
}
sendTaskService := settings_service.UserSettings{
UserName: currentUser,
OldPassword: req.OldPassword,
NewPassword: req.NewPassword,
}
err := sendTaskService.EditUserPasswd()
if err != nil {
appG.CResponse(http.StatusBadRequest, fmt.Sprintf("修改密码失败!错误原因:%s", err), nil)
return
}
appG.CResponse(http.StatusOK, "修改成功!", nil)
}
+53
View File
@@ -0,0 +1,53 @@
package routers
import (
"github.com/gin-gonic/gin"
"message-nest/middleware"
"message-nest/routers/api"
"message-nest/routers/api/v1"
)
// InitRouter initialize routing information
func InitRouter() *gin.Engine {
r := gin.New()
r.Use(gin.Logger())
r.Use(gin.Recovery())
r.Use(middleware.Cors())
r.POST("/auth", api.GetAuth)
apiv1 := r.Group("/api/v1")
apiv1.Use(middleware.JWT())
{
// sendways
apiv1.POST("/sendways/add", v1.AddMsgSendWay)
apiv1.POST("/sendways/delete", v1.DeleteMsgSendWay)
apiv1.POST("/sendways/edit", v1.EditSendWay)
apiv1.POST("/sendways/test", v1.TestSendWay)
apiv1.GET("/sendways/list", v1.GetMsgSendWayList)
apiv1.GET("/sendways/get", v1.GetMsgSendWay)
// sendtasks
apiv1.GET("/sendtasks/list", v1.GetMsgSendTaskList)
apiv1.POST("/sendtasks/add", v1.AddMsgSendTask)
apiv1.POST("/sendtasks/delete", v1.DeleteMsgSendTask)
// sendtasks/ins
apiv1.POST("/sendtasks/ins/addmany", v1.AddManyTasksIns)
apiv1.POST("/sendtasks/ins/addone", v1.AddTasksIns)
apiv1.GET("/sendtasks/ins/gettask", v1.GetMsgSendWayIns)
apiv1.POST("/sendtasks/ins/delete", v1.DeleteMsgTaskIns)
// message/send
apiv1.POST("/message/send", v1.DoSendMassage)
apiv1.GET("/sendlogs/list", v1.GetTaskSendLogsList)
// settings
apiv1.POST("/settings/setpasswd", v1.EditPasswd)
}
return r
}