-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
manage.go
130 lines (101 loc) · 3.22 KB
/
manage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package manage
import (
"encoding/json"
"github.com/ouqiang/gocron/internal/models"
"github.com/ouqiang/gocron/internal/modules/logger"
"github.com/ouqiang/gocron/internal/modules/utils"
"gopkg.in/macaron.v1"
)
func Slack(ctx *macaron.Context) string {
settingModel := new(models.Setting)
slack, err := settingModel.Slack()
jsonResp := utils.JsonResponse{}
if err != nil {
logger.Error(err)
return jsonResp.Success(utils.SuccessContent, nil)
}
return jsonResp.Success(utils.SuccessContent, slack)
}
func UpdateSlack(ctx *macaron.Context) string {
url := ctx.QueryTrim("url")
template := ctx.QueryTrim("template")
settingModel := new(models.Setting)
err := settingModel.UpdateSlack(url, template)
return utils.JsonResponseByErr(err)
}
func CreateSlackChannel(ctx *macaron.Context) string {
channel := ctx.QueryTrim("channel")
settingModel := new(models.Setting)
if settingModel.IsChannelExist(channel) {
jsonResp := utils.JsonResponse{}
return jsonResp.CommonFailure("Channel已存在")
}
_, err := settingModel.CreateChannel(channel)
return utils.JsonResponseByErr(err)
}
func RemoveSlackChannel(ctx *macaron.Context) string {
id := ctx.ParamsInt(":id")
settingModel := new(models.Setting)
_, err := settingModel.RemoveChannel(id)
return utils.JsonResponseByErr(err)
}
// endregion
// region 邮件
func Mail(ctx *macaron.Context) string {
settingModel := new(models.Setting)
mail, err := settingModel.Mail()
jsonResp := utils.JsonResponse{}
if err != nil {
logger.Error(err)
return jsonResp.Success(utils.SuccessContent, nil)
}
return jsonResp.Success("", mail)
}
type MailServerForm struct {
Host string `binding:"Required;MaxSize(100)"`
Port int `binding:"Required;Range(1-65535)"`
User string `binding:"Required;MaxSize(64);Email"`
Password string `binding:"Required;MaxSize(64)"`
}
func UpdateMail(ctx *macaron.Context, form MailServerForm) string {
jsonByte, _ := json.Marshal(form)
settingModel := new(models.Setting)
template := ctx.QueryTrim("template")
err := settingModel.UpdateMail(string(jsonByte), template)
return utils.JsonResponseByErr(err)
}
func CreateMailUser(ctx *macaron.Context) string {
username := ctx.QueryTrim("username")
email := ctx.QueryTrim("email")
settingModel := new(models.Setting)
if username == "" || email == "" {
jsonResp := utils.JsonResponse{}
return jsonResp.CommonFailure("用户名、邮箱均不能为空")
}
_, err := settingModel.CreateMailUser(username, email)
return utils.JsonResponseByErr(err)
}
func RemoveMailUser(ctx *macaron.Context) string {
id := ctx.ParamsInt(":id")
settingModel := new(models.Setting)
_, err := settingModel.RemoveMailUser(id)
return utils.JsonResponseByErr(err)
}
func WebHook(ctx *macaron.Context) string {
settingModel := new(models.Setting)
webHook, err := settingModel.Webhook()
jsonResp := utils.JsonResponse{}
if err != nil {
logger.Error(err)
return jsonResp.Success(utils.SuccessContent, nil)
}
return jsonResp.Success("", webHook)
}
func UpdateWebHook(ctx *macaron.Context) string {
url := ctx.QueryTrim("url")
template := ctx.QueryTrim("template")
settingModel := new(models.Setting)
err := settingModel.UpdateWebHook(url, template)
return utils.JsonResponseByErr(err)
}
// endregion