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 }