Browse Source

ADD:登录添加手机标识

zoie 11 months ago
parent
commit
f969a48ad3
2 changed files with 14 additions and 6 deletions
  1. 1 1
      model/user.go
  2. 13 5
      pkg/jwtauth/jwtauth.go

+ 1 - 1
model/user.go

@@ -10,7 +10,7 @@ type User struct {
 	Salt     string `json:"-" gorm:"size:255;comment:加盐"`
 	DeptId   int    `json:"deptId" gorm:"comment:部门"`                             // 部门id
 	PostId   int    `json:"postId" gorm:"comment:岗位"`                             // 岗位id
-	Remark   string `json:"remark" gorm:"size:255;comment:备注"`                    // 备注
+	Remark   string `json:"remark,omitempty" gorm:"size:255;comment:备注"`                    // 备注
 	Status   string `json:"status" gorm:"size:4;not null;default:'2';comment:状态"` // 1-停用 2-正常
 }
 

+ 13 - 5
pkg/jwtauth/jwtauth.go

@@ -148,6 +148,7 @@ type GinJWTMiddleware struct {
 	// 保存token的key
 	SaveNewestToken func(c *gin.Context, userId int64, token string, expire int64) error
 	GetNewestToken  func(c *gin.Context, userId int64) (string, error)
+	SetEnterDeptId  func(c *gin.Context, newTToken string,userId int64) error
 }
 
 var (
@@ -166,7 +167,7 @@ var (
 	// ErrFailedAuthentication indicates authentication failed, could be faulty username or password
 	//ErrFailedAuthentication = errors.New("incorrect Username or Password")
 	ErrFailedAuthentication = errors.New("用户名或密码不正确")
-	ErrFailedSmsVerifyCode = errors.New("短信验证码错误")
+	ErrFailedSmsVerifyCode  = errors.New("短信验证码错误")
 
 	ErrAccountDeactivated = errors.New("账号已停用")
 
@@ -237,6 +238,7 @@ var (
 	// DeptIdKey 部门  Old
 	DeptIdKey   = "deptId"
 	DeptNameKey = "deptName"
+	RandomKey   = "randomName"
 )
 
 // New for check error with GinJWTMiddleware
@@ -496,13 +498,16 @@ func (mw *GinJWTMiddleware) LoginHandler(c *gin.Context) {
 	expire := mw.TimeFunc().Add(mw.Timeout)
 
 	// 登录类型 手机登录
-	if t,ok := claims["mobile"];ok{
-		if t.(bool) {
-			expire = mw.TimeFunc().Add(time.Duration(876000) * time.Hour)
-
+	if v, ok := data.(map[string]interface{}); ok {
+		if t, ok2 := v["mobile"].(bool); ok2 {
+			if t {
+				expire = mw.TimeFunc().Add(time.Duration(876000/2) * time.Hour)
+				//mw.Timeout = time.Duration(876000) * time.Hour
+			}
 		}
 	}
 
+
 	claims["exp"] = expire.Unix()
 	claims["orig_iat"] = mw.TimeFunc().Unix()
 	tokenString, err := mw.signedString(token)
@@ -589,6 +594,9 @@ func (mw *GinJWTMiddleware) RefreshToken(c *gin.Context) (string, time.Time, err
 			mw.CookieHTTPOnly,
 		)
 	}
+	if mw.SetEnterDeptId != nil {
+		mw.SetEnterDeptId(c,tokenString,int64(claims["identity"].(float64)))
+	}
 
 	if claims["single"].(bool) {
 		_ = mw.SaveNewestToken(c, int64(claims["identity"].(float64)), tokenString, int64(mw.Timeout)/3600)