作者 tangxuhui
@@ -3,6 +3,7 @@ module gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway @@ -3,6 +3,7 @@ module gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway
3 go 1.16 3 go 1.16
4 4
5 require ( 5 require (
  6 + github.com/GeeTeam/gt3-golang-sdk v0.0.0-20200116043922-446ca8a507d2
6 github.com/beego/beego/v2 v2.0.1 7 github.com/beego/beego/v2 v2.0.1
7 github.com/dgrijalva/jwt-go v3.2.0+incompatible 8 github.com/dgrijalva/jwt-go v3.2.0+incompatible
8 github.com/go-pg/pg/v10 v10.10.1 9 github.com/go-pg/pg/v10 v10.10.1
1 cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= 1 cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
2 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= 2 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
  3 +github.com/GeeTeam/gt3-golang-sdk v0.0.0-20200116043922-446ca8a507d2 h1:KBk8MKqHvIztjEW4KAKnR9VPT4iTZYz0D4Ju0GFVsV0=
  4 +github.com/GeeTeam/gt3-golang-sdk v0.0.0-20200116043922-446ca8a507d2/go.mod h1:vqCh/LExdPmL5SULXE/9DHbuE4t3iLkzRYoNGxEvg/o=
3 github.com/Knetic/govaluate v3.0.0+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0= 5 github.com/Knetic/govaluate v3.0.0+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
4 github.com/Shopify/sarama v1.25.0 h1:ch1ywjRLjfJtU+EaiJ+l0rWffQ6TRpyYmW4DX7Cb2SU= 6 github.com/Shopify/sarama v1.25.0 h1:ch1ywjRLjfJtU+EaiJ+l0rWffQ6TRpyYmW4DX7Cb2SU=
5 github.com/Shopify/sarama v1.25.0/go.mod h1:y/CFFTO9eaMTNriwu/Q+W4eioLqiDMGkA1W+gmdfj8w= 7 github.com/Shopify/sarama v1.25.0/go.mod h1:y/CFFTO9eaMTNriwu/Q+W4eioLqiDMGkA1W+gmdfj8w=
@@ -7,8 +7,9 @@ import ( @@ -7,8 +7,9 @@ import (
7 ) 7 )
8 8
9 type AccessTokenCommand struct { 9 type AccessTokenCommand struct {
10 - AuthCode string `json:"authCode" valid:"Required"`  
11 - SessionMode int `json:"sessionMode"` 10 + AuthCode string `json:"authCode" valid:"Required"`
  11 + SessionMode int `json:"sessionMode"`
  12 + LoginPlatform string `json:"-"`
12 } 13 }
13 14
14 func (orgAddCommand *AccessTokenCommand) Valid(validation *validation.Validation) { 15 func (orgAddCommand *AccessTokenCommand) Valid(validation *validation.Validation) {
@@ -12,6 +12,7 @@ type LoginCommand struct { @@ -12,6 +12,7 @@ type LoginCommand struct {
12 Password string `json:"password"` 12 Password string `json:"password"`
13 Captcha string `json:"captcha"` 13 Captcha string `json:"captcha"`
14 SessionMode int `json:"sessionMode"` 14 SessionMode int `json:"sessionMode"`
  15 + DeviceType string `json:"-"`
15 } 16 }
16 17
17 func (orgAddCommand *LoginCommand) Valid(validation *validation.Validation) { 18 func (orgAddCommand *LoginCommand) Valid(validation *validation.Validation) {
@@ -7,8 +7,9 @@ import ( @@ -7,8 +7,9 @@ import (
7 ) 7 )
8 8
9 type LoginPwdCommand struct { 9 type LoginPwdCommand struct {
10 - Username string `json:"username" valid:"Required"`  
11 - Password string `json:"password"` 10 + Username string `json:"username" valid:"Required"`
  11 + Password string `json:"password"`
  12 + DeviceType string `json:"-"`
12 // 图形验证码操作成功的识别字段,服务端需使用图形验证插件(geetest)提供的验证模块 13 // 图形验证码操作成功的识别字段,服务端需使用图形验证插件(geetest)提供的验证模块
13 // 对相应数据进行验证,用以确定该次请求为用户手动操作的正确行为 14 // 对相应数据进行验证,用以确定该次请求为用户手动操作的正确行为
14 CaptchaChallenge string `json:"captchaChallenge"` 15 CaptchaChallenge string `json:"captchaChallenge"`
@@ -10,6 +10,7 @@ type LoginSmsCommand struct { @@ -10,6 +10,7 @@ type LoginSmsCommand struct {
10 Phone string `json:"phone" valid:"Required"` 10 Phone string `json:"phone" valid:"Required"`
11 Code string `json:"code" valid:"Required"` //登录方式(signInPassword 密码登录、signInCaptcha 验证码登录) 11 Code string `json:"code" valid:"Required"` //登录方式(signInPassword 密码登录、signInCaptcha 验证码登录)
12 SessionMode int `json:"sessionMode"` 12 SessionMode int `json:"sessionMode"`
  13 + DeviceType string `json:"-"`
13 } 14 }
14 15
15 func (cmd *LoginSmsCommand) Valid(validation *validation.Validation) { 16 func (cmd *LoginSmsCommand) Valid(validation *validation.Validation) {
@@ -3,4 +3,5 @@ package query @@ -3,4 +3,5 @@ package query
3 type QrcodeLoginStatusQuery struct { 3 type QrcodeLoginStatusQuery struct {
4 Key string `json:"key"` 4 Key string `json:"key"`
5 SessionMode int `json:"sessionMode"` 5 SessionMode int `json:"sessionMode"`
  6 + DeviceType string `json:"-"`
6 } 7 }
  1 +package query
  2 +
  3 +type CaptchaInitRequest struct {
  4 + UserIp string `json:"userIp" valid:"Required"`
  5 +}
  6 +
  7 +func (CaptchaInitRequest *CaptchaInitRequest) ValidateCommand() error {
  8 + //valid := validation.Validation{}
  9 + //b, err := valid.Valid(CaptchaInitRequest)
  10 + //if err != nil {
  11 + // return err
  12 + //}
  13 + //if !b {
  14 + // for _, validErr := range valid.Errors {
  15 + // return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  16 + // }
  17 + //}
  18 + return nil
  19 +}
1 package service 1 package service
2 2
3 import ( 3 import (
  4 + "github.com/GeeTeam/gt3-golang-sdk/geetest"
4 "github.com/google/uuid" 5 "github.com/google/uuid"
  6 + "github.com/linmadan/egglib-go/utils/json"
5 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/auth/query" 7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/auth/query"
6 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/auth/dto" 8 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/auth/dto"
7 "time" 9 "time"
@@ -17,17 +19,26 @@ import ( @@ -17,17 +19,26 @@ import (
17 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/sms_serve" 19 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/sms_serve"
18 ) 20 )
19 21
  22 +const (
  23 + captchaID = "33a2abf9c5df0d6bc3b89fb39280114b"
  24 + privateKey = "13320fd2b10199e9a2440a4fbb4d46f7"
  25 +)
  26 +
20 // 组织管理 27 // 组织管理
21 type AuthService struct { 28 type AuthService struct {
22 } 29 }
23 30
24 //AuthLogin 用户登录 31 //AuthLogin 用户登录
25 -func (srv AuthService) AuthLogin(loginCommand *command.LoginCommand) (interface{}, error) { 32 +func (svr AuthService) AuthLogin(loginCommand *command.LoginCommand) (interface{}, error) {
26 var ( 33 var (
27 - authCode string  
28 - result interface{}  
29 - err error 34 + authCode string
  35 + result interface{}
  36 + err error
  37 + loginPlatform string = domain.LoginPlatformApp
30 ) 38 )
  39 + if loginCommand.DeviceType == domain.DeviceTypeWeb {
  40 + loginPlatform = domain.LoginPlatformWeb
  41 + }
31 creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{}) 42 creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
32 _, err = creationUserGateway.AuthRefreshIM(allied_creation_user.ReqAuthRefreshIM{ 43 _, err = creationUserGateway.AuthRefreshIM(allied_creation_user.ReqAuthRefreshIM{
33 Phone: loginCommand.Phone, 44 Phone: loginCommand.Phone,
@@ -36,16 +47,20 @@ func (srv AuthService) AuthLogin(loginCommand *command.LoginCommand) (interface{ @@ -36,16 +47,20 @@ func (srv AuthService) AuthLogin(loginCommand *command.LoginCommand) (interface{
36 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 47 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
37 } 48 }
38 switch loginCommand.GrantType { 49 switch loginCommand.GrantType {
39 - case "signInPassword":  
40 - authCode, err = srv.SignInPassword(loginCommand.Phone, loginCommand.Password)  
41 - case "signInCaptcha":  
42 - authCode, err = srv.SignInCaptcha(loginCommand.Phone, loginCommand.Captcha) 50 + case domain.LoginPwd:
  51 + authCode, err = svr.SignInPassword(loginCommand.Phone, loginCommand.Password, loginPlatform)
  52 + case domain.LoginSmsCode:
  53 + authCode, err = svr.SignInCaptcha(loginCommand.Phone, loginCommand.Captcha, loginPlatform)
43 default: 54 default:
44 err = application.ThrowError(application.TRANSACTION_ERROR, "登录方式无法解析") 55 err = application.ThrowError(application.TRANSACTION_ERROR, "登录方式无法解析")
45 } 56 }
46 - result, err = srv.GetAuthAccessToken(&command.AccessTokenCommand{  
47 - AuthCode: authCode,  
48 - SessionMode: loginCommand.SessionMode, 57 + if err != nil {
  58 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  59 + }
  60 + result, err = svr.GetAuthAccessToken(&command.AccessTokenCommand{
  61 + AuthCode: authCode,
  62 + SessionMode: loginCommand.SessionMode,
  63 + LoginPlatform: loginPlatform,
49 }) 64 })
50 return map[string]interface{}{ 65 return map[string]interface{}{
51 "access": result, 66 "access": result,
@@ -53,7 +68,7 @@ func (srv AuthService) AuthLogin(loginCommand *command.LoginCommand) (interface{ @@ -53,7 +68,7 @@ func (srv AuthService) AuthLogin(loginCommand *command.LoginCommand) (interface{
53 } 68 }
54 69
55 //AuthLogin 用户登录 70 //AuthLogin 用户登录
56 -func (srv AuthService) AuthLoginPwd(loginCommand *command.LoginPwdCommand) (interface{}, error) { 71 +func (svr AuthService) AuthLoginPwd(loginCommand *command.LoginPwdCommand) (interface{}, error) {
57 if err := loginCommand.ValidateCommand(); err != nil { 72 if err := loginCommand.ValidateCommand(); err != nil {
58 return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 73 return nil, application.ThrowError(application.ARG_ERROR, err.Error())
59 } 74 }
@@ -63,11 +78,18 @@ func (srv AuthService) AuthLoginPwd(loginCommand *command.LoginPwdCommand) (inte @@ -63,11 +78,18 @@ func (srv AuthService) AuthLoginPwd(loginCommand *command.LoginPwdCommand) (inte
63 Password: loginCommand.Password, 78 Password: loginCommand.Password,
64 SessionMode: loginCommand.SessionMode, 79 SessionMode: loginCommand.SessionMode,
65 } 80 }
66 - return srv.AuthLogin(login) 81 + if len(loginCommand.CaptchaChallenge) > 0 {
  82 + geetest := geetest.NewGeetestLib(captchaID, privateKey, 2*time.Second)
  83 + validateResult := geetest.SuccessValidate(loginCommand.CaptchaChallenge, loginCommand.CaptchaValidate, loginCommand.CaptchaSeccode, "", "")
  84 + if !validateResult {
  85 + log.Logger.Error("validate captcha fail")
  86 + }
  87 + }
  88 + return svr.AuthLogin(login)
67 } 89 }
68 90
69 //AuthLogin 用户登录 91 //AuthLogin 用户登录
70 -func (srv AuthService) AuthLoginSms(loginCommand *command.LoginSmsCommand) (interface{}, error) { 92 +func (svr AuthService) AuthLoginSms(loginCommand *command.LoginSmsCommand) (interface{}, error) {
71 if err := loginCommand.ValidateCommand(); err != nil { 93 if err := loginCommand.ValidateCommand(); err != nil {
72 return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 94 return nil, application.ThrowError(application.ARG_ERROR, err.Error())
73 } 95 }
@@ -77,11 +99,44 @@ func (srv AuthService) AuthLoginSms(loginCommand *command.LoginSmsCommand) (inte @@ -77,11 +99,44 @@ func (srv AuthService) AuthLoginSms(loginCommand *command.LoginSmsCommand) (inte
77 Captcha: loginCommand.Code, 99 Captcha: loginCommand.Code,
78 SessionMode: loginCommand.SessionMode, 100 SessionMode: loginCommand.SessionMode,
79 } 101 }
80 - return srv.AuthLogin(login) 102 + return svr.AuthLogin(login)
  103 +}
  104 +
  105 +//AuthLoginQrcode 扫码登录
  106 +func (svr AuthService) AuthLoginQrcode(queryParam *query.QrcodeLoginStatusQuery) (interface{}, error) {
  107 + qrmsg := domain.QrcodeMessage{}
  108 + err := qrmsg.ParseToken(queryParam.Key)
  109 + if err != nil {
  110 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  111 + }
  112 + qrCache := cache.LoginQrcodeCache{}
  113 + qrmsgCache, err := qrCache.Get(qrmsg.Id)
  114 + if err != nil {
  115 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  116 + }
  117 + loginToken := domain.LoginToken{
  118 + UserId: qrmsgCache.UserId,
  119 + UserBaseId: qrmsgCache.UserBaseId,
  120 + Account: qrmsgCache.Account,
  121 + Platform: domain.LoginPlatformWeb,
  122 + CompanyId: qrmsgCache.CompanyId,
  123 + OrgId: qrmsgCache.OrgId,
  124 + }
  125 + accessToken, err := loginToken.GenerateAccessToken()
  126 + if err != nil {
  127 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  128 + }
  129 + _ = accessToken
  130 + result, err := svr.getToken(loginToken)
  131 + data := map[string]interface{}{
  132 + "isLogin": qrmsgCache.IsLogin,
  133 + "access": result["token"],
  134 + }
  135 + return data, nil
81 } 136 }
82 137
83 //SendSmsCaptcha 发送验证码短信 138 //SendSmsCaptcha 发送验证码短信
84 -func (srv AuthService) SendSmsCaptcha(smsCodeCommand *command.SendSmsCodeCommand) error { 139 +func (svr AuthService) SendSmsCaptcha(smsCodeCommand *command.SendSmsCodeCommand) error {
85 smsServeGateway := sms_serve.NewHttplibHttplibSmsServe() 140 smsServeGateway := sms_serve.NewHttplibHttplibSmsServe()
86 err := smsServeGateway.SendSms(smsCodeCommand.Phone) 141 err := smsServeGateway.SendSms(smsCodeCommand.Phone)
87 if err != nil { 142 if err != nil {
@@ -91,7 +146,7 @@ func (srv AuthService) SendSmsCaptcha(smsCodeCommand *command.SendSmsCodeCommand @@ -91,7 +146,7 @@ func (srv AuthService) SendSmsCaptcha(smsCodeCommand *command.SendSmsCodeCommand
91 } 146 }
92 147
93 //SignInPassword 使用账号密码校验 148 //SignInPassword 使用账号密码校验
94 -func (srv AuthService) SignInPassword(account string, password string) (string, error) { 149 +func (svr AuthService) SignInPassword(account string, password string, loginPlatform string) (string, error) {
95 creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{}) 150 creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
96 _, err := creationUserGateway.AuthCheckPassword(allied_creation_user.ReqAuthCheckPassword{ 151 _, err := creationUserGateway.AuthCheckPassword(allied_creation_user.ReqAuthCheckPassword{
97 Password: password, 152 Password: password,
@@ -103,7 +158,7 @@ func (srv AuthService) SignInPassword(account string, password string) (string, @@ -103,7 +158,7 @@ func (srv AuthService) SignInPassword(account string, password string) (string,
103 ltoken := domain.LoginToken{ 158 ltoken := domain.LoginToken{
104 UserId: 0, 159 UserId: 0,
105 Account: account, 160 Account: account,
106 - Platform: domain.LoginPlatformApp, 161 + Platform: loginPlatform,
107 CompanyId: 0, 162 CompanyId: 0,
108 } 163 }
109 authcode, err := ltoken.GenerateAuthCode() 164 authcode, err := ltoken.GenerateAuthCode()
@@ -117,7 +172,7 @@ func (srv AuthService) SignInPassword(account string, password string) (string, @@ -117,7 +172,7 @@ func (srv AuthService) SignInPassword(account string, password string) (string,
117 } 172 }
118 173
119 //SignInCaptcha 使用手机验证码登录 174 //SignInCaptcha 使用手机验证码登录
120 -func (srv AuthService) SignInCaptcha(phone string, captcha string) (string, error) { 175 +func (svr AuthService) SignInCaptcha(phone string, captcha string, loginPlatform string) (string, error) {
121 smsServeGateway := sms_serve.NewHttplibHttplibSmsServe() 176 smsServeGateway := sms_serve.NewHttplibHttplibSmsServe()
122 err := smsServeGateway.CheckSmsCode(phone, captcha) 177 err := smsServeGateway.CheckSmsCode(phone, captcha)
123 if err != nil { 178 if err != nil {
@@ -126,7 +181,7 @@ func (srv AuthService) SignInCaptcha(phone string, captcha string) (string, erro @@ -126,7 +181,7 @@ func (srv AuthService) SignInCaptcha(phone string, captcha string) (string, erro
126 ltoken := domain.LoginToken{ 181 ltoken := domain.LoginToken{
127 UserId: 0, 182 UserId: 0,
128 Account: phone, 183 Account: phone,
129 - Platform: domain.LoginPlatformApp, 184 + Platform: loginPlatform,
130 CompanyId: 0, 185 CompanyId: 0,
131 } 186 }
132 authcode, err := ltoken.GenerateAuthCode() 187 authcode, err := ltoken.GenerateAuthCode()
@@ -137,7 +192,7 @@ func (srv AuthService) SignInCaptcha(phone string, captcha string) (string, erro @@ -137,7 +192,7 @@ func (srv AuthService) SignInCaptcha(phone string, captcha string) (string, erro
137 } 192 }
138 193
139 //GetAuthAccessToken 获取令牌Token 194 //GetAuthAccessToken 获取令牌Token
140 -func (srv AuthService) GetAuthAccessToken(accessTokenCommand *command.AccessTokenCommand) (interface{}, error) { 195 +func (svr AuthService) GetAuthAccessToken(accessTokenCommand *command.AccessTokenCommand) (interface{}, error) {
141 if err := accessTokenCommand.ValidateCommand(); err != nil { 196 if err := accessTokenCommand.ValidateCommand(); err != nil {
142 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 197 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
143 } 198 }
@@ -146,138 +201,12 @@ func (srv AuthService) GetAuthAccessToken(accessTokenCommand *command.AccessToke @@ -146,138 +201,12 @@ func (srv AuthService) GetAuthAccessToken(accessTokenCommand *command.AccessToke
146 if err != nil { 201 if err != nil {
147 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 202 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
148 } 203 }
149 - phone := ltoken.Account  
150 - creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})  
151 - userSeachResult, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{  
152 - CompanyId: ltoken.CompanyId,  
153 - Phone: phone,  
154 - })  
155 - if err != nil {  
156 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
157 - }  
158 - if len(userSeachResult.Users) == 0 {  
159 - return nil, application.ThrowError(application.TRANSACTION_ERROR, "获取用户信息失败")  
160 - }  
161 - transactionContext, err := factory.CreateTransactionContext(nil)  
162 - if err != nil {  
163 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
164 - }  
165 - if err := transactionContext.StartTransaction(); err != nil {  
166 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
167 - }  
168 - defer func() {  
169 - transactionContext.RollbackTransaction()  
170 - }()  
171 - var loginAccessRepository domain.LoginAccessRepository  
172 - if loginAccessRepository, err = factory.CreateLoginAccessRepository(map[string]interface{}{  
173 - "transactionContext": transactionContext,  
174 - }); err != nil {  
175 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
176 - }  
177 - _, lAccess, err := loginAccessRepository.Find(map[string]interface{}{  
178 - "account": phone,  
179 - "platform": domain.LoginPlatformApp,  
180 - })  
181 - if err != nil {  
182 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
183 - }  
184 - var currentAccess *domain.LoginAccess  
185 - if len(lAccess) > 0 {  
186 - currentAccess = lAccess[0]  
187 - currentAccess.UpdatedTime = time.Now()  
188 - } else {  
189 - currentAccess = &domain.LoginAccess{  
190 - UserBaseId: int64(userSeachResult.Users[0].UserBaseId),  
191 - UserId: int64(userSeachResult.Users[0].UserId),  
192 - Account: userSeachResult.Users[0].UserInfo.Phone,  
193 - Platform: domain.LoginPlatformApp,  
194 - OrganizationId: int64(userSeachResult.Users[0].Org.OrgId),  
195 - AccessToken: "",  
196 - RefreshToken: "",  
197 - AccessExpired: 0,  
198 - RefreshExpired: 0,  
199 - CreatedTime: time.Now(),  
200 - UpdatedTime: time.Now(),  
201 - }  
202 - if userSeachResult.Users[0].Company != nil {  
203 - currentAccess.CompanyId = int64(userSeachResult.Users[0].Company.CompanyId)  
204 - }  
205 - }  
206 - //判定当前凭证的companyId,OrganizationId 是否在用户列表中  
207 - var currentOrgIsOK bool  
208 -loopUser1:  
209 - for _, v := range userSeachResult.Users {  
210 - if v.Company.CompanyId == int(currentAccess.CompanyId) {  
211 - for _, vv := range v.UserOrg {  
212 - if vv.OrgID == int(currentAccess.OrganizationId) {  
213 - currentOrgIsOK = true  
214 - currentAccess.UserId = int64(v.UserId)  
215 - currentAccess.UserBaseId = int64(v.UserBaseId)  
216 - break loopUser1  
217 - }  
218 - }  
219 - }  
220 - }  
221 - //记录上一次的登录公司组织信息不可用 ,重置登录记录  
222 - //使用找到的第一个可用的公司组织  
223 - if !currentOrgIsOK {  
224 - loopUser2:  
225 - for _, v := range userSeachResult.Users {  
226 - currentAccess.CompanyId = int64(v.Company.CompanyId)  
227 - for _, vv := range v.UserOrg {  
228 - currentAccess.UserId = int64(v.UserId)  
229 - currentAccess.UserBaseId = int64(v.UserBaseId)  
230 - currentAccess.OrganizationId = int64(vv.OrgID)  
231 - currentOrgIsOK = true  
232 - break loopUser2  
233 - }  
234 - }  
235 - }  
236 -  
237 - loginToken := domain.LoginToken{  
238 - UserId: currentAccess.UserId,  
239 - Account: currentAccess.Account,  
240 - UserBaseId: currentAccess.UserBaseId,  
241 - CompanyId: currentAccess.CompanyId,  
242 - OrgId: currentAccess.OrganizationId,  
243 - Platform: currentAccess.Platform,  
244 - }  
245 -  
246 - accessTokenStr, err := loginToken.GenerateAccessToken()  
247 - if err != nil {  
248 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
249 - }  
250 - currentAccess.AccessToken = accessTokenStr  
251 - currentAccess.AccessExpired = loginToken.ExpiresAt  
252 - refreshTokenStr, err := loginToken.GenerateRefreshToken()  
253 - if err != nil {  
254 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
255 - }  
256 - currentAccess.RefreshToken = refreshTokenStr  
257 - currentAccess.AccessExpired = loginToken.ExpiresAt  
258 - //先存数据库  
259 - _, err = loginAccessRepository.Save(currentAccess)  
260 - if err != nil {  
261 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
262 - }  
263 - if err := transactionContext.CommitTransaction(); err != nil {  
264 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
265 - }  
266 - //后处理redis缓存  
267 - tokenCache := cache.LoginTokenCache{}  
268 - tokenCache.RemoveAccessToken(currentAccess.Account, domain.LoginPlatformApp)  
269 - tokenCache.RemoveRefreshToken(currentAccess.Account, domain.LoginPlatformApp)  
270 - tokenCache.SaveAccessToken(currentAccess)  
271 - tokenCache.SaveRefreshToken(currentAccess)  
272 - nowTime := time.Now().Unix()  
273 - return map[string]interface{}{  
274 - "refreshToken": accessTokenStr,  
275 - "accessToken": refreshTokenStr,  
276 - "expiresIn": currentAccess.AccessExpired - nowTime,  
277 - }, nil 204 + ltoken.Platform = accessTokenCommand.LoginPlatform
  205 + result, err := svr.getToken(*ltoken)
  206 + return result["token"], err
278 } 207 }
279 208
280 -func (srv AuthService) RefreshAuthAccessToken(refreshTokenCommand *command.RefreshTokenCommand) (interface{}, error) { 209 +func (svr AuthService) RefreshAuthAccessToken(refreshTokenCommand *command.RefreshTokenCommand) (interface{}, error) {
281 if err := refreshTokenCommand.ValidateCommand(); err != nil { 210 if err := refreshTokenCommand.ValidateCommand(); err != nil {
282 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 211 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
283 } 212 }
@@ -286,113 +215,15 @@ func (srv AuthService) RefreshAuthAccessToken(refreshTokenCommand *command.Refre @@ -286,113 +215,15 @@ func (srv AuthService) RefreshAuthAccessToken(refreshTokenCommand *command.Refre
286 if err != nil { 215 if err != nil {
287 return nil, application.ThrowError(application.TRANSACTION_ERROR, "refreshToken 不可用,"+err.Error()) 216 return nil, application.ThrowError(application.TRANSACTION_ERROR, "refreshToken 不可用,"+err.Error())
288 } 217 }
289 - token, err := srv.getToken(domain.Operator{}, ltoken)  
290 - // phone := ltoken.Account  
291 - // creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})  
292 - // userSearchResult, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{  
293 - // Phone: phone,  
294 - // })  
295 - // if err != nil {  
296 - // return nil, application.ThrowError(application.TRANSACTION_ERROR, "用户信息获取失败,"+err.Error())  
297 - // }  
298 - // //判定当前凭证的companyId,OrganizationId 是否在用户列表中  
299 - // var currentOrgIsOK bool  
300 - //loopUser1:  
301 - // for _, v := range userSearchResult.Users {  
302 - // if v.Company.CompanyId == int(ltoken.CompanyId) {  
303 - // for _, vv := range v.UserOrg {  
304 - // if vv.OrgID == int(ltoken.OrgId) {  
305 - // currentOrgIsOK = true  
306 - // break loopUser1  
307 - // }  
308 - // }  
309 - // }  
310 - // }  
311 - // if !currentOrgIsOK {  
312 - // return nil, application.ThrowError(application.TRANSACTION_ERROR, "登录的公司组织不可用")  
313 - // }  
314 - //  
315 - // transactionContext, err := factory.CreateTransactionContext(nil)  
316 - // if err != nil {  
317 - // return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
318 - // }  
319 - // if err := transactionContext.StartTransaction(); err != nil {  
320 - // return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
321 - // }  
322 - // defer func() {  
323 - // transactionContext.RollbackTransaction()  
324 - // }()  
325 - // var loginAccessRepository domain.LoginAccessRepository  
326 - // if loginAccessRepository, err = factory.CreateLoginAccessRepository(map[string]interface{}{  
327 - // "transactionContext": transactionContext,  
328 - // }); err != nil {  
329 - // return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
330 - // }  
331 - // _, lAccess, err := loginAccessRepository.Find(map[string]interface{}{  
332 - // "account": phone,  
333 - // "platform": domain.LoginPlatformApp,  
334 - // })  
335 - // if err != nil {  
336 - // return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
337 - // }  
338 - // var currentAccess *domain.LoginAccess  
339 - // if len(lAccess) > 0 {  
340 - // currentAccess = lAccess[0]  
341 - // currentAccess.UpdatedTime = time.Now()  
342 - // } else {  
343 - // currentAccess = &domain.LoginAccess{  
344 - // UserBaseId: ltoken.UserBaseId,  
345 - // UserId: ltoken.UserId,  
346 - // Account: ltoken.Account,  
347 - // Platform: domain.LoginPlatformApp,  
348 - // CompanyId: ltoken.CompanyId,  
349 - // OrganizationId: ltoken.OrgId,  
350 - // AccessToken: "",  
351 - // RefreshToken: "",  
352 - // AccessExpired: 0,  
353 - // RefreshExpired: 0,  
354 - // CreatedTime: time.Now(),  
355 - // UpdatedTime: time.Now(),  
356 - // }  
357 - // }  
358 - // accessTokenStr, err := ltoken.GenerateAccessToken()  
359 - // if err != nil {  
360 - // return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
361 - // }  
362 - // currentAccess.AccessToken = accessTokenStr  
363 - // currentAccess.AccessExpired = ltoken.ExpiresAt  
364 - // refreshTokenStr, err := ltoken.GenerateRefreshToken()  
365 - // if err != nil {  
366 - // return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
367 - // }  
368 - // currentAccess.RefreshToken = refreshTokenStr  
369 - // currentAccess.RefreshExpired = ltoken.ExpiresAt  
370 - // //先存数据库  
371 - // _, err = loginAccessRepository.Save(currentAccess)  
372 - // if err != nil {  
373 - // return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
374 - // }  
375 - // if err := transactionContext.CommitTransaction(); err != nil {  
376 - // return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
377 - // }  
378 - // //后处理redis缓存  
379 - // tokenCache := cache.LoginTokenCache{}  
380 - // tokenCache.RemoveAccessToken(currentAccess.Account, domain.LoginPlatformApp)  
381 - // tokenCache.RemoveRefreshToken(currentAccess.Account, domain.LoginPlatformApp)  
382 - // tokenCache.SaveAccessToken(currentAccess)  
383 - // tokenCache.SaveRefreshToken(currentAccess)  
384 - // nowTime := time.Now().Unix()  
385 - // return map[string]interface{}{  
386 - // "refreshToken": accessTokenStr,  
387 - // "accessToken": refreshTokenStr,  
388 - // "expiresIn": currentAccess.AccessExpired - nowTime,  
389 - // }, nil  
390 - return token["token"], err 218 + token, err := svr.getToken(ltoken)
  219 + return map[string]interface{}{
  220 + "access": token["token"],
  221 + }, err
391 } 222 }
392 223
393 //GetUserMenus 获取用户信息 224 //GetUserMenus 获取用户信息
394 -func (srv AuthService) GetUserInfo(userInfoCommand *command.UserInfoCommand) (interface{}, error) {  
395 - user, err := srv.getUserInfo(userInfoCommand.Operator) 225 +func (svr AuthService) GetUserInfo(userInfoCommand *command.UserInfoCommand) (interface{}, error) {
  226 + user, err := svr.getUserInfo(userInfoCommand.Operator)
396 if err != nil { 227 if err != nil {
397 return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error()) 228 return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
398 } 229 }
@@ -402,7 +233,7 @@ func (srv AuthService) GetUserInfo(userInfoCommand *command.UserInfoCommand) (in @@ -402,7 +233,7 @@ func (srv AuthService) GetUserInfo(userInfoCommand *command.UserInfoCommand) (in
402 } 233 }
403 234
404 //GetUserMenus 获取用户菜单 235 //GetUserMenus 获取用户菜单
405 -func (srv AuthService) GetUserMenus(userMenusCommand *command.UserMenusCommand) (interface{}, error) { 236 +func (svr AuthService) GetUserMenus(userMenusCommand *command.UserMenusCommand) (interface{}, error) {
406 creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser( 237 creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
407 userMenusCommand.Operator) 238 userMenusCommand.Operator)
408 resultMenu, err := creationUserGateway.UserAccessMenus(allied_creation_user.ReqUserAccessMenus{ 239 resultMenu, err := creationUserGateway.UserAccessMenus(allied_creation_user.ReqUserAccessMenus{
@@ -417,7 +248,7 @@ func (srv AuthService) GetUserMenus(userMenusCommand *command.UserMenusCommand) @@ -417,7 +248,7 @@ func (srv AuthService) GetUserMenus(userMenusCommand *command.UserMenusCommand)
417 } 248 }
418 249
419 //GetUserMenus 获取用户组织 250 //GetUserMenus 获取用户组织
420 -func (srv AuthService) GetUserOrg(userOrgCommand *command.UserOrgCommand) (interface{}, error) { 251 +func (svr AuthService) GetUserOrg(userOrgCommand *command.UserOrgCommand) (interface{}, error) {
421 creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(userOrgCommand.Operator) 252 creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(userOrgCommand.Operator)
422 result, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{ 253 result, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
423 Offset: 0, 254 Offset: 0,
@@ -446,7 +277,7 @@ func (srv AuthService) GetUserOrg(userOrgCommand *command.UserOrgCommand) (inter @@ -446,7 +277,7 @@ func (srv AuthService) GetUserOrg(userOrgCommand *command.UserOrgCommand) (inter
446 } 277 }
447 278
448 //OrgSwitch 组织切换 279 //OrgSwitch 组织切换
449 -func (srv AuthService) OrgSwitch(switchOrgCommand *command.SwitchOrgCommand) (interface{}, error) { 280 +func (svr AuthService) OrgSwitch(switchOrgCommand *command.SwitchOrgCommand) (interface{}, error) {
450 if err := switchOrgCommand.ValidateCommand(); err != nil { 281 if err := switchOrgCommand.ValidateCommand(); err != nil {
451 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 282 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
452 } 283 }
@@ -456,7 +287,7 @@ func (srv AuthService) OrgSwitch(switchOrgCommand *command.SwitchOrgCommand) (in @@ -456,7 +287,7 @@ func (srv AuthService) OrgSwitch(switchOrgCommand *command.SwitchOrgCommand) (in
456 return nil, application.ThrowError(application.TRANSACTION_ERROR, "accessToken 不可用,"+err.Error()) 287 return nil, application.ThrowError(application.TRANSACTION_ERROR, "accessToken 不可用,"+err.Error())
457 } 288 }
458 ltoken.OrgId = switchOrgCommand.OrgId 289 ltoken.OrgId = switchOrgCommand.OrgId
459 - token, err := srv.getToken(domain.Operator{}, ltoken) 290 + token, err := svr.getToken(ltoken)
460 if err != nil { 291 if err != nil {
461 return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error()) 292 return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
462 } 293 }
@@ -466,7 +297,7 @@ func (srv AuthService) OrgSwitch(switchOrgCommand *command.SwitchOrgCommand) (in @@ -466,7 +297,7 @@ func (srv AuthService) OrgSwitch(switchOrgCommand *command.SwitchOrgCommand) (in
466 return nil, application.ThrowError(application.TRANSACTION_ERROR, "用户不存在") 297 return nil, application.ThrowError(application.TRANSACTION_ERROR, "用户不存在")
467 } 298 }
468 } 299 }
469 - user, err := srv.getUserInfo(domain.Operator{UserId: userId}) 300 + user, err := svr.getUserInfo(domain.Operator{UserId: userId})
470 if err != nil { 301 if err != nil {
471 return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error()) 302 return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
472 } 303 }
@@ -487,7 +318,7 @@ func (srv AuthService) OrgSwitch(switchOrgCommand *command.SwitchOrgCommand) (in @@ -487,7 +318,7 @@ func (srv AuthService) OrgSwitch(switchOrgCommand *command.SwitchOrgCommand) (in
487 } 318 }
488 319
489 // CompanySignUp 企业注册 320 // CompanySignUp 企业注册
490 -func (srv AuthService) CompanySignUp(companySignUpCommand *command.CompanySignUpCommand) (interface{}, error) { 321 +func (svr AuthService) CompanySignUp(companySignUpCommand *command.CompanySignUpCommand) (interface{}, error) {
491 //TODO:验证码验证测试去掉,后期恢复回来 322 //TODO:验证码验证测试去掉,后期恢复回来
492 //smsServeGateway := sms_serve.NewHttplibHttplibSmsServe() 323 //smsServeGateway := sms_serve.NewHttplibHttplibSmsServe()
493 //err := smsServeGateway.CheckSmsCode(companySignUpCommand.Phone, companySignUpCommand.SmsCode) 324 //err := smsServeGateway.CheckSmsCode(companySignUpCommand.Phone, companySignUpCommand.SmsCode)
@@ -510,7 +341,7 @@ func (srv AuthService) CompanySignUp(companySignUpCommand *command.CompanySignUp @@ -510,7 +341,7 @@ func (srv AuthService) CompanySignUp(companySignUpCommand *command.CompanySignUp
510 } 341 }
511 342
512 // ResetPassword 重置密码(找回密码) 343 // ResetPassword 重置密码(找回密码)
513 -func (srv AuthService) ResetPassword(resetPasswordCommand *command.ResetPasswordCommand) (interface{}, error) { 344 +func (svr AuthService) ResetPassword(resetPasswordCommand *command.ResetPasswordCommand) (interface{}, error) {
514 if err := resetPasswordCommand.ValidateCommand(); err != nil { 345 if err := resetPasswordCommand.ValidateCommand(); err != nil {
515 return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 346 return nil, application.ThrowError(application.ARG_ERROR, err.Error())
516 } 347 }
@@ -533,7 +364,7 @@ func (srv AuthService) ResetPassword(resetPasswordCommand *command.ResetPassword @@ -533,7 +364,7 @@ func (srv AuthService) ResetPassword(resetPasswordCommand *command.ResetPassword
533 return result, err 364 return result, err
534 } 365 }
535 366
536 -func (srv AuthService) getUserInfo(operator domain.Operator) (interface{}, error) { 367 +func (svr AuthService) getUserInfo(operator domain.Operator) (interface{}, error) {
537 creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser( 368 creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
538 operator) 369 operator)
539 resultUser, err := creationUserGateway.UserGet(allied_creation_user.ReqGetUser{ 370 resultUser, err := creationUserGateway.UserGet(allied_creation_user.ReqGetUser{
@@ -563,33 +394,46 @@ func (srv AuthService) getUserInfo(operator domain.Operator) (interface{}, error @@ -563,33 +394,46 @@ func (srv AuthService) getUserInfo(operator domain.Operator) (interface{}, error
563 return user, nil 394 return user, nil
564 } 395 }
565 396
566 -func (srv AuthService) getToken(operator domain.Operator, ltoken domain.LoginToken) (map[string]interface{}, error) { 397 +func (svr AuthService) getToken(ltoken domain.LoginToken) (map[string]interface{}, error) {
567 creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{}) 398 creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
568 userSearchResult, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{ 399 userSearchResult, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
569 Phone: ltoken.Account, 400 Phone: ltoken.Account,
570 }) 401 })
571 if err != nil { 402 if err != nil {
572 - return nil, application.ThrowError(application.TRANSACTION_ERROR, "用户信息获取失败,"+err.Error()) 403 + return nil, application.ThrowError(application.BUSINESS_ERROR, "用户信息获取失败,"+err.Error())
573 } 404 }
574 - //判定当前凭证的companyId,OrganizationId 是否在用户列表中 405 + // 1 . 判定当前凭证的companyId,OrganizationId 是否在用户列表中
575 var currentOrgIsOK bool 406 var currentOrgIsOK bool
576 - var currentUserId int64 407 + var currentUser allied_creation_user.UserDetail
577 loopUser1: 408 loopUser1:
578 for _, v := range userSearchResult.Users { 409 for _, v := range userSearchResult.Users {
579 - //if v.Company.CompanyId == int(ltoken.CompanyId) {  
580 for _, vv := range v.UserOrg { 410 for _, vv := range v.UserOrg {
581 if vv.OrgID == int(ltoken.OrgId) { 411 if vv.OrgID == int(ltoken.OrgId) {
582 currentOrgIsOK = true 412 currentOrgIsOK = true
583 - currentUserId = int64(v.UserId) 413 + currentUser = v
584 break loopUser1 414 break loopUser1
585 } 415 }
586 } 416 }
587 - //} 417 + }
  418 + if !currentOrgIsOK {
  419 + loopUser2:
  420 + for _, v := range userSearchResult.Users {
  421 + for _, vv := range v.UserOrg {
  422 + ltoken.OrgId = int64(vv.OrgID)
  423 + currentOrgIsOK = true
  424 + currentUser = v
  425 + break loopUser2
  426 + }
  427 + }
588 } 428 }
589 if !currentOrgIsOK { 429 if !currentOrgIsOK {
590 return nil, application.ThrowError(application.TRANSACTION_ERROR, "登录的公司组织不可用") 430 return nil, application.ThrowError(application.TRANSACTION_ERROR, "登录的公司组织不可用")
591 } 431 }
  432 + ltoken.UserId = int64(currentUser.UserId)
  433 + ltoken.UserBaseId = int64(currentUser.UserBaseId)
  434 + ltoken.CompanyId = int64(currentUser.Company.CompanyId)
592 435
  436 + // 2. 更新currentAccess信息
593 transactionContext, err := factory.CreateTransactionContext(nil) 437 transactionContext, err := factory.CreateTransactionContext(nil)
594 if err != nil { 438 if err != nil {
595 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 439 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
@@ -604,35 +448,27 @@ loopUser1: @@ -604,35 +448,27 @@ loopUser1:
604 if loginAccessRepository, err = factory.CreateLoginAccessRepository(map[string]interface{}{ 448 if loginAccessRepository, err = factory.CreateLoginAccessRepository(map[string]interface{}{
605 "transactionContext": transactionContext, 449 "transactionContext": transactionContext,
606 }); err != nil { 450 }); err != nil {
607 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 451 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
608 } 452 }
609 _, lAccess, err := loginAccessRepository.Find(map[string]interface{}{ 453 _, lAccess, err := loginAccessRepository.Find(map[string]interface{}{
610 "account": ltoken.Account, 454 "account": ltoken.Account,
611 - "platform": domain.LoginPlatformApp, 455 + "platform": ltoken.Platform,
612 }) 456 })
613 if err != nil { 457 if err != nil {
614 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 458 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
615 } 459 }
616 - var currentAccess *domain.LoginAccess 460 + var currentAccess = &domain.LoginAccess{CreatedTime: time.Now()}
617 if len(lAccess) > 0 { 461 if len(lAccess) > 0 {
618 currentAccess = lAccess[0] 462 currentAccess = lAccess[0]
619 - currentAccess.UpdatedTime = time.Now()  
620 - } else {  
621 - currentAccess = &domain.LoginAccess{  
622 - UserBaseId: ltoken.UserBaseId,  
623 - UserId: ltoken.UserId,  
624 - Account: ltoken.Account,  
625 - Platform: domain.LoginPlatformApp,  
626 - CompanyId: ltoken.CompanyId,  
627 - OrganizationId: ltoken.OrgId,  
628 - AccessToken: "",  
629 - RefreshToken: "",  
630 - AccessExpired: 0,  
631 - RefreshExpired: 0,  
632 - CreatedTime: time.Now(),  
633 - UpdatedTime: time.Now(),  
634 - }  
635 } 463 }
  464 + currentAccess.UserId = int64(ltoken.UserId)
  465 + currentAccess.UserBaseId = int64(ltoken.UserBaseId)
  466 + currentAccess.Account = ltoken.Account
  467 + currentAccess.Platform = ltoken.Platform
  468 + currentAccess.CompanyId = int64(ltoken.CompanyId)
  469 + currentAccess.OrganizationId = ltoken.OrgId
  470 + currentAccess.UpdatedTime = time.Now()
  471 +
636 accessTokenStr, err := ltoken.GenerateAccessToken() 472 accessTokenStr, err := ltoken.GenerateAccessToken()
637 if err != nil { 473 if err != nil {
638 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 474 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
@@ -641,22 +477,24 @@ loopUser1: @@ -641,22 +477,24 @@ loopUser1:
641 currentAccess.AccessExpired = ltoken.ExpiresAt 477 currentAccess.AccessExpired = ltoken.ExpiresAt
642 refreshTokenStr, err := ltoken.GenerateRefreshToken() 478 refreshTokenStr, err := ltoken.GenerateRefreshToken()
643 if err != nil { 479 if err != nil {
644 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 480 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
645 } 481 }
646 currentAccess.RefreshToken = refreshTokenStr 482 currentAccess.RefreshToken = refreshTokenStr
647 currentAccess.RefreshExpired = ltoken.ExpiresAt 483 currentAccess.RefreshExpired = ltoken.ExpiresAt
648 - //先存数据库 484 +
  485 + //存数据库
649 _, err = loginAccessRepository.Save(currentAccess) 486 _, err = loginAccessRepository.Save(currentAccess)
650 if err != nil { 487 if err != nil {
651 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 488 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
652 } 489 }
653 if err := transactionContext.CommitTransaction(); err != nil { 490 if err := transactionContext.CommitTransaction(); err != nil {
654 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 491 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
655 } 492 }
656 - //后处理redis缓存 493 +
  494 + //redis缓存
657 tokenCache := cache.LoginTokenCache{} 495 tokenCache := cache.LoginTokenCache{}
658 - tokenCache.RemoveAccessToken(currentAccess.Account, domain.LoginPlatformApp)  
659 - tokenCache.RemoveRefreshToken(currentAccess.Account, domain.LoginPlatformApp) 496 + tokenCache.RemoveAccessToken(currentAccess.Account, ltoken.Platform)
  497 + tokenCache.RemoveRefreshToken(currentAccess.Account, ltoken.Platform)
660 tokenCache.SaveAccessToken(currentAccess) 498 tokenCache.SaveAccessToken(currentAccess)
661 tokenCache.SaveRefreshToken(currentAccess) 499 tokenCache.SaveRefreshToken(currentAccess)
662 nowTime := time.Now().Unix() 500 nowTime := time.Now().Unix()
@@ -667,12 +505,12 @@ loopUser1: @@ -667,12 +505,12 @@ loopUser1:
667 } 505 }
668 return map[string]interface{}{ 506 return map[string]interface{}{
669 "token": token, 507 "token": token,
670 - "userId": currentUserId, 508 + "userId": currentUser.UserId,
671 }, nil 509 }, nil
672 } 510 }
673 511
674 //GetCompanyOrgsByUser 获取登录用户的公司组织列表 512 //GetCompanyOrgsByUser 获取登录用户的公司组织列表
675 -func (srv AuthService) GetCompanyOrgsByUser(queryParam *query.GetCompanyOrgsByUserQuery) (interface{}, error) { 513 +func (svr AuthService) GetCompanyOrgsByUser(queryParam *query.GetCompanyOrgsByUserQuery) (interface{}, error) {
676 514
677 creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(queryParam.Operator) 515 creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(queryParam.Operator)
678 result, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{ 516 result, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
@@ -708,7 +546,7 @@ func (srv AuthService) GetCompanyOrgsByUser(queryParam *query.GetCompanyOrgsByUs @@ -708,7 +546,7 @@ func (srv AuthService) GetCompanyOrgsByUser(queryParam *query.GetCompanyOrgsByUs
708 } 546 }
709 547
710 //GetQrcode 获取扫码登录需要的二维码 548 //GetQrcode 获取扫码登录需要的二维码
711 -func (srv AuthService) GetQrcode() (interface{}, error) { 549 +func (svr AuthService) GetQrcode() (interface{}, error) {
712 qrmsg := domain.QrcodeMessage{} 550 qrmsg := domain.QrcodeMessage{}
713 _, err := qrmsg.GenerateImageBase64() //imgBase64 551 _, err := qrmsg.GenerateImageBase64() //imgBase64
714 if err != nil { 552 if err != nil {
@@ -726,41 +564,8 @@ func (srv AuthService) GetQrcode() (interface{}, error) { @@ -726,41 +564,8 @@ func (srv AuthService) GetQrcode() (interface{}, error) {
726 return data, nil 564 return data, nil
727 } 565 }
728 566
729 -//QrcodeLoginStatus 询问扫码登录状态  
730 -func (srv AuthService) QrcodeLoginStatus(queryParam *query.QrcodeLoginStatusQuery) (interface{}, error) {  
731 - qrmsg := domain.QrcodeMessage{}  
732 - err := qrmsg.ParseToken(queryParam.Key)  
733 - if err != nil {  
734 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
735 - }  
736 - qrCache := cache.LoginQrcodeCache{}  
737 - qrmsgCache, err := qrCache.Get(qrmsg.Id)  
738 - if err != nil {  
739 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
740 - }  
741 - loginToken := domain.LoginToken{  
742 - UserId: qrmsgCache.UserId,  
743 - UserBaseId: qrmsgCache.UserBaseId,  
744 - Account: qrmsgCache.Account,  
745 - Platform: domain.LoginPlatformWeb,  
746 - CompanyId: qrmsgCache.CompanyId,  
747 - OrgId: qrmsgCache.OrgId,  
748 - }  
749 - accessToken, err := loginToken.GenerateAccessToken()  
750 - if err != nil {  
751 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
752 - }  
753 - _ = accessToken  
754 - //TODO 填充token数据  
755 - data := map[string]interface{}{  
756 - "isLogin": qrmsgCache.IsLogin,  
757 - "access": "",  
758 - }  
759 - return data, nil  
760 -}  
761 -  
762 //CheckSmsCode 验证手机短信验证码 567 //CheckSmsCode 验证手机短信验证码
763 -func (srv AuthService) CheckSmsCode(smsCodeCommand *command.CheckSmsCodeCommand) (interface{}, error) { 568 +func (svr AuthService) CheckSmsCode(smsCodeCommand *command.CheckSmsCodeCommand) (interface{}, error) {
764 smsServeGateway := sms_serve.NewHttplibHttplibSmsServe() 569 smsServeGateway := sms_serve.NewHttplibHttplibSmsServe()
765 err := smsServeGateway.CheckSmsCode(smsCodeCommand.Phone, smsCodeCommand.SmsCode) 570 err := smsServeGateway.CheckSmsCode(smsCodeCommand.Phone, smsCodeCommand.SmsCode)
766 if err != nil { 571 if err != nil {
@@ -780,3 +585,25 @@ func (srv AuthService) CheckSmsCode(smsCodeCommand *command.CheckSmsCodeCommand) @@ -780,3 +585,25 @@ func (srv AuthService) CheckSmsCode(smsCodeCommand *command.CheckSmsCodeCommand)
780 "smsCodeIdentity": uid.String(), 585 "smsCodeIdentity": uid.String(),
781 }, nil 586 }, nil
782 } 587 }
  588 +
  589 +func (svr *AuthService) CaptchaInit(request *query.CaptchaInitRequest) (interface{}, error) {
  590 + var rsp map[string]interface{}
  591 + var err error
  592 +
  593 + newGeetest := geetest.NewGeetestLib(captchaID, privateKey, 2*time.Second)
  594 + status, responseBt := newGeetest.PreProcess("", request.UserIp)
  595 + var geetestRsp geetest.FailbackRegisterRespnse
  596 + json.Unmarshal(responseBt, &geetestRsp)
  597 + rspData := map[string]interface{}{
  598 + "success": geetestRsp.Success,
  599 + "gt": geetestRsp.GT,
  600 + "challenge": geetestRsp.Challenge,
  601 + "newCaptcha": geetestRsp.NewCaptcha,
  602 + }
  603 + if status == 0 {
  604 + return nil, application.ThrowError(application.BUSINESS_ERROR, "获取图形验证码失败,请重试")
  605 + }
  606 +
  607 + rsp = rspData
  608 + return rsp, err
  609 +}
@@ -8,6 +8,17 @@ const ( @@ -8,6 +8,17 @@ const (
8 LoginPlatformWeb string = "web" 8 LoginPlatformWeb string = "web"
9 ) 9 )
10 10
  11 +const (
  12 + LoginPwd = "signInPassword"
  13 + LoginSmsCode = "signInCaptcha"
  14 +)
  15 +
  16 +const (
  17 + DeviceTypeIos = "1"
  18 + DeviceTypeAndroid = "2"
  19 + DeviceTypeWeb = "4"
  20 +)
  21 +
11 // 登录凭证存储 22 // 登录凭证存储
12 type LoginAccess struct { 23 type LoginAccess struct {
13 LoginAccessId int64 `json:"loginAccessId"` 24 LoginAccessId int64 `json:"loginAccessId"`
@@ -25,6 +25,10 @@ type LoginToken struct { @@ -25,6 +25,10 @@ type LoginToken struct {
25 CompanyId int64 `json:"companyId"` 25 CompanyId int64 `json:"companyId"`
26 // 组织id 26 // 组织id
27 OrgId int64 `json:"orgId"` 27 OrgId int64 `json:"orgId"`
  28 + // 用户关联的组织列表
  29 + Orgs []int64 `json:"orgs"`
  30 + // 会话模式 1: 短时效模式 2: 长时效模式(默认)
  31 + SessionMode int `json:"sessionMode"`
28 } 32 }
29 33
30 func (t *LoginToken) GenerateAccessToken() (string, error) { 34 func (t *LoginToken) GenerateAccessToken() (string, error) {
@@ -41,10 +45,14 @@ func (t *LoginToken) GenerateAccessToken() (string, error) { @@ -41,10 +45,14 @@ func (t *LoginToken) GenerateAccessToken() (string, error) {
41 45
42 func (t *LoginToken) GenerateRefreshToken() (string, error) { 46 func (t *LoginToken) GenerateRefreshToken() (string, error) {
43 nowTime := time.Now().Unix() 47 nowTime := time.Now().Unix()
  48 + expire := refreshTokenExpiresAt
  49 + if t.SessionMode == 1 {
  50 + expire = accessTokenExpiresAt
  51 + }
44 t.StandardClaims = jwt.StandardClaims{ 52 t.StandardClaims = jwt.StandardClaims{
45 NotBefore: nowTime, 53 NotBefore: nowTime,
46 IssuedAt: nowTime, 54 IssuedAt: nowTime,
47 - ExpiresAt: nowTime + accessTokenExpiresAt, 55 + ExpiresAt: nowTime + expire,
48 Issuer: "allied_creation_gateway", 56 Issuer: "allied_creation_gateway",
49 } 57 }
50 token := jwt.NewWithClaims(jwt.SigningMethodHS256, *t) 58 token := jwt.NewWithClaims(jwt.SigningMethodHS256, *t)
@@ -44,3 +44,7 @@ func InitRedist() *redis.Client { @@ -44,3 +44,7 @@ func InitRedist() *redis.Client {
44 } 44 }
45 return clientRedis 45 return clientRedis
46 } 46 }
  47 +
  48 +func GetRedis() *redis.Client {
  49 + return clientRedis
  50 +}
@@ -168,7 +168,7 @@ func (controller *AuthController) QrcodeLoginStatus() { @@ -168,7 +168,7 @@ func (controller *AuthController) QrcodeLoginStatus() {
168 authService := service.AuthService{} 168 authService := service.AuthService{}
169 cmd := &query.QrcodeLoginStatusQuery{} 169 cmd := &query.QrcodeLoginStatusQuery{}
170 Must(controller.Unmarshal(cmd)) 170 Must(controller.Unmarshal(cmd))
171 - data, err := authService.QrcodeLoginStatus(cmd) 171 + data, err := authService.AuthLoginQrcode(cmd)
172 controller.Response(data, err) 172 controller.Response(data, err)
173 } 173 }
174 174
@@ -179,3 +179,12 @@ func (controller *AuthController) CheckSmsCode() { @@ -179,3 +179,12 @@ func (controller *AuthController) CheckSmsCode() {
179 data, err := authService.CheckSmsCode(cmd) 179 data, err := authService.CheckSmsCode(cmd)
180 controller.Response(data, err) 180 controller.Response(data, err)
181 } 181 }
  182 +
  183 +func (controller *AuthController) CaptchaInit() {
  184 + authService := service.AuthService{}
  185 + cmd := &query.CaptchaInitRequest{}
  186 + Must(controller.Unmarshal(cmd))
  187 + cmd.UserIp = controller.Ctx.Request.RemoteAddr
  188 + data, err := authService.CaptchaInit(cmd)
  189 + controller.Response(data, err)
  190 +}
@@ -10,6 +10,7 @@ func init() { @@ -10,6 +10,7 @@ func init() {
10 web.Router("/v1/auth/login/pwd", &controllers.AuthController{}, "Post:LoginPwd") 10 web.Router("/v1/auth/login/pwd", &controllers.AuthController{}, "Post:LoginPwd")
11 web.Router("/v1/auth/login/sms", &controllers.AuthController{}, "Post:LoginSms") 11 web.Router("/v1/auth/login/sms", &controllers.AuthController{}, "Post:LoginSms")
12 web.Router("/v1/auth/login/qrcode", &controllers.AuthController{}, "Post:QrcodeLoginStatus") 12 web.Router("/v1/auth/login/qrcode", &controllers.AuthController{}, "Post:QrcodeLoginStatus")
  13 + web.Router("/v1/auth/captcha-init", &controllers.AuthController{}, "Post:CaptchaInit")
13 web.Router("/v1/auth/qrcode-init", &controllers.AuthController{}, "Post:GetQrcode") 14 web.Router("/v1/auth/qrcode-init", &controllers.AuthController{}, "Post:GetQrcode")
14 web.Router("/v1/auth/sms-code", &controllers.AuthController{}, "Post:SendSmsCode") 15 web.Router("/v1/auth/sms-code", &controllers.AuthController{}, "Post:SendSmsCode")
15 web.Router("/v1/auth/check-sms-code", &controllers.AuthController{}, "Post:CheckSmsCode") 16 web.Router("/v1/auth/check-sms-code", &controllers.AuthController{}, "Post:CheckSmsCode")