合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
# 微信SDK # silenceper ## 配置参数 ```go package wx // 公众号配置 const ( AppID = "wx7fa401xe810n1eb7" AppSecret = "35673237f5676a727b45b22f4ca8e34a" EncodingAESKey = "" ) // 微信支付配置 const ( MchID = "1602872469" // 商户ID Key = "454dawdahhWWW441D8EXDle454741211" // 商户Key NotifyURL = "http://www.test.com/v1/notify" // 通知回调链接 ) ``` ## 初始化 ```go package wx import ( "github.com/silenceper/wechat/v2" "github.com/silenceper/wechat/v2/cache" "github.com/silenceper/wechat/v2/miniprogram" miniConfig "github.com/silenceper/wechat/v2/miniprogram/config" "github.com/silenceper/wechat/v2/officialaccount" offConfig "github.com/silenceper/wechat/v2/officialaccount/config" "github.com/silenceper/wechat/v2/pay" payConfig "github.com/silenceper/wechat/v2/pay/config" ) var WC *wechat.Wechat // 微信 var Official *officialaccount.OfficialAccount // 微信公众号 var MiniProgram *miniprogram.MiniProgram // 微信小程序 var Pay *pay.Pay // 微信支付 var memory *cache.Memory // 微信缓存 func init() { WC = wechat.NewWechat() memory = cache.NewMemory() InitOfficial() InitMiniProgram() InitPay() } func InitOfficial() { cfg1 := &offConfig.Config{ AppID: AppID, AppSecret: AppSecret, Cache: memory, } Official = WC.GetOfficialAccount(cfg1) } func InitMiniProgram() { cfg2 := &miniConfig.Config{ AppID: AppID, AppSecret: AppSecret, Cache: memory, } MiniProgram = WC.GetMiniProgram(cfg2) } func InitPay() { cfg3 := &payConfig.Config{ AppID: AppID, MchID: MchID, Key: Key, NotifyURL: NotifyURL, } Pay = WC.GetPay(cfg3) } ``` ## 使用 ```go package main import ( "fmt" "os" ) func main() { // 注:使用该工具无需关注AccessToken的获取&失效问题 // 工具内部已经封装对AccessToken的缓存与刷新逻辑 // 用户只需关注实际需要调用的API即可 // 示例:获取公众号所有用户openid ds, err := wx.Official.GetUser().ListAllUserOpenIDs() if err != nil { fmt.Println(err) } else { fmt.Println("count", len(ds)) for i, v := range ds { fmt.Println(i, v) } } } ``` 打印结果 ```bash count 59 0 otlUS1HpVEUEnE-64Q3EZDbxvV2g 1 otlUS1BxyVhVUD44QoJZ-mSokB_c 2 otlUS1O64PI6svk4tvGa6oU3rfSk 3 otlUS1DEse6XKfyJd2WyV5hliNCs ... 57 otlUS1DdGvBajcxni1szCyNeiYsk 58 otlUS1EH6jiBHlXzMLqBgFz2vvBs ```