core

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 22, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// 基础错误码
	ErrCodeSuccess           = 0
	ErrCodeInvalidCredential = 40001
	ErrCodeInvalidGrantType  = 40002
	ErrCodeInvalidOpenID     = 40003
	ErrCodeInvalidMediaType  = 40004
	ErrCodeInvalidFileSize   = 40005
	ErrCodeInvalidFileFormat = 40006
	ErrCodeInvalidParams     = 40013
	ErrCodeUnauthorized      = 48001
	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"
	EventTypeUnauthorized          = "unauthorized"
	EventTypeAuthorized            = "authorized"
	EventTypeUpdateAuthorized      = "updateauthorized"

	// 时间相关常量
	EventTimestampTolerance = 300 // 事件时间戳容忍范围(秒)
)

API相关公共常量

Variables

This section is empty.

Functions

This section is empty.

Types

type APIResponse

type APIResponse struct {
	ErrCode int    `json:"errcode"` // 错误码,0表示成功
	ErrMsg  string `json:"errmsg"`  // 错误描述
}

APIResponse 微信API通用响应结构

func (*APIResponse) Error

func (r *APIResponse) Error() string

Error 实现error接口

func (*APIResponse) IsSuccess

func (r *APIResponse) IsSuccess() bool

IsSuccess 检查API响应是否成功

type ErrorParserFunc added in v0.1.0

type ErrorParserFunc func(statusCode int, headers http.Header, body []byte) error

ErrorParserFunc 非 2xx 响应的错误解析回调类型

说明:

  • 返回 nil 表示响应体不是该服务的错误格式,由调用方回退到通用错误
  • 微信支付 V3 等接口在 4xx/5xx 时会返回结构化错误 JSON,需解析后供业务判断

type HTTPClient

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

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响应状态码
  • 提供详细的日志记录

func (*Request) MakeRaw

func (r *Request) MakeRaw(req *http.Request) (*http.Response, error)

MakeRaw 发送原始HTTP请求,返回响应对象

type VerifyResponseFunc added in v0.1.0

type VerifyResponseFunc func(ctx context.Context, statusCode int, headers http.Header, body []byte) error

VerifyResponseFunc 响应验证回调函数类型

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL