Documentation
¶
Index ¶
Constants ¶
View Source
const ( // 基础错误码 ErrCodeSuccess = 0 ErrCodeInvalidCredential = 40001 ErrCodeInvalidGrantType = 40002 ErrCodeInvalidOpenID = 40003 ErrCodeInvalidMediaType = 40004 ErrCodeInvalidFileSize = 40005 ErrCodeInvalidFileFormat = 40006 ErrCodeInvalidParams = 40013 ErrCodeAccessDenied = 48004 ErrCodeAPIQuotaExceeded = 45009 // 基础API域名 BaseAPIURL = "https://api.weixin.qq.com" OpenBaseURL = "https://open.weixin.qq.com" MPBaseURL = "https://mp.weixin.qq.com" PayAPIURL = "https://api.mch.weixin.qq.com" ResURL = "https://res.wx.qq.com" // 授权页面URL MobileAuthPageURL = "https://open.weixin.qq.com/wxaopen/safe/bindcomponent" PCAuthPageURL = "https://mp.weixin.qq.com/cgi-bin/componentloginpage" SubscribeAuthPageURL = "https://mp.weixin.qq.com/mp/subscribemsg" // 通用授权参数 GrantTypeAuthorizationCode = "authorization_code" GrantTypeRefreshToken = "refresh_token" GrantTypeClientCredential = "client_credential" ResponseTypeCode = "code" // OAuth作用域 OAuthScopeBase = "snsapi_base" OAuthScopeUserInfo = "snsapi_userinfo" // 通用操作结果 SuccessResponse = "success" // 事件响应成功 EventResponseSuccess = "success" // 消息类型常量 MessageTypeText = "text" MessageTypeImage = "image" MessageTypeVoice = "voice" MessageTypeVideo = "video" MessageTypeShortVideo = "shortvideo" MessageTypeLocation = "location" MessageTypeLink = "link" MessageTypeEvent = "event" // 事件类型常量 EventTypeComponentVerifyTicket = "component_verify_ticket" EventTypeAuthorized = "authorized" EventTypeUpdateAuthorized = "updateauthorized" // 时间相关常量 EventTimestampTolerance = 300 // 事件时间戳容忍范围(秒) )
API相关公共常量
Variables ¶
This section is empty.
Functions ¶
func ApplyClientOptions ¶ added in v0.1.3
func ApplyClientOptions( opts []any, warn func(msg string), onLogger func(debugger.LoggerInterface), onHTTPClient func(HTTPClient), extraHandlers ...func(option any) (handled bool), )
ApplyClientOptions 遍历各模块构造函数的可选参数并分发。
内置支持两种通用类型:
- debugger.LoggerInterface: 自定义日志器(回调 onLogger)
- HTTPClient: 自定义HTTP客户端(回调 onHTTPClient)
extraHandlers 允许模块注册额外类型的处理逻辑(如 openplatform.EventHandler), 返回 true 表示该参数已被消费,不再进入通用分支。 未被任何分支处理的类型通过 warn 回调记录警告(warn 为 nil 时忽略)。
注意:warn 使用闭包而非 logger 值,以便在 SetLogger 生效后仍能取到当前日志器, 与重构前各模块循环内 client.logger.Warn(...) 的行为保持一致。
参数:
- opts: 可选参数切片
- warn: 未知参数类型的警告回调(接收已格式化的消息),可为 nil
- onLogger: 处理 debugger.LoggerInterface 的回调,可为 nil
- onHTTPClient: 处理 HTTPClient 的回调,可为 nil
- extraHandlers: 模块自定义类型的处理器,可为空
返回值: 无 异常: 不主动panic;回调内部的行为由调用方决定
使用示例:
core.ApplyClientOptions(opts, func(msg string) { client.logger.Warn(msg) },
client.SetLogger, client.SetHTTPClient)
// 带模块扩展类型:
core.ApplyClientOptions(opt, warn, client.SetLogger, client.SetHTTPClient,
func(option any) (handled bool) {
if h, ok := option.(EventHandler); ok {
client.SetEventHandler(h)
return true
}
return false
})
Types ¶
type APIResponse ¶
type APIResponse struct {
ErrCode int `json:"errcode"` // 错误码,0表示成功
ErrMsg string `json:"errmsg"` // 错误描述
}
APIResponse 微信API通用响应结构
type ErrorParserFunc ¶ added in v0.1.0
ErrorParserFunc 非 2xx 响应的错误解析回调类型
说明:
- 返回 nil 表示响应体不是该服务的错误格式,由调用方回退到通用错误
- 微信支付 V3 等接口在 4xx/5xx 时会返回结构化错误 JSON,需解析后供业务判断
type HTTPClient ¶
HTTPClient HTTP客户端接口
type ReqMakeOpt ¶
type ReqMakeOpt struct {
Method string // HTTP方法,如GET、POST等
URL string // 请求URL
Query interface{} // 查询参数,支持map[string]string或结构体
Body interface{} // 请求体
Result interface{} // 响应体,需要传入指针
RawResult *[]byte // 原始响应体接收器,非 nil 时跳过 JSON 解析(如下载账单文件)
Headers map[string]string // 自定义请求头,可选
VerifyResponse VerifyResponseFunc // 响应验证回调,可选
ErrorParser ErrorParserFunc // 非 2xx 响应的错误解析回调,可选
}
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
func NewRequest ¶
func NewRequest(httpClient HTTPClient, logger logger.LoggerInterface) *Request
func (*Request) Make ¶
func (r *Request) Make(ctx context.Context, options *ReqMakeOpt) error
Make 发送HTTP请求的通用方法
参数:
- ctx: 请求上下文,用于控制请求超时和取消
- options: 请求配置选项,包含方法、URL、查询参数、请求体和结果接收器
返回:
- error: 请求执行过程中的错误,包含详细的错误信息
功能:
- 支持GET、POST等常用HTTP方法
- 自动处理查询参数拼接,支持map[string]string和结构体
- 支持JSON格式的请求体和响应体
- 自动验证HTTP响应状态码
- 提供详细的日志记录
Click to show internal directories.
Click to hide internal directories.