TopMenus.go 2.2 KB
package Api

import (
	"encoding/json"
	"github.com/aarongao/tools"
	"github.com/gin-gonic/gin"
	"gopkg.in/mgo.v2/bson"
	"letu/DB"
	"letu/Lib/LeYouTu"
)

// @Title 查询所有菜单
// @Description 菜单管理 - 查询所有菜单
// @Accept  json
// @Produce  json
// @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")

	ScenicId, err := LeYouTu.GetScenicId(c)
	if err != nil {
		return
	}

	var STopMenus []*DB.STopMenus
	DB.CTopMenus.Find(bson.M{"ScenicId": ScenicId}).All(&STopMenus)

	if STopMenus == nil {
		STopMenus = []*DB.STopMenus{}
	}

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

}

// @Title 更新菜单
// @Description 菜单管理 - 更新菜单
// @Accept  json
// @Produce  json
// @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")

	ScenicId, err := LeYouTu.GetScenicId(c)
	if err != nil {
		return
	}

	if c.PostForm("Tags") == "" {
		c.JSON(200, tools.ResponseError{
			1,
			"标签不能为空",
		})
		return
	}

	var Tags []string
	json.Unmarshal([]byte(c.PostForm("Tags")), &Tags)

	var id bson.ObjectId
	if pid := c.PostForm("id"); pid == "null" {
		id = bson.NewObjectId()
	} else {
		id = bson.ObjectIdHex(pid)
	}

	DB.CTopMenus.UpsertId(
		id,
		bson.M{"$set": bson.M{
			"ScenicId": ScenicId,
			"Title":    c.PostForm("Title"),
			"Tags":     Tags,
		}},
	)

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

}