Categorygithub.com/cmliang/weapp/v3
modulepackage
3.0.0-20231227064950-b8a8e684ac77
Repository: https://github.com/cmliang/weapp.git
Documentation: pkg.go.dev

# README

微信小程序服务端 SDK (for Golang)

说明

  • v1 版本入口
  • v2 版本入口
  • 查看完整文档
  • SDK 暂不包含支付相关内容 已有很多优秀的支付相关模块;
  • 微信小程序的功能和接口一直在持续更新迭代,如果遇到没有的接口或者不符合当前实际情况的接口请提交 issue 或者发起 pull request;

获取代码


go get -u github.com/cmliang/weapp/v3

初始化

  • 初始化 SDK
package main

import (
 "github.com/cmliang/weapp/v3"
)

func main() {
 sdk := weapp.NewClient("your-appid", "your-secret")
}
  • 自定义 HTTP 客户端
package main

import (
 "crypto/tls"
 "net/http"
 "time"

 "github.com/cmliang/weapp/v3"
)

func main() {
 cli := &http.Client{
  Timeout: 10 * time.Second,
  Transport: &http.Transport{
   // 跳过安全校验
   TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  },
 }

 sdk := weapp.NewClient(
  "your-appid",
  "your-secret",
  weapp.WithHttpClient(cli),
 )
}

  • 自定义日志
package main

import (
 "log"
 "os"

 "github.com/cmliang/weapp/v3"
 "github.com/cmliang/weapp/v3/logger"
)

func main() {
 lgr := logger.NewLogger(log.New(os.Stdout, "\r\n", log.LstdFlags), logger.Info, true)

 sdk := weapp.NewClient(
  "your-appid",
  "your-secret",
  weapp.WithLogger(lgr),
 )

 // 任意切换日志等级
 sdk.SetLogLevel(logger.Silent)
}

  • 自定义缓存
package main

import (
 "time"

 "github.com/cmliang/weapp/v3"
)

type MyCache struct{}

func (cc *MyCache) Set(key string, val interface{}, timeout time.Duration) {
 // ...
}

func (cc *MyCache) Get(key string) (interface{}, bool) {
 return "your-access-token", true
}

func main() {
 cc := new(MyCache)

 sdk := weapp.NewClient(
  "your-appid",
  "your-secret",
  weapp.WithCache(cc),
 )
}

  • 自定义 token 获取方法
package main

import (
 "github.com/cmliang/weapp/v3"
)

func main() {
 tokenGetter := func() (token string, expireIn uint) {

  expireIn = 1000
  token = "your-custom-token"

  return token, expireIn
 }

 sdk := weapp.NewClient(
  "your-appid",
  "your-secret",
  weapp.WithAccessTokenSetter(tokenGetter),
 )
}


调用接口示例

官方文档

package main

import (
 "fmt"
 "log"

 "github.com/cmliang/weapp/v3"
 "github.com/cmliang/weapp/v3/auth"
)

func main() {
 sdk := weapp.NewClient("your-appid", "your-secret")

 cli := sdk.NewAuth()

    // 用户支付完成后获取该用户的 UnionId
 rsp, err := cli.GetPaidUnionId(&auth.GetPaidUnionIdRequest{})
 if err != nil {
  log.Fatal(err)
 }

    // 检查加密信息是否由微信生成
 rsp, err := cli.CheckEncryptedData(&auth.CheckEncryptedDataRequest{})
 if err != nil {
  log.Fatal(err)
 }

    // 登录凭证校验
 rsp, err := cli.Code2Session(&auth.Code2SessionRequest{})
 if err != nil {
  log.Fatal(err)
 }

    // 获取小程序全局唯一后台接口调用凭据
 rsp, err := cli.GetAccessToken(&auth.GetAccessTokenRequest{})
 if err != nil {
  log.Fatal(err)
 }

    // 检查微信是否返回错误
 if err := rsp.GetResponseError(); err != nil {
  log.Println(err)
 }

 fmt.Println(rsp)
}


接收微信通知

官方文档

package main

import (
 "log"
 "net/http"

 "github.com/cmliang/weapp/v3"
 "github.com/cmliang/weapp/v3/server"
)

func main() {
 sdk := weapp.NewClient("your-appid", "your-secret")

 //  通用处理器
 handler := func(req map[string]interface{}) map[string]interface{} {
  switch req["MsgType"] {
  case "text":
   // Do something cool ...
  }

  return nil
 }

    // HTTP handler
 http.HandleFunc("/wechat/notify", func(w http.ResponseWriter, r *http.Request) {
  srv, err := sdk.NewServer("token", "aesKey", "mchID", "apiKey", false, handler)
  if err != nil {
   log.Fatalf("init server error: %s", err)
  }

  // 调用事件处理器后 通用处理器不再处理该事件
  srv.OnCustomerServiceTextMessage(func(tmr *server.TextMessageResult) *server.TransferCustomerMessage {

   return &server.TransferCustomerMessage{}
  })

  if err := srv.Serve(w, r); err != nil {
   log.Fatalf("serving error: %s", err)
  }
 })
}

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Functions

