Explorar el Código

system下user token role等

terry hace 2 años
padre
commit
635ea722cd

+ 1 - 1
app/device/dao/common.go

@@ -4,7 +4,7 @@ import (
 	"fmt"
 	_ "github.com/go-sql-driver/mysql"
 	"github.com/jinzhu/gorm"
-	"iot_manager_service/app/user/dao"
+	"iot_manager_service/app/system/dao"
 )
 
 var Db *gorm.DB

app/user/controller/role.go → app/system/controller/role.go


app/user/controller/tenant.go → app/system/controller/tenant.go


+ 16 - 12
app/user/controller/token.go

@@ -6,18 +6,18 @@ import (
 	"strconv"
 	"time"
 
-	"iot_manager_service/app/user/model"
+	"iot_manager_service/app/system/model"
 	"iot_manager_service/config"
 	"iot_manager_service/util"
 	"net/http"
 	"strings"
 )
 
-var Auth = new(auth)
+var Token = new(token)
 
-type auth struct{}
+type token struct{}
 
-func (c *auth) Token(ctx *gin.Context) {
+func (c *token) Token(ctx *gin.Context) {
 	tenantId := ctx.Query("tenantId")
 	userName := ctx.Query("username")
 	password := ctx.Query("password")
@@ -26,7 +26,7 @@ func (c *auth) Token(ctx *gin.Context) {
 
 	checkLock()
 
-	userType := ctx.GetHeader(model.USER_TYPE_HEADER_KEY)
+	userType := ctx.GetHeader(model.UserTypeHeaderKey)
 	token := model.Token{
 		TenantId:     tenantId,
 		UserName:     userName,
@@ -41,11 +41,11 @@ func (c *auth) Token(ctx *gin.Context) {
 		return
 	}
 	if userInfo == nil || userInfo.User == nil {
-		ctx.JSON(http.StatusOK, util.NormalResponse(http.StatusBadRequest, model.USER_NOT_FOUND, nil))
+		ctx.JSON(http.StatusOK, util.NormalResponse(http.StatusBadRequest, model.UserNotFound, nil))
 		return
 	}
 	if len(userInfo.Roles) == 0 {
-		ctx.JSON(http.StatusOK, util.NormalResponse(http.StatusBadRequest, model.USER_HAS_NO_ROLE, nil))
+		ctx.JSON(http.StatusOK, util.NormalResponse(http.StatusBadRequest, model.UserHasNoRole, nil))
 		return
 	}
 
@@ -116,13 +116,13 @@ func getRefreshToken(info model.UserInfo) string {
 
 func grant(token model.Token, ctx *gin.Context) (*model.UserInfo, *util.Errors) {
 	info := &model.UserInfo{}
-	key := ctx.GetHeader(model.CAPTCHA_HEADER_KEY)
-	code := ctx.GetHeader(model.CAPTCHA_HEADER_CODE)
+	key := ctx.GetHeader(model.CaptchaHeaderKey)
+	code := ctx.GetHeader(model.CaptchaHeaderCode)
 	// 获取验证码
-	redisCode := util.Redis.Get(model.CAPTCHA_KEY + key).String()
+	redisCode := util.Redis.Get(model.CaptchaKey + key).String()
 	// 判断验证码
 	if config.Instance().Server.CodeEnable && (code == "" || !strings.EqualFold(redisCode, code)) {
-		return nil, util.NormalResponse(http.StatusBadRequest, model.CAPTCHA_NOT_CORRECT, nil)
+		return nil, util.NormalResponse(http.StatusBadRequest, model.CaptchaNotCorrect, nil)
 	}
 
 	if token.UserName != "" && token.Password != "" {
@@ -133,7 +133,11 @@ func grant(token model.Token, ctx *gin.Context) (*model.UserInfo, *util.Errors)
 		//}
 		// 获取用户类型
 		// 根据不同用户类型调用对应的接口返回数据,用户可自行拓展
-		// info.User = userService.GetUser(token.tenantId, token.UserName, token.password)
+		// info.Token = userService.GetUser(token.tenantId, token.UserName, token.password)
 	}
 	return info, nil
 }
+
+func (c *token) Captcha(ctx *gin.Context) {
+
+}

app/user/controller/user.go → app/system/controller/user.go


app/user/dao/common.go → app/system/dao/common.go


app/user/dao/user.go → app/system/dao/user.go


+ 8 - 11
app/user/model/token.go

@@ -1,6 +1,6 @@
 package model
 
-import "iot_manager_service/app/user/dao"
+import "iot_manager_service/app/system/dao"
 
 type Token struct {
 	TenantId     string
@@ -59,20 +59,17 @@ const (
 )
 
 const (
-	CAPTCHA_HEADER_KEY            = "Captcha-Key"
-	CAPTCHA_HEADER_CODE           = "Captcha-Code"
-	CAPTCHA_NOT_CORRECT           = "验证码不正确"
-	TENANT_HEADER_KEY             = "Tenant-Id"
-	DEFAULT_TENANT_ID             = "000000"
-	USER_TYPE_HEADER_KEY          = "User-Type"
-	DEFAULT_USER_TYPE             = "web"
-	USER_NOT_FOUND                = "用户名或密码错误"
-	USER_HAS_NO_ROLE              = "未获得用户的角色信息"
+	CaptchaHeaderKey              = "Captcha-Key"
+	CaptchaHeaderCode             = "Captcha-Code"
+	CaptchaNotCorrect             = "验证码不正确"
+	UserTypeHeaderKey             = "User-Type"
+	UserNotFound                  = "用户名或密码错误"
+	UserHasNoRole                 = "未获得用户的角色信息"
 	USER_HAS_NO_TENANT            = "未获得用户的租户信息"
 	USER_HAS_NO_TENANT_PERMISSION = "租户授权已过期,请联系管理员"
 	HEADER_KEY                    = "Authorization"
 	HEADER_PREFIX                 = "Basic "
 	DEFAULT_AVATAR                = "https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png"
 
-	CAPTCHA_KEY = "blade:auth::blade:captcha:"
+	CaptchaKey = "blade:auth::blade:captcha:"
 )

+ 2 - 2
main.go

@@ -4,7 +4,7 @@ import (
 	"github.com/gin-gonic/gin"
 	"github.com/jinzhu/gorm"
 	device "iot_manager_service/app/device/dao"
-	user "iot_manager_service/app/user/dao"
+	system "iot_manager_service/app/system/dao"
 	"iot_manager_service/config"
 	_ "iot_manager_service/config"
 	"iot_manager_service/router"
@@ -40,5 +40,5 @@ func initDB() {
 		db.LogMode(false)
 	}
 	device.InitDB(db)
-	user.InitDB(db)
+	system.InitDB(db)
 }

+ 4 - 2
router/router.go

@@ -4,7 +4,7 @@ import (
 	"github.com/gin-gonic/gin"
 	"iot_manager_service/app/device/controller"
 	"iot_manager_service/app/middleware"
-	user "iot_manager_service/app/user/controller"
+	user "iot_manager_service/app/system/controller"
 )
 
 func InitRouter(engine *gin.Engine) {
@@ -334,8 +334,10 @@ func InitRouter(engine *gin.Engine) {
 		onDemandLightingControl.POST("/changHandSwitch", controller.OnDemandLightingControl.ChangeHandSwitch)
 	}
 
+	//登录校验相关
 	auth := engine.Group("/api/blade-auth")
 	{
-		auth.POST("/oauth/token", user.Auth.Token)
+		auth.POST("/oauth/token", user.Token.Token)
+		auth.POST("/oauth/captcha", user.Token.Captcha)
 	}
 }

+ 1 - 1
util/redis.go

@@ -4,7 +4,7 @@ import (
 	"fmt"
 	"github.com/go-redis/redis"
 	"iot_manager_service/app/device/dao"
-	dao2 "iot_manager_service/app/user/dao"
+	dao2 "iot_manager_service/app/system/dao"
 	"iot_manager_service/config"
 	"strconv"
 	"time"