|
@@ -229,3 +229,119 @@ func (service *AuthService) MeInfo(param *command.GetMeInfo) (map[string]interfa |
|
@@ -229,3 +229,119 @@ func (service *AuthService) MeInfo(param *command.GetMeInfo) (map[string]interfa |
229
|
"user": info,
|
229
|
"user": info,
|
230
|
}, nil
|
230
|
}, nil
|
231
|
}
|
231
|
}
|
|
|
232
|
+
|
|
|
233
|
+// Authorize 移动端授权登录
|
|
|
234
|
+func (srv *AuthService) Authorize(param *command.MobileLoginCommand) (map[string]interface{}, error) {
|
|
|
235
|
+ transactionContext, err := factory.CreateTransactionContext(nil)
|
|
|
236
|
+ if err != nil {
|
|
|
237
|
+ return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
238
|
+ }
|
|
|
239
|
+ if errStart := transactionContext.StartTransaction(); errStart != nil {
|
|
|
240
|
+ return nil, application.ThrowError(application.TRANSACTION_ERROR, errStart.Error())
|
|
|
241
|
+ }
|
|
|
242
|
+ defer func() {
|
|
|
243
|
+ _ = transactionContext.RollbackTransaction()
|
|
|
244
|
+ }()
|
|
|
245
|
+ // 统一用户中心登录
|
|
|
246
|
+ authCodeReply, err := factory.UCenterApi().AppAuthCode(param.Credentials, param.Cuid, param.Cid)
|
|
|
247
|
+ if err != nil || !authCodeReply.IsOk() {
|
|
|
248
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "统一用户中心认证失败")
|
|
|
249
|
+ }
|
|
|
250
|
+ // 用户权限校验
|
|
|
251
|
+ // 登录平台ID, 29-员工绩效
|
|
|
252
|
+ userAuthReply, err := factory.BusinessAdminApi().GetUserAuth(int64(param.Muid), constant.PLATFORM_FONT_ID)
|
|
|
253
|
+ if err != nil {
|
|
|
254
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "用户鉴权失败")
|
|
|
255
|
+ }
|
|
|
256
|
+ if !userAuthReply.IsOk() {
|
|
|
257
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, userAuthReply.Message())
|
|
|
258
|
+ }
|
|
|
259
|
+ //获取公司数据
|
|
|
260
|
+ companyRepository := factory.CreateCompanyRepository(map[string]interface{}{
|
|
|
261
|
+ "transactionContext": transactionContext,
|
|
|
262
|
+ })
|
|
|
263
|
+ company, err := companyRepository.FindOne(map[string]interface{}{
|
|
|
264
|
+ "id": param.Cid,
|
|
|
265
|
+ })
|
|
|
266
|
+ if err != nil {
|
|
|
267
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取公司数据失败")
|
|
|
268
|
+ }
|
|
|
269
|
+ userRepository := factory.CreateUserRepository(map[string]interface{}{
|
|
|
270
|
+ "transactionContext": transactionContext,
|
|
|
271
|
+ })
|
|
|
272
|
+ user, err := userRepository.FindOne(map[string]interface{}{
|
|
|
273
|
+ "id": param.Muid,
|
|
|
274
|
+ "companyId": company.Id,
|
|
|
275
|
+ })
|
|
|
276
|
+ if err != nil {
|
|
|
277
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取用户数据失败")
|
|
|
278
|
+ }
|
|
|
279
|
+ if user.Status != domain.UserStatusEnable {
|
|
|
280
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "用户被禁用")
|
|
|
281
|
+ }
|
|
|
282
|
+ if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
283
|
+ return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
284
|
+ }
|
|
|
285
|
+ userAuth := &domain.UserAuth{
|
|
|
286
|
+ UserId: user.Id,
|
|
|
287
|
+ CompanyId: user.CompanyId,
|
|
|
288
|
+ CompanyName: company.Name,
|
|
|
289
|
+ Phone: user.Account,
|
|
|
290
|
+ PlatformId: constant.PLATFORM_FONT_ID,
|
|
|
291
|
+ Name: user.Name,
|
|
|
292
|
+ AdminType: user.AdminType,
|
|
|
293
|
+ }
|
|
|
294
|
+ accessToken, err := userAuth.CreateAccessToken()
|
|
|
295
|
+ if err != nil {
|
|
|
296
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
297
|
+ }
|
|
|
298
|
+ respData := map[string]interface{}{
|
|
|
299
|
+ "authCode": accessToken,
|
|
|
300
|
+ }
|
|
|
301
|
+ return respData, nil
|
|
|
302
|
+}
|
|
|
303
|
+
|
|
|
304
|
+func (srv *AuthService) AccessToken(param *command.AccessTokenCommand) (map[string]interface{}, error) {
|
|
|
305
|
+ userAuth := domain.UserAuth{}
|
|
|
306
|
+ _, err := userAuth.ParseAccessToken(param.AuthCode)
|
|
|
307
|
+ if err != nil {
|
|
|
308
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "authcode 失效")
|
|
|
309
|
+ }
|
|
|
310
|
+
|
|
|
311
|
+ accessToken, err := userAuth.CreateAccessToken()
|
|
|
312
|
+ if err != nil {
|
|
|
313
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "生成 accessToken 失败")
|
|
|
314
|
+ }
|
|
|
315
|
+ refreshToken, err := userAuth.CreateRefreshToken()
|
|
|
316
|
+ if err != nil {
|
|
|
317
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "生成 refreshToken 解析失败")
|
|
|
318
|
+ }
|
|
|
319
|
+ respData := map[string]interface{}{
|
|
|
320
|
+ "refreshToken": refreshToken,
|
|
|
321
|
+ "accessToken": accessToken,
|
|
|
322
|
+ "expiresIn": domain.JWTExpiresSecond,
|
|
|
323
|
+ }
|
|
|
324
|
+ return respData, nil
|
|
|
325
|
+}
|
|
|
326
|
+
|
|
|
327
|
+func (srv *AuthService) RefreshToken(param *command.RefreshTokenCommand) (map[string]interface{}, error) {
|
|
|
328
|
+ userAuth := domain.UserAuth{}
|
|
|
329
|
+ _, err := userAuth.ParseAccessToken(param.RefreshToken)
|
|
|
330
|
+ if err != nil {
|
|
|
331
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "refresh_token 失效")
|
|
|
332
|
+ }
|
|
|
333
|
+ accessToken, err := userAuth.CreateAccessToken()
|
|
|
334
|
+ if err != nil {
|
|
|
335
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "生成 accessToken 失败")
|
|
|
336
|
+ }
|
|
|
337
|
+ refreshToken, err := userAuth.CreateRefreshToken()
|
|
|
338
|
+ if err != nil {
|
|
|
339
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "生成 refreshToken 解析失败")
|
|
|
340
|
+ }
|
|
|
341
|
+ respData := map[string]interface{}{
|
|
|
342
|
+ "refreshToken": refreshToken,
|
|
|
343
|
+ "accessToken": accessToken,
|
|
|
344
|
+ "expiresIn": domain.JWTExpiresSecond,
|
|
|
345
|
+ }
|
|
|
346
|
+ return respData, nil
|
|
|
347
|
+} |