wechat

package module
v0.0.0-...-63002a4 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2021 License: MIT Imports: 21 Imported by: 0

README

go-wechat

🎉 微信公众号SDK

Usage

go get -u gitee.com/sky-zhou_admin/go-wechat

Import

import "gitee.com/sky-zhou_admin/go-wechat"

Coding

var (
	//client 会自动刷新token
	client wechat.Clienter
)

func init() {
	appID := "wx000000"
	appSecret := "aaaaabbbbbcccc"

	client = wechat.NewClient(appID, appSecret)
}

Context

context := wechat.NewContext(context.Background(), client)
client, err := wechat.FromContext(context)
if err != nil {
	fmt.Println(err)
}
client.GetAccessToken()
client.GetJsAPITicket()

使用全局唯一AccessToken

Fix 获取 access_token 时 AppSecret 错误,或者 access_token 无效。

服务器端

使用wechat-auto-refresh-server作为自动刷新服务器

客户端
package main

import (
	"log"

	"github.com/go-redis/redis/v8"
	"gitee.com/sky-zhou_admin/go-wechat"
	"gitee.com/sky-zhou_admin/go-wechat"
)

var (
	// Redis 缓存
	Redis *redis.Client
	// client 微信客户端
	client wechat.Clienter
)

func init() {
	initRedis()
}

func initRedis() {
	// 初始化Redis
	client := redis.NewClient(&redis.Options{
		Addr:     "127.0.0.1:3679",
		Password: "xxxxx",
		DB:       1,
	})
	_, err := client.Ping(context.Background()).Result()
	if err != nil {
		log.Fatalf("Init redis connection failed: %s \n", err)
		return
	}
	Redis = client
}

func main() {
	client = wechat.NewClientFromRedis()
	// 使用自定义Redis
	// client = wechat.NewClientFromRedis(wechat.ClientFromRedisOptionRedisClient(Redis))
	// 使用自定义RedisKey
	// client = wechat.NewClientFromRedis(
	// 	wechat.ClientFromRedisOptionRedisClient(Redis),
	// 	wechat.ClientFromRedisOptionAccessTokenKey("test_access_token"),
	// 	wechat.ClientFromRedisOptionJsAPITicketKey("test_js_api_ticket"),
	// )
	// 获取内容
	log.Printf("AccessToken:%s\n", client.GetAccessToken())
	log.Printf("JsAPITicket:%s\n", client.GetJsAPITicket())

	signal.AwaitExit()
}

例子

上传文件

filename := "test.jpg"
file, err := os.Open(filename)
if err != nil {
	log.Println(err)
	return
}
defer file.Close()

materialA := material.NewMaterial(client)
result, merr := materialA.UploadTempFile(filename, material.TypeImage, file)
if merr != nil {
	log.Println(merr)
}
log.Printf("%+v\n", result)

发送客服消息

customService := custom.NewCustom(client)
text := custom.NewTextRequest("o7n1T53CxFZ82ztXqBQKqp_XObEo", "这是客服发送的内容")
err := customService.SendText(text)
if err != nil {
	log.Println(err)
}

Documentation

Index

Constants

View Source
const (
	// RedisAccessTokenKey ...
	RedisAccessTokenKey = "github.com/nilorg/go-wechat/access_token"
	// RedisJsAPITicketKey ...
	RedisJsAPITicketKey = "github.com/nilorg/go-wechat/js_api_ticket"
)
View Source
const MetadataAccessTokenKey = "wechat-access-token"

MetadataAccessTokenKey Metadata wehcat AccessToken key.

View Source
const MetadataJsAPITicketKey = "wechat-js-api-ticket"

MetadataJsAPITicketKey Metadata wehcat JsAPITicketKey key.

Variables

View Source
var (
	// AppID 应用Key
	AppID = ""
	// AppSecret 秘密
	AppSecret = ""
)
View Source
var (
	// ErrContextNotFoundClient 上下文不存在客户端错误
	ErrContextNotFoundClient = errors.New("上下文中没有获取到微信客户端")
)
View Source
var (
	// ErrMetadataNotFoundClient 元数据不存在客户端AccessToken错误
	ErrMetadataNotFoundClient = errors.New("Metadata不存在客户端AccessToken")
)

Functions

func Decrypt

func Decrypt(value string) ([]byte, string)

Decrypt 解密

func Download

func Download(uri string, dis io.Writer) (result []byte, err error)

Download 下载非视频文件

func Encrypt

func Encrypt(value string) string

Encrypt 加密

func Execute

func Execute(url string, param Parameter) (json *simplejson.Json, err error)

