🌱

微信开发

初始化

package common import ( "github.com/zlsgo/wechat" ) // Wx 微信实例 var ( Wx *wechat.Engine WxOpen *wechat.Engine WxQy *wechat.Engine Weapp *wechat.Engine Pay *wechat.Pay ) func init() { // 开启调试日志 wechat.Debug() // 加载文件缓存数据 _ = wechat.LoadCacheData("wechat.json") // 支持公众号 企业微信 开放平台 小程序 微信支付 Wx = wechat.New(&wechat.Mp{ AppID: "", AppSecret: "", Token: "", }) WxOpen = wechat.New(&wechat.Open{ AppID: "", AppSecret: "", EncodingAesKey: "", }) WxQy = wechat.New(&wechat.Qy{ CorpID: "", Secret: "", Token: "", EncodingAesKey: "", }) Pay = wechat.NewPay(wechat.Pay{ MchId: "", Key: "", CertPath: "", KeyPath: "", }) } func SaveWxCacheData() (string, error) { // 保存缓存数据至文件 return wechat.SaveCacheData("wechat.json") }

接入推送

r.Any("/push", func(c *znet.Context) { body, _ := c.GetDataRaw() reply, err := Wx.Reply(c.GetAllQueryMaps(), zstring.String2Bytes(body)) if err != nil { c.String(211, err.Error()) return } if c.Request.Method == "GET" { // Get 请求是响应微信发送的Token验证 validMsg, err := reply.Valid() if err != nil { c.String(211, err.Error()) return } c.String(200, validMsg) return } received, err := reply.Data() if err != nil { c.String(211, err.Error()) return } // 查看具体数据 c.Log.Dump(received) replyXml := received.ReplyText("收到消息: " + received.MsgType) c.String(200, replyXml) })
 

请求接口

// 获取请求库 Wx.Http() // Get 请求时自动带上 AccessToken Wx.HttpAccessTokenGet(url string, v ...interface{})(*zjson.Res, error) // Post 请求时自动带上 AccessToken Wx.HttpAccessTokenGet(url string, v ...interface{})(*zjson.Res, error)
接口请求示例
请求临时二维码生成接口
// 请求数据 post := map[string]interface{}{ "expire_seconds": 604800, "action_name": "QR_SCENE", "action_info": map[string]interface{}{"scene": map[string]interface{}{"scene_id": 123}}, } // 发起请求 res, err := Wx.HttpAccessTokenPost("https://api.weixin.qq.com/cgi-bin/qrcode/create", post) // 如果接口请求失败 if err != nil { // 获取具体错误说明 errMsg := wechat.ErrorMsg(err) zlog.Error(errMsg) return } // 接口返回数据 data := res.String() // {"ticket":"gQH88DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyX2RvV2xQODQ4elUxVWdEN055MTYAAgSQbL5iAwSAOgkA","expire_seconds":604800,"url":"http:\/\/weixin.qq.com\/q\/02_doWlP848zU1UgD7Ny16"} zlog.Success(data) // 获取二维码链接 url := res.Get("url").String() // http://weixin.qq.com/q/02cyZKkB848zU1UYD7Ny1O zlog.Success(url)

更多

具体请参考项目模板