Operator.go 5.09 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"
	"letu/DB"
	"letu/Lib/Auth"
	"letu/Lib/JWT"
	"time"
)

// @Title 登录
// @Description 操作员管理 - 操作员登录
// @Accept  json
// @Produce  json
// @Param   Username     aaron    string     true        "用户名"
// @Param   Password     123    string     true        "密码"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":{"Id":"5e09c64c1c09c6f0f7ca2fa9","Token":"640bf934e425aba5d3c90998b2641f2f0ca07261d334d9615d1cd4790b5f34e7"}} 调用其它需要登陆的接口时携带token,有过期时间"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /LoginOperator? [post]
func LoginOperator(c *gin.Context) {
	c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
	c.Header("Access-Control-Allow-Credentials", "true")

	selected := bson.M{}
	var User *DB.SMember
	if c.PostForm("Username") != "" && c.PostForm("Password") != "" {
		selected["Username"] = c.PostForm("Username")
		selected["Password"] = c.PostForm("Password")
		DB.CMember.FindOne(tools.GetContext(), selected).Decode(&User)

		if User == nil {
			c.JSON(200, tools.ResponseError{
				1,
				"用户不存在",
			})
			return
		}

	} else {
		c.JSON(200, tools.ResponseError{
			1,
			"不能为空",
		})
		return
	}

	// 生成token
	dd, _ := time.ParseDuration("8760h")
	User.Token, _ = JWT.CreateToken(User, time.Now().Add(dd).Unix())
	c.JSON(200, tools.ResponseSeccess{
		0,
		User,
	})

}

// @Title 操作员管理
// @Description 操作员管理 - 修改用户信息
// @Accept  json
// @Produce  json
// @Param   id     aaron    string     true        "用户id""
// @Param   ScenicId     5e0d504e24e03431008b4567    string     true        "景区id"
// @Param   Token     wgergejfwe    string     true        "用户token"
// @Param   Username     aarongao    string     true        "用户名"
// @Param   Remarks     18616619599    string     true        "备注"
// @Param   Password     12345    string     true        "密码"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":"ok"}"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /UpdateOperator? [post]
func UpdateOperator(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("Username") == "" || c.PostForm("Password") == "" {
		c.JSON(200, tools.ResponseError{
			1,
			"用户名密码不能为空",
		})
		return
	}

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

	objID, errForObjectID := primitive.ObjectIDFromHex(c.PostForm("id"))
	if errForObjectID == nil {

		_, err = DB.CMember.UpdateOne(tools.GetContext(),
			bson.M{"_id": objID},
			bson.M{"$set": bson.M{
				"Auth":     _auth,
				"Username": c.PostForm("Username"),
				"Password": c.PostForm("Password"),
				"Remarks":  c.PostForm("Remarks"),
			}},
		)
	} else {

		objectID := primitive.NewObjectID()
		User := &DB.SMember{
			Id:       &objectID,
			UserType: "operator",
			ScenicId: c.PostForm("ScenicId"),
			Username: c.PostForm("Username"),
			Password: c.PostForm("Password"),
			Auth:     _auth,
			Remarks:  c.PostForm("Remarks"),
		}

		_, err := DB.CMember.InsertOne(tools.GetContext(), User)
		if err != nil {

			c.JSON(200, tools.ResponseError{
				1,
				"用户名重复",
			})
			return
		}

	}

	if err == nil {
		c.JSON(200, tools.ResponseSeccess{
			0,
			"ok",
		})
	} else {
		c.JSON(200, tools.ResponseError{
			1,
			err.Error(),
		})
	}

}

// @Title 操作员管理
// @Description 操作员管理 - 所有操作员
// @Accept  json
// @Produce  json
// @Param   ScenicId     5dfb03070a9ac17ac7a82054    string     true        "景区id"
// @Param   Token     5dfb03070a9ac17ac7a82054    string     true        "用户token"
// @Success 200 {object} tools.ResponseSeccess ""
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /AllOperator? [get]
func AllOperator(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.Query("ScenicId"), user)
	if err != nil {
		c.JSON(200, tools.ResponseError{
			401,
			"没有权限",
		})
		return
	}

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

	if aMember == nil {
		aMember = []*DB.SMember{}
	}
	c.JSON(200, tools.ResponseSeccess{
		0,
		aMember,
	})
}