Execute 执行

func Get

func Get(url string, args map[string]string) (result []byte, err error)

Get send get request.

func GetErrorMsg

func GetErrorMsg(code int) string

GetErrorMsg 获取error msg

func NewContext

func NewContext(ctx context.Context, c Clienter) context.Context

NewContext 创建微信客户端上下文

func NewError

func NewError(buf []byte) error

NewError 创建错误

func Post

func Post(url, contentType string, args map[string]string) (result string, err error)

Post send post request.

func PostJSON

func PostJSON(url string, jsonObject interface{}) (result []byte, err error)

PostJSON send post request.

func Upload

func Upload(uri, filename string, description *VideoDescription, srcFile io.Reader) (result []byte, err error)

Upload send post request.

Types

type AccessTokenReply

type AccessTokenReply struct {
	AccessToken string `json:"access_token"`
	ExpiresIn   int    `json:"expires_in"`
}

AccessTokenReply ...

type CDATA

type CDATA string

CDATA ...

func (CDATA) MarshalXML

func (c CDATA) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML ...

func (CDATA) String

func (c CDATA) String() string

String ...

type Client

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

Client 客户端

func NewClient

func NewClient(appID, appSecret string) *Client

NewClient 创建客户端

func (*Client) AppID

func (c *Client) AppID() string

AppID ...

func (*Client) AppSecret

func (c *Client) AppSecret() string

AppSecret ...

func (*Client) GetAccessToken

func (c *Client) GetAccessToken() string

GetAccessToken 获取 accessToken

func (*Client) GetJsAPITicket

func (c *Client) GetJsAPITicket() string

GetJsAPITicket 获取 js api ticket

type ClientFromRedis

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

ClientFromRedis Redis客户端

func NewClientFromRedis

func NewClientFromRedis(opts ...ClientFromRedisOption) *ClientFromRedis

NewClientFromRedis 创建客户端

func (*ClientFromRedis) GetAccessToken

func (client *ClientFromRedis) GetAccessToken() string

GetAccessToken 获取AccessToken

func (*ClientFromRedis) GetJsAPITicket

func (client *ClientFromRedis) GetJsAPITicket() string

GetJsAPITicket 获取JsAPITicket

type ClientFromRedisOption

type ClientFromRedisOption func(*ClientFromRedisOptions)

ClientFromRedisOption 为可选参数赋值的函数

func ClientFromRedisOptionAccessTokenKey

func ClientFromRedisOptionAccessTokenKey(accessTokenKey string) ClientFromRedisOption

ClientFromRedisOptionAccessTokenKey ...

func ClientFromRedisOptionJsAPITicketKey

func ClientFromRedisOptionJsAPITicketKey(jsAPITicketKey string) ClientFromRedisOption

ClientFromRedisOptionJsAPITicketKey ...

func ClientFromRedisOptionRedisClient

func ClientFromRedisOptionRedisClient(client *redis.Client) ClientFromRedisOption

ClientFromRedisOptionRedisClient ...

type ClientFromRedisOptions

type ClientFromRedisOptions struct {
	RedisClient    *redis.Client
	AccessTokenKey string
	JsAPITicketKey string
}

ClientFromRedisOptions 可选参数列表

func NewClientFromRedisOptions

func NewClientFromRedisOptions(opts ...ClientFromRedisOption) ClientFromRedisOptions

NewClientFromRedisOptions 创建可选参数

type Clienter

type Clienter interface {
	GetAccessToken() string
	GetJsAPITicket() string
}

Clienter 微信客户端接口

func FromContext

func FromContext(ctx context.Context) (Clienter, error)

FromContext 从上下文中获取微信客户端

func FromMetadata

func FromMetadata(metadata map[string]string) (Clienter, error)

FromMetadata 从元数据中获取微信客户端

type Configer

type Configer interface {
	AppID() string
	AppSecret() string
}

Configer 微信客户端接口

type JSONer

type JSONer interface {
	JSON() string
}

JSONer json接口

type JsAPITicketReply

type JsAPITicketReply struct {
	Ticket    string `json:"ticket"`
	ExpiresIn int    `json:"expires_in"`
}

JsAPITicketReply ...

type Parameter

type Parameter map[string]string

Parameter 参数

type VideoDescription

type VideoDescription struct {
	Title        string `json:"title"`        // 视频素材的标题
	Introduction string `json:"introduction"` // 视频素材的描述
}

VideoDescription 视频描述

type XMLer

type XMLer interface {
	XML() string
}

XMLer xml接口

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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