TopMenus.go
4.37 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
package Api
import (
"encoding/json"
"github.com/aarongao/tools"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options"
"letu/DB"
"letu/Lib/Auth"
"strconv"
)
// @Title 查询所有菜单
// @Description 菜单管理 - 查询所有菜单
// @Accept json
// @Produce json
// @Param ScenicId 5e0d504e24e03431008b4567 string true "景区id"
// @Param Token wgergejfwe string true "用户token"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":[{"Id":"","ScenicId":"","Title":"玩水","Tags":["玩水"]},{"Id":"","ScenicId":"","Title":"设施","Tags":["服务设施","游玩项目"]}]}"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /TopMenus/All? [get]
func AllTopMenus(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
c.Header("Access-Control-Allow-Credentials", "true")
var STopMenus []DB.STopMenus
cur, err := DB.CTopMenus.Find(tools.GetContext(), bson.M{"ScenicId": c.Query("ScenicId")}, &options.FindOptions{Sort: bson.M{"Order": 1}})
defer cur.Close(tools.GetContext())
if err == nil {
for cur.Next(tools.GetContext()) {
var e DB.STopMenus
cur.Decode(&e)
STopMenus = append(STopMenus, e)
}
}
if STopMenus == nil {
STopMenus = []DB.STopMenus{}
}
c.JSON(200, tools.ResponseSeccess{
0,
STopMenus,
})
}
// @Title 更新菜单
// @Description 菜单管理 - 更新菜单
// @Accept json
// @Produce json
// @Param ScenicId 5e0d504e24e03431008b4567 string true "景区id"
// @Param Token wgergejfwe string true "用户token"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":[{"Id":"","ScenicId":"","Title":"玩水","Tags":["玩水"]},{"Id":"","ScenicId":"","Title":"设施","Tags":["服务设施","游玩项目"]}]}"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /TopMenus/Update? [post]
func UpdateTopMenus(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
c.Header("Access-Control-Allow-Credentials", "true")
_user, _ := c.Get("UserInfo")
user := _user.(*DB.SMember)
err := Auth.CheckScenicAuth(c.PostForm("ScenicId"), user)
if err != nil {
c.JSON(200, tools.ResponseError{
401,
"没有权限",
})
return
}
if c.PostForm("Tags") == "" {
c.JSON(200, tools.ResponseError{
1,
"标签不能为空",
})
return
}
var Tags []string
json.Unmarshal([]byte(c.PostForm("Tags")), &Tags)
var id primitive.ObjectID
if pid := c.PostForm("id"); pid == "null" {
id = primitive.NewObjectID()
} else {
id, _ = primitive.ObjectIDFromHex(pid)
}
Order, err := strconv.ParseFloat(c.PostForm("Order"), 64)
upsert := true
DB.CTopMenus.FindOneAndUpdate(tools.GetContext(),
bson.M{"_id": id},
bson.M{"$set": bson.M{
"ScenicId": c.PostForm("ScenicId"),
"Name": c.PostForm("Name"),
"Order": Order,
"Tags": Tags,
}}, &options.FindOneAndUpdateOptions{
Upsert: &upsert,
},
)
c.JSON(200, tools.ResponseSeccess{
0,
"ok",
})
}
// @Title 删除菜单
// @Description 删除菜单
// @Accept json
// @Produce json
// @Param id 5dfb03070a9ac17ac7a82054 string true "公告id"
// @Param ScenicId 5dfb03070a9ac17ac7a82054 string true "景区id"
// @Param Token wgergejfwe string true "用户token"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":"ok"}"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /TopMenus/Remove? [post]
func RemoveTopMenus(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
c.Header("Access-Control-Allow-Credentials", "true")
_user, _ := c.Get("UserInfo")
user := _user.(*DB.SMember)
err := Auth.CheckScenicAuth(c.PostForm("ScenicId"), user)
if err != nil {
c.JSON(200, tools.ResponseError{
401,
"没有权限",
})
return
}
_, err = primitive.ObjectIDFromHex(c.PostForm("id"))
if err != nil {
c.JSON(200, tools.ResponseError{
1,
"id不正确",
})
return
}
objID, _ := primitive.ObjectIDFromHex(c.PostForm("id"))
DB.CTopMenus.DeleteOne(tools.GetContext(), bson.M{"_id": objID})
c.JSON(200, tools.ResponseSeccess{
0,
"ok",
})
}