作者 yangfu

Merge branch 'dev' of http://gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway into dev

# Conflicts:
#	lastupdate.tmp
@@ -2,9 +2,14 @@ package main @@ -2,9 +2,14 @@ package main
2 2
3 import ( 3 import (
4 "github.com/beego/beego/v2/server/web" 4 "github.com/beego/beego/v2/server/web"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/cache"
5 _ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/port/beego" 6 _ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/port/beego"
6 ) 7 )
7 8
  9 +func init() {
  10 + cache.InitRedist()
  11 +}
  12 +
8 func main() { 13 func main() {
9 web.Run() 14 web.Run()
10 } 15 }
1 package command 1 package command
2 2
  3 +import (
  4 + "fmt"
  5 +
  6 + "github.com/beego/beego/v2/core/validation"
  7 +)
  8 +
3 type AccessTokenCommand struct { 9 type AccessTokenCommand struct {
4 - AuthCode string `json:"authCode"` 10 + AuthCode string `json:"authCode" valid:"Required"`
  11 +}
  12 +
  13 +func (orgAddCommand *AccessTokenCommand) Valid(validation *validation.Validation) {
  14 +
  15 +}
  16 +
  17 +func (orgAddCommand *AccessTokenCommand) ValidateCommand() error {
  18 + valid := validation.Validation{}
  19 + b, err := valid.Valid(orgAddCommand)
  20 + if err != nil {
  21 + return err
  22 + }
  23 + if !b {
  24 + for _, validErr := range valid.Errors {
  25 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  26 + }
  27 + }
  28 + return nil
5 } 29 }
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + "github.com/beego/beego/v2/core/validation"
  7 +)
  8 +
  9 +type RefreshTokenCommand struct {
  10 + RefreshToken string `json:"refreshToken" valid:"Required"`
  11 +}
  12 +
  13 +func (orgAddCommand *RefreshTokenCommand) Valid(validation *validation.Validation) {
  14 +
  15 +}
  16 +
  17 +func (orgAddCommand *RefreshTokenCommand) ValidateCommand() error {
  18 + valid := validation.Validation{}
  19 + b, err := valid.Valid(orgAddCommand)
  20 + if err != nil {
  21 + return err
  22 + }
  23 + if !b {
  24 + for _, validErr := range valid.Errors {
  25 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  26 + }
  27 + }
  28 + return nil
  29 +}
