admin_login_controller.go 4.9 KB
package controllers

import (
	"encoding/json"
	"errors"
	"fmt"
	"time"

	"github.com/GeeTeam/gt3-golang-sdk/geetest"
	"github.com/astaxie/beego/logs"
	userCmd "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/users/command"
	userService "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/users/service"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
)

type AdminLoginController struct {
	BaseController
}

////Prepare 重写 BaseController 的Prepare方法
func (c *AdminLoginController) Prepare() {
	c.BaseController.Prepare()
	//排除登录 以及获取图形验证
	urlIn := map[string]int{
		"/v1/auth/captcha-init": 1,
		"/v1/auth/login":        1,
		"/v1/auth/login2":       1,
	}

	nowUrl := c.Ctx.Input.URL()
	if _, ok := urlIn[nowUrl]; ok {
		//跳过校验
		return
	}
	if ok := c.ValidJWTToken(); !ok {
		return
	}
}

//Login 用户登录
// func (c *AdminLoginController) Login() {
// 	type Paramter struct {
// 		Username string `json:"username"`
// 		Password string `json:"password"`
// 	}
// 	var (
// 		param Paramter
// 		err   error
// 	)
// 	if err = c.BindJsonData(&param); err != nil {
// 		c.ResponseError(fmt.Errorf("json解析失败:%s", err))
// 		return
// 	}
// 	newAdminuserquery := adminuserquery.GetAdminUserQuery{AdminAccount: param.Username}
// 	newAdminUserService := adminuserservice.NewAdminUserService(nil)
// 	adminuser, err := newAdminUserService.GetAdminUser(&newAdminuserquery)
// 	if err != nil {
// 		logs.Error("获取用户数据失败:%s", err)
// 		c.ResponseError(errors.New("用户不存在"))
// 		return
// 	}
// 	if adminuser.Password != param.Password {
// 		c.ResponseError(errors.New("账号或密码错误"))
// 		return
// 	}
// 	if !adminuser.IsUsable {
// 		c.ResponseError(errors.New("用户被禁用"))
// 	}
// 	//TODO
// 	newJwt := lib.NewMyToken(adminuser.Id, 0)
// 	newToken, err := newJwt.CreateJWTToken()
// 	if err != nil {
// 		logs.Error("生成jwt数据失败:%s", err)
// 		c.ResponseError(errors.New("服务异常"))
// 		return
// 	}
// 	rspdata := map[string]interface{}{
// 		"access": map[string]interface{}{
// 			"accessToken": newToken,
// 			"expiresIn":   lib.JWtExpiresSecond,
// 		},
// 	}
// 	c.ResponseData(rspdata)
// 	return
// }

func (c *AdminLoginController) Login() {
	var (
		param userCmd.LoginBySecretKeyCommand
	)
	if err := c.BindJsonData(&param); err != nil {
		c.ResponseError(fmt.Errorf("json解析失败:%s", err))
		return
	}
	newUserService := userService.NewUsersService(nil)
	resp, err := newUserService.UserLoginBySecretKey(param)
	if err != nil {
		c.ResponseError(err)
		return
	}
	c.ResponseData(resp)
	return
}

//Login2 开发临时使用
func (c *AdminLoginController) Login2() {
	newToken := `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1OTk4MzExNTQsImlhdCI6MTU5OTc4Nzk1NCwiaXNzIjoibW1tX3BhcnRuZXJtZyIsIm5iZiI6MTU5OTc4Nzk1NCwidWlkIjozMjU0ODUxNzgyMTYzMjAwLCJjb21wYW55SWQiOjF9.GuBx5awkHtvgDddWLquzfRFam6FpkNlBvKXci7_gSYA`
	returnData := map[string]interface{}{
		"access": map[string]interface{}{
			"accessToken": newToken,
			"expiresIn":   lib.JWtExpiresSecond,
		},
	}
	c.ResponseData(returnData)
	return
}

//CaptchaInit 极验初始化
func (c *AdminLoginController) CaptchaInit() {
	const (
		captchaID  = "33a2abf9c5df0d6bc3b89fb39280114b"
		privateKey = "13320fd2b10199e9a2440a4fbb4d46f7"
	)
	newGeetest := geetest.NewGeetestLib(captchaID, privateKey, 2*time.Second)
	userip := c.Ctx.Input.IP()
	_, responseBt := newGeetest.PreProcess("", userip)
	// c.SetSession("geetest_status", status)
	var geetestRsp geetest.FailbackRegisterRespnse
	json.Unmarshal(responseBt, &geetestRsp)
	//对前端定义的数据格式进行适配。。。
	rspData := map[string]interface{}{
		"success":    geetestRsp.Success,
		"gt":         geetestRsp.GT,
		"challenge":  geetestRsp.Challenge,
		"newCaptcha": geetestRsp.NewCaptcha,
	}
	c.ResponseData(rspData)
	return
}

//AdminpPofile 获取登录用户的权限配置
func (c *AdminLoginController) AdminpPofile() {
	adminId := c.GetUserId()
	newUserService := userService.NewUsersService(nil)
	respData, err := newUserService.GetUserPofile(adminId)
	if err != nil {
		logs.Error("获取用户数据失败:%s", err)
		c.ResponseError(errors.New("用户不存在"))
		return
	}
	c.ResponseData(respData)
	return
}

// func (c *AdminLoginController) PwdUpdate() {
// 	//用与适配前端定义的数据结构
// 	type Paramater struct {
// 		Pwd string `json:"pwd"`
// 	}
// 	var (
// 		param Paramater
// 		err   error
// 	)
// 	if err = c.BindJsonData(&param); err != nil {
// 		logs.Error(err)
// 		c.ResponseError(errors.New("json数据解析失败"))
// 		return
// 	}
// 	uid := c.GetUserId()
// 	newAdminUserService := adminuserservice.NewAdminUserService(nil)
// 	cmd := adminuserCmd.UpdateAdminUserPwdCommand{
// 		Id:       uid,
// 		Password: param.Pwd,
// 	}
// 	err = newAdminUserService.UpdateAdminPassword(cmd)
// 	if err != nil {
// 		c.ResponseError(err)
// 		return
// 	}
// 	c.ResponseData(nil)
// 	return
// }