wgo

package module
v0.0.0-...-fcc14e7 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

README

WeChat Go SDK

WGo CI GoDoc Go Report Card CII Best Practices

In Development

A Go SDK for the WeChat Open Platform API.

Usage

Client
package main

import "github.com/aimuz/wgo"

func main() {
	client := wgo.NewClient( < appid >)
	client := wgo.NewClientWithSecret( < appid >, < secret >)
}

Roadmap

  • TODO

Contributors

Pull requests are always welcome! Please base pull requests against the main branch and follow the contributing guide.

Feedback

We’d love to hear your thoughts on this project. Feel free to drop us a note!

License

Copyright 2022 The WGo Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidSignType = errors.New("wgo: invalid sign type")

ErrInvalidSignType is returned when sign type is invalid

View Source
var ErrOpenIDEmpty = errors.New("openID empty")

ErrOpenIDEmpty is returned when the OpenID is empty

Functions

func NewError

func NewError(errCode int, errMsg string) error

NewError ...

Types

type Client

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

Client is a WGo client.

func NewClient

func NewClient(appid string, opts ...ClientOption) *Client

NewClient ...

func NewClientWithSecret

func NewClientWithSecret(appid string, secret string, opts ...ClientOption) *Client

NewClientWithSecret ...

func (*Client) CheckEncryptedData

func (c *Client) CheckEncryptedData()

CheckEncryptedData ...

func (*Client) CheckSessionKey

func (c *Client) CheckSessionKey(ctx context.Context, openID, signature string) error

CheckSessionKey 校验服务器所保存的登录态 session_key 是否合法。 为了保持 session_key 私密性,接口不明文传输 session_key,而是通过校验登录态签名完成。 see: https://developers.weixin.qq.com/minigame/dev/api-backend/open-api/login/auth.checkSessionKey.html

func (*Client) CheckSessionKeyWithSignType

func (c *Client) CheckSessionKeyWithSignType(ctx context.Context, openID, signature string, st SignType) error

CheckSessionKeyWithSignType 校验服务器所保存的登录态 session_key 是否合法。 为了保持 session_key 私密性,接口不明文传输 session_key,而是通过校验登录态签名完成。 see: https://developers.weixin.qq.com/minigame/dev/api-backend/open-api/login/auth.checkSessionKey.html

func (*Client) ClearQuota

func (c *Client) ClearQuota(ctx context.Context) error

ClearQuota ...

func (*Client) ClearQuotaByAppSecret

func (c *Client) ClearQuotaByAppSecret(ctx context.Context) error

ClearQuotaByAppSecret ...

func (*Client) Code2Session

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

Code2Session 登录凭证校验。 通过 wx.login 接口获得临时登录凭证 code 后传到开发者服务器调用此接口完成登录流程。 see: https://developers.weixin.qq.com/minigame/dev/api-backend/open-api/login/auth.code2Session.html

func (*Client) CreateQRCode

func (c *Client) CreateQRCode(ctx context.Context, path string, width int) (*CreateQRCodeResponse, error)

CreateQRCode Obtains Mini Program QR code in scenarios where a small number of codes are required.

Note that Mini Program codes generated via this API never expire, but the quantity is limited. For details,
see Obtaining the QR Code https://developers.weixin.qq.com/miniprogram/en/dev/framework/open-ability/qr-code.html.
see: https://developers.weixin.qq.com/miniprogram/en/dev/api-backend/open-api/qr-code/wxacode.createQRCode.html

func (*Client) GenerateNFCScheme

func (c *Client) GenerateNFCScheme()

GenerateNFCScheme ...

func (*Client) GenerateScheme

func (c *Client) GenerateScheme()

GenerateScheme ...

func (c *Client) GenerateShortLink()

GenerateShortLink ...

func (c *Client) GenerateURLLink()

GenerateURLLink ...

func (*Client) GetAPIQuota

func (c *Client) GetAPIQuota(ctx context.Context, cgiPath string) (*GetAPIQuotaResponse, error)

GetAPIQuota ...

func (*Client) GetPaidUnionID

func (c *Client) GetPaidUnionID()

GetPaidUnionID ...

func (*Client) GetPhoneNumber

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

