-
Notifications
You must be signed in to change notification settings - Fork 38
/
user_tags_controller.go
158 lines (149 loc) · 4.41 KB
/
user_tags_controller.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
package wechat
import (
"fmt"
"github.com/silenceper/wechat/v2/officialaccount"
"github.com/silenceper/wechat/v2/officialaccount/user"
"github.com/xiusin/pine"
"github.com/xiusin/pine/cache"
"github.com/xiusin/pinecms/src/application/controllers/backend"
"github.com/xiusin/pinecms/src/application/models/tables"
"github.com/xiusin/pinecms/src/common/helper"
)
type WechatUserTagsController struct {
backend.BaseController
p UserTags
}
func (c *WechatUserTagsController) Construct() {
c.BaseController.Construct()
}
func (c *WechatUserTagsController) PostList(cacher cache.AbstractCache) {
appid, _ := c.Input().GetString("appid")
var tags = []*user.TagInfo{}
if appid == "" {
helper.Ajax("请输入公众号ID", 1, c.Ctx())
return
}
cacheKey := fmt.Sprintf(CacheKeyWechatUserTags, appid)
err := cacher.Remember(cacheKey, &tags, func() (interface{}, error) {
account, _ := GetOfficialAccount(appid)
return account.GetUser().GetTag()
}, CacheTimeSecs)
if err != nil {
helper.Ajax(err, 1, c.Ctx())
return
}
helper.Ajax(tags, 0, c.Ctx())
}
func (c *WechatUserTagsController) PostDelete(cacher cache.AbstractCache) {
appid, _ := c.Input().GetString("appid")
id, _ := c.Input().GetInt64("id")
cacheKey := fmt.Sprintf(CacheKeyWechatUserTags, appid)
var tags []*user.TagInfo
err := cacher.GetWithUnmarshal(cacheKey, &tags)
if err != nil {
helper.Ajax(err, 1, c.Ctx())
return
}
var idx = -1
for i, tag := range tags {
if tag.ID == int32(id) {
idx = i
break
}
}
if idx > -1 {
account, _ := GetOfficialAccount(appid)
err = account.GetUser().DeleteTag(int32(id))
if err != nil {
helper.Ajax(err, 1, c.Ctx())
} else {
tags = append(tags[:idx], tags[idx+1:]...)
cacher.SetWithMarshal(cacheKey, &tags, CacheTimeSecs)
helper.Ajax("删除标签成功", 0, c.Ctx())
}
} else {
helper.Ajax("标签不存在或已被删除", 1, c.Ctx())
}
}
func (c *WechatUserTagsController) PostAdd(cacher cache.AbstractCache) {
c.Ctx().BindJSON(&c.p)
if c.p.Appid == "" || c.p.Name == "" {
helper.Ajax("必要参数为空", 1, c.Ctx())
return
}
cacheKey := fmt.Sprintf(CacheKeyWechatUserTags, c.p.Appid)
account, _ := GetOfficialAccount(c.p.Appid)
if tagInfo, err := account.GetUser().CreateTag(c.p.Name); err != nil {
helper.Ajax(err, 1, c.Ctx())
return
} else {
var tags []*user.TagInfo
cacher.GetWithUnmarshal(cacheKey, &tags)
tags = append(tags, tagInfo)
cacher.SetWithMarshal(cacheKey, &tags, CacheTimeSecs)
helper.Ajax("新增标签成功", 0, c.Ctx())
return
}
}
func (c *WechatUserTagsController) PostEdit(cacher cache.AbstractCache) {
c.Ctx().BindJSON(&c.p)
if c.p.Appid == "" || c.p.Name == "" || c.p.Id == 0 {
helper.Ajax("必要参数为空", 1, c.Ctx())
return
}
cacheKey := fmt.Sprintf(CacheKeyWechatUserTags, c.p.Appid)
var tags []*user.TagInfo
if err := cacher.GetWithUnmarshal(cacheKey, &tags); err != nil {
helper.Ajax(err, 1, c.Ctx())
return
}
for i, tag := range tags {
if tag.ID == int32(c.p.Id) {
tag.Name = c.p.Name
tags[i] = tag
account, _ := GetOfficialAccount(c.p.Appid)
if err := account.GetUser().UpdateTag(int32(c.p.Id), c.p.Name); err != nil {
helper.Ajax(err, 1, c.Ctx())
return
} else {
cacher.SetWithMarshal(cacheKey, &tags, CacheTimeSecs)
helper.Ajax("修改标签成功", 0, c.Ctx())
return
}
}
}
helper.Ajax("没有找到标签", 1, c.Ctx())
}
func (c *WechatUserTagsController) PostTagging() {
c.Ctx().BindJSON(&c.p)
if c.p.Appid == "" || len(c.p.Openids) == 0 || c.p.Id == 0 || c.p.Action == "" {
helper.Ajax("必要参数为空", 1, c.Ctx())
return
}
account, _ := GetOfficialAccount(c.p.Appid)
var err error
if c.p.Action == "tagging" {
err = account.GetUser().BatchTag(c.p.Openids, int32(c.p.Id))
} else {
err = account.GetUser().BatchUntag(c.p.Openids, int32(c.p.Id))
}
if err != nil {
helper.Ajax(err, 1, c.Ctx())
return
}
c.refreshUserInfo(account)
helper.Ajax("操作成功", 0, c.Ctx())
}
func (c *WechatUserTagsController) refreshUserInfo(account *officialaccount.OfficialAccount) {
for _, openid := range c.p.Openids {
u, err := account.GetUser().GetUserInfo(openid)
if err == nil {
_, err = c.Orm.Where("openid = ?", openid).Cols("tagid_list").Update(&tables.WechatMember{TagidList: u.TagIDList})
if err != nil {
pine.Logger().Warning("更新用户", openid, "失败: ", err.Error())
}
} else {
pine.Logger().Warning("更新用户", openid, "失败: ", err.Error())
}
}
}