基于 Go 设计的微信 API
发布于 4 年前 作者 yangping 2105 次浏览 来自 分享

支持连接不同的地区的微信服务器
支持一行代码从被动消息的 http.Request 中安全取出消息成MessageData。内部实现了识别并解密消息、校验请求的Query数据。
支持自动填充Query中的access_token数据。
链式调用,让不同需求的业务能一气和成!
时效性凭证安置方式约定
access_token、js_sdk_ticket 这类需要每7200秒刷新一次的,放到crontab中。

核心设计
算法
一个基础账号对象MpAccount,它有三个行为:

为微信H5的网址签名 UrlSign(url string)
读取被动消息通知 ReadMessage(req *http.Request)
主动发出请求 NewMpReq(path mp_api.MpApi) *mpReq
数据结构
常量:constant.go
基础信息:basic_information.go
自定义菜单:custom_menus.go
消息:message.go
媒体文件上传:media.go
微信网页开发:oa_web_apps.go
用户管理:user.go
账号管理:account.go
对话能力:guide.go
小程序:mini_program.go
只实现了很有限的数据。若需要使用本库自带的数据结构之外的API。完全可以参考本库的数据结构写法,自行另起书写(注意不同业务的tag名称不同)。 并能得到一样的兼容体验!

举例
a := MpAccount{
AppId: “your_app_id”,
AccessToken: “38_XtyPcVUODHd8q3TNYPVGAZ2WNRx_nW4gnclObbv78tsEa1Y_bwdkLALDMEb4372wYqcC_CanjU9O0Zw4MqHiqxrIukk_G4ElAUxyv_ASOb0V2y8647cbxbYU-G8CbtnPdLNub8NrqtUVrSTnWAPaAGALPE”,
// …
ServerHost: mp_api.ServerHostShangHai, // 选择离自己最近的服务主机
}

// 一个简单的只带access_token的GET API
var list mp_api.MessageCustomServiceKfListRes
if err := a.NewMpReq(mp_api.MessageCustomServiceKfList).Bind(&list).Do(); err != nil {
	t.Error(err)
}
log.Println(list)

// 一个POST API
var rs mp_api.AccountShortUrlRes
err = a.NewMpReq(mp_api.AccountShortUrl).SendData(mp_api.AccountShortUrlData{
	Action:  mp_api.ShortUrlAction,
	LongUrl: "https://developers.weixin.qq.com/doc/offiaccount/Account_Management/URL_Shortener.html",
}).Bind(&rs).Do()
if err != nil {
	t.Error(err)
}
log.Println(rs)

// 一个上传媒体文件的API
err = a.NewMpReq(mp_api.MessageCustomServiceKfAccountUploadHeadImg).Query(mp_api.MessageCustomServiceKfAccountUploadHeadImgQuery{
	KfAccount: "1@1",
}).Upload(resp.Body, "png")
if err != nil {
	t.Error(err)
}

商户账号API(V2版)
App、JSAPI、小程序下单 分账 付款至微信零钱 付款至个人银行卡 发红包

