From 8bea60754bf2e534b709d6d702990895915b9c51 Mon Sep 17 00:00:00 2001 From: aarongao Date: Tue, 31 Mar 2020 14:19:52 +0800 Subject: [PATCH] new --- API/Notice.go | 273 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ DB/db.go | 14 +++++++++++++- README.md | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Version.md | 12 ++++++++++++ main.go | 6 ++++++ 5 files changed, 412 insertions(+), 1 deletion(-) create mode 100644 API/Notice.go diff --git a/API/Notice.go b/API/Notice.go new file mode 100644 index 0000000..893bf78 --- /dev/null +++ b/API/Notice.go @@ -0,0 +1,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 +} diff --git a/DB/db.go b/DB/db.go index 3ff30d3..3208d7e 100644 --- a/DB/db.go +++ b/DB/db.go @@ -22,6 +22,7 @@ var CTrajectory *mongo.Collection //移动轨迹 var CIcons *mongo.Collection //图标信息 var CTopMenus *mongo.Collection //菜单 var CDevice *mongo.Collection //设备清单 +var CNotice *mongo.Collection //公告 var DB *mongo.Database type SItem struct { @@ -51,6 +52,17 @@ type SModel struct { Model string `bson:"Model" json:"Model"` Action string `bson:"Action" json:"Action"` } + +type SNotice struct { + Id *primitive.ObjectID `bson:"_id" json:"Id" valid:"required"` + ScenicId string `bson:"ScenicId" json:"ScenicId" valid:"required"` + Title string `bson:"Title" json:"Title" valid:"required"` + Url string `bson:"Url" json:"Url" valid:"required"` + CreateTime int64 `bson:"CreateTime,omitempty" json:"CreateTime"` //创建时间 + UpdateTime int64 `bson:"UpdateTime,omitempty" json:"UpdateTime"` //最后一次修改时间 + Expiry int64 `bson:"Expiry" json:"Expiry"` //有效期 + ExpiryString string `bson:"ExpiryString" json:"ExpiryString" valid:"required,in(15分钟|1小时|今天|今年)"` //有效期 +} type SIcons struct { Id *primitive.ObjectID `bson:"_id" json:"Id" valid:"required"` ScenicId string `bson:"ScenicId" json:"ScenicId"` @@ -149,7 +161,7 @@ type SInvestigation struct { } type SMember struct { Id *primitive.ObjectID `bson:"_id" json:"Id" valid:"required"` - UserType string `bson:"UserType" json:"UserType"` // "root" or "operator" or "visitor" + UserType string `bson:"UserType" json:"UserType" valid:"required,in(root|operator|visitor)"` // "root" or "operator" or "visitor" ScenicId string `bson:"ScenicId,omitempty" json:"ScenicId,omitempty"` Username string `bson:"Username,omitempty" json:"Username,omitempty"` Password string `bson:"Password,omitempty" json:"Password,omitempty"` diff --git a/README.md b/README.md index f84fd58..28f150e 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,11 @@ 1. [查询线路信息](#lineinfo-get) 1. [操作员管理 - 操作员登录](#loginoperator-post) 1. [用户管理 - 用户登录&注册](#loginuser-post) +1. [查询公告-单条](#notice-info-get) +1. [查询公告-列表](#notice-list-get) +1. [创建公告](#notice-create-post) +1. [删除公告](#notice-remove-post) +1. [修改公告](#notice-modify-post) 1. [用户管理 - 注册客户端](#registerdevice-post) 1. [用户管理 - 删除用户(注销)](#removeuser-post) 1. [返回景区基础信息](#scenicinfo-get) @@ -545,6 +550,109 @@ + + +#### /Notice/Info (GET) + + +查询公告-单条 + +| Param Name | Example | Data Type | Description | Required? | +|-----|-----|-----|-----|-----| +| id | 5dfb03070a9ac17ac7a82054 | string | id | Yes | + + +| Code | Type | Model | Message | +|-----|-----|-----|-----| +| 200 | object | [ResponseSeccess](#github.com.aarongao.tools.ResponseSeccess) | {"errcode":0,"result":{"Id":"5e82d2539561231535f72958","Title":"公告2","Url":"http://www.google.cn","CreateTime":1585631827,"UpdateTime":0,"Expiry":1585670400,"ExpiryString":"今天"}} | +| 500 | object | [ResponseError](#github.com.aarongao.tools.ResponseError) | {"errcode":1,"errmsg":"错误原因"} | + + + + +#### /Notice/List (GET) + + +查询公告-列表 + +| Param Name | Example | Data Type | Description | Required? | +|-----|-----|-----|-----|-----| +| ScenicId | wgergejfwe | string | 景区id | Yes | +| ExpiryState | all | string | 是否有效,默认只显示有效数据,==all所有数据 | | + + +| Code | Type | Model | Message | +|-----|-----|-----|-----| +| 200 | object | [ResponseSeccess](#github.com.aarongao.tools.ResponseSeccess) | {"errcode":0,"result":[{"Id":"5e82c27f41914b0fdcac489f","Title":"公告","Url":"http://www.google.cn","CreateTime":1585627775,"UpdateTime":0,"Expiry":1585670400,"ExpiryString":"今天"}...]} | +| 500 | object | [ResponseError](#github.com.aarongao.tools.ResponseError) | {"errcode":1,"errmsg":"错误原因"} | + + + + +#### /Notice/Create (POST) + + +创建公告 + +| Param Name | Example | Data Type | Description | Required? | +|-----|-----|-----|-----|-----| +| ScenicId | wgergejfwe | string | 景区id | Yes | +| Title | 营业时间公告 | string | 公告名称 | Yes | +| Url | http://abc.com | string | 公告链接 | Yes | +| ExpiryString | http://abc.com | string | 有效期(15分钟|1小时|今天|今年) | Yes | +| Token | wgergejfwe | string | 用户token | Yes | + + +| Code | Type | Model | Message | +|-----|-----|-----|-----| +| 200 | object | [ResponseSeccess](#github.com.aarongao.tools.ResponseSeccess) | {"errcode":0,"result":"ok"} | +| 500 | object | [ResponseError](#github.com.aarongao.tools.ResponseError) | {"errcode":1,"errmsg":"错误原因"} | + + + + +#### /Notice/Remove (POST) + + +删除公告 + +| Param Name | Example | Data Type | Description | Required? | +|-----|-----|-----|-----|-----| +| id | 5dfb03070a9ac17ac7a82054 | string | 公告id | Yes | +| ScenicId | 5dfb03070a9ac17ac7a82054 | string | 景区id | Yes | +| Token | wgergejfwe | string | 用户token | Yes | + + +| Code | Type | Model | Message | +|-----|-----|-----|-----| +| 200 | object | [ResponseSeccess](#github.com.aarongao.tools.ResponseSeccess) | {"errcode":0,"result":"ok"} | +| 500 | object | [ResponseError](#github.com.aarongao.tools.ResponseError) | {"errcode":1,"errmsg":"错误原因"} | + + + + +#### /Notice/Modify (POST) + + +修改公告 + +| Param Name | Example | Data Type | Description | Required? | +|-----|-----|-----|-----|-----| +| id | 5dfb03070a9ac17ac7a82054 | string | 公告id | Yes | +| ScenicId | wgergejfwe | string | 景区id | Yes | +| Title | 营业时间公告 | string | 公告名称 | Yes | +| Url | http://abc.com | string | 公告链接 | Yes | +| ExpiryString | http://abc.com | string | 有效期(15分钟|1小时|今天|今年) | Yes | +| Token | wgergejfwe | string | 用户token | Yes | + + +| Code | Type | Model | Message | +|-----|-----|-----|-----| +| 200 | object | [ResponseSeccess](#github.com.aarongao.tools.ResponseSeccess) | {"errcode":0,"result":"ok"} | +| 500 | object | [ResponseError](#github.com.aarongao.tools.ResponseError) | {"errcode":1,"errmsg":"错误原因"} | + + + #### /RegisterDevice (POST) diff --git a/Version.md b/Version.md index 5d508b2..49e91f2 100644 --- a/Version.md +++ b/Version.md @@ -20,6 +20,18 @@ } ``` +3. 新增加公告管理模块接口 + + ``` + /Notice/Info + /Notice/List + /Notice/Create(需要携带Token) + /Notice/Remove(需要携带Token) + /Notice/Modify(需要携带Token) + ``` + + + ##### 发布流程: 1. 提交APP代码到git(v1.1分支) diff --git a/main.go b/main.go index 3470bdf..a968249 100644 --- a/main.go +++ b/main.go @@ -94,6 +94,7 @@ func main() { DB.CIcons = DB.DB.Collection("Icons") DB.CTopMenus = DB.DB.Collection("TopMenus") DB.CDevice = DB.DB.Collection("Device") + DB.CNotice = DB.DB.Collection("Notice") DelayMessage.CDelayMessage = DB.DB.Collection("DelayMessage") DelayMessage.CDelayErrorLog = DB.DB.Collection("DelayErrorLog") @@ -145,6 +146,11 @@ func main() { InitController("POST", "/DealyMessage/Create", Api.CreateDealyMessage, &DB.SModel{"通知管理", "增加"}) InitController("GET", "/DealyMessage/Info", Api.DealyMessageInfo, &DB.SModel{"通知管理", "查看所有"}) InitController("POST", "/DealyMessage/Remove", Api.RemoveDealyMessage, &DB.SModel{"通知管理", "删除"}) + InitController("POST", "/Notice/Create", Api.CreateNotice, &DB.SModel{"公告管理", "增加"}) + InitController("GET", "/Notice/Info", Api.NoticeInfo, &DB.SModel{}) + InitController("GET", "/Notice/List", Api.NoticeList, &DB.SModel{}) + InitController("POST", "/Notice/Remove", Api.RemoveNotice, &DB.SModel{"公告管理", "删除"}) + InitController("POST", "/Notice/Modify", Api.ModifyNotice, &DB.SModel{"公告管理", "修改"}) InitController("POST", "/Icon/Update", Api.UpdateIcon, &DB.SModel{"图标管理", "修改管理"}) InitController("GET", "/Icon/All", Api.AllIcons, &DB.SModel{}) InitController("GET", "/Icon/Info", Api.IconInfo, &DB.SModel{}) -- libgit2 0.21.0