wxsign

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

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

Go to latest
Published: Aug 2, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

README

功能

  • 获取微信分享所需要的js签名信息

  • 返回签名信息

{
    Appid     string `json:"appid"`
	Noncestr  string `json:"noncestr"`
	Timestamp string `json:"timestamp"`
	Url       string `json:"url"`
	Signature string `json:"signature"`
}	

安装

go get github.com/chuixueximen/wxsign

使用

package main

import (
	"fmt"

	"github.com/chuixueximen/wxsign"
	"github.com/go-redis/redis/v8"
)

func init() {
	// 初始化缓存access_token及ticket的redis
	rdsClient := redis.NewClient(&redis.Options{
		Addr: "127.0.0.1:6379",
		Username: "",
		Password: "",
		DB:       0,
	})
	wxsign.WxSignRdsInit(rdsClient)
}

func main() {
	ws := wxsign.New(
		"appid",
		"secret",
		// 缓存access_token使用的redis key
		"wxsign:token",
		// 缓存ticket使用的redis key
		"wxsign:ticket",
	)
	sign, err := ws.GetJsSign("https://www.ooz.ink")
	if err != nil {
		fmt.Print("Get js sign err-> %#v", err)
		return
	}
	fmt.Print("Js Sign: %#v", sign)
}

Documentation

Index

Constants

View Source
const (
	// APIURLPrefix 微信授权请求
	WxAPIURLPrefix = "https://api.weixin.qq.com/cgi-bin"
	// AuthURL get access_token route
	WxAuthURL = "/token?grant_type=client_credential&"
	// GetTicketURL get ticket route
	WxGetTicketURL = "/ticket/getticket?"

	// TokenExpire token缓存的时间
	TokenExpire = 3600
	// TicketExpire tocket缓存时间
	TicketExpire = 3600
)

Variables

This section is empty.

Functions

func Signature

func Signature(jsTicket, noncestr, timestamp, url string) string

Signature

func WxSignRdsInit

func WxSignRdsInit(rc *redis.Client)

Init 初始化 redis client

Types

type WxJsSign

type WxJsSign struct {
	Appid     string `json:"appid"`
	Noncestr  string `json:"noncestr"`
	Timestamp string `json:"timestamp"`
	Url       string `json:"url"`
	Signature string `json:"signature"`
}

WxJsSign

type WxSign

type WxSign struct {
	// Appid 公众号appid
	Appid string
	// AppSecret 公众号秘钥
	AppSecret string
	// TokenRdsKey access_token缓存key
	TokenRdsKey string
	// TicketRdsKey ticket缓存key
	TicketRdsKey string
}

func New

func New(appid, secret, tokenKey, ticketKey string) *WxSign

New 创建对象

func (*WxSign) GetAccessToken

func (wSign *WxSign) GetAccessToken() (accessToken string, err error)

GetAccessToken 获取普通api调用需要的access_token 因为有次数限制,需要缓存

func (*WxSign) GetJsSign

func (wSign *WxSign) GetJsSign(url string) (*WxJsSign, error)

GetJsSign GetJsSign

func (*WxSign) GetTicket

func (wSign *WxSign) GetTicket() (ticket string, err error)

GetTicket 获取JSAPI授权TICKET

func (*WxSign) GetTicketByCache

func (wSign *WxSign) GetTicketByCache() string

GetTicketByCache 从缓存获取ticket

func (*WxSign) GetTokenByCache

func (wSign *WxSign) GetTokenByCache() string

GetTokenByCache 从缓存获取access_token

func (*WxSign) PushTicketByCache

func (wSign *WxSign) PushTicketByCache(token string, duration time.Duration)

PushTicket 将微信jsticket 存到 redis 中

func (*WxSign) PushTokenByCache

func (wSign *WxSign) PushTokenByCache(token string, duration time.Duration)

PushToken 将微信token 存到 redis 中

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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