微信授权登录与数据解密在Go-Zero中如何处理
通过调用微信接口获取code换取openid,并使用aes-256-cbc算法解密用户敏感数据。具体做法是先在go-zero服务中集成http客户端请求微信api,然后利用go标准库的crypto/aes包处理解密逻辑,确保密钥和iv正确拼接。

如何获取并存储用户的OpenID
使用微信提供的jscode2session接口,将前端传来的code作为参数发送请求,从返回的JSON中提取openid和session_key。这一步是后续解密的基础,必须保证session_key的安全存储。

Windows 微信自动发送工具。支持搜索微信联系人并发送文字、图片、文件和**批量文件**。通过 GUI 自动化控制微信窗口,无需微信开放接口。触发场景: 离开电脑时,远程指令OpenClaw发送图片/文件给微信联系人。条件: 微信打开并登录(允许最小化)。举例:1、把桌面上的龙虾图片发送给张三(默认微信发送);2、查找D盘最新的报价单Excel表并发送给李四(默认微信发送);3、搜索报价单并批量发送给联系人。
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
type WxResponse struct {
OpenID string `json:"openid"`
SessionKey string `json:"session_key"`
ErrCode int `json:"errcode"`
}
func GetOpenID(code, appID, appSecret string) (*WxResponse, error) {
url := fmt.Sprintf("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code", appID, appSecret, code)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
var wxResp WxResponse
json.Unmarshal(body, &wxResp)
if wxResp.ErrCode != 0 {
return nil, fmt.Errorf("wechat error: %d", wxResp.ErrCode)
}
return &wxResp, nil
}
func main() {
// 示例调用
result, err := GetOpenID("CODE", "APPID", "SECRET")
if err != nil {
fmt.Println(err)
} else {
fmt.Printf("OpenID: %s, SessionKey: %s\n", result.OpenID, result.SessionKey)
}
}如何解密微信加密的用户数据
使用AES-128-CBC模式,以session_key作为密钥,unionid或openid作为IV进行解密。注意需要去除密文末尾的PKCS7填充,并处理Base64解码后的字节流。

package main
import (
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"fmt"
)
func DecryptWechatData(sessionKey, encryptedData, ivStr string) (string, error) {
key, _ := base64.StdEncoding.DecodeString(sessionKey)
iv, _ := base64.StdEncoding.DecodeString(ivStr)
data, _ := base64.StdEncoding.DecodeString(encryptedData)
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
blockMode := cipher.NewCBCDecrypter(block, iv)
origData := make([]byte, len(data))
blockMode.CryptBlocks(origData, data)
// PKCS7 Unpadding
length := len(origData)
unpadding := int(origData[length-1])
origData = origData[:length-unpadding]
return string(origData), nil
}
func main() {
// 示例调用
decrypted, err := DecryptWechatData("SESSION_KEY", "ENCRYPTED_DATA", "IV_STRING")
if err != nil {
fmt.Println(err)
} else {
fmt.Println(decrypted)
}
}


















