login_access.go 4.6 KB
package domain

import (
	"github.com/linmadan/egglib-go/core/application"
	"time"
)

//登录的平台
const (
	LoginPlatformApp         string = "app"          //天联共创app
	LoginPlatformWeb         string = "web"          //天联共创web
	LoginPlatformOperatorWeb string = "operator-web" //天联共创运营-web
)

const (
	LoginPwd     = "signInPassword"
	LoginSmsCode = "signInCaptcha"
)

const (
	DeviceTypeIos     = "1"
	DeviceTypeAndroid = "2"
	DeviceTypeWeb     = "4"
)

const (
	InvalidAccessToken  = 901
	InvalidRefreshToken = 902
	InvalidSign         = 903
	InvalidClientId     = 904
	InvalidUUid         = 905
)

var codeMsg = map[int]string{
	InvalidAccessToken:  "access token 过期或无效,需刷新令牌",
	InvalidRefreshToken: "refresh token 过期或失效,需重新进行登录认证操作",
	InvalidSign:         "sign 签名无效,需重新登录手机 APP",
	InvalidClientId:     "client id 或 client secret 无效,需强制更新手机 APP",
	InvalidUUid:         "uuid 无效",
}

// 登录凭证存储
type LoginAccess struct {
	LoginAccessId int64   `json:"loginAccessId"`
	UserBaseId    int64   `json:"userBaseId"`
	UserId        int64   `json:"userId"`
	OrgIds        []int64 `json:"orgIds"`
	// 账号
	Account string `json:"account"`
	// 对应平台
	Platform string `json:"platform"`
	// 公司id
	CompanyId int64 `json:"companyId"`
	// 组织id
	OrganizationId int64 `json:"organizationId"`
	// 登录凭证存储
	AccessToken string `json:"accessToken"`
	// 刷新登录凭证
	RefreshToken string `json:"refreshToken"`
	// 登录凭证到期时间,时间戳精度秒
	AccessExpired int64 `json:"accessExpired"`
	// 刷新登录凭证到期时间,时间戳精度秒
	RefreshExpired int64 `json:"refreshExpired"`
	// 创建时间
	CreatedTime time.Time `json:"createdTime"`
	// 更新时间
	UpdatedTime time.Time `json:"updatedTime"`
}

type LoginAccessRepository interface {
	Save(loginAccess *LoginAccess) (*LoginAccess, error)
	Remove(loginAccess *LoginAccess) (*LoginAccess, error)
	FindOne(queryOptions map[string]interface{}) (*LoginAccess, error)
	Find(queryOptions map[string]interface{}) (int64, []*LoginAccess, error)
}

func (loginAccess *LoginAccess) Identify() interface{} {
	if loginAccess.LoginAccessId == 0 {
		return nil
	}
	return loginAccess.LoginAccessId
}

func (loginAccess *LoginAccess) Update(data map[string]interface{}) error {
	if account, ok := data["account"]; ok {
		loginAccess.Account = account.(string)
	}
	if platform, ok := data["platform"]; ok {
		loginAccess.Platform = platform.(string)
	}
	if companyId, ok := data["companyId"]; ok {
		loginAccess.CompanyId = companyId.(int64)
	}
	if organizationId, ok := data["organizationId"]; ok {
		loginAccess.OrganizationId = organizationId.(int64)
	}
	if accessToken, ok := data["accessToken"]; ok {
		loginAccess.AccessToken = accessToken.(string)
	}
	if refreshToken, ok := data["refreshToken"]; ok {
		loginAccess.RefreshToken = refreshToken.(string)
	}
	if accessExpired, ok := data["accessExpired"]; ok {
		loginAccess.AccessExpired = accessExpired.(int64)
	}
	if refreshExpired, ok := data["refreshExpired"]; ok {
		loginAccess.RefreshExpired = refreshExpired.(int64)
	}
	if createdTime, ok := data["createdTime"]; ok {
		loginAccess.CreatedTime = createdTime.(time.Time)
	}
	if updatedTime, ok := data["updatedTime"]; ok {
		loginAccess.UpdatedTime = updatedTime.(time.Time)
	}
	return nil
}

func ParsePlatform(deviceType string) string {
	if deviceType == DeviceTypeWeb {
		return LoginPlatformWeb
	}
	return LoginPlatformApp
}

func ParseCodeMsg(code int) string {
	if v, ok := codeMsg[code]; ok {
		return v
	}
	return ""
}

func (loginAccess *LoginAccess) ResetLoginAccess(loginToken LoginToken) (interface{}, error) {
	loginAccess.UserId = int64(loginToken.UserId)
	loginAccess.UserBaseId = int64(loginToken.UserBaseId)
	loginAccess.Account = loginToken.Account
	loginAccess.Platform = loginToken.Platform
	loginAccess.CompanyId = int64(loginToken.CompanyId)
	loginAccess.OrganizationId = loginToken.OrgId
	loginAccess.OrgIds = loginToken.OrgIds
	loginAccess.UpdatedTime = time.Now()

	accessTokenStr, err := loginToken.GenerateAccessToken()
	if err != nil {
		return nil, err
	}
	loginAccess.AccessToken = accessTokenStr
	loginAccess.AccessExpired = loginToken.ExpiresAt
	refreshTokenStr, err := loginToken.GenerateRefreshToken()
	if err != nil {
		return nil, err
	}
	loginAccess.RefreshToken = refreshTokenStr
	loginAccess.RefreshExpired = loginToken.ExpiresAt
	return nil, nil
}

func NewApplicationError(code int) error {
	return &application.ServiceError{
		Code:    code,
		Message: ParseCodeMsg(code),
	}
}