初始化客户端并用自定义配置替换默认配置.
自定义获取access_token的方法.
自定义缓存.
自定义 HTTP Client.
自定义日志.

# Constants

绑定.
系统失败.
成功.
背面.
正面.
同意申请.
删除已拒绝的申请者.
拒绝申请.
APP或H5订单.
小程序订单.
不展示.
保价.
禁播.
异常.
已过期.
已结束.
直播中.
未开始.
暂停.
手机直播.
推流.
音频.
图片.
developer为开发版;trial为体验版;formal为正式版;默认为正式版.
developer为开发版;trial为体验版;formal为正式版;默认为正式版.
developer为开发版;trial为体验版;formal为正式版;默认为正式版.
折扣价格.
一口价.
区间价格.
拍照模式.
扫描模式.
审核失败.
审核通过.
取消对用户的"正在输入"状态.
对用户下发"正在输入"状态.
展示.
图片.
解除绑定.
不保价.
未开始.
target_state = 0 时必填,文字内容模板中 member_count 的值.
target_state = 1 时必填,点击「进入」启动小程序时使用的路径。 对于小游戏,没有页面的概念,可以用于传递查询字符串(query),如 "?foo=bar".
target_state = 0 时必填,文字内容模板中 room_limit 的值.
target_state = 1 时必填,点击「进入」启动小程序时使用的版本。有效参数值为:develop(开发版),trial(体验版),release(正式版).
已开始.
No description provided by the author
No description provided by the author

# Structs

