-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
245 lines (206 loc) · 6.58 KB
/
service.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package proclaim
import (
"context"
"encoding/json"
"errors"
"github.com/PuerkitoBio/goquery"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
amqpClient "github.com/fuhsicloud/fuhsicloud/src/amqp"
"github.com/fuhsicloud/fuhsicloud/src/config"
"github.com/fuhsicloud/fuhsicloud/src/middleware"
"github.com/fuhsicloud/fuhsicloud/src/repository"
"github.com/fuhsicloud/fuhsicloud/src/repository/types"
"github.com/fuhsicloud/fuhsicloud/src/util/paginator"
"github.com/fuhsicloud/fuhsicloud/src/util/upload"
"strings"
)
var (
ErrInvalidArgument = errors.New("invalid argument")
ErrProclaimParamsRefused = errors.New("参数校验未通过.")
ErrProclaimList = errors.New("获取公告列表失败")
ErrProclaimListCount = errors.New("获取公告列表总数失败")
)
type Service interface {
Get(ctx context.Context, id int) (resp *types.Notices, err error)
Post(ctx context.Context, req proclaimRequest) (err error)
List(ctx context.Context, name string, page int, limit int) (res map[string]interface{}, err error)
ContentHtmlHandle(content string) (body string, err error)
}
type service struct {
logger log.Logger
cf *config.Config
amqpClient amqpClient.AmqpClient
store repository.Repository
}
func NewService(logger log.Logger, cf *config.Config, amqpClient amqpClient.AmqpClient, store repository.Repository) Service {
return &service{
logger: logger,
cf: cf,
amqpClient: amqpClient,
store: store,
}
}
func (c *service) Get(ctx context.Context, id int) (resp *types.Notices, err error) {
return c.store.Proclaim().FindById(id)
}
func (c *service) Post(ctx context.Context, req proclaimRequest) (err error) {
memberId := ctx.Value(middleware.UserIdContext).(int64)
_ = level.Info(c.logger).Log("title", req.Title, "content", req.Content, "proclaim_type", req.ProclaimType)
if req.Title == "" || req.Content == "" {
return ErrProclaimParamsRefused
}
var proclaimReceive string
if req.ProclaimType == "namespace" {
for i := 0; i < len(req.Namespace); i++ {
proclaimReceive += req.Namespace[i] + ","
}
} else if req.ProclaimType == "user" {
for i := 0; i < len(req.UserList); i++ {
proclaimReceive += req.UserList[i] + ","
}
}
content, err := c.ContentHtmlHandle(req.Content)
if err != nil {
_ = level.Error(c.logger).Log("proclaim", "post", "c.ContentHtmlHandle.Err", err.Error())
return
}
data := new(types.Notices)
data.Title = req.Title
data.Content = content
data.Type = 1
data.MemberID = int(memberId)
data.ProclaimType = req.ProclaimType
data.ProclaimReceive = proclaimReceive
_ = c.store.Proclaim().CreateReturnId(data)
b, _ := json.Marshal(data)
//公告内容放队列,推送邮件用
defer func() {
if err := c.amqpClient.PublishOnQueue(amqpClient.ProclaimTopic, func() []byte {
return []byte(b)
}); err != nil {
_ = level.Error(c.logger).Log("amqpClient", "PublicProclaimQueue", "err", err.Error())
}
}()
return err
}
func (c *service) ContentHtmlHandle(content string) (body string, err error) {
type uploadConfig struct {
UploadPath string `yaml:"upload_path"`
DomainPrefix string `yaml:"domain_prefix"`
}
var upConf uploadConfig
upConf.UploadPath = c.cf.GetString("server", "upload_path")
upConf.DomainPrefix = c.cf.GetString("server", "domain")
doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
doc.Find(".image-wrap").Each(func(i int, s *goquery.Selection) {
//获取src内容
src, _ := s.Find("img").Attr("src")
if strings.Contains(src, "base64") { //如果是引用网络图片,则不替换
srcArr := strings.Split(src, ",")
postfix := strings.Split(strings.Split(srcArr[0], "/")[1], ";")[0]
base64Str := srcArr[1]
var im upload.ImgInfo
im.Base64 = base64Str
im.Type = postfix
im.Path = upConf.UploadPath + "images"
re, err := im.Base64ToFile()
if err != nil {
_ = level.Error(c.logger).Log("ContentHtmlHandle.Base64ToFile.Err", err.Error())
return
}
//图片url
imgUrl := upConf.DomainPrefix + "images" + re.PathSmall
s.Find("img").SetAttr("src", imgUrl) //替换src
}
})
body, err = doc.Find("body").Html()
if err != nil {
_ = level.Error(c.logger).Log("doc", "Find", "Html", "body", "err", err.Error())
}
_ = level.Info(c.logger).Log("ContentHtmlHandle.ContentToHtml", body)
return
}
func (c *service) List(ctx context.Context, name string, page int, limit int) (res map[string]interface{}, err error) {
count, err := c.store.Proclaim().Count(name, 1)
if err != nil {
_ = level.Error(c.logger).Log("template", "Count", "err", err.Error())
return nil, ErrProclaimListCount
}
p := paginator.NewPaginator(page, limit, count)
list, err := c.store.Proclaim().FindOffsetLimit(name, 1, p.Offset(), limit)
if err != nil {
_ = level.Error(c.logger).Log("proclaim", "FindOffsetLimit", "err", err.Error())
return nil, ErrProclaimList
}
//用户列表
ml, err := c.store.Member().GetMembersAll()
//业务线列表
nsl, err := c.store.Namespace().FindAll()
var listMap []interface{}
for _, val := range list {
var prText, memberName, conText string
if val.ProclaimType == "namespace" {
nsArr := strings.Split(val.ProclaimReceive, ",")
for _, n := range nsl {
for _, ns := range nsArr {
if n.Name == ns {
prText += n.Name + ","
}
}
}
//截取最后一个,
if len(prText) > 0 {
prText = prText[0 : len(prText)-1]
}
} else if val.ProclaimType == "user" {
uidArr := strings.Split(val.ProclaimReceive, ",")
for _, u := range ml {
for _, uid := range uidArr {
if u.Email == uid {
prText += u.Username + ","
}
}
}
//截取最后一个,
if len(prText) > 0 {
prText = prText[0 : len(prText)-1]
}
} else {
prText = "全部"
}
for _, u := range ml {
if int(u.ID) == val.MemberID {
memberName = u.Username
}
}
c := val.Content
if len(c) > 25 {
conText = string([]rune(c)[:25]) + "..."
} else {
conText = c
}
data := map[string]interface{}{
"id": val.ID,
"title": val.Title,
"content": conText,
"member_id": val.MemberID,
"member_name": memberName,
"proclaim_type": val.ProclaimType,
"proclaim_receive": val.ProclaimReceive,
"proclaim_receive_text": prText,
"created_at": val.CreatedAt.Time.Format("2006-01-02 15:04:05"),
}
listMap = append(listMap, data)
}
res = map[string]interface{}{
"list": listMap,
"page": map[string]interface{}{
"total": count,
"pageTotal": p.PageTotal(),
"pageSize": limit,
"page": p.Page(),
},
}
return
}