https://www.imdb.com/list/ls500987655/
https://www.imdb.com/list/ls500987651/
https://www.imdb.com/list/ls500987671/
https://www.imdb.com/list/ls500987674/
https://www.imdb.com/list/ls500987612/
https://www.imdb.com/list/ls500987630/
https://www.imdb.com/list/ls500987663/
https://www.imdb.com/list/ls500987664/
https://www.imdb.com/list/ls500987647/
https://www.wenjuan.com/s/UZBZJvwpH3/
https://www.wenjuan.com/s/UZBZJvwpH3
https://www.wenjuan.com/z/UZBZJvwpH3/
https://www.wenjuan.com/z/UZBZJvwpH3
https://www.wenjuan.com/s/UZBZJv1rJV/
https://www.wenjuan.com/s/UZBZJv1rJV
https://www.wenjuan.com/z/UZBZJv1rJV/
https://www.wenjuan.com/z/UZBZJv1rJV
https://www.wenjuan.com/s/UZBZJvJXgz/
https://www.wenjuan.com/s/UZBZJvJXgz
https://www.wenjuan.com/z/UZBZJvJXgz/
https://www.wenjuan.com/z/UZBZJvJXgz
https://www.wenjuan.com/s/UZBZJvtwCg/
https://www.wenjuan.com/s/UZBZJvtwCg
https://www.wenjuan.com/z/UZBZJvtwCg/
https://www.wenjuan.com/z/UZBZJvtwCg
https://www.wenjuan.com/s/UZBZJvR1M5/
https://www.wenjuan.com/s/UZBZJvR1M5
https://www.wenjuan.com/z/UZBZJvR1M5/
https://www.wenjuan.com/z/UZBZJvR1M5
https://www.wenjuan.com/s/UZBZJvzyxjV/
https://www.wenjuan.com/s/UZBZJvzyxjV
https://www.wenjuan.com/z/UZBZJvzyxjV/
https://www.wenjuan.com/z/UZBZJvzyxjV
https://www.wenjuan.com/s/UZBZJvQQE7l/
https://www.wenjuan.com/s/UZBZJvQQE7l
https://www.wenjuan.com/z/UZBZJvQQE7l/
https://www.wenjuan.com/z/UZBZJvQQE7l
https://www.wenjuan.com/s/UZBZJvQhbg/
https://www.wenjuan.com/s/UZBZJvQhbg
https://www.wenjuan.com/z/UZBZJvQhbg/
https://www.wenjuan.com/z/UZBZJvQhbg
https://www.wenjuan.com/s/UZBZJvm01U/
https://www.wenjuan.com/s/UZBZJvm01U
https://www.wenjuan.com/z/UZBZJvm01U/
https://www.wenjuan.com/z/UZBZJvm01U
https://www.wenjuan.com/s/UZBZJvhY8DR/
https://www.wenjuan.com/s/UZBZJvhY8DR
https://www.wenjuan.com/z/UZBZJvhY8DR/
https://www.wenjuan.com/z/UZBZJvhY8DR
https://www.wenjuan.com/s/UZBZJvrGHC/
https://www.wenjuan.com/s/UZBZJvrGHC
https://www.wenjuan.com/z/UZBZJvrGHC/
https://www.wenjuan.com/z/UZBZJvrGHC
https://www.wenjuan.com/s/UZBZJvaHSu1/
https://www.wenjuan.com/s/UZBZJvaHSu1
https://www.wenjuan.com/z/UZBZJvaHSu1/
https://www.wenjuan.com/z/UZBZJvaHSu1
https://www.wenjuan.com/s/UZBZJvlLfNF/
https://www.wenjuan.com/s/UZBZJvlLfNF
https://www.wenjuan.com/z/UZBZJvlLfNF/
https://www.wenjuan.com/z/UZBZJvlLfNF
https://www.wenjuan.com/s/UZBZJvP0VK/
https://www.wenjuan.com/s/UZBZJvP0VK
https://www.wenjuan.com/z/UZBZJvP0VK/
https://www.wenjuan.com/z/UZBZJvP0VK
https://www.wenjuan.com/s/UZBZJv4PEM/
https://www.wenjuan.com/s/UZBZJv4PEM
https://www.wenjuan.com/z/UZBZJv4PEM/
https://www.wenjuan.com/z/UZBZJv4PEM
https://www.wenjuan.com/s/UZBZJv6k2W/
https://www.wenjuan.com/s/UZBZJv6k2W
https://www.wenjuan.com/z/UZBZJv6k2W/
https://www.wenjuan.com/z/UZBZJv6k2W
https://www.wenjuan.com/s/UZBZJvUDOm/
https://www.wenjuan.com/s/UZBZJvUDOm
https://www.wenjuan.com/z/UZBZJvUDOm/
https://www.wenjuan.com/z/UZBZJvUDOm

https://www.imdb.com/list/ls500987643/
https://www.imdb.com/list/ls500987692/
https://www.imdb.com/list/ls500987687/
https://www.imdb.com/list/ls500987258/
https://www.imdb.com/list/ls500987277/
https://www.imdb.com/list/ls500987211/
https://www.imdb.com/list/ls500987218/
https://www.imdb.com/list/ls500987232/
https://www.imdb.com/list/ls500987265/
https://www.imdb.com/list/ls500987224/
https://www.imdb.com/list/ls500987229/
https://www.imdb.com/list/ls500987247/
https://www.imdb.com/list/ls500987242/
https://www.imdb.com/list/ls500987298/
https://www.imdb.com/list/ls500987281/
https://www.imdb.com/list/ls500987405/
https://www.imdb.com/list/ls500987403/
https://www.imdb.com/list/ls500987457/
https://www.imdb.com/list/ls500987459/
https://www.imdb.com/list/ls500987473/
https://www.imdb.com/list/ls500987472/
https://www.imdb.com/list/ls500987412/
https://www.imdb.com/list/ls500987418/
https://www.imdb.com/list/ls500987467/
https://www.imdb.com/list/ls500987463/
https://www.imdb.com/list/ls500987425/
https://www.imdb.com/list/ls500987423/

回到顶部