GetPhoneNumber 获取手机号 该接口用于将code换取用户手机号。 说明,每个 code 只能使用一次,code 的有效期为 5min。

func (*Client) GetPluginOpenPId

func (c *Client) GetPluginOpenPId()

GetPluginOpenPId ...

func (*Client) GetQRCode

func (c *Client) GetQRCode(ctx context.Context, req *GetQRCodeRequest) (*GetQRCodeResponse, error)

GetQRCode Obtains Mini Program QR code in scenarios where a small number of codes are required.

Mini Program codes generated via this API are permanently effective, but the quantity is limited. For details,
see Obtaining the QR Code: https://developers.weixin.qq.com/miniprogram/en/dev/framework/open-ability/qr-code.html
see: https://developers.weixin.qq.com/miniprogram/en/dev/api-backend/open-api/qr-code/wxacode.get.html

func (*Client) GetRidInfo

func (c *Client) GetRidInfo(ctx context.Context, rid string) (*GetRidInfoResponse, error)

GetRidInfo ...

func (*Client) GetUnlimitedQRCode

func (c *Client) GetUnlimitedQRCode(ctx context.Context, req *GetUnlimitedQRCodeRequest) (*GetUnlimitedQRCodeResponse, error)

GetUnlimitedQRCode Obtains the Mini Program code in scenarios where a large number of codes are required.

Mini Program codes generated via this API are permanently effective.
There is no upper limit on the number of codes. For more information on how to use it,
see Obtaining the QR Code: https://developers.weixin.qq.com/miniprogram/en/dev/framework/open-ability/qr-code.html
see: https://developers.weixin.qq.com/miniprogram/en/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html
chinese see: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html

func (*Client) GetUserEncryptKey

func (c *Client) GetUserEncryptKey()

GetUserEncryptKey ...

func (*Client) NewRequest

func (c *Client) NewRequest() *rest.Request

NewRequest creates a new request

func (*Client) QueryScheme

func (c *Client) QueryScheme()

QueryScheme ...

func (c *Client) QueryURLLink()

QueryURLLink ...

type ClientOption

type ClientOption func(*clientConfig)

ClientOption is a client option for the WGo Client config.

func WithHTTPClient

func WithHTTPClient(client *http.Client) ClientOption

WithHTTPClient sets the http client for the WGo client

func WithSecret

func WithSecret(secret string) ClientOption

func WithToken

func WithToken(token string) ClientOption

WithToken sets the token for the WGo client

func WithTokenSource

func WithTokenSource(tokenSource TokenSource) ClientOption

WithTokenSource sets the token source for the WGo client

type Code2SessionResponse

type Code2SessionResponse struct {
	OpenID     string `json:"openid"`
	SessionKey string `json:"session_key"`
	UnionID    string `json:"unionid"`
}

Code2SessionResponse is the response to an Code2Session

type CreateQRCodeResponse

type CreateQRCodeResponse struct {
	Buffer      []byte `json:"buffer"`
	ContentType string `json:"contentType"`
}

CreateQRCodeResponse is the response of CreateQRCode

type Error

type Error struct {
	Code int    `json:"errcode"`
	Msg  string `json:"errmsg"`
	Err  error  `json:"-"`
}

Error ...

func (*Error) Error

func (e *Error) Error() string

Error implements the error interface

func (*Error) Unwrap

func (e *Error) Unwrap() error

type GetAPIQuota

type GetAPIQuota struct {
	DailyLimit int `json:"daily_limit"`
	Used       int `json:"used"`
	Remain     int `json:"remain"`
}

GetAPIQuota ...

type GetAPIQuotaResponse

type GetAPIQuotaResponse struct {
	Quota GetAPIQuota `json:"quota"`
}

GetAPIQuotaResponse ...

type GetPhoneNumberResponse

type GetPhoneNumberResponse struct {
	PhoneInfo PhoneInfo `json:"phone_info"`
}

GetPhoneNumberResponse 获取手机号响应

type GetQRCodeRequest

type GetQRCodeRequest struct {
	Path      string          `json:"path"`
	Width     int             `json:"width"`
	AutoColor bool            `json:"auto_color"`
	LineColor QRCodeLineColor `json:"line_color"`
	IsHyaline bool            `json:"is_hyaline"`
}

