wechatslim

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2024 License: MIT Imports: 17 Imported by: 0

README

wechatslim

Simple, slim wechat client.

Docs

Wechat Mini Program Docs

import "github.com/caiguanhao/wechatslim"

var data struct {
	Date                         string  `json:"ref_date"`
	SessionsCount                int     `json:"session_cnt"`
	PageViews                    int     `json:"visit_pv"`
	UniqueVisitors               int     `json:"visit_uv"`
	NewUniqueVisitors            int     `json:"visit_uv_new"`
	SecondsOnAppPerSession       float64 `json:"stay_time_session"`
	SecondsOnAppPerUniqueVisitor float64 `json:"stay_time_uv"`
	AveragePageDepth             float64 `json:"visit_depth"`
}

ctx := context.Background()
date := time.Now().AddDate(0, 0, -1).Format("20060102")
wechat := wechatslim.New("wx0000000000000000", "00000000000000000000000000000000")
wechat.Debug = true // show request and response body
wechat.MustNewRequest(ctx, "POST",
	wechatslim.UrlAnalysisGetDailyVisitTrend,
	wechatslim.ReqBodyAnalysisGetDailyVisitTrend{
		BeginDate: date,
		EndDate:   date,
	},
).MustDo(&data, "list.*")
fmt.Println(data)

Documentation

Index

Constants

View Source
const (
	UrlApi                          = "https://api.weixin.qq.com"
	UrlAnalysisGetDailyVisitTrend   = UrlApi + "/datacube/getweanalysisappiddailyvisittrend?access_token={ACCESS_TOKEN}"
	UrlAnalysisGetWeeklyVisitTrend  = UrlApi + "/datacube/getweanalysisappidweeklyvisittrend?access_token={ACCESS_TOKEN}"
	UrlAnalysisGetMonthlyVisitTrend = UrlApi + "/datacube/getweanalysisappidmonthlyvisittrend?access_token={ACCESS_TOKEN}"
	UrlAnalysisGetUserPortrait      = UrlApi + "/datacube/getweanalysisappiduserportrait?access_token={ACCESS_TOKEN}"
	UrlAnalysisGetVisitDistribution = UrlApi + "/datacube/getweanalysisappidvisitdistribution?access_token={ACCESS_TOKEN}"
	UrlAnalysisGetVisitPage         = UrlApi + "/datacube/getweanalysisappidvisitpage?access_token={ACCESS_TOKEN}"
	UrlCreateWXAQrcode              = UrlApi + "/cgi-bin/wxaapp/createwxaqrcode?access_token={ACCESS_TOKEN}"
	UrlGetWXACodeUnlimit            = UrlApi + "/wxa/getwxacodeunlimit?access_token={ACCESS_TOKEN}"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	AppId     string
	AppSecret string
	Debug     bool
	// contains filtered or unexported fields
}

func New

func New(appId, appSecret string) *Client

New creates new client.

func (*Client) AccessToken added in v1.2.0

func (c *Client) AccessToken() *wechatAccessToken

AccessToken returns current access token.

func (*Client) CreateWXAQrcode added in v1.1.0

func (c *Client) CreateWXAQrcode(ctx context.Context, path string) ([]byte, error)

CreateWXAQrcode generates new Wechat mini program QR code (given path) and returns the JPEG image. Please note that Wechat allows total of only 100,000 QR codes created by this API for every account. https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.createQRCode.html

func (*Client) GetAccessToken added in v1.1.0

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

GetAccessToken gets access token and caches it to client.AccessToken.

func (*Client) GetWXACodeUnlimit

func (c *Client) GetWXACodeUnlimit(ctx context.Context, page, scene string) ([]byte, error)

GetWXACodeUnlimit generates new Wechat mini program QR code (given page and scene) and returns the PNG image. If page is empty, index page will be used. https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html

func (*Client) JsCodeToSession

func (c *Client) JsCodeToSession(ctx context.Context, code string) (*WechatSession, error)

JsCodeToSession gets user's OpenId, UnionId and session key by the code from wx.login().

func (*Client) MustNewRequest added in v1.2.0

func (c *Client) MustNewRequest(ctx context.Context, method, url string, reqBody interface{}) *Request

MustNewRequest is like NewRequest but panics if operation fails.

func (*Client) NewRequest added in v1.2.0

func (c *Client) NewRequest(ctx context.Context, method, url string, reqBody interface{}) (*Request, error)

NewRequest create a new request given context, method, url and request body. {ACCESS_TOKEN} in the URL will be replaced with the current access token. Request body can be io.Reader or data structures that can be JSON-marshaled.

type ReqBodyAnalysisGetDailyVisitTrend added in v1.2.0

type ReqBodyAnalysisGetDailyVisitTrend struct {
	BeginDate string `json:"begin_date"`
	EndDate   string `json:"end_date"`
}

type ReqBodyAnalysisGetMonthlyVisitTrend added in v1.2.2

type ReqBodyAnalysisGetMonthlyVisitTrend = ReqBodyAnalysisGetDailyVisitTrend

type ReqBodyAnalysisGetUserPortrait added in v1.2.3

type ReqBodyAnalysisGetUserPortrait = ReqBodyAnalysisGetDailyVisitTrend

type ReqBodyAnalysisGetVisitDistribution added in v1.2.3

type ReqBodyAnalysisGetVisitDistribution = ReqBodyAnalysisGetDailyVisitTrend

type ReqBodyAnalysisGetVisitPage added in v1.2.3

type ReqBodyAnalysisGetVisitPage = ReqBodyAnalysisGetDailyVisitTrend

type ReqBodyAnalysisGetWeeklyVisitTrend added in v1.2.2

type ReqBodyAnalysisGetWeeklyVisitTrend = ReqBodyAnalysisGetDailyVisitTrend

type ReqBodyCreateWXAQrcode added in v1.2.0

type ReqBodyCreateWXAQrcode struct {
	Path  string `json:"path"`
	Width int    `json:"width"`
}

type ReqBodyGetWXACodeUnlimit added in v1.2.0

type ReqBodyGetWXACodeUnlimit struct {
	Page        string `json:"page"`
	Scene       string `json:"scene"`
	Width       int    `json:"width"`
	Transparent bool   `json:"is_hyaline"`
}

type Request added in v1.2.0

type Request struct {
	*http.Request
	// contains filtered or unexported fields
}

func (*Request) Do added in v1.2.0

func (req *Request) Do(dest ...interface{}) error

Do sends the HTTP request and receives the response, unmarshals JSON response into the optional dest. Specify JSON path after each dest to efficiently get required info from deep nested structs. Original body is returned if dest is *[]byte.

func (*Request) MustDo added in v1.2.0

func (req *Request) MustDo(dest ...interface{})

MustDo is like Do but panics if operation fails.

type WechatError

type WechatError struct {
	Code    int    `json:"errcode"`
	Message string `json:"errmsg"`
}

func (WechatError) Error

func (e WechatError) Error() string

type WechatSession added in v1.2.0

type WechatSession struct {
	OpenId     string `json:"openid"`
	SessionKey string `json:"session_key"`
	UnionId    string `json:"unionid"`
}

type WechatUserInfo added in v1.2.0

type WechatUserInfo struct {
	PhoneNumber     string `json:"phoneNumber"`
	PurePhoneNumber string `json:"purePhoneNumber"`
	CountryCode     string `json:"countryCode"`
}

func Decrypt

func Decrypt(sessionKey, encryptedData, iv string) (*WechatUserInfo, error)

Decrypt decrypts encrypted data given session key and IV.

Jump to

Keyboard shortcuts

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