miniprogram

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: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Code2SessionURL 小程序登录凭证校验地址
	Code2SessionURL = "https://api.weixin.qq.com/sns/jscode2session"

	// GetPhoneNumberURL 通过组件 code 换取手机号地址
	GetPhoneNumberURL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber"

	// AccessTokenURL 获取小程序 access_token 地址
	AccessTokenURL = "https://api.weixin.qq.com/cgi-bin/token"

	// GrantTypeAuthorizationCode 授权类型:authorization_code
	GrantTypeAuthorizationCode = "authorization_code"

	// GrantTypeClientCredential 授权类型:client_credential
	GrantTypeClientCredential = "client_credential"
)

微信小程序相关 API 地址

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client 微信小程序客户端

func NewClient

func NewClient(config *Config, opts ...any) *Client

NewClient 创建新的小程序客户端(使用默认文件存储)

参数:

  • config: 小程序配置信息
  • opts: 可选参数,支持 debugger.LoggerInterface 与 core.HTTPClient

返回:

  • *Client: 小程序客户端实例

func NewClientWithStorage

func NewClientWithStorage(config *Config, storage storage.TokenStorage, opts ...any) *Client

NewClientWithStorage 创建新的小程序客户端(使用自定义存储)

参数:

  • config: 小程序配置信息
  • storage: 自定义存储实例
  • opts: 可选参数,支持 debugger.LoggerInterface 与 core.HTTPClient

返回:

  • *Client: 小程序客户端实例

func (*Client) Code2Session

func (c *Client) Code2Session(ctx context.Context, code string) (*Code2SessionResponse, error)

Code2Session 小程序登录凭证校验

参数:

  • ctx: 上下文
  • code: 前端 wx.login 获取的临时登录凭证

返回:

  • *Code2SessionResponse: 登录凭证校验结果
  • error: 请求失败或微信返回错误时返回错误

文档:

func (*Client) DecryptUserInfo

func (c *Client) DecryptUserInfo(sessionKey, encryptedData, iv string, result interface{}) error

DecryptUserInfo 解密微信小程序用户信息或手机号

参数:

  • sessionKey: 登录接口返回的 session_key,Base64 编码
  • encryptedData: 前端返回的 encryptedData,Base64 编码
  • iv: 前端返回的 iv,Base64 编码
  • result: 解密后 JSON 反序列化的目标结构体指针

返回:

  • error: 解密失败或水印校验不通过时返回错误

示例:

var userInfo miniprogram.UserInfo
err := client.DecryptUserInfo(sessionKey, encryptedData, iv, &userInfo)

func (*Client) GetAccessToken

func (c *Client) GetAccessToken(ctx context.Context) (string, error)

GetAccessToken 获取小程序 access_token

参数:

  • ctx: 上下文

返回:

  • string: access_token
  • error: 获取失败时返回错误

func (*Client) GetConfig

func (c *Client) GetConfig() *Config

GetConfig 获取配置信息

返回:

  • *Config: 小程序配置副本指针

func (*Client) GetLogger

func (c *Client) GetLogger() logger.LoggerInterface

GetLogger 获取日志器

返回:

  • logger.LoggerInterface: 日志器实例

func (*Client) GetPhoneNumber

func (c *Client) GetPhoneNumber(ctx context.Context, code string, openid ...string) (*GetPhoneNumberResponse, error)

GetPhoneNumber 通过组件 code 换取用户手机号

参数:

  • ctx: 上下文
  • code: 前端 getPhoneNumber 组件返回的动态令牌
  • openid: 可选参数,用户唯一标识,填入后微信会校验 openid 与 code 的绑定关系

返回:

  • *GetPhoneNumberResponse: 手机号换取结果
  • error: 请求失败或微信返回错误时返回错误

文档:

func (*Client) SetHTTPClient

func (c *Client) SetHTTPClient(client core.HTTPClient)

SetHTTPClient 设置自定义 HTTP 客户端

参数:

  • client: 自定义 HTTP 客户端

func (*Client) SetLogger

func (c *Client) SetLogger(log debugger.LoggerInterface)

SetLogger 设置自定义日志器

参数:

  • log: 自定义日志器,nil 则使用默认日志器

type Code2SessionResponse

type Code2SessionResponse struct {
	core.APIResponse
	OpenID     string `json:"openid"`            // 用户唯一标识
	SessionKey string `json:"session_key"`       // 会话密钥
	UnionID    string `json:"unionid,omitempty"` // 用户在开放平台的唯一标识,可能为空
}

Code2SessionResponse 小程序登录凭证校验响应

type Config

type Config struct {
	AppID     string `json:"app_id" ini:"app_id"`         // 小程序 appid
	AppSecret string `json:"app_secret" ini:"app_secret"` // 小程序 appsecret
}

Config 微信小程序配置结构体

func (*Config) Validate

func (c *Config) Validate() error

Validate 验证配置的有效性

返回:

  • error: 配置无效时返回错误

type GetPhoneNumberRequest

type GetPhoneNumberRequest struct {
	Code   string `json:"code"`             // 前端 getPhoneNumber 组件返回的动态令牌
	OpenID string `json:"openid,omitempty"` // 可选,用户唯一标识,填入后微信会校验 openid 与 code 的绑定关系
}

GetPhoneNumberRequest 通过组件 code 换取手机号请求

type GetPhoneNumberResponse

type GetPhoneNumberResponse struct {
	core.APIResponse
	PhoneInfo PhoneInfo `json:"phone_info"` // 手机号信息
}

GetPhoneNumberResponse 通过组件 code 换取手机号响应

type PhoneInfo

type PhoneInfo struct {
	PhoneNumber     string `json:"phoneNumber"`     // 用户绑定的手机号(国外手机号会有国家码)
	PurePhoneNumber string `json:"purePhoneNumber"` // 没有国家码的手机号
	CountryCode     string `json:"countryCode"`     // 国家码
}

PhoneInfo 手机号信息

type UserInfo

type UserInfo struct {
	OpenID    string    `json:"openId"`            // 用户 openId
	NickName  string    `json:"nickName"`          // 用户昵称
	Gender    int       `json:"gender"`            // 用户性别,0 未知,1 男,2 女
	City      string    `json:"city"`              // 用户所在城市
	Province  string    `json:"province"`          // 用户所在省份
	Country   string    `json:"country"`           // 用户所在国家
	AvatarURL string    `json:"avatarUrl"`         // 用户头像 URL
	UnionID   string    `json:"unionId,omitempty"` // 用户在开放平台的唯一标识
	Watermark Watermark `json:"watermark"`         // 数据水印
}

UserInfo 解密后的微信小程序用户信息

type Watermark

type Watermark struct {
	AppID     string `json:"appid"`     // 小程序 appid
	Timestamp int64  `json:"timestamp"` // 数据水印时间戳
}

Watermark 微信开放数据水印

字段说明:

  • AppID: 数据归属的小程序 appid
  • Timestamp: 数据生成时间戳

注意:

  • 官方建议服务端解密后校验 watermark.appid 是否与自身 appid 一致
  • 可结合 timestamp 做时效性校验

文档:

Jump to

Keyboard shortcuts

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