-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
setting.go
174 lines (140 loc) · 3.86 KB
/
setting.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package models
import (
"encoding/json"
)
type Setting struct {
Id int `xorm:"int pk autoincr"`
Code string `xorm:"varchar(32) notnull"`
Key string `xorm:"varchar(64) notnull"`
Value string `xorm:"varchar(4096) notnull default '' "`
}
const SlackCode = "slack"
const SlackUrlKey = "url"
const SlackChannelKey = "channel"
const MailCode = "mail"
const MailServerKey = "server"
const MailUserKey = "user"
// 初始化基本字段 邮件、slack等
func (setting *Setting) InitBasicField() {
setting.Code = SlackCode;
setting.Key = SlackUrlKey
Db.Insert(setting)
setting.Id = 0
setting.Code = MailCode
setting.Key = MailServerKey
Db.Insert(setting)
}
// region slack配置
type Slack struct {
Url string
Channels []Channel
}
type Channel struct {
Id int
Name string
}
func (setting *Setting) Slack() (Slack, error) {
list := make([]Setting, 0)
err := Db.Where("code = ?", SlackCode).Find(&list)
slack := Slack{Url:"", Channels:make([]Channel, 0)}
if err != nil {
return slack, err
}
setting.formatSlack(list, &slack)
return slack, err
}
func (setting *Setting) formatSlack(list []Setting, slack *Slack) {
for _, v := range list {
if v.Key == SlackUrlKey {
slack.Url = v.Value
continue
}
slack.Channels = append(slack.Channels, Channel{
v.Id, v.Value,
})
}
}
// 更新slack webhook url
func (setting *Setting) UpdateSlackUrl(url string) (int64, error) {
setting.Value = url
return Db.Cols("value").Update(setting, Setting{Code:SlackCode, Key:SlackUrlKey})
}
// 创建slack渠道
func (setting *Setting) CreateChannel(channel string) (int64, error) {
setting.Code = SlackCode
setting.Key = SlackChannelKey
setting.Value = channel
return Db.Insert(setting)
}
func (setting *Setting) IsChannelExist(channel string) (bool) {
setting.Code = SlackCode
setting.Key = SlackChannelKey
setting.Value = channel
count, _ := Db.Count(setting)
return count > 0
}
// 删除slack渠道
func (setting *Setting) RemoveChannel(id int) (int64, error) {
setting.Code = SlackCode
setting.Key = SlackChannelKey
setting.Id = id
return Db.Delete(setting)
}
// endregion
type Mail struct {
Host string
Port int
User string
Password string
MailUsers []MailUser
}
type MailUser struct {
Id int
Username string
Email string
}
// region 邮件配置
func (setting *Setting) Mail() (Mail, error) {
list := make([]Setting, 0)
err := Db.Where("code = ?", MailCode).Find(&list)
mail := Mail{MailUsers:make([]MailUser, 0)}
if err != nil {
return mail, err
}
setting.formatMail(list, &mail)
return mail, err
}
func (setting *Setting) formatMail(list []Setting, mail *Mail) {
mailUser := MailUser{}
for _, v := range list {
if v.Key == MailServerKey {
json.Unmarshal([]byte(v.Value), mail)
continue
}
json.Unmarshal([]byte(v.Value), &mailUser)
mailUser.Id = v.Id
mail.MailUsers = append(mail.MailUsers, mailUser)
}
}
func (setting *Setting) UpdateMailServer(config string) (int64, error) {
setting.Value = config
return Db.Cols("value").Update(setting, Setting{Code:MailCode, Key:MailServerKey})
}
func (setting *Setting) CreateMailUser(username, email string) (int64, error) {
setting.Code = MailCode
setting.Key = MailUserKey
mailUser := MailUser{0, username, email}
jsonByte, err := json.Marshal(mailUser)
if err != nil {
return 0, err
}
setting.Value = string(jsonByte)
return Db.Insert(setting)
}
func (setting *Setting) RemoveMailUser(id int) (int64, error) {
setting.Code = MailCode
setting.Key = MailUserKey
setting.Id = id
return Db.Delete(setting)
}
// endregion