mini_subscribe_logic.go
11.4 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package message
import (
"context"
"fmt"
"github.com/jinzhu/copier"
"github.com/silenceper/wechat/v2"
"github.com/silenceper/wechat/v2/cache"
miniConfig "github.com/silenceper/wechat/v2/miniprogram/config"
"github.com/silenceper/wechat/v2/miniprogram/subscribe"
"github.com/zeromicro/go-zero/core/logx"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
"strings"
"time"
)
type MiniSubscribeLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewMiniSubscribeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniSubscribeLogic {
return &MiniSubscribeLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *MiniSubscribeLogic) getSubScribe() *subscribe.Subscribe {
miniprogram := wechat.NewWechat().GetMiniProgram(&miniConfig.Config{
AppID: l.svcCtx.Config.Wechat.AppID,
AppSecret: l.svcCtx.Config.Wechat.AppSecret,
Cache: cache.NewMemory(),
})
return miniprogram.GetSubscribe()
}
// getOpenId 获取绑定用户openID
func (l *MiniSubscribeLogic) getOpenId(conn transaction.Conn, userId int64) (string, error) {
userInfo, err := l.svcCtx.UserRepository.FindOne(l.ctx, conn, userId)
if err != nil {
return "", err
}
//获取微信绑定
userWechat, err := l.svcCtx.UserWechatRepository.FindOneByPhone(l.ctx, conn, userInfo.Phone)
if err != nil {
return "", err
}
return userWechat.OpenId, nil
}
// saveAndDecrease 保存并扣减订阅消息
func (l *MiniSubscribeLogic) saveAndDecrease(conn transaction.Conn, companyId, userId int64, mType int, msg *subscribe.Message, sendErr error) error {
templateData := make(map[string]interface{})
_ = copier.Copy(&templateData, msg.Data)
subscribeMessage := &domain.MessageSubscribe{
Type: mType,
CompanyId: companyId,
UserId: userId,
OpenId: msg.ToUser,
TemplateId: msg.TemplateID,
TemplateData: templateData,
}
if sendErr != nil {
subscribeMessage.Result = "fail"
subscribeMessage.Error = sendErr.Error()
} else {
subscribeMessage.Result = "ok"
}
_, err := l.svcCtx.MessageSubscribeRepository.Insert(l.ctx, conn, subscribeMessage)
if err != nil {
return err
}
//扣减次数
if sendErr == nil {
userSubscribe, err := l.svcCtx.UserSubscribeRepository.FindOneByType(l.ctx, conn, companyId, userId, mType)
if err == nil && userSubscribe.Count > 0 {
userSubscribe.Count = userSubscribe.Count - 1
_, err = l.svcCtx.UserSubscribeRepository.Update(l.ctx, conn, userSubscribe)
if err != nil {
return err
}
}
}
return nil
}
func (l *MiniSubscribeLogic) messageSubscribe(companyId, userId int64, mType int) *domain.MessageSubscribe {
return &domain.MessageSubscribe{
Type: mType,
CompanyId: companyId,
UserId: userId,
}
}
// getReplyCommentUserInfo 获取评论消息用户信息 用户名称+职位 例:张三-董事办
func (l *MiniSubscribeLogic) getReplyCommentUserInfo(conn transaction.Conn, companyId, userId int64) (string, error) {
userInfo, err := l.svcCtx.UserRepository.FindOne(l.ctx, conn, userId)
if err != nil {
return "", xerr.NewErrMsgErr("获取评论用户信息失败", err)
}
users := []string{userInfo.Name}
if len(userInfo.Roles) > 0 {
_, roles, err := l.svcCtx.RoleRepository.Find(l.ctx, conn, domain.IndexCompanyId(companyId)().MustWithKV("ids", userInfo.Roles))
if err == nil && len(roles) > 0 {
roleNames := make([]string, 0)
for _, item := range roles {
roleNames = append(roleNames, item.Name)
}
users = append(users, strings.Join(roleNames, "、"))
}
}
return strings.Join(users, "-"), nil
}
// ReplyComment 发送评论订阅消息
// @param conn 数据库连接
// @param article 文章
// @param comment 评论
func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain.Article, comment *domain.ArticleComment) error {
subCtx := l.getSubScribe()
//评论用户+职位 例: 张三-董事办
fromUserName, err := l.getReplyCommentUserInfo(conn, comment.CompanyId, comment.FromUserId)
if err != nil {
return xerr.NewErrMsgErr("发送消息失败", err)
}
//评论消息
msg := &subscribe.Message{
TemplateID: domain.SubscribeTemplateComment,
Data: map[string]*subscribe.DataItem{
//文章标题
"thing1": &subscribe.DataItem{Value: article.GetSubscribeMessageTitle()},
//评论内容
"thing2": &subscribe.DataItem{Value: comment.GetSubscribeMessageContent()},
//评论时间
"time3": &subscribe.DataItem{Value: time.Now().Format("2006-01-02 15:04:05")},
//评论用户
"thing5": &subscribe.DataItem{Value: fromUserName},
//备注
"thing9": &subscribe.DataItem{Value: ""},
},
MiniprogramState: l.svcCtx.Config.Wechat.QrcodeEnv,
}
//发帖人接收消息
openId, err := l.getOpenId(conn, article.AuthorId)
//未绑定微信号,直接返回
if err == nil && openId != "" {
msg.ToUser = openId
msg.Page = fmt.Sprintf("/pages/detail/more-comment?id=%v", article.Id) //跳转页面 帖子评论聚合页
//备注
userCount, err := l.svcCtx.ArticleCommentRepository.CommentUserCount(l.ctx, conn, comment.CompanyId, comment.ArticleId)
msg.Data["thing9"] = &subscribe.DataItem{Value: fmt.Sprintf("您的帖子最近已有%v人评论,点击查看详情", userCount)}
//发送微信订阅消息
err = subCtx.Send(msg)
err = l.saveAndDecrease(conn, comment.CompanyId, article.AuthorId, domain.SubscribeTypeReplyComment, msg, err)
if err != nil {
return xerr.NewErrMsgErr("评论失败", err)
}
}
//评论回复
if comment.Pid > 0 {
toOpenId, err := l.getOpenId(conn, comment.ToUserId)
if err == nil && toOpenId != "" {
msg.ToUser = toOpenId
msg.Page = fmt.Sprintf("/pages/detail/reply-comment?id=%v&commentId=%v", article.Id, comment.Pid) //跳转页面评论聚合页
//备注
replyCount, err := l.svcCtx.ArticleCommentRepository.ReplyUserCount(l.ctx, conn, comment.CompanyId, comment.Pid)
if err == nil {
msg.Data["thing9"] = &subscribe.DataItem{Value: fmt.Sprintf("您的评论最近已有%v人回复,点击查看详情", replyCount)}
//发送微信订阅消息
err = subCtx.Send(msg)
err = l.saveAndDecrease(conn, comment.CompanyId, comment.ToUserId, domain.SubscribeTypeReplyComment, msg, err)
if err != nil {
return xerr.NewErrMsgErr("评论失败", err)
}
}
}
}
//@消息
if len(comment.AtWho) > 0 {
for _, at := range comment.AtWho {
atOpenId, err := l.getOpenId(conn, at.Id)
//未绑定微信跳过
if err != nil || atOpenId == "" {
continue
}
msg.ToUser = atOpenId
msg.Page = fmt.Sprintf("/pages/detail/reply-comment?id=%v&commentId=%v", article.Id, comment.Pid) //跳转页面 评论详情页
//备注
msg.Data["thing9"] = &subscribe.DataItem{Value: fmt.Sprintf("%v在评论中提到了你", comment.FromUser.Name)}
//发送微信订阅消息
err = subCtx.Send(msg)
err = l.saveAndDecrease(conn, comment.CompanyId, at.Id, domain.SubscribeTypeReplyComment, msg, err)
if err != nil {
return xerr.NewErrMsgErr("评论失败", err)
}
}
}
return nil
}
// LikeArticle 帖子点赞订阅消息
func (l *MiniSubscribeLogic) LikeArticle(conn transaction.Conn, article *domain.Article, userInfo *domain.User) error {
subCtx := l.getSubScribe()
openId, err := l.getOpenId(conn, article.AuthorId)
if err != nil || openId == "" {
return nil
}
newArticle, err := l.svcCtx.ArticleRepository.FindOne(l.ctx, conn, article.Id)
if err != nil {
return xerr.NewErrMsgErr("未获取到帖子信息", err)
}
msg := &subscribe.Message{
ToUser: openId,
TemplateID: domain.SubscribeTemplateLike,
Page: fmt.Sprintf("/pages/detail/detail?id=%v", article.Id),
Data: map[string]*subscribe.DataItem{
//点赞用户
"name1": &subscribe.DataItem{Value: userInfo.Name},
//点赞时间
"date2": &subscribe.DataItem{Value: time.Now().Format("2006-01-02 15:04:05")},
//动态内容
"thing8": &subscribe.DataItem{Value: article.GetSubscribeMessageTitle()},
//被赞次数
"number4": &subscribe.DataItem{Value: newArticle.CountLove},
//温馨提示
"thing5": &subscribe.DataItem{Value: "这条内容很受欢迎哦,快来看看吧"},
},
MiniprogramState: l.svcCtx.Config.Wechat.QrcodeEnv,
}
err = subCtx.Send(msg)
err = l.saveAndDecrease(conn, article.CompanyId, article.AuthorId, domain.SubscribeTypeLike, msg, err)
if err != nil {
return xerr.NewErrMsgErr("点赞失败", err)
}
return nil
}
// LikeComment 点赞评论订阅消息
func (l *MiniSubscribeLogic) LikeComment(conn transaction.Conn, comment *domain.ArticleComment, userInfo *domain.User) error {
subCtx := l.getSubScribe()
openId, err := l.getOpenId(conn, comment.FromUserId)
if err != nil || openId == "" {
return nil
}
//获取被赞次数
newComment, err := l.svcCtx.ArticleCommentRepository.FindOne(l.ctx, conn, comment.Id)
if err != nil {
return xerr.NewErrMsgErr("未获取到评论信息", err)
}
msg := &subscribe.Message{
ToUser: openId,
TemplateID: domain.SubscribeTemplateLike,
Page: fmt.Sprintf("/pages/detail/reply-comment?id=%v&commentId=%v", comment.ArticleId, comment.Id),
Data: map[string]*subscribe.DataItem{
//点赞用户
"name1": &subscribe.DataItem{Value: userInfo.Name},
//点赞时间
"date2": &subscribe.DataItem{Value: time.Now().Format("2006-01-02 15:04:05")},
//动态内容
"thing8": &subscribe.DataItem{Value: comment.GetSubscribeMessageContent()},
//被赞次数
"number4": &subscribe.DataItem{Value: newComment.CountUserLove},
//温馨提示
"thing5": &subscribe.DataItem{Value: "这条内容很受欢迎哦,快来看看吧"},
},
MiniprogramState: l.svcCtx.Config.Wechat.QrcodeEnv,
}
err = subCtx.Send(msg)
err = l.saveAndDecrease(conn, comment.CompanyId, comment.FromUserId, domain.SubscribeTypeLike, msg, err)
if err != nil {
return xerr.NewErrMsgErr("点赞失败", err)
}
return nil
}
// FollowArticle 发帖关注更新提醒
func (l *MiniSubscribeLogic) FollowArticle(conn transaction.Conn, article *domain.Article) error {
subCtx := l.getSubScribe()
//获取关注帖子作者的人员
_, userInfo, err := l.svcCtx.UserFollowRepository.Find(l.ctx, conn, domain.NewQueryOptions().WithKV("toUserIds", []int64{article.AuthorId}))
if err == nil && len(userInfo) > 0 {
for _, item := range userInfo {
openId, err := l.getOpenId(conn, item.FromUserId)
if err != nil || openId == "" {
continue
}
msg := &subscribe.Message{
ToUser: openId,
TemplateID: domain.SubscribeTemplateFollow,
Page: fmt.Sprintf("/pages/detail/detail?id=%v", article.Id),
Data: map[string]*subscribe.DataItem{
//创作者
"thing1": &subscribe.DataItem{Value: article.Author.Name},
//作品名称
"thing2": &subscribe.DataItem{Value: article.Title},
//内容摘要
"thing5": &subscribe.DataItem{Value: ""},
//发布时间
"time6": &subscribe.DataItem{Value: time.Now().Format("2006-01-02 15:04:05")},
//温馨提示
"thing3": &subscribe.DataItem{Value: "你关注的人发布了新的帖子"},
},
MiniprogramState: l.svcCtx.Config.Wechat.QrcodeEnv,
}
err = subCtx.Send(msg)
err = l.saveAndDecrease(conn, article.CompanyId, item.FromUserId, domain.SubscribeTypeFollow, msg, err)
if err != nil {
return xerr.NewErrMsgErr("保存订阅消息失败", err)
}
}
}
return nil
}