corporation

package
v1.0.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2020 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

企业微信开发 SDK

See: https://work.weixin.qq.com/api/doc

Index

Examples

Constants

This section is empty.

Variables

View Source
var WXServerUrl = "https://qyapi.weixin.qq.com"

微信 api 服务器地址

Functions

func GetAccessToken

func GetAccessToken(app *App) (accessToken string, err error)

从 公众号实例 的 AccessToken 管理器 获取 access_token

如果没有 access_token 或者 已过期,那么刷新

获得新的 access_token 后 过期时间设置为 0.9 * expiresIn 提供一定冗余

Types

type AccessToken

type AccessToken struct {
	Cache                 cachego.Cache
	GetAccessTokenHandler GetAccessTokenFunc
}

AccessToken 管理器 处理缓存 和 刷新 逻辑

type App

type App struct {
	Config      AppConfig
	AccessToken AccessToken
	Client      Client
	Server      Server
	Corporation *Corporation
}

应用

func (*App) SetAccessTokenCacheDriver

func (app *App) SetAccessTokenCacheDriver(driver cachego.Cache)

SetAccessTokenCacheDriver 设置 AccessToken 缓存器 默认为文件缓存:目录 os.TempDir()

驱动接口类型 为 cachego.Cache

func (*App) SetGetAccessTokenHandler

func (app *App) SetGetAccessTokenHandler(f GetAccessTokenFunc)

SetGetAccessTokenHandler 设置 AccessToken 获取方法。默认 从本地缓存获取(过期从微信接口刷新)

如果有多实例服务,可以设置为 Redis 或 RPC 等中控服务器 获取 就可以避免 AccessToken 刷新冲突

type AppConfig

type AppConfig struct {
	AgentId        string
	Secret         string
	Token          string
	EncodingAESKey string
}

应用配置

type Client

type Client struct {
	Ctx *App
}

HttpClient 用于向微信接口发送请求

func (*Client) HTTPGet

func (client *Client) HTTPGet(uri string) (resp []byte, err error)

HTTPGet GET 请求

func (*Client) HTTPPost

func (client *Client) HTTPPost(uri string, payload io.Reader, contentType string) (resp []byte, err error)

HTTPPost POST 请求

type Corporation

type Corporation struct {
	Config CorporationConfig
	Logger *log.Logger
}

Corporation 企业实例

func New

func New(config CorporationConfig) (corporation *Corporation)

创建企业实例

func (*Corporation) NewApp

func (corporation *Corporation) NewApp(config AppConfig) (app *App)

创建应用实例

func (*Corporation) SetLogger

func (corporation *Corporation) SetLogger(logger *log.Logger)

SetLogger 日志记录 默认输出到 os.Stdout

可以新建 logger 输出到指定文件

如果不想开启日志,可以 SetLogger(nil)

Example
package main

import (
	"log"
	"os"

	"github.com/fastwego/wechat4work/corporation"
)

func main() {

	var Ctx *corporation.Corporation

	// 输出日志到控制台
	Ctx.SetLogger(log.New(os.Stdout, "[corporation ]", log.LstdFlags))

	// 记录日志到指定文件
	logFile, _ := os.OpenFile("/path/to/file", os.O_WRONLY, 0644)
	Ctx.SetLogger(log.New(logFile, "[corporation ]", log.LstdFlags))

	// 关闭日志
	Ctx.SetLogger(nil)
}
Output:

type CorporationConfig

type CorporationConfig struct {
	Corpid string
}

配置

type GetAccessTokenFunc

type GetAccessTokenFunc func(ctx *App) (accessToken string, err error)

GetAccessTokenFunc 获取 access_token 方法接口

type Server

type Server struct {
	Ctx *App
}

响应微信请求 或 推送消息/事件 的服务器

func (*Server) EchoStr

func (s *Server) EchoStr(writer http.ResponseWriter, request *http.Request)

EchoStr 服务器接口校验

msg_signature=ASDFQWEXZCVAQFASDFASDFSS &timestamp=13500001234 &nonce=123412323 &echostr=ENCRYPT_STR

func (*Server) ParseXML

func (s *Server) ParseXML(request *http.Request) (m interface{}, err error)

ParseXML 解析微信推送过来的消息/事件

POST /api/callback?msg_signature=ASDFQWEXZCVAQFASDFASDFSS &timestamp=13500001234 &nonce=123412323

<xml>

<ToUserName><![CDATA[toUser]]></ToUserName>
<AgentID><![CDATA[toAgentID]]></AgentID>
<Encrypt><![CDATA[msg_encrypt]]></Encrypt>

</xml>

func (*Server) Response

func (s *Server) Response(writer http.ResponseWriter, request *http.Request, reply interface{}) (err error)

Response 响应微信消息

Directories

Path Synopsis
apis
app
Package app 应用管理
Package app 应用管理
calendar
Package calendar 日程
Package calendar 日程
contact
Package contact 通讯录管理
Package contact 通讯录管理
externalcontact
Package externalcontact 外部联系人管理
Package externalcontact 外部联系人管理
health
Package health 健康上报
Package health 健康上报
invoice
Package invoice 电子发票
Package invoice 电子发票
living
Package living 企业直播
Package living 企业直播
material
Package material 素材管理
Package material 素材管理
meetingroom
Package meetingroom 会议室
Package meetingroom 会议室
message
Package message 消息推送
Package message 消息推送
msgaudit
Package msgaudit 会话内容存档
Package msgaudit 会话内容存档
oa
Package oa OA
Package oa OA
pay
Package pay 企业支付
Package pay 企业支付
util
Package util 开发辅助
Package util 开发辅助
verify
Package verify 身份验证
Package verify 身份验证
Package test 模拟微信服务器 测试
Package test 模拟微信服务器 测试
type

Jump to

Keyboard shortcuts

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