作者 tangxvhui

添加手机端登录接口

@@ -2,5 +2,5 @@ package command @@ -2,5 +2,5 @@ package command
2 2
3 type LoginCommand struct { 3 type LoginCommand struct {
4 Code string `json:"code" valid:"Required"` //授权code 4 Code string `json:"code" valid:"Required"` //授权code
5 - PlatformId int `json:"platformId" valid:"Required"` //登录平台ID,28-绩效管理后台 29-员工绩效 5 + PlatformId int `json:"platformId" valid:"Required"` //登录平台ID,constant.IdPlatformAdmin=28 绩效管理后台 constant.IdPlatformUser=29 员工绩效
6 } 6 }
@@ -8,19 +8,19 @@ import ( @@ -8,19 +8,19 @@ import (
8 "github.com/beego/beego/validation" 8 "github.com/beego/beego/validation"
9 ) 9 )
10 10
11 -type AuthorizeCommand struct {  
12 - Token string `json:"credentials" valid:"Required;"` //登录凭证 11 +type MobileLoginCommand struct {
  12 + Credentials string `json:"credentials" valid:"Required;"` //登录凭证
  13 + Cuid int `json:"cuid,string" valid:"Required;"` //统一用户中心用户 UID
  14 + Cid int `json:"cid,string" valid:"Required;"` //统一用户中心公司 ID
  15 + Muid int `json:"muid,string" valid:"Required;"` //企业平台中的用户 UID
13 //ClientId string `json:"clientId"` //客户端密钥 16 //ClientId string `json:"clientId"` //客户端密钥
14 - Cuid int64 `json:"cuid" valid:"Required;"` //统一用户中心用户 UID  
15 - Cid int64 `json:"cid" valid:"Required;"` //统一用户中心公司 ID  
16 - Muid int64 `json:"muid" valid:"Required;"` //企业平台中的用户 UID  
17 } 17 }
18 18
19 -func (authorizeCommand *AuthorizeCommand) Valid(validation *validation.Validation) { 19 +func (authorizeCommand *MobileLoginCommand) Valid(validation *validation.Validation) {
20 20
21 } 21 }
22 22
23 -func (authorizeCommand *AuthorizeCommand) ValidateCommand() error { 23 +func (authorizeCommand *MobileLoginCommand) ValidateCommand() error {
24 valid := validation.Validation{} 24 valid := validation.Validation{}
25 b, err := valid.Valid(authorizeCommand) 25 b, err := valid.Valid(authorizeCommand)
26 if err != nil { 26 if err != nil {
@@ -4,6 +4,7 @@ import ( @@ -4,6 +4,7 @@ import (
4 "github.com/linmadan/egglib-go/core/application" 4 "github.com/linmadan/egglib-go/core/application"
5 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/auth/command" 5 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/auth/command"
6 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory" 6 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
  7 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/constant"
7 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain" 8 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
8 ) 9 )
9 10
@@ -53,7 +54,7 @@ func (service *AuthService) Login(loginCommand *command.LoginCommand) (interface @@ -53,7 +54,7 @@ func (service *AuthService) Login(loginCommand *command.LoginCommand) (interface
53 "companyId": company.Id, 54 "companyId": company.Id,
54 }) 55 })
55 if err != nil { 56 if err != nil {
56 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取公司数据失败") 57 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取用户数据失败")
57 } 58 }
58 if user.Status != domain.UserStatusEnable { 59 if user.Status != domain.UserStatusEnable {
59 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "用户被禁用") 60 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "用户被禁用")
@@ -81,9 +82,8 @@ func (service *AuthService) Login(loginCommand *command.LoginCommand) (interface @@ -81,9 +82,8 @@ func (service *AuthService) Login(loginCommand *command.LoginCommand) (interface
81 }, nil 82 }, nil
82 } 83 }
83 84
84 -//手机端登录 ,来源于能力展示app  
85 -  
86 -func (service *AuthService) MobileLogin(param command.AuthorizeCommand) (map[string]interface{}, error) { 85 +// 员工绩效 手机端登录,来源于能力展示app
  86 +func (service *AuthService) MobileLogin(param *command.MobileLoginCommand) (map[string]interface{}, error) {
87 transactionContext, err := factory.CreateTransactionContext(nil) 87 transactionContext, err := factory.CreateTransactionContext(nil)
88 if err != nil { 88 if err != nil {
89 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 89 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
@@ -94,13 +94,61 @@ func (service *AuthService) MobileLogin(param command.AuthorizeCommand) (map[str @@ -94,13 +94,61 @@ func (service *AuthService) MobileLogin(param command.AuthorizeCommand) (map[str
94 defer func() { 94 defer func() {
95 _ = transactionContext.RollbackTransaction() 95 _ = transactionContext.RollbackTransaction()
96 }() 96 }()
97 - 97 + // 统一用户中心登录
  98 + authCodeReply, err := factory.UCenterApi().AppAuthCode(param.Credentials, param.Cuid, param.Cid)
  99 + if err != nil || !authCodeReply.IsOk() {
  100 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "统一用户中心认证失败")
  101 + }
  102 + // 用户权限校验
  103 + // 登录平台ID,28-绩效管理后台 29-员工绩效
  104 + userAuthReply, err := factory.BusinessAdminApi().GetUserAuth(authCodeReply.Data.MUid, constant.IdPlatformUser)
  105 + if err != nil {
  106 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "用户鉴权失败")
  107 + }
  108 + if !userAuthReply.IsOk() {
  109 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, userAuthReply.Message())
  110 + }
  111 + //获取公司数据
  112 + companyRepository := factory.CreateCompanyRepository(map[string]interface{}{
  113 + "transactionContext": transactionContext,
  114 + })
  115 + company, err := companyRepository.FindOne(map[string]interface{}{
  116 + "id": authCodeReply.Data.CompanyId,
  117 + })
  118 + if err != nil {
  119 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取公司数据失败")
  120 + }
  121 + userRepository := factory.CreateUserRepository(map[string]interface{}{
  122 + "transactionContext": transactionContext,
  123 + })
  124 + user, err := userRepository.FindOne(map[string]interface{}{
  125 + "id": authCodeReply.Data.MUid,
  126 + "companyId": company.Id,
  127 + })
  128 + if err != nil {
  129 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取用户数据失败")
  130 + }
  131 + if user.Status != domain.UserStatusEnable {
  132 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "用户被禁用")
  133 + }
  134 + userAuth := &domain.UserAuth{
  135 + UserId: user.Id,
  136 + CompanyId: user.CompanyId,
  137 + Phone: user.Account,
  138 + PlatformId: constant.IdPlatformUser,
  139 + Name: user.Name,
  140 + AdminType: user.AdminType,
  141 + }
  142 + accessToken, err := userAuth.CreateAccessToken()
  143 + if err != nil {
  144 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  145 + }
98 if err := transactionContext.CommitTransaction(); err != nil { 146 if err := transactionContext.CommitTransaction(); err != nil {
99 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 147 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
100 } 148 }
101 result := map[string]interface{}{ 149 result := map[string]interface{}{
102 "access": map[string]interface{}{ 150 "access": map[string]interface{}{
103 - "accessToken": "", 151 + "accessToken": accessToken,
104 "expiresIn": domain.JWTExpiresSecond, 152 "expiresIn": domain.JWTExpiresSecond,
105 }, 153 },
106 } 154 }
@@ -4,9 +4,15 @@ import "os" @@ -4,9 +4,15 @@ import "os"
4 4
5 const SERVICE_NAME = "performance" 5 const SERVICE_NAME = "performance"
6 6
  7 +// 登录平台ID,28-绩效管理后台 29-员工绩效
  8 +const (
  9 + IdPlatformAdmin int = 28
  10 + IdPlatformUser int = 29
  11 +)
  12 +
7 var LOG_LEVEL = "debug" 13 var LOG_LEVEL = "debug"
8 14
9 -//过期时间 7天时间 15 +// 过期时间 7天时间
10 var AdminJwtExpiresIn = int64(3600 * 24 * 7) 16 var AdminJwtExpiresIn = int64(3600 * 24 * 7)
11 17
12 var AdminJWTSecretKey = "sg-storage" 18 var AdminJWTSecretKey = "sg-storage"
@@ -4,7 +4,10 @@ import "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/se @@ -4,7 +4,10 @@ import "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/se
4 4
5 // UCenterApi 统一用户中心 5 // UCenterApi 统一用户中心
6 type UCenterApi interface { 6 type UCenterApi interface {
  7 + // pc 端
7 AuthCode(code string) (*reply.UCenterAuthCode, error) 8 AuthCode(code string) (*reply.UCenterAuthCode, error)
  9 + //手机app端
  10 + AppAuthCode(tokenCode string, uid int, companyId int) (*reply.UCenterAuthCode, error)
8 } 11 }
9 12
10 // BusinessAdminApi 企业平台 13 // BusinessAdminApi 企业平台
@@ -21,9 +21,17 @@ func (controller *AuthController) Login() { @@ -21,9 +21,17 @@ func (controller *AuthController) Login() {
21 } 21 }
22 22
23 func (controller *AuthController) User() { 23 func (controller *AuthController) User() {
24 -  
25 userAuth := controller.Ctx.Input.GetData(domain.UserAuth{}).(*domain.UserAuth) 24 userAuth := controller.Ctx.Input.GetData(domain.UserAuth{}).(*domain.UserAuth)
26 controller.Response(map[string]interface{}{ 25 controller.Response(map[string]interface{}{
27 "user": userAuth, 26 "user": userAuth,
28 }, nil) 27 }, nil)
29 } 28 }
  29 +
  30 +// Login PC端登录
  31 +func (controller *AuthController) MobileLogin() {
  32 + authService := &service.AuthService{}
  33 + loginCommand := &command.MobileLoginCommand{}
  34 + _ = controller.Unmarshal(loginCommand)
  35 + resp, err := authService.MobileLogin(loginCommand)
  36 + controller.Response(resp, err)
  37 +}
@@ -9,6 +9,8 @@ import ( @@ -9,6 +9,8 @@ import (
9 func init() { 9 func init() {
10 web.Router("/login", &controllers.AuthController{}, "Post:Login") 10 web.Router("/login", &controllers.AuthController{}, "Post:Login")
11 11
  12 + web.Router("/login/mobile", &controllers.AuthController{}, "Post:MobileLogin")
  13 + //
12 web.InsertFilter("/auth/admin/*", web.BeforeExec, middlewares.CheckAdminToken()) 14 web.InsertFilter("/auth/admin/*", web.BeforeExec, middlewares.CheckAdminToken())
13 web.Router("/auth/admin/user", &controllers.AuthController{}, "Get:User") 15 web.Router("/auth/admin/user", &controllers.AuthController{}, "Get:User")
14 16