TopMenus.go 3.23 KB
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"
)

// @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")

	_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 STopMenus []DB.STopMenus
	cur, err := DB.CTopMenus.Find(tools.GetContext(), bson.M{"ScenicId": c.PostForm("ScenicId")})
	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)
	}

	upsert := true
	DB.CTopMenus.FindOneAndUpdate(tools.GetContext(),
		bson.M{"_id": id},
		bson.M{"$set": bson.M{
			"ScenicId": c.PostForm("ScenicId"),
			"Title":    c.PostForm("Title"),
			"Tags":     Tags,
		}}, &options.FindOneAndUpdateOptions{
			Upsert: &upsert,
		},
	)

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

}