Browse Source

user crud,登录完善

terry 2 years ago
parent
commit
25b978a356

+ 25 - 9
app/system/controller/token.go

@@ -6,6 +6,7 @@ import (
 	"github.com/golang-jwt/jwt"
 	"github.com/mojocn/base64Captcha"
 	"github.com/satori/go.uuid"
+	"iot_manager_service/app/system/dao"
 	"iot_manager_service/app/system/service"
 	"strconv"
 	"time"
@@ -95,7 +96,7 @@ func (c *auth) Token(ctx *gin.Context) {
 
 func (c *auth) Logout(ctx *gin.Context) {
 	emptyKeyFunc := func(t *jwt.Token) (interface{}, error) { return []byte(config.Instance().Server.TokenSign), nil }
-	authorization := ctx.GetHeader("Authorization")
+	authorization := ctx.GetHeader(model.HeaderKey)
 	token, err := jwt.ParseWithClaims(authorization, &model.JwtToken{}, emptyKeyFunc)
 	if err != nil {
 		ctx.JSON(http.StatusUnauthorized, util.NormalResponse(http.StatusUnauthorized, err.Error(), nil))
@@ -127,13 +128,16 @@ func (c *auth) Clear(ctx *gin.Context) {
 
 func (c *auth) Login(ctx *gin.Context) {
 	passKey := ctx.Query("passKey")
-	tenant, err := service.TenantService.Get(passKey)
+	tenant, err := service.TenantService.GetTenantByPasskey(passKey)
 	if err != nil {
 		ctx.JSON(http.StatusOK, util.SuccessResponse(model.TenantNotFound, nil))
 		return
 	}
 	rsp := model.RspLogin{
-		ID: tenant.TenantId,
+		ID:            tenant.TenantId,
+		Name:          tenant.LoginDisplayName,
+		BackgroundUrl: tenant.BackgroundUrl,
+		SysLogoUrl:    tenant.SysLogoUrl,
 	}
 	ctx.JSON(http.StatusOK, util.SuccessResponse(util.Success, rsp))
 }
@@ -200,17 +204,19 @@ func grant(token model.Token, ctx *gin.Context) (*model.UserInfo, *util.Errors)
 
 	if token.UserName != "" && token.Password != "" {
 		// 获取租户信息
-		//Tenant tenant = tenantService.getByTenantId(tenantId);
-		//if (TokenUtil.judgeTenant(tenant)) {
-		//    throw new ServiceException(TokenUtil.USER_HAS_NO_TENANT_PERMISSION);
-		//}
+		tenant, _ := service.TenantService.GetOne(token.TenantId)
+		if tenant == nil {
+			return nil, util.NormalResponse(http.StatusOK, model.UserHasNoTenant, nil)
+		}
+		if judgeTenant(tenant) {
+			return nil, util.NormalResponse(http.StatusOK, model.UserHasNoTenantPermission, nil)
+		}
 		// 获取用户类型
 		// 根据不同用户类型调用对应的接口返回数据,用户可自行拓展
-		// info.Auth = userService.GetUser(auth.tenantId, auth.UserName, auth.password)
+		info.User, _ = service.UserService.GetOne(token.TenantId, token.UserName, token.Password)
 	}
 
 	//测试代码start
-
 	info.TenantId = "000000"
 	info.ID = 11112222
 	info.Roles = []string{"admin"}
@@ -219,6 +225,16 @@ func grant(token model.Token, ctx *gin.Context) (*model.UserInfo, *util.Errors)
 	return info, nil
 }
 
+func judgeTenant(tenant *dao.Tenant) bool {
+	if tenant.TenantId == model.AdminTenantId {
+		return false
+	}
+	if tenant.ExpireTime.IsZero() || tenant.ExpireTime.Before(time.Now()) {
+		return true
+	}
+	return false
+}
+
 func getAccessTokenKey(tenantId string, uId int64, random string) string {
 	return fmt.Sprintf("access_token_%s_%d_%s", tenantId, uId, random)
 }

+ 53 - 25
app/system/dao/tenant.go

@@ -3,29 +3,57 @@ package dao
 import "time"
 
 type Tenant struct {
-	ID                      int64     `gorm:"primary_key" json:"id"`                             //编号
-	TenantId                string    `gorm:"type:varchar(12);default '000000'" json:"tenantId"` //租户id
-	Code                    string    `gorm:"type:varchar(12)" json:"code"`                      //用户编号
-	Account                 string    `gorm:"type:varchar(12)" json:"account"`                   //账号
-	Password                string    `gorm:"type:varchar(12)" json:"password"`                  //密码
-	Name                    string    `gorm:"type:varchar(12)" json:"name"`                      //昵称
-	RealName                string    `gorm:"type:varchar(12)" json:"real_name"`                 //真名
-	Avatar                  string    `gorm:"type:varchar(12)" json:"avatar"`                    //头像
-	Email                   string    `gorm:"type:varchar(12)" json:"email"`                     //邮箱
-	Phone                   string    `gorm:"type:varchar(12)" json:"phone"`                     //手机
-	Birthday                string    `gorm:"type:datetime" json:"birthday"`                     //生日
-	Sex                     int       `gorm:"type:smallint" json:"sex"`                          //生日
-	RoleId                  string    `gorm:"type:varchar(1000)" json:"role_id"`                 //角色id
-	DeptId                  string    `gorm:"type:varchar(1000)" json:"dept_id"`                 //部门id
-	PostId                  string    `gorm:"type:varchar(1000)" json:"post_id"`                 //岗位id
-	CreateUser              int64     `gorm:"type:bigint" json:"createUser"`                     //创建人
-	CreateDept              int64     `gorm:"type:bigint" json:"createDept"`                     //创建部门
-	CreateTime              time.Time `gorm:"type:datetime" json:"createTime"`                   //新增时间
-	UpdateUser              int64     `gorm:"type:bigint" json:"updateUser"`                     //修改人
-	UpdateTime              time.Time `gorm:"type:datetime" json:"updateTime"`                   //修改时间
-	Status                  int       `gorm:"type:int  " json:"status"`                          //状态
-	IsDeleted               int       `gorm:"type:int" json:"isDeleted"`                         //是否删除 0=未删除,1=删除
-	GroupId                 int       `gorm:"type:int" json:"groupId"`                           //用户分组id
-	BigScreenIndexCameraIds string    `gorm:"type:varchar(255)" json:"bigScreenIndexCameraIds"`  //数据大屏中摄像头保存位置
-	SecuritySixScreen       string    `gorm:"type:varchar(255)" json:"security_six_screen"`      //安防页面六分屏
+	ID               int64     `gorm:"primary_key" json:"id"`                             //编号
+	TenantId         string    `gorm:"type:varchar(12);default '000000'" json:"tenantId"` //租户id
+	TenantName       string    `gorm:"type:varchar(50)" json:"tenantName"`                //租户名称
+	Domain           string    `gorm:"type:varchar(255)" json:"domain"`                   //域名地址
+	BackgroundUrl    string    `gorm:"type:varchar(1000)" json:"backgroundUrl"`           //系统背景
+	SysLogoUrl       string    `gorm:"type:varchar(1000)" json:"sysLogoUrl"`              //logo图表
+	Linkman          string    `gorm:"type:varchar(20)" json:"linkman"`                   //联系人
+	ContactNumber    string    `gorm:"type:varchar(20)" json:"contactNumber"`             //联系电话
+	Address          string    `gorm:"type:varchar(255)" json:"address"`                  //联系地址
+	AccountNumber    int       `gorm:"type:int;default -1" json:"accountNumber"`          //邮箱
+	ExpireTime       time.Time `gorm:"type:datetime" json:"expireTime"`                   //过期时间
+	DatasourceId     int64     `gorm:"type:bigint" json:"datasourceId"`                   //数据源
+	LicenseKey       string    `gorm:"type:varchar(1000)" json:"licenseKey"`              //授权码
+	BigScreeName     string    `gorm:"type:varchar(60)" json:"bigScreeName"`              //大屏端显示名称
+	CreateUser       int64     `gorm:"type:bigint" json:"createUser"`                     //创建人
+	CreateDept       int64     `gorm:"type:bigint" json:"createDept"`                     //创建部门
+	CreateTime       time.Time `gorm:"type:datetime" json:"createTime"`                   //新增时间
+	UpdateUser       int64     `gorm:"type:bigint" json:"updateUser"`                     //修改人
+	UpdateTime       time.Time `gorm:"type:datetime" json:"updateTime"`                   //修改时间
+	Status           int       `gorm:"type:int  " json:"status"`                          //状态
+	IsDeleted        int       `gorm:"type:int" json:"isDeleted"`                         //是否删除 0=未删除,1=删除
+	PassKey          string    `gorm:"type:varchar(10)" json:"passKey"`                   //租户登录Key
+	LoginDisplayName string    `gorm:"type:varchar(60)" json:"loginDisplayName"`          //登录页面显示名称
+	ContactEmail     string    `gorm:"type:varchar(255)" json:"contactEmail"`             //联系邮箱
+}
+
+func (Tenant) TableName() string {
+	return "tenant"
+}
+
+func (c *Tenant) GetTenant() error {
+	return Db.Model(&c).Where("tenant_id = ?", c.TenantId).Find(&c).Error
+}
+
+func (c *Tenant) GetTenantByPasskey() error {
+	return Db.Model(&c).Where("pass_key = ?", c.PassKey).Find(&c).Error
+}
+
+type TenantCode struct {
+	ID       int64   `gorm:"primary_key" json:"id"`              //编号
+	TenantId string  `gorm:"type:varchar(12)" json:"tenantId"`   //租户id
+	CityCode string  `gorm:"type:varchar(6)" json:"cityCode"`    //城市行政编码
+	CityName string  `gorm:"type:varchar(255)" json:"cityName"`  //城市名称
+	CityLng  float64 `gorm:"type:double(18, 14)" json:"cityLng"` //经度
+	CityLat  float64 `gorm:"type:double(18, 14)" json:"cityLat"` //经度
+}
+
+func (TenantCode) TableName() string {
+	return "tenant_code"
+}
+
+func (c *TenantCode) GetTenantCode() error {
+	return Db.Model(&c).Where("tenant_id = ?", c.TenantId).Find(&c).Error
 }

+ 5 - 5
app/system/dao/user.go

@@ -12,15 +12,15 @@ type User struct {
 	Account                 string    `gorm:"type:varchar(12)" json:"account"`                   //账号
 	Password                string    `gorm:"type:varchar(12)" json:"password"`                  //密码
 	Name                    string    `gorm:"type:varchar(12)" json:"name"`                      //昵称
-	RealName                string    `gorm:"type:varchar(12)" json:"real_name"`                 //真名
+	RealName                string    `gorm:"type:varchar(12)" json:"realName"`                  //真名
 	Avatar                  string    `gorm:"type:varchar(12)" json:"avatar"`                    //头像
 	Email                   string    `gorm:"type:varchar(12)" json:"email"`                     //邮箱
 	Phone                   string    `gorm:"type:varchar(12)" json:"phone"`                     //手机
 	Birthday                string    `gorm:"type:datetime" json:"birthday"`                     //生日
 	Sex                     int       `gorm:"type:smallint" json:"sex"`                          //生日
-	RoleId                  string    `gorm:"type:varchar(1000)" json:"role_id"`                 //角色id
-	DeptId                  string    `gorm:"type:varchar(1000)" json:"dept_id"`                 //部门id
-	PostId                  string    `gorm:"type:varchar(1000)" json:"post_id"`                 //岗位id
+	RoleId                  string    `gorm:"type:varchar(1000)" json:"roleId"`                  //角色id
+	DeptId                  string    `gorm:"type:varchar(1000)" json:"deptId"`                  //部门id
+	PostId                  string    `gorm:"type:varchar(1000)" json:"postId"`                  //岗位id
 	CreateUser              int64     `gorm:"type:bigint" json:"createUser"`                     //创建人
 	CreateDept              int64     `gorm:"type:bigint" json:"createDept"`                     //创建部门
 	CreateTime              time.Time `gorm:"type:datetime" json:"createTime"`                   //新增时间
@@ -30,7 +30,7 @@ type User struct {
 	IsDeleted               int       `gorm:"type:int" json:"isDeleted"`                         //是否删除 0=未删除,1=删除
 	GroupId                 int       `gorm:"type:int" json:"groupId"`                           //用户分组id
 	BigScreenIndexCameraIds string    `gorm:"type:varchar(255)" json:"bigScreenIndexCameraIds"`  //数据大屏中摄像头保存位置
-	SecuritySixScreen       string    `gorm:"type:varchar(255)" json:"security_six_screen"`      //安防页面六分屏
+	SecuritySixScreen       string    `gorm:"type:varchar(255)" json:"securitySixScreen"`        //安防页面六分屏
 }
 
 func (User) TableName() string {

+ 17 - 12
app/system/model/token.go

@@ -91,16 +91,21 @@ const (
 )
 
 const (
-	CaptchaHeaderKey              = "Captcha-Key"
-	CaptchaHeaderCode             = "Captcha-Code"
-	CaptchaNotCorrect             = "验证码不正确"
-	UserTypeHeaderKey             = "User-Type"
-	UserNotFound                  = "用户名或密码错误"
-	UserHasNoRole                 = "未获得用户的角色信息"
-	TenantNotFound                = "请输入专属登录地址!"
-	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"
+	AdminTenantId = "1000000000000000" //默认租户
+	DES_KEY       = "0000000000000000" //租户授权码默认16位密钥
+)
+
+const (
+	CaptchaHeaderKey          = "Captcha-Key"
+	CaptchaHeaderCode         = "Captcha-Code"
+	CaptchaNotCorrect         = "验证码不正确"
+	UserTypeHeaderKey         = "User-Type"
+	UserNotFound              = "用户名或密码错误"
+	UserHasNoRole             = "未获得用户的角色信息"
+	TenantNotFound            = "请输入专属登录地址!"
+	UserHasNoTenant           = "未获得用户的租户信息"
+	UserHasNoTenantPermission = "租户授权已过期,请联系管理员"
+	HeaderKey                 = "Authorization"
+	HEADER_PREFIX             = "Basic "
+	DEFAULT_AVATAR            = "https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png"
 )

+ 15 - 2
app/system/service/tenantService.go

@@ -2,7 +2,6 @@ package service
 
 import (
 	"iot_manager_service/app/system/dao"
-	"iot_manager_service/util"
 )
 
 // 租户管理服务
@@ -10,10 +9,24 @@ var TenantService = new(tenantService)
 
 type tenantService struct{}
 
-func (s *tenantService) Get(passKey string) (*dao.Tenant, *util.Errors) {
+func (s *tenantService) GetTenantByPasskey(passKey string) (*dao.Tenant, error) {
 	tenant := &dao.Tenant{
 		TenantId: passKey,
 	}
+	err := tenant.GetTenantByPasskey()
+	if err != nil {
+		return nil, err
+	}
+	return tenant, nil
+}
 
+func (s *tenantService) GetOne(tenantId string) (*dao.Tenant, error) {
+	tenant := &dao.Tenant{
+		TenantId: tenantId,
+	}
+	err := tenant.GetTenant()
+	if err != nil {
+		return nil, err
+	}
 	return tenant, nil
 }

+ 38 - 0
app/system/service/userService.go

@@ -0,0 +1,38 @@
+package service
+
+import (
+	"crypto/sha1"
+	"fmt"
+	"io"
+	"iot_manager_service/app/system/dao"
+)
+
+// 用户管理服务
+var UserService = new(userService)
+
+type userService struct{}
+
+func (s *userService) GetUserByPasskey(passKey string) (*dao.User, error) {
+	user := &dao.User{}
+	err := user.GetUser()
+	if err != nil {
+		return nil, err
+	}
+	return user, nil
+}
+
+func (s *userService) GetOne(tenantId, account, password string) (dao.User, error) {
+	t := sha1.New()
+	_, _ = io.WriteString(t, password)
+	password = fmt.Sprintf("%x", t.Sum(nil))
+	user := dao.User{
+		TenantId: tenantId,
+		Account:  account,
+		Password: password,
+	}
+	err := user.GetUser()
+	if err != nil {
+		return dao.User{}, err
+	}
+	return user, nil
+}