GetQRCodeRequest is a request to get the GetQRCode

type GetQRCodeResponse

type GetQRCodeResponse struct {
	Buffer      []byte `json:"buffer"`
	ContentType string `json:"contentType"`
}

GetQRCodeResponse is the response of GetQRCode

type GetRidInfo

type GetRidInfo struct {
	InvokeTime   int    `json:"invoke_time"`
	CostInMs     int    `json:"cost_in_ms"`
	RequestURL   string `json:"request_url"`
	RequestBody  string `json:"request_body"`
	ResponseBody string `json:"response_body"`
	ClientIP     string `json:"client_ip"`
}

GetRidInfo ...

type GetRidInfoResponse

type GetRidInfoResponse struct {
	Request GetRidInfo `json:"request"`
}

GetRidInfoResponse ...

type GetUnlimitedQRCodeRequest

type GetUnlimitedQRCodeRequest struct {
	// Scene is required
	// 	A maximum of 32 visible characters including numbers, uppercase and lowercase English letters,
	//	and some special characters !#$&'()*+,/:;=?@-._~ are supported. Other characters need to be encoded
	//	as valid characters. (Because % is not supported, Chinese characters cannot be processed via urlencode,
	//	so you must use other encoding schemes instead.)
	Scene string `json:"scene"`

	// Page This must be an existing page of a published Mini Program (otherwise, an error is reported),
	//	for example, pages/index/index. Do not prefix a root path with /. This field cannot contain parameters
	//	(parameters can be contained in the scene field).
	//	If this field is left empty, the user is redirected to the homepage by default.
	Page string `json:"page"`

	// Width The width of the QR code (in px). The minimum value is 280 px and the maximum value is 1280 px.
	Width int `json:"width"`

	// AutoColor The automatically configured line color. If the color is still black,
	//	it indicates that the setting of the dominant tone is not recommended. Its value is "false" by default.
	AutoColor bool `json:"auto_color"`

	// LineColor It takes effect when auto_color is "false". The color is specified via RGB,
	//	for example, {"r":"xxx","g":"xxx","b":"xxx"}, and indicated by a decimal value.
	LineColor QRCodeLineColor `json:"line_color"`

	// IsHyaline Specifies whether a transparent background is required.
	//	"true" indicates a transparent background is generated for the Mini Program.
	IsHyaline bool `json:"is_hyaline"`
}

GetUnlimitedQRCodeRequest is a request to get the GetUnlimitedQRCode see: https://developers.weixin.qq.com/miniprogram/en/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html#Request-Parameters chinese see: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html#%E8%AF%B7%E6%B1%82%E5%8F%82%E6%95%B0

type GetUnlimitedQRCodeResponse

type GetUnlimitedQRCodeResponse struct {
	Buffer      []byte `json:"buffer"`
	ContentType string `json:"contentType"`
}

GetUnlimitedQRCodeResponse is the response of GetUnlimitedQRCode

see: https://developers.weixin.qq.com/miniprogram/en/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html#Return-Values
chinese see: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html#%E8%BF%94%E5%9B%9E%E5%80%BC

type GetUserInfoRequest

type GetUserInfoRequest struct {
	OpenID string `json:"openid"`
	Lang   string `json:"lang"`
}

GetUserInfoRequest is the request for the GetUserInfo method

type GetUserInfoResponse

type GetUserInfoResponse struct {
	Subscribe      int    `json:"subscribe"`
	OpenID         string `json:"openid"`
	Language       string `json:"language"`
	SubscribeTime  int    `json:"subscribe_time"`
	UnionID        string `json:"unionid"`
	Remark         string `json:"remark"`
	GroupID        int    `json:"groupid"`
	TagIDList      []int  `json:"tagid_list"`
	SubscribeScene string `json:"subscribe_scene"`
	QrScene        int    `json:"qr_scene"`
	QrSceneStr     string `json:"qr_scene_str"`
}

GetUserInfoResponse is the response to a GetUserInfo request

type GrantType

type GrantType string

GrantType 授权类型

const (
	// GrantTypeClientCredential 获取 AccessToken 使用该值
	GrantTypeClientCredential GrantType = "client_credential"

	// GrantTypeAuthorizationCode 小程序登录使用该类型
	GrantTypeAuthorizationCode GrantType = "authorization_code"
)

