package gn import ( "eta_gn/eta_bridge/models/eta" "eta_gn/eta_bridge/models/response" "eta_gn/eta_bridge/services" "eta_gn/eta_bridge/services/gn" "fmt" "time" ) // LoginEta // @Description: 获取eta的session // @author: Roc // @datetime 2024-01-23 17:44:15 // @param code string // @return resp response.LoginResp // @return err error // @return errMsg string // func LoginEta(code string) (resp response.GnUserResp, err error, errMsg string) { func LoginEta(code string) (resp response.LoginResp, err error, errMsg string) { tokenResp, e := gn.GetToken(code) if e != nil { err = fmt.Errorf("获取AccessToken失败, %v", e) return } accessToken := tokenResp.AccessToken // 获取用户信息 res, e := gn.GetUserInfo(code, accessToken) if e != nil { err = fmt.Errorf("获取用户基础信息失败, Err: %v, Code: %s, Token: %s", e, code, accessToken) return } //resp.Sub = res.Sub //resp.PreferredUsername = res.PreferredUsername //resp.Name = res.Name //resp.FamilyName = res.FamilyName //resp.GivenName = res.GivenName // 获取eta用户信息 adminInfo, e := eta.GetSysUserByAdminName(res.PreferredUsername) if e != nil { err = fmt.Errorf("用户不存在, UserName: %s", res.PreferredUsername) return } // 生成登录session sessionResp, session, e, _ := services.CreateEtaSession(adminInfo) if e != nil { err = fmt.Errorf("登录失败, %v", e) return } resp = sessionResp // 记录两方session的绑定信息 thirdSession := &eta.SysThirdSession{ SysUserID: uint64(session.SysUserId), UserName: session.UserName, AccessToken: session.AccessToken, ThirdAccessToken: tokenResp.AccessToken, ThirdExpiredTime: time.Now().Add(time.Duration(tokenResp.ExpiresIn-100) * time.Second), CreatedTime: time.Now(), LastUpdatedTime: time.Now(), } err = eta.AddSysThirdSession(thirdSession) return } func GetUserInfoByToken(token string) (res *gn.UserBaseInfoResp, err error, errMsg string) { // 获取用户信息 res, e := gn.GetUserInfoBySci(token) if e != nil { err = fmt.Errorf("获取用户基础信息失败, Err: %v, Token: %s", e, token) return } return }