Tag.go
5.06 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
package Api
import (
"github.com/aarongao/tools"
"github.com/gin-gonic/gin"
"gopkg.in/mgo.v2/bson"
"letu/DB"
"letu/Lib/LeYouTu"
"time"
)
// @Title 标签列表
// @Description 标签 - 所有标签
// @Accept json
// @Produce json
// @Param ScenicId 5dfb03070a9ac17ac7a82054 string true "景区id"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":[{"Type":"menu","Name":"服务设施"},{"Type":"normal","Name":"不错"},{"Type":"thrilling","Name":"刺激"},{"Type":"recommend","Name":"必玩"}]}"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /AllTag? [get]
func AllTag(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
c.Header("Access-Control-Allow-Credentials", "true")
ScenicId, err := LeYouTu.GetScenicId(c)
if err != nil {
return
}
var Stags []*DB.STag
DB.CTags.Find(bson.M{"ScenicId": ScenicId}).All(&Stags)
if Stags == nil {
Stags = []*DB.STag{}
}
c.JSON(200, tools.ResponseSeccess{
0,
Stags,
})
}
// @Title 标签列表
// @Description 标签 - 按照标签分组查看所有标签
// @Accept json
// @Produce json
// @Param ScenicId 5dfb03070a9ac17ac7a82054 string true "景区id"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":[{"Tags":["儿童","成人","青少年"],"Type":"age"}]}"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /AllTagGroup? [get]
func AllTagGroup(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
c.Header("Access-Control-Allow-Credentials", "true")
ScenicId, err := LeYouTu.GetScenicId(c)
if err != nil {
return
}
// cache
cache := DB.Redis.Get("Tags_" + ScenicId)
if cache != nil {
c.JSON(200, tools.ResponseSeccess{
0,
cache,
})
println("读取缓存Tags", cache)
return
}
var Stags []*DB.STag
DB.CTags.Find(bson.M{"ScenicId": ScenicId}).All(&Stags)
if Stags == nil {
Stags = []*DB.STag{}
}
Group := make(map[string][]string)
// 去重
for _, v := range Stags {
Group[v.Type] = append(Group[v.Type], v.Name)
}
// 转为数组
aGroup := []Tag{}
for k, v := range Group {
aGroup = append(aGroup, Tag{k, v})
}
DB.Redis.Set("Tags_"+ScenicId, aGroup, time.Second*3600*24)
c.JSON(200, tools.ResponseSeccess{
0,
aGroup,
})
}
// @Title 标签
// @Description 标签 - 增加标签
// @Accept json
// @Produce json
// @Param ScenicId 5dfb03070a9ac17ac7a82054 string true "景区id"
// @Param TagName 广场 string true "标签名称"
// @Param TagGroup location string true "群组名称"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":"ok"}"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /Tag/Create? [post]
func CreateTag(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
c.Header("Access-Control-Allow-Credentials", "true")
ScenicId, err := LeYouTu.GetScenicId(c)
if err != nil {
return
}
if c.PostForm("TagName") == "" {
c.JSON(200, tools.ResponseError{
1,
"标签名称不能为空",
})
return
}
if c.PostForm("TagGroup") == "" {
c.JSON(200, tools.ResponseError{
1,
"群组名称不能为空",
})
return
}
if c.PostForm("TagGroup") == "type" {
c.JSON(200, tools.ResponseError{
1,
"此类型只能加不能加",
})
return
}
DB.CTags.Insert(DB.STag{
ScenicId,
c.PostForm("TagGroup"),
c.PostForm("TagName"),
})
DB.Redis.Delete("Tags_" + ScenicId)
println("清楚缓存Tags")
c.JSON(200, tools.ResponseSeccess{
0,
"ok",
})
}
// @Title 标签
// @Description 标签 - 删除标签
// @Accept json
// @Produce json
// @Param ScenicId 5dfb03070a9ac17ac7a82054 string true "景区id"
// @Param TagName 广场 string true "标签名称"
// @Param TagGroup location string true "群组名称"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":"ok"}"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /Tag/Remove? [post]
func RemoveTag(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
c.Header("Access-Control-Allow-Credentials", "true")
ScenicId, err := LeYouTu.GetScenicId(c)
if err != nil {
return
}
if c.PostForm("TagName") == "" {
c.JSON(200, tools.ResponseError{
1,
"标签名称不能为空",
})
return
}
if c.PostForm("TagGroup") == "" {
c.JSON(200, tools.ResponseError{
1,
"群组名称不能为空",
})
return
}
if c.PostForm("TagGroup") == "type" {
c.JSON(200, tools.ResponseError{
1,
"此类型只能加不能删",
})
return
}
DB.CTags.Remove(bson.M{"ScenicId": ScenicId,"Name":c.PostForm("TagName"),"Type":c.PostForm("TagGroup")})
DB.Redis.Delete("Tags_" + ScenicId)
println("清楚缓存Tags")
c.JSON(200, tools.ResponseSeccess{
0,
"ok",
})
}
type Tag struct {
Type string
Tags []string
}