@@ -8,6 +8,7 @@ import ( @@ -8,6 +8,7 @@ import (
8 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/factory" 8 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/factory"
9 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/mobile/auth/command" 9 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/mobile/auth/command"
10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain" 10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
  11 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/cache"
11 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_user" 12 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_user"
12 ) 13 )
13 14
@@ -64,6 +65,9 @@ func (srv AuthService) SignInCaptcha(phone string, captcha string) (interface{}, @@ -64,6 +65,9 @@ func (srv AuthService) SignInCaptcha(phone string, captcha string) (interface{},
64 65
65 //GetAuthAccessToken 获取令牌Token 66 //GetAuthAccessToken 获取令牌Token
66 func (srv AuthService) GetAuthAccessToken(accessTokenCommand *command.AccessTokenCommand) (interface{}, error) { 67 func (srv AuthService) GetAuthAccessToken(accessTokenCommand *command.AccessTokenCommand) (interface{}, error) {
  68 + if err := accessTokenCommand.ValidateCommand(); err != nil {
  69 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  70 + }
67 ltoken := domain.LoginToken{} 71 ltoken := domain.LoginToken{}
68 err := ltoken.ParseToken(accessTokenCommand.AuthCode) 72 err := ltoken.ParseToken(accessTokenCommand.AuthCode)
69 if err != nil { 73 if err != nil {
@@ -123,6 +127,32 @@ func (srv AuthService) GetAuthAccessToken(accessTokenCommand *command.AccessToke @@ -123,6 +127,32 @@ func (srv AuthService) GetAuthAccessToken(accessTokenCommand *command.AccessToke
123 UpdatedTime: time.Now(), 127 UpdatedTime: time.Now(),
124 } 128 }
125 } 129 }
  130 + //判定当前凭证的companyId,OrganizationId 是否在用户列表中
  131 + var currentOrgIsOK bool
  132 +loopUser1:
  133 + for _, v := range userSeachResult.Users {
  134 + if v.Company.CompanyId == int(currentAccess.CompanyId) {
  135 + for _, vv := range v.UserOrg {
  136 + if vv.OrgID == int(currentAccess.OrganizationId) {
  137 + currentOrgIsOK = true
  138 + break loopUser1
  139 + }
  140 + }
  141 + }
  142 + }
  143 + //记录上一次的登录公司组织信息不可用 ,重登录置记录
  144 + if !currentOrgIsOK {
  145 + loopUser2:
  146 + for _, v := range userSeachResult.Users {
  147 + currentAccess.CompanyId = int64(v.Company.CompanyId)
  148 + for _, vv := range v.UserOrg {
  149 + currentAccess.OrganizationId = int64(vv.OrgID)
  150 + currentOrgIsOK = true
  151 + break loopUser2
  152 + }
  153 + }
  154 + }
  155 + //TODO
126 loginToken := domain.LoginToken{ 156 loginToken := domain.LoginToken{
127 UserId: currentAccess.UserId, 157 UserId: currentAccess.UserId,
128 Account: currentAccess.Account, 158 Account: currentAccess.Account,
@@ -143,12 +173,133 @@ func (srv AuthService) GetAuthAccessToken(accessTokenCommand *command.AccessToke @@ -143,12 +173,133 @@ func (srv AuthService) GetAuthAccessToken(accessTokenCommand *command.AccessToke
143 } 173 }
144 currentAccess.RefreshToken = refreshTokenStr 174 currentAccess.RefreshToken = refreshTokenStr
145 currentAccess.AccessExpired = loginToken.ExpiresAt 175 currentAccess.AccessExpired = loginToken.ExpiresAt
  176 + //先存数据库
  177 + _, err = loginAccessRepository.Save(currentAccess)
  178 + if err != nil {
  179 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  180 + }
  181 + if err := transactionContext.CommitTransaction(); err != nil {
  182 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  183 + }
  184 + tokenCache := cache.LoginTokenCache{}
  185 + tokenCache.RemoveAccessToken(currentAccess.Account, domain.LoginPlatformApp)
  186 + tokenCache.RemoveRefreshToken(currentAccess.Account, domain.LoginPlatformApp)
  187 + tokenCache.SaveAccessToken(currentAccess)
  188 + tokenCache.SaveRefreshToken(currentAccess)
  189 + nowTime := time.Now().Unix()
  190 + return map[string]interface{}{
  191 + "refreshToken": accessTokenStr,
  192 + "accessToken": refreshTokenStr,
  193 + "expiresIn": currentAccess.AccessExpired - nowTime,
  194 + }, nil
  195 +}
146 196
  197 +func (srv AuthService) RefreshAuthAccessToken(refreshTokenCommand *command.RefreshTokenCommand) (interface{}, error) {
  198 + if err := refreshTokenCommand.ValidateCommand(); err != nil {
  199 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  200 + }
  201 + ltoken := domain.LoginToken{}
  202 + err := ltoken.ParseToken(refreshTokenCommand.RefreshToken)
  203 + if err != nil {
  204 + return nil, application.ThrowError(application.TRANSACTION_ERROR, "refreshToken 不可用,"+err.Error())
  205 + }
  206 + phone := ltoken.Account
  207 + creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(0, 0, 0)
  208 + userSeachResult, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
  209 + Phone: phone,
  210 + })
  211 + if err != nil {
  212 + return nil, application.ThrowError(application.TRANSACTION_ERROR, "用户信息获取失败,"+err.Error())
  213 + }
  214 + //判定当前凭证的companyId,OrganizationId 是否在用户列表中
  215 + var currentOrgIsOK bool
  216 +loopUser1:
  217 + for _, v := range userSeachResult.Users {
  218 + if v.Company.CompanyId == int(ltoken.CompanyId) {
  219 + for _, vv := range v.UserOrg {
  220 + if vv.OrgID == int(ltoken.OrgId) {
  221 + currentOrgIsOK = true
  222 + break loopUser1
  223 + }
  224 + }
  225 + }
  226 + }
  227 + if !currentOrgIsOK {
  228 + return nil, application.ThrowError(application.TRANSACTION_ERROR, "登录的公司组织不可用")
  229 + }
  230 +
  231 + transactionContext, err := factory.CreateTransactionContext(nil)
  232 + if err != nil {
  233 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  234 + }
  235 + if err := transactionContext.StartTransaction(); err != nil {
  236 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  237 + }
  238 + defer func() {
  239 + transactionContext.RollbackTransaction()
  240 + }()
  241 + var loginAccessRepository domain.LoginAccessRepository
  242 + if loginAccessRepository, err = factory.CreateLoginAccessRepository(map[string]interface{}{
  243 + "transactionContext": transactionContext,
  244 + }); err != nil {
  245 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  246 + }
  247 + _, lAccess, err := loginAccessRepository.Find(map[string]interface{}{
  248 + "account": phone,
  249 + "platform": domain.LoginPlatformApp,
  250 + })
  251 + if err != nil {
  252 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  253 + }
  254 + var currentAccess *domain.LoginAccess
  255 + if len(lAccess) > 0 {
  256 + currentAccess = lAccess[0]
  257 + currentAccess.UpdatedTime = time.Now()
  258 + } else {
  259 + currentAccess = &domain.LoginAccess{
  260 + UserBaseId: ltoken.UserBaseId,
  261 + UserId: ltoken.UserId,
  262 + Account: ltoken.Account,
  263 + Platform: domain.LoginPlatformApp,
  264 + CompanyId: ltoken.CompanyId,
  265 + OrganizationId: ltoken.OrgId,
  266 + AccessToken: "",
  267 + RefreshToken: "",
  268 + AccessExpired: 0,
  269 + RefreshExpired: 0,
  270 + CreatedTime: time.Now(),
  271 + UpdatedTime: time.Now(),
  272 + }
  273 + }
  274 + accessTokenStr, err := ltoken.GenerateAccessToken()
  275 + if err != nil {
  276 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  277 + }
  278 + currentAccess.AccessToken = accessTokenStr
  279 + currentAccess.AccessExpired = ltoken.ExpiresAt
  280 + refreshTokenStr, err := ltoken.GenerateRefreshToken()
  281 + if err != nil {
  282 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  283 + }
  284 + currentAccess.RefreshToken = refreshTokenStr
  285 + currentAccess.RefreshExpired = ltoken.ExpiresAt
147 //先存数据库 286 //先存数据库
  287 + _, err = loginAccessRepository.Save(currentAccess)
  288 + if err != nil {
  289 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  290 + }
148 if err := transactionContext.CommitTransaction(); err != nil { 291 if err := transactionContext.CommitTransaction(); err != nil {
149 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 292 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
150 } 293 }
151 - //先删缓存  
152 - //后加缓存  
153 - return nil, nil 294 + tokenCache := cache.LoginTokenCache{}
  295 + tokenCache.RemoveAccessToken(currentAccess.Account, domain.LoginPlatformApp)
  296 + tokenCache.RemoveRefreshToken(currentAccess.Account, domain.LoginPlatformApp)
  297 + tokenCache.SaveAccessToken(currentAccess)
  298 + tokenCache.SaveRefreshToken(currentAccess)
  299 + nowTime := time.Now().Unix()
  300 + return map[string]interface{}{
  301 + "refreshToken": accessTokenStr,
  302 + "accessToken": refreshTokenStr,
  303 + "expiresIn": currentAccess.AccessExpired - nowTime,
  304 + }, nil
154 } 305 }
@@ -16,6 +16,7 @@ const ( @@ -16,6 +16,7 @@ const (
16 type LoginToken struct { 16 type LoginToken struct {
17 jwt.StandardClaims 17 jwt.StandardClaims
18 UserId int64 `json:"userId"` 18 UserId int64 `json:"userId"`
  19 + UserBaseId int64 `json:"userBaseId"`
19 // 账号 20 // 账号
20 Account string `json:"account"` 21 Account string `json:"account"`
21 // 对应平台 22 // 对应平台
@@ -12,7 +12,7 @@ const ( @@ -12,7 +12,7 @@ const (
12 KEY_PREFIX string = "allied:creation:gateway:" 12 KEY_PREFIX string = "allied:creation:gateway:"
13 ) 13 )
14 14
15 -func Init() *redis.Client { 15 +func InitRedist() *redis.Client {
16 options := redis.Options{ 16 options := redis.Options{
17 Network: "tcp", 17 Network: "tcp",
18 Addr: fmt.Sprintf("%s:%s", "127.0.0.1", "6379"), 18 Addr: fmt.Sprintf("%s:%s", "127.0.0.1", "6379"),
@@ -36,5 +36,10 @@ func Init() *redis.Client { @@ -36,5 +36,10 @@ func Init() *redis.Client {
36 } 36 }
37 // 新建一个client 37 // 新建一个client
38 clientRedis = redis.NewClient(&options) 38 clientRedis = redis.NewClient(&options)
  39 + pong, err := clientRedis.Ping().Result()
  40 + if err != nil {
  41 + e := fmt.Errorf("redis 连接失败,%s,%w", pong, err)
  42 + panic(e)
  43 + }
39 return clientRedis 44 return clientRedis
40 } 45 }
  1 +package mobile_client
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/mobile/auth/command"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/mobile/auth/service"
  6 +)
  7 +
  8 +type AuthController struct {
  9 + baseController
  10 +}
  11 +
  12 +func (controller *AuthController) Login() {
  13 + authService := service.AuthService{}
  14 + loginCmd := &command.LoginCommand{}
  15 + err := controller.Unmarshal(loginCmd)
  16 + if err != nil {
  17 + controller.Response(nil, err)
  18 + return
  19 + }
  20 + data, err := authService.AuthLogin(loginCmd)
  21 + controller.Response(data, err)
  22 +}
  23 +
  24 +func (controller *AuthController) GetAuthAccessToken() {
  25 + authService := service.AuthService{}
  26 + accessTokenCommand := &command.AccessTokenCommand{}
  27 + err := controller.Unmarshal(accessTokenCommand)
  28 + if err != nil {
  29 + controller.Response(nil, err)
  30 + return
  31 + }
  32 + data, err := authService.GetAuthAccessToken(accessTokenCommand)
  33 + controller.Response(data, err)
  34 +}
  35 +
  36 +func (controller *AuthController) RefreshAuthAccessToken() {
  37 + authService := service.AuthService{}
  38 + refreshTokenCmd := &command.RefreshTokenCommand{}
  39 + err := controller.Unmarshal(refreshTokenCmd)
  40 + if err != nil {
  41 + controller.Response(nil, err)
  42 + return
  43 + }
  44 + data, err := authService.RefreshAuthAccessToken(refreshTokenCmd)
  45 + controller.Response(data, err)
  46 +}
  1 +package mobile_client
  2 +
  3 +import (
  4 + "github.com/linmadan/egglib-go/web/beego"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
  6 +)
  7 +
  8 +type baseController struct {
  9 + beego.BaseController
  10 +}
  11 +
  12 +func (controller *baseController) returnPageListData(count int64, data interface{}, err error, pageNumber int) {
  13 + dataMap := map[string]interface{}{
  14 + "grid": map[string]interface{}{
  15 + "total": count,
  16 + //"pageNumber": pageNumber,
  17 + "list": data,
  18 + },
  19 + }
  20 + controller.Response(dataMap, err)
  21 +}
  22 +
  23 +func (controller *baseController) GetUserId() int64 {
  24 + return 1
  25 +}
  26 +
  27 +func (controller *baseController) GetCompanyId() int64 {
  28 + return 1
  29 +}
  30 +
  31 +func (controller *baseController) GetUserBaseId() int64 {
  32 + return 1
  33 +}
  34 +
  35 +func (controller *baseController) GetOrgId() int64 {
  36 + return 1
  37 +}
  38 +
  39 +func (controller *baseController) GetOperator() domain.Operator {
  40 + return domain.Operator{
  41 + UserId: 9,
  42 + CompanyId: 23,
  43 + OrgId: 45,
  44 + UserBaseId: 1,
  45 + }
  46 +}
  1 +package routers
  2 +
  3 +import (
  4 + "github.com/beego/beego/v2/server/web"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/port/beego/controllers/mobile_client"
  6 +)
  7 +
  8 +func init() {
  9 + web.Router("/v1/app/auth/login", &mobile_client.AuthController{}, "Post:Login")
  10 + web.Router("/v1/app/auth/access-token", &mobile_client.AuthController{}, "Post:GetAuthAccessToken")
  11 + web.Router("/v1/app/auth/refresh-token", &mobile_client.AuthController{}, "Post:RefreshAuthAccessToken")
  12 +}