Icon.go 3.42 KB
package Api

import (
	"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   id     5dfb03070a9ac17ac7a82054    string     true        "图标id"
// @Success 200 {object} tools.ResponseSeccess "Name名称:Picture图片地址:id图标id"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /Icon/Info? [get]
func IconInfo(c *gin.Context) {
	c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
	c.Header("Access-Control-Allow-Credentials", "true")

	if c.Query("id") == "" {
		c.JSON(200, tools.ResponseError{
			1,
			"id为空",
		})
		return
	}

	var SIcon *DB.SIcons
	objID, _ := primitive.ObjectIDFromHex(c.Query("id"))
	DB.CIcons.FindOne(tools.GetContext(), bson.M{"_id": objID}).Decode(&SIcon)

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

}

// @Title 更新图标信息
// @Description 图标管理 - 增加或修改图标信息
// @Accept  json
// @Produce  json
// @Param   id     5dfb03070a9ac17ac7a82054    string     true        "图标id"
// @Param   ScenicId     5e0d504e24e03431008b4567    string     true        "景区id"
// @Param   Token     wgergejfwe    string     true        "用户token"
// @Success 200 {object} tools.ResponseSeccess "Name名称:Picture图片地址:id图标id:ScenicId景区id"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /Icon/Update? [post]
func UpdateIcon(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 id primitive.ObjectID
	if pid := c.PostForm("id"); pid == "null" {
		id = primitive.NewObjectID()
	} else {
		id, _ = primitive.ObjectIDFromHex(pid)
	}

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

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

}

// @Title 所有图标
// @Description 图标管理 - 所有图标
// @Accept  json
// @Produce  json
// @Param   ScenicId     5dfb03070a9ac17ac7a82054    string     true        "景区id"
// @Success 200 {object} tools.ResponseSeccess "Name名称:Picture图片地址:id图标id:ScenicId景区id"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /Icon/All? [get]
func AllIcons(c *gin.Context) {
	c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
	c.Header("Access-Control-Allow-Credentials", "true")

	var SIcons = []DB.SIcons{}
	cur, err := DB.CIcons.Find(tools.GetContext(), bson.M{"ScenicId": c.Query("ScenicId")})
	defer cur.Close(tools.GetContext())
	if err == nil {
		for cur.Next(tools.GetContext()) {
			var e DB.SIcons
			cur.Decode(&e)
			SIcons = append(SIcons, e)
		}
	}

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

}