解决无法获取用户绑定信息

This commit is contained in:
2025-06-16 16:50:10 +08:00
parent 2903be6223
commit 2bbe45f40c
7 changed files with 85 additions and 106 deletions

View File

@ -48,8 +48,8 @@ func newgamelifeClient(ctx context.Context) *gamelifeClient {
"prod": "https://h5.cafe.qq.com/pmd-mobile.cafe.bind-account.pc/#/bind-manage",
},
getBoundUrl: map[string]string{
"test": "https://api-testcafe.qq.com/tipmp.user.authinfoo_cgi.authinfo_cgi/GetPlatUserInfo",
"prod": "https://api.cafe.qq.com/tipmp.user.authinfoo_cgi.authinfo_cgi/GetPlatUserInfo",
"test": "https://api-test.cafe.qq.com/tipmp.user.authinfo_cgi.authinfo_cgi/GetPlatUserInfo",
"prod": "https://api.cafe.qq.com/tipmp.user.authinfo_cgi.authinfo_cgi/GetPlatUserInfo",
},
}
glog.Infof(ctx, "初始化 gamelifeClient 成功")
@ -167,10 +167,6 @@ func (s *gamelifeClient) GetUrl(ctx context.Context, popenid, appname, nickname
if !isBound {
rooturl = s.unBoundUrlMap[s.Mode]
}
baseUrl, err := url.Parse(rooturl)
if err != nil {
return "", ecode.Fail.Sub("解析基础 URL 失败")
}
cacheData, err := g.Redis().Get(ctx, fmt.Sprintf(consts.GameLifeUserKey, popenid))
if err != nil {
@ -203,48 +199,48 @@ func (s *gamelifeClient) GetUrl(ctx context.Context, popenid, appname, nickname
}
// 加密用户信息
aesEncrypt, err := encrypt.AesEncrypt(marshal, []byte(gamelifeCache.Aes), []byte(gamelifeCache.IV))
aesEncrypt, err := encrypt.AesEncryptCBCPKCS5(marshal, []byte(gamelifeCache.Aes), []byte(gamelifeCache.IV))
if err != nil {
return "", ecode.Fail.Sub("加密用户信息失败")
}
platUserInfoStr := encrypt.Base64Encode(aesEncrypt)
explatData := g.MapStrStr{"Token": gamelifeCache.Token, "PlatUserInfoStr": platUserInfoStr}
// 构建查询参数
queryParams := baseUrl.Query()
queryParams.Add("app_name", appname)
queryParams.Add("mini_program_band", consts.GamelifeMiniProgramBand)
queryParams := url.Values{}
queryParams.Add("extplat_plat", s.PlatId)
queryParams.Add("extplat_type", consts.GamelifeExtplatType)
queryParams.Add("extplat_extra", consts.GamelifeExtplatExtraPc)
queryParams.Add("extplat_data", url.QueryEscape(gconv.String(explatData)))
queryParams.Add("app_name", appname)
// 根据 isBound 设置 bind_type 和 nickname
if bindType == 1 {
queryParams.Add("bind_type", consts.GamelifeExtplatBoundTypeQQ)
} else {
queryParams.Add("bind_type", consts.GamelifeExtplatBoundTypeWX)
}
// 仅在解绑时添加 nickname
queryParams.Add("extplat_type", consts.GamelifeExtplatType)
queryParams.Add("mini_program_band", consts.GamelifeMiniProgramBand)
queryParams.Add("extplat_extra", consts.GamelifeExtplatExtraPc)
queryParams.Add("extplat_data", gconv.String(explatData))
// 解绑时加 nickname
if !isBound {
queryParams.Add("nickname", nickname)
}
// 将请求参数更新到缓存中
gamelifeCache.Params = queryParams.Encode()
baseUrl.RawQuery = queryParams.Encode()
return baseUrl.String(), nil
// 拼接最终 URL
url := fmt.Sprintf("%s?%s", rooturl, queryParams.Encode())
return url, nil
}
// GetBound 获取用户绑定情况
func (s *gamelifeClient) GetBound(ctx context.Context, popenid string) (string, error) {
baseUrl, err := url.Parse(s.getBoundUrl[s.Mode])
if err != nil {
return "", ecode.Fail.Sub("解析基础 URL 失败")
}
func (s *gamelifeClient) GetBound(ctx context.Context, popenid string) (*model.UserBoundResult, error) {
// 获取基础 URL
rooturl := s.getBoundUrl[s.Mode]
// 获取缓存
cacheData, err := g.Redis().Get(ctx, fmt.Sprintf(consts.GameLifeUserKey, popenid))
if err != nil {
return "", ecode.Fail.Sub("从缓存中获取用户信息失败")
return nil, ecode.Fail.Sub("从缓存中获取用户信息失败")
}
var gamelifeCache model.UserGamelifeCache
@ -252,38 +248,44 @@ func (s *gamelifeClient) GetBound(ctx context.Context, popenid string) (string,
// 如果缓存不存在或已过期,重新调用 GetUserKeyIV
data, err := s.GetUserKeyIV(ctx, popenid)
if err != nil {
return "", ecode.Fail.Sub("获取用户信息失败")
return nil, ecode.Fail.Sub("获取用户信息失败")
}
gamelifeCache = model.UserGamelifeCache{Aes: data.Aes, IV: data.IV, Token: data.Token}
} else {
// 缓存存在,直接解析
if err = json.Unmarshal(cacheData.Bytes(), &gamelifeCache); err != nil {
return "", ecode.Fail.Sub("解析用户信息失败")
return nil, ecode.Fail.Sub("解析用户信息失败")
}
}
// 序列化原始数据
// 加密原始数据
oriData := g.Map{
"PopenId": popenid,
"TimeStamp": time.Now().Unix(),
}
marshal, err := json.Marshal(oriData)
if err != nil {
return "", ecode.Fail.Sub("序列化用户信息失败")
return nil, ecode.Fail.Sub("序列化用户信息失败")
}
// 加密用户信息
aesEncrypt, err := encrypt.AesEncrypt(marshal, []byte(gamelifeCache.Aes), []byte(gamelifeCache.IV))
aesEncrypt, err := encrypt.AesEncryptCBCPKCS5(marshal, []byte(gamelifeCache.Aes), []byte(gamelifeCache.IV))
if err != nil {
return "", ecode.Fail.Sub("加密用户信息失败")
return nil, ecode.Fail.Sub("加密用户信息失败")
}
platUserInfoStr := encrypt.Base64Encode(aesEncrypt)
explatData := g.MapStrStr{"Token": gamelifeCache.Token, "PlatUserInfoStr": platUserInfoStr}
queryParams := baseUrl.Query()
queryParams.Add("plat_id", s.PlatId)
queryParams.Add("plat_user_info", popenid)
queryParams.Add("plat_user_str", url.QueryEscape(gconv.String(explatData)))
baseUrl.RawQuery = queryParams.Encode()
postBody := g.MapStrStr{
"plat_id": s.PlatId,
"plat_user_info": popenid,
"plat_user_str": gconv.String(g.MapStrStr{"Token": gamelifeCache.Token, "PlatUserInfoStr": platUserInfoStr}),
}
var result model.UserBoundResult
resp, err := resty.New().R().SetBody(postBody).SetResult(&result).Post(rooturl)
if err != nil {
return nil, ecode.Fail.Sub("向游戏人生获取绑定信息出现异常")
}
if resp.StatusCode() != 200 {
return nil, ecode.Fail.Sub("向游戏人生获取绑定信息失败")
}
glog.Infof(ctx, "获取用户游戏人生绑定信息: %v", result)
return baseUrl.String(), nil
return &result, nil
}