解决无法获取用户绑定信息
This commit is contained in:
@ -1,58 +1,31 @@
|
||||
package encrypt
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"errors"
|
||||
)
|
||||
|
||||
// AesEncrypt 使用 AES-CBC 模式对数据进行加密。
|
||||
//
|
||||
// 参数:
|
||||
// - plainText: 原始明文数据(必须是任意长度)
|
||||
// - key: 加密密钥(长度必须是 16、24 或 32 字节)
|
||||
// - iv: 初始化向量(必须是 16 字节)
|
||||
//
|
||||
// 返回值:
|
||||
// - 加密后的密文
|
||||
// - 错误信息(如果加密失败)
|
||||
func AesEncrypt(plainText, key, iv []byte) ([]byte, error) {
|
||||
// PKCS5 填充
|
||||
func pkcs5Padding(src []byte, blockSize int) []byte {
|
||||
padding := blockSize - len(src)%blockSize
|
||||
padText := bytes.Repeat([]byte{byte(padding)}, padding)
|
||||
return append(src, padText...)
|
||||
}
|
||||
|
||||
func AesEncryptCBCPKCS5(plainText, key, iv []byte) ([]byte, error) {
|
||||
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(iv) != aes.BlockSize {
|
||||
return nil, errors.New("IV 长度必须为 16 字节")
|
||||
}
|
||||
|
||||
plainText = pkcs7Padding(plainText, aes.BlockSize)
|
||||
cipherText := make([]byte, len(plainText))
|
||||
// PKCS5 填充(与 PKCS7 相同,只是 block size 为 8)
|
||||
paddedText := pkcs5Padding(plainText, block.BlockSize())
|
||||
|
||||
cipherText := make([]byte, len(paddedText))
|
||||
mode := cipher.NewCBCEncrypter(block, iv)
|
||||
mode.CryptBlocks(cipherText, plainText)
|
||||
mode.CryptBlocks(cipherText, paddedText)
|
||||
|
||||
return cipherText, nil
|
||||
}
|
||||
|
||||
// pkcs7Padding 对数据进行 PKCS7 填充。
|
||||
//
|
||||
// 参数:
|
||||
// - data: 原始数据
|
||||
// - blockSize: 块大小(通常为 16)
|
||||
//
|
||||
// 返回值:
|
||||
// - 填充后的数据
|
||||
func pkcs7Padding(data []byte, blockSize int) []byte {
|
||||
padding := blockSize - len(data)%blockSize
|
||||
padText := bytesRepeat(byte(padding), padding)
|
||||
return append(data, padText...)
|
||||
}
|
||||
|
||||
// bytesRepeat 返回一个重复 count 次的字节切片。
|
||||
func bytesRepeat(b byte, count int) []byte {
|
||||
buf := make([]byte, count)
|
||||
for i := 0; i < count; i++ {
|
||||
buf[i] = b
|
||||
}
|
||||
return buf
|
||||
}
|
||||
|
||||
@ -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-test。cafe.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
|
||||
}
|
||||
|
||||
@ -63,7 +63,7 @@ type (
|
||||
func GenerateToken(in *TokenIn) (string, error) {
|
||||
expire := in.ExpireTime
|
||||
if expire <= 0 {
|
||||
expire = 2 * time.Hour
|
||||
expire = 24 * time.Hour
|
||||
}
|
||||
|
||||
claims := jwtClaims{
|
||||
|
||||
Reference in New Issue
Block a user