æ·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
+12
View File
@@ -0,0 +1,12 @@
package auth_service
import "message-nest/models"
type Auth struct {
Username string
Password string
}
func (a *Auth) Check() (bool, error) {
return models.CheckAuth(a.Username, a.Password)
}
+82
View File
@@ -0,0 +1,82 @@
package send_ins_service
import (
"encoding/json"
"fmt"
"message-nest/models"
"message-nest/pkg/app"
)
type SendTaskInsService struct {
ID string
Name string
CreatedBy string
ModifiedBy string
CreatedOn string
PageNum int
PageSize int
}
// ValidateDiffWay 各种发信渠道具体字段校验
func (sw *SendTaskInsService) ValidateDiffIns(ins models.SendTasksIns) (string, interface{}) {
var empty interface{}
if ins.WayType == "Email" {
var emailConfig models.InsEmailConfig
err := json.Unmarshal([]byte(ins.Config), &emailConfig)
if err != nil {
return "邮箱auth反序列化失败!", empty
}
_, Msg := app.CommonPlaygroundValid(emailConfig)
return Msg, emailConfig
}
return "", empty
}
func (st *SendTaskInsService) ManyAdd(taskIns []models.SendTasksIns) string {
for _, ins := range taskIns {
errStr, _ := st.ValidateDiffIns(ins)
if errStr != "" {
return errStr
}
}
err := models.ManyAddTaskIns(taskIns)
if err != nil {
return fmt.Sprintf("%s", err)
}
return ""
}
func (st *SendTaskInsService) AddOne(ins models.SendTasksIns) string {
errStr, _ := st.ValidateDiffIns(ins)
if errStr != "" {
return errStr
}
err := models.AddTaskInsOne(ins)
if err != nil {
return fmt.Sprintf("%s", err)
}
return ""
}
func (st *SendTaskInsService) Delete() error {
return models.DeleteMsgTaskIns(st.ID)
}
func (st *SendTaskInsService) Count() (int, error) {
return models.GetSendTasksTotal(st.Name, st.getMaps())
}
func (st *SendTaskInsService) GetAll() ([]models.SendTasks, error) {
tasks, err := models.GetSendTasks(st.PageNum, st.PageSize, st.Name, st.getMaps())
if err != nil {
return nil, err
}
return tasks, nil
}
func (st *SendTaskInsService) getMaps() map[string]interface{} {
maps := make(map[string]interface{})
return maps
}
+31
View File
@@ -0,0 +1,31 @@
package send_logs_service
import (
"message-nest/models"
)
type SendTaskLogsService struct {
ID int
TaskId string
Name string
PageNum int
PageSize int
}
func (st *SendTaskLogsService) Count() (int, error) {
return models.GetSendLogsTotal(st.Name, st.TaskId, st.getMaps())
}
func (st *SendTaskLogsService) GetAll() ([]models.LogsResult, error) {
tasks, err := models.GetSendLogs(st.PageNum, st.PageSize, st.Name, st.TaskId, st.getMaps())
if err != nil {
return nil, err
}
return tasks, nil
}
func (st *SendTaskLogsService) getMaps() map[string]interface{} {
maps := make(map[string]interface{})
return maps
}
@@ -0,0 +1,129 @@
package send_message_service
import (
"fmt"
"message-nest/models"
"message-nest/pkg/logging"
"message-nest/pkg/message"
"message-nest/service/send_task_service"
"message-nest/service/send_way_service"
"strings"
)
type SendMessageService struct {
TaskID string
Text string
HTML string
MarkDown string
}
func (sm *SendMessageService) Send() string {
var logOutput []string
status := 1
sendTaskService := send_task_service.SendTaskService{
ID: sm.TaskID,
}
task, err := sendTaskService.GetTaskWithIns()
if err != nil {
return fmt.Sprintf("任务不存在!任务id: %s", sm.TaskID)
}
for idx, ins := range task.InsData {
way, err := models.GetWayByID(ins.WayID)
if err != nil {
logOutput = append(logOutput, fmt.Sprintf("渠道信息不存在!渠道id%s", ins.WayID))
}
wayService := send_way_service.SendWay{
ID: fmt.Sprintf("%s", way.ID),
Name: way.Name,
Auth: way.Auth,
Type: way.Type,
}
logOutput = append(logOutput, fmt.Sprintf(">> 实例 %d", idx+1))
logOutput = append(logOutput, fmt.Sprintf("开始发送,实例: %s", ins.WayID))
logOutput = append(logOutput, fmt.Sprintf("实例类型: %s + %s", ins.WayType, ins.ContentType))
logOutput = append(logOutput, fmt.Sprintf("实例配置: %s", ins.Config))
errStr, msgObj := wayService.ValidateDiffWay()
if errStr != "" {
sm.MarkStatus(errStr, &status)
logOutput = append(logOutput, fmt.Sprintf("实例渠道认证校验失败: %s", errStr))
continue
}
// 邮箱类型的实例
emailAuth, ok := msgObj.(send_way_service.WayDetailEmail)
if ok {
errMsg := sm.SendTaskEmail(emailAuth)
sm.MarkStatus(errMsg, &status)
logOutput = append(logOutput, sm.TransError(errMsg))
continue
}
logOutput = append(logOutput, fmt.Sprintf("未知渠道的发信实例: %s", ins.ID))
}
logOutput = sm.FormatSendContent(logOutput)
sm.RecordSendLog(logOutput, status)
return ""
}
// FormatSendContent 格式化输出的发送内容
func (sm *SendMessageService) FormatSendContent(logOutput []string) []string {
logOutput = append(logOutput, fmt.Sprintf(">> 发送的内容:"))
if sm.Text != "" {
logOutput = append(logOutput, fmt.Sprintf("Text: %s", sm.Text))
}
if sm.HTML != "" {
logOutput = append(logOutput, fmt.Sprintf("HTML: %s", sm.HTML))
}
if sm.MarkDown != "" {
logOutput = append(logOutput, fmt.Sprintf("MarkDown: %s", sm.MarkDown))
}
return logOutput
}
// MarkStatus 标记任务状态
func (sm *SendMessageService) MarkStatus(errStr string, status *int) {
if errStr != "" {
*status = 0
}
}
// RecordSendLog 记录发送日志
func (sm *SendMessageService) RecordSendLog(logOutput []string, status int) {
if len(logOutput) <= 0 {
return
}
log := models.SendTasksLogs{
Log: strings.Join(logOutput, "\n"),
TaskID: sm.TaskID,
Status: status,
}
err := log.Add()
if err != nil {
logging.Logger.Error(fmt.Sprintf("添加日志失败!原因是:%s", err))
}
}
// TransError 转化错误
func (sm *SendMessageService) TransError(err string) string {
if err == "" {
return "发送成功!\n"
} else {
return fmt.Sprintf("发送失败:%s", err)
}
}
// SendTaskEmail 执行发送邮件
func (sm *SendMessageService) SendTaskEmail(auth send_way_service.WayDetailEmail) string {
var emailer message.EmailMessage
emailer.Init(auth.Server, auth.Port, auth.Account, auth.Passwd)
//errMsg := emailer.SendTextMessage("sayheya@qq.com", "test", "This is a test email from message-nest.")
//return errMsg
return ""
}
+49
View File
@@ -0,0 +1,49 @@
package send_task_service
import (
"message-nest/models"
)
type SendTaskService struct {
ID string
Name string
CreatedBy string
ModifiedBy string
CreatedOn string
PageNum int
PageSize int
}
func (st *SendTaskService) Add() error {
return models.AddSendTask(st.Name, st.CreatedBy)
}
func (st *SendTaskService) AddWithID() error {
return models.AddSendTaskWithID(st.Name, st.ID, st.CreatedBy)
}
func (st *SendTaskService) Delete() error {
return models.DeleteMsgTask(st.ID)
}
func (st *SendTaskService) GetTaskWithIns() (models.TaskIns, error) {
return models.GetTasksIns(st.ID)
}
func (st *SendTaskService) Count() (int, error) {
return models.GetSendTasksTotal(st.Name, st.getMaps())
}
func (st *SendTaskService) GetAll() ([]models.SendTasks, error) {
tasks, err := models.GetSendTasks(st.PageNum, st.PageSize, st.Name, st.getMaps())
if err != nil {
return nil, err
}
return tasks, nil
}
func (st *SendTaskService) getMaps() map[string]interface{} {
maps := make(map[string]interface{})
return maps
}
+117
View File
@@ -0,0 +1,117 @@
package send_way_service
import (
"encoding/json"
"errors"
"fmt"
"message-nest/models"
"message-nest/pkg/app"
"message-nest/pkg/message"
"strings"
)
type SendWay struct {
ID string
Name string
Type string
CreatedBy string
ModifiedBy string
Auth string
CreatedOn string
PageNum int
PageSize int
}
// WayDetailEmail 邮箱渠道明细字段
type WayDetailEmail struct {
Server string `validate:"required,max=50" label:"SMTP服务地址"`
Port int `validate:"required,max=65535" label:"SMTP服务端口"`
Account string `validate:"required,email" label:"邮箱账号"`
Passwd string `validate:"required,max=50" label:"邮箱密码"`
}
// WayDetailDTalk 钉钉渠道明细字段
type WayDetailDTalk struct {
WebhookUrl string `validate:"required,url" label:"钉钉webhookUrl地址"`
}
func (sw *SendWay) GetByID() (interface{}, error) {
return models.GetWayByID(sw.ID)
}
func (sw *SendWay) Add() error {
return models.AddSendWay(sw.Name, sw.Auth, sw.Type, sw.CreatedBy, sw.ModifiedBy)
}
func (sw *SendWay) Edit() error {
data := make(map[string]interface{})
data["modified_by"] = sw.ModifiedBy
data["name"] = sw.Name
data["auth"] = sw.Auth
return models.EditSendWay(sw.ID, data)
}
func (sw *SendWay) Delete() error {
tasks := models.FindTaskByWayId(sw.ID)
if len(tasks) > 0 {
var names []string
for _, task := range tasks {
names = append(names, task.Name)
}
return errors.New(fmt.Sprintf("已经存在使用的任务,删除失败!任务名:%s", strings.Join(names, ", ")))
}
return models.DeleteMsgWay(sw.ID)
}
func (sw *SendWay) Count() (int, error) {
return models.GetSendWaysTotal(sw.Name, sw.Type, sw.getMaps())
}
func (sw *SendWay) GetAll() ([]models.SendWays, error) {
tags, err := models.GetSendWays(sw.PageNum, sw.PageSize, sw.Name, sw.Type, sw.getMaps())
if err != nil {
return nil, err
}
return tags, nil
}
func (sw *SendWay) getMaps() map[string]interface{} {
maps := make(map[string]interface{})
return maps
}
// ValidateDiffWay 各种发信渠道具体字段校验
func (sw *SendWay) ValidateDiffWay() (string, interface{}) {
var empty interface{}
if sw.Type == "Email" {
var email WayDetailEmail
err := json.Unmarshal([]byte(sw.Auth), &email)
if err != nil {
return "邮箱auth反序列化失败!", empty
}
_, Msg := app.CommonPlaygroundValid(email)
return Msg, email
} else if sw.Type == "Dtalk" {
var dtalk WayDetailDTalk
err := json.Unmarshal([]byte(sw.Auth), &dtalk)
if err != nil {
return "钉钉auth反序列化失败!", empty
}
_, Msg := app.CommonPlaygroundValid(dtalk)
return Msg, dtalk
}
return fmt.Sprintf("未知的发信渠道校验: %s", sw.Type), empty
}
// TestSendWay 尝试带发信测试连通性
func (sw *SendWay) TestSendWay(msgObj interface{}) string {
emailAuth, ok := msgObj.(WayDetailEmail)
if ok {
var emailer message.EmailMessage
emailer.Init(emailAuth.Server, 465, emailAuth.Account, emailAuth.Passwd)
errMsg := emailer.SendTextMessage(emailAuth.Account, "test", "This is a test email from message-nest.")
return errMsg
}
return fmt.Sprintf("未知的发信渠道校验: %s", sw.Type)
}
+23
View File
@@ -0,0 +1,23 @@
package settings_service
import (
"errors"
"message-nest/models"
)
type UserSettings struct {
UserName string
OldPassword string
NewPassword string
}
// EditUserPasswd 用户设置密码
func (us *UserSettings) EditUserPasswd() error {
ok, _ := models.CheckAuth(us.UserName, us.OldPassword)
if !ok {
return errors.New("旧密码校验失败!")
}
var user = make(map[string]string)
user["password"] = us.NewPassword
return models.EditUser(us.UserName, user)
}