AbnormalConfirmer 异常件退回商家商家确认器.
AccountList 所有绑定的物流账号.
AddNearbyPoiResponse response of add position.
AddTemplateResponse 添加模版消息返回数据.
AICropResponse 图片智能裁剪后的返回数据.
Attribute 描述内容.
BankCardResponse 识别银行卡返回数据.
BusinessLicenseResponse 营业执照 OCR 识别返回数据.
BusinessUpdater 商户审核结果更新器.
CancelDeliveryOrderResponse 取消配送单返回数据.
CancelOrderResponse 取消订单返回数据.
CardResponse 识别卡片返回数据.
CargoDetail 包裹详情.
CheckMediaResponse 异步校验图片/音频返回数据.
No description provided by the author
Color QRCode color.
ContactUser 联系人.
CreateActivityIDResponse 动态消息.
CreateDeliveryOrderResponse 返回数据.
CreateExpressOrderResponse 创建订单返回数据.
No description provided by the author
CSMsgImage 客服图片消息.
CSMsgLink 图文链接消息.
CSMsgMPCard 接收的卡片消息.
CSMsgText 接收的文本消息.
DailySummary 用户访问小程序数据概况.
DeliveryAgent 骑手信息.
DeliveryCargo 货物信息.
DeliveryGoods 货物.
DeliveryGoodsDetail 货物详情.
DeliveryList 支持的快递公司列表.
DeliveryOrderCanceler 取消配送单参数.
DeliveryOrderCreator 下配送单参数.
DeliveryOrderGetter 请求参数.
DeliveryOrderInfo 订单信息.
DeliveryOrderUpdater 请求参数.
DeliveryShop 商品信息,会展示到物流通知消息中.
DeliveryTipAdder 增加小费参数.
DeliveryUser 发件人信息,闪送、顺丰同城急送必须填写,美团配送、达达,若传了shop_no的值可不填该字段.
Distribution 分布数据.
DistributionItem 分布数据项.
DrivingLicenseResponse 识别行驶证返回数据.
ExpreseeUserInfo 收件人/发件人信息.
ExpressAccount 物流账号.
ExpressCargo 包裹信息.
ExpressInsure 订单保价.
ExpressOrder 物流订单.
ExpressOrderCreator 订单创建器.
ExpressOrderGetter 订单获取器.
ExpressPathNode 运单轨迹节点.
ExpressPathUpdater 运单轨迹更新器.
ExpressService 服务类型.
ExpressShop 商家信息.
ExpressTemplatePreviewer 面单模板预览器.
FaceIdentifyResponse 人脸识别结果返回.
No description provided by the author
GetAllImmediateDeliveryResponse 获取已支持的配送公司列表接口返回数据.
GetBindAccountResponse 返回数据.
GetContactResponse 获取面单联系人信息返回数据.
GetDeliveryOrderResponse 返回数据.
GetExpressOrderResponse 获取运单返回数据.
GetExpressPathResponse 运单轨迹.
GetPaidUnionIDResponse response data.
GetPluginDevApplyListResponse 查询已添加的插件返回数据.
GetPluginListResponse 查询已添加的插件返回数据.
GetPrinterResponse 获取打印员返回数据.
GetPubTemplateKeyWordsByIdResponse 模板标题下的关键词列表.
GetPubTemplateTitleListResponse 帐号所属类目下的公共模板标题.
GetTemplateCategoryResponse 删除帐号下的某个模板返回数据.
GetTemplateListResponse 获取模板列表返回的数据.
KFInfo // 客服信息.
LicensePoint 证件点.
LicensePosition 证件位置.
直播间创建器.
No description provided by the author
No description provided by the author
LoginResponse 返回给用户的数据.
Miniprogram 小程序.
Mobile 解密后的用户手机号码信息.
NearbyPoi 附近地点.
Page 页面.
分页器.
PicList 门店图片.
Portrait 肖像.
PositionList 地点列表.
PreDeliveryOrderResponse 返回数据.
PreviewTemplateResponse 预览面单模板返回数据.
PrintedTextResponse 通用印刷体 OCR 识别返回数据.
PrinterUpdater 打印员更新器.
QRCode 小程序码参数.
QRCodeCreator 二维码创建器.
QRCodePoint 二维码角的位置.
QuotaGetResponse 电子面单余额.
QuotaGetter 电子面单余额获取器.
Retain 用户留存.
RetainResponse 生物认证秘钥签名验证请求返回数据.
RunData 解密后的最近30天微信运动步数.
ScanQRCodeResponse 小程序的条码/二维码识别后的返回数据.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
SearchSubmitPage 请求收录的页面.
SearchSubmitPagesRequest 小程序页面收录请求.
ServiceInfo 必服务标签.
ServiceInfos 必服务标签列表.
SetpInfo 运动步数.
ShareInfo 解密后的分享信息.
No description provided by the author
No description provided by the author
SubscribeMessage 订阅消息.
Summary 概况.
SuperResolutionResponse 图片高清化后的返回数据.
TokenResponse 获取 access_token 成功返回数据.
Trend 用户趋势.
UniformMpTmpMsg 公众号模板消息.
UniformMsgKeyword 关键字.
UniformMsgMiniprogram 小程序.
UniformMsgSender 统一服务消息.
UniformWeappTmpMsg 小程序模板消息.
UnlimitedQRCode 小程序码参数.
UpdatableMsg 动态消息.
UpdatableMsgParameter 参数 member_count target_state = 0 时必填,文字内容模板中 member_count 的值 room_limit target_state = 0 时必填,文字内容模板中 room_limit 的值 path target_state = 1 时必填,点击「进入」启动小程序时使用的路径。 对于小游戏,没有页面的概念,可以用于传递查询字符串(query),如 "?foo=bar" version_type target_state = 1 时必填,点击「进入」启动小程序时使用的版本。 有效参数值为:develop(开发版),trial(体验版),release(正式版).
UpdatableMsgTempInfo 动态消息对应的模板信息.
UpdateDeliveryOrderMocker 请求参数.
UpdateExpressOrderTester 模拟的快递公司更新订单.
UploadTempMediaResponse 上传媒体文件返回.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
UserInfo 解密后的用户信息.
UserPortrait response data of get user portrait.
VehicleLicenseResponse 识别卡片返回数据.
VerifySignatureResponse 生物认证秘钥签名验证请求返回数据.
VisitDistribution 用户小程序访问分布数据.
VisitPage 页面访问数据.
VisitTrend 生物认证秘钥签名验证请求返回数据.

# Type aliases

用户自定义获取access_token的方法.
BindStatus 账号绑定状态.
BindType 绑定动作类型.
BusinessResultCode 商户审核结果状态码.
CardType 卡片方向.
DevAction 修改操作.
ExpressOrderCanceler 订单取消器.
ExpressOrderSource 订单来源.
ExpressPathGetter 查询运单轨迹所需参数.
IDCardResponse 识别身份证返回数据.
InsureStatus 保价状态.
直播间状态.
No description provided by the author
MediaType 检测内容类型.
MiniprogramState 跳转小程序类型.
NearbyPoiShowStatus 展示状态.
No description provided by the author
RecognizeMode 图片识别模式.
SetTypingCommand 下发客服当前输入状态命令.
TempMediaType 文件类型.
UniformMsgData 模板消息内容.
UpdatableMsgParamName 参数 name 的合法值.
UpdatableMsgTargetState 动态消息修改后的状态.
URLLink 有效期类型.