type JSONValidator

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

JSONValidator 是一个结构体,它用于在解析 JSON 数据之前验证 JSON 是否符合要求。

主要的作用是检查 JSON 数据中是否包含名为 errcode 和 errmsg 的字段,
如果存在 errcode 字段,则返回一个包含 errcode 和 errmsg 的错误。
否则,JSONValidator 将解析 JSON 数据。

func NewJSONValidator

func NewJSONValidator(val interface{}) *JSONValidator

NewJSONValidator 是一个函数,它创建并返回一个 JSONValidator 的实例。 它接受一个参数 val,它是要解析 JSON 数据的目标结构体或值。

func (*JSONValidator) UnmarshalJSON

func (r *JSONValidator) UnmarshalJSON(body []byte) error

UnmarshalJSON 是 JSONValidator 的方法,它用于在解析 JSON 数据之前验证 JSON 是否符合要求。

它首先从 JSON 数据中获取 errcode 和 errmsg 字段,并检查 errcode 是否大于 0。
如果 errcode 大于 0,则 UnmarshalJSON 方法会返回一个包含 errcode 和 errmsg 的错误。
如果 errcode 不大于 0,则 UnmarshalJSON 方法将使用 encoding/json 包的 Unmarshal 函数解析 JSON 数据到目标结构体或值中。

type PhoneInfo

type PhoneInfo struct {
	// PhoneNumber 用户绑定的手机号(国外手机号会有区号)
	PhoneNumber string `json:"phoneNumber"`

	// PurePhoneNumber 没有区号的手机号
	PurePhoneNumber string `json:"purePhoneNumber"`

	// CountryCode 区号
	CountryCode string `json:"countryCode"`

	// Watermark 数据水印
	Watermark PhoneInfoWatermark `json:"watermark"`
}

PhoneInfo 用户手机号信息

type PhoneInfoWatermark

type PhoneInfoWatermark struct {
	// 用户获取手机号操作的时间戳
	Timestamp int `json:"timestamp"`
	// 小程序appid
	AppID string `json:"appid"`
}

PhoneInfoWatermark 数据水印

type QRCodeLineColor

type QRCodeLineColor struct {
	R int `json:"r"`
	G int `json:"g"`
	B int `json:"b"`
}

QRCodeLineColor It takes effect when auto_color is "false".

The color is specified via RGB, for example, {"r":"xxx","g":"xxx","b":"xxx"}, and indicated by a decimal value.

type RoundTripper

type RoundTripper struct {
	TokenSource TokenSource
	// contains filtered or unexported fields
}

RoundTripper is a RoundTripper that implements the http.RoundTripper interface

func (*RoundTripper) RoundTrip

func (r *RoundTripper) RoundTrip(request *http.Request) (*http.Response, error)

RoundTrip Add the AccessToken parameter to the request

type SignType

type SignType string

SignType ...

const (
	// SignTypeHMACSHA1 ...
	// Deprecated: Suggested Use [SignTypeHMACSHA256]
	SignTypeHMACSHA1 SignType = "hmac_sha1"

	// SignTypeHMACSHA256 ...
	SignTypeHMACSHA256 SignType = "hmac_sha256"
)

func (SignType) IsValid

func (st SignType) IsValid() error

IsValid checks CheckSessionKey sig_method

type Token

type Token struct {
	// AccessToken is the token that authorizes and authenticates
	// the requests.
	AccessToken string `json:"access_token"`

	// Expiry is the optional expiration time of the access token.
	//
	// If zero, TokenSource implementations will reuse the same
	// token forever and RefreshToken or equivalent
	// mechanisms for that TokenSource will not be used.
	Expiry time.Time `json:"expiry,omitempty"`
}

Token represents the credentials used to authorize the requests to access protected resources on the OAuth 2.0 provider's backend.

Most users of this package should not access fields of Token directly. They're exported mostly for use by related packages implementing derivative OAuth2 flows.

type TokenSource

type TokenSource interface {
	// Token returns a token or an error.
	// Token must be safe for concurrent use by multiple goroutines.
	// The returned Token must not be modified.
	Token() (*Token, error)
}

A TokenSource is anything that can return a token.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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