Blame view

API/Notice.go 7.62 KB
8bea6075   aarongao   new
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
package Api

import (
	"github.com/aarongao/tools"
	"github.com/asaskevich/govalidator"
	"github.com/gin-gonic/gin"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"letu/DB"
	"letu/Lib/Auth"
	"time"
)

// @Title 查询公告
// @Description 查询公告-单条
// @Accept  json
// @Produce  json
// @Param   id     5dfb03070a9ac17ac7a82054    string     true        "id"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":{"Id":"5e82d2539561231535f72958","Title":"公告2","Url":"http://www.google.cn","CreateTime":1585631827,"UpdateTime":0,"Expiry":1585670400,"ExpiryString":"今天"}}"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /Notice/Info? [get]
func NoticeInfo(c *gin.Context) {
	c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
	c.Header("Access-Control-Allow-Credentials", "true")

	var aNotice *DB.SNotice
	objId, _ := primitive.ObjectIDFromHex(c.Query("id"))
	DB.CNotice.FindOne(tools.GetContext(), bson.M{"_id": objId}).Decode(&aNotice)

	c.JSON(200, tools.ResponseSeccess{
		0,
		aNotice,
	})
}

// @Title 查询公告
// @Description 查询公告-列表
// @Accept  json
// @Produce  json
// @Param   ScenicId     wgergejfwe    string     true        "景区id"
// @Param   ExpiryState     all    string     false        "是否有效,默认只显示有效数据,==all所有数据"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":[{"Id":"5e82c27f41914b0fdcac489f","Title":"公告","Url":"http://www.google.cn","CreateTime":1585627775,"UpdateTime":0,"Expiry":1585670400,"ExpiryString":"今天"}...]}"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /Notice/List? [get]
func NoticeList(c *gin.Context) {
	c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
	c.Header("Access-Control-Allow-Credentials", "true")

	var aNotice []*DB.SNotice

	var _select primitive.M
	if c.Query("ExpiryState") == "all"{
		_select = bson.M{"ScenicId":c.Query("ScenicId")}
	}else{
		_select = bson.M{"Expiry": bson.M{"$gte": time.Now().Unix()},"ScenicId":c.Query("ScenicId")}
	}


	cur, err := DB.CNotice.Find(tools.GetContext(), _select)//
	defer cur.Close(tools.GetContext())
	if err == nil {
		for cur.Next(tools.GetContext()) {
			var e *DB.SNotice
			cur.Decode(&e)
			aNotice = append(aNotice, e)
		}
	}

	if aNotice == nil {
		aNotice = []*DB.SNotice{}
	}
	c.JSON(200, tools.ResponseSeccess{
		0,
		aNotice,
	})
}

// @Title 创建公告
// @Description 创建公告
// @Accept  json
// @Produce  json
// @Param   ScenicId     wgergejfwe    string     true        "景区id"
// @Param   Title     营业时间公告    string     true        "公告名称"
// @Param   Url     http://abc.com    string     true        "公告链接"
// @Param   ExpiryString     http://abc.com    string     true        "有效期(15分钟|1小时|今天|今年)"
// @Param   Token     wgergejfwe    string     true        "用户token"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":"ok"}"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /Notice/Create? [post]
func CreateNotice(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
	}

	var Expiry = getExp(c.PostForm("ExpiryString"))

	NowTime := time.Now()
	objectID := primitive.NewObjectID()
	Notice := &DB.SNotice{
		&objectID,
		c.PostForm("ScenicId"),
		c.PostForm("Title"),
		c.PostForm("Url"),
		NowTime.Unix(),
		0,
		Expiry,
		c.PostForm("ExpiryString"),
	}

	_, err = govalidator.ValidateStruct(Notice);
	if err != nil {
		c.JSON(200, tools.ResponseError{
			1,
			err.Error(),
		})
		return
	}

	_, err = DB.CNotice.InsertOne(tools.GetContext(), Notice)
	if err != nil {
		c.JSON(200, tools.ResponseError{
			1,
			err.Error(),
		})
		return
	}

	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 /Notice/Remove? [post]
func RemoveNotice(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.CNotice.DeleteOne(tools.GetContext(), bson.M{"_id": objID})

	c.JSON(200, tools.ResponseSeccess{
		0,
		"ok",
	})

}

// @Title 修改公告
// @Description 修改公告
// @Accept  json
// @Produce  json
// @Param   id     5dfb03070a9ac17ac7a82054    string     true        "公告id"
// @Param   ScenicId     wgergejfwe    string     true        "景区id"
// @Param   Title     营业时间公告    string     true        "公告名称"
// @Param   Url     http://abc.com    string     true        "公告链接"
// @Param   ExpiryString     http://abc.com    string     true        "有效期(15分钟|1小时|今天|今年)"
// @Param   Token     wgergejfwe    string     true        "用户token"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":"ok"}"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /Notice/Modify? [post]
func ModifyNotice(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
	}

	var Expiry = getExp(c.PostForm("ExpiryString"))

	NowTime := time.Now()
	objectID, _ := primitive.ObjectIDFromHex(c.PostForm("id"))

	Notice := &DB.SNotice{
		&objectID,
		c.PostForm("ScenicId"),
		c.PostForm("Title"),
		c.PostForm("Url"),
		0,
		NowTime.Unix(),
		Expiry,
		c.PostForm("ExpiryString"),
	}
	_, err = govalidator.ValidateStruct(Notice);
	if err != nil {
		c.JSON(200, tools.ResponseError{
			1,
			err.Error(),
		})
		return
	}

	_, err = DB.CNotice.UpdateOne(tools.GetContext(),
		bson.M{"_id": objectID},
		bson.M{"$set": Notice},
	)

	c.JSON(200, tools.ResponseSeccess{
		0,
		"ok",
	})

}

func getExp(ExpiryString string) int64 {

	var Expiry int64
	NowTime := time.Now()
	if ExpiryString == "15分钟" {
		Expiry = NowTime.Add(time.Minute * 15).Unix()
	}
	if ExpiryString == "1小时" {
		Expiry = NowTime.Add(time.Minute * 60).Unix()
	}
	if ExpiryString == "今天" {
		md := NowTime.Add(time.Hour * 24)
		Expiry = time.Date(md.Year(), md.Month(), md.Day(), 0, 0, 0, 0, NowTime.Location()).Unix()
	}
	if ExpiryString == "今年" {
		Expiry = time.Date(NowTime.Year()+1, 1, 1, 0, 0, 0, 0, NowTime.Location()).Unix()
	}
	return Expiry
}