wxauth

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2020 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

*

  • 缺省的微信网页授权处理

*

  • 微信网页授权处理主流程:固定线程数处理所有的请求
  • 工作方式:
  • 1. wxapi.ParseRedirectArgs(*http.Request)用于解析微信授权网页的code和state参数
  • 2. wxapi.AuthRedirect(code, state)用于处理结果
  • 3. wxapi.AuthRedirectUrl(http.ResponseWriter, *http.Request, code, state)用于全权处理网页授权,优先级高于AuthRediret()

Index

Constants

View Source
const (
	CODE = iota
	STATE
)

Variables

View Source
var HandleRedirect = func(openId, state string) (string, map[string]string, string, error) {
	return "success", nil, "", nil
}

@deprecated,建议用RegisterRedirectUrl() 根据服务号菜单state做跳转的实现,缺省实现可以被RegisterRedictHandler覆盖

Functions

func CallWxAPI

func CallWxAPI(url string, method string, postData interface{}) ([]byte, error)

func GetUserInfo

func GetUserInfo(accessToken, openId string) (map[string]interface{}, error)

func JsonCall

func JsonCall(url string, method string, jsonData interface{}) ([]byte, error)

func ParseRedirectArgs

func ParseRedirectArgs(r *http.Request) (string, string, error)

分析微信网页授权参数,分别返回 (code, state, error)

Types

type AccessToken

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

func NewAccessToken

func NewAccessToken() *AccessToken

func NewAccessTokenWithParams

func NewAccessTokenWithParams(params *wxconf.WxParamsT) *AccessToken

func (*AccessToken) Get

func (token *AccessToken) Get() (string, error)

type RedirectHandler

type RedirectHandler func(appId, openId, state string) (c string, h map[string]string, r string, err error)

---------------- 支持多服务号的实现 ------------------ *

  • @deprecated,建议用RegisterRedirectUrl()
  • [函数签名]根据服务号菜单state做跳转
  • @param appId 服务号的appId,用于区分服务号
  • @param openId 订阅用户的openId
  • @param state 微信网页授权中的参数,用来标识某个菜单
  • @return
  • c 需要显示服务号对话框中的内容
  • h 需要在微信内嵌浏览器中设置的header信息,包括Cookie
  • r 需要通过302跳转的URL。如果r不是空串,c的内容被忽略
  • err 如果没有错误返回nil,非nil表示错误

func ToAppIdRedirectHandler

func ToAppIdRedirectHandler(handler RedirectHandlerWithoutAppId) RedirectHandler

@deprecated,建议用RegisterRedirectUrl()

type RedirectHandlerWithoutAppId

type RedirectHandlerWithoutAppId func(openId, state string) (string, map[string]string, string, error)

@deprecated,建议用RegisterRedirectUrl()

type WxAppIdAuthHandler

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

func StartAuthThreads

func StartAuthThreads(params *wxconf.WxParamsT, workerNum int) *WxAppIdAuthHandler

应用初始化时调用,启动若干个线程处理微信网页授权

func (*WxAppIdAuthHandler) AuthRedirect

func (handler *WxAppIdAuthHandler) AuthRedirect(code string, state string) (string, map[string]string, string, error)

获取微信网页授权的处理结果,分别返回 [网页内容(非空), 需要设置的header, 跳转的url(非空), error]

func (*WxAppIdAuthHandler) AuthRedirectUrl

func (handler *WxAppIdAuthHandler) AuthRedirectUrl(w http.ResponseWriter, r *http.Request, code, state string)

网页授权全权转发给redirectUrl

func (*WxAppIdAuthHandler) GetAppId

func (handler *WxAppIdAuthHandler) GetAppId() string

func (*WxAppIdAuthHandler) HasRedirectUrl

func (handler *WxAppIdAuthHandler) HasRedirectUrl() bool

func (*WxAppIdAuthHandler) RegisterRedictHandler

func (p *WxAppIdAuthHandler) RegisterRedictHandler(handler RedirectHandler)

@deprecated,建议用RegisterRedirectUrl() 注册网页授权处理函数,该函数优先级"低于"RegisterRedirectUrl()注册的URL

func (*WxAppIdAuthHandler) RegisterRedirectUrl

func (p *WxAppIdAuthHandler) RegisterRedirectUrl(redirectUrl string, userInfoFlag ...string)

注册转发HTTP(s) URL,该URL将全权决定网页授权的处理。如果该URL存在,优先级要"高于"RegisterRedictHandler()注册函数。 参数JSON: {"appId": "xxx", "openId": "xxx", "state": "state"} 该URL的以POST形式接收参数,而且会得到所有的HTTP头信息,可以设置任何的响应头信息,响应结果直接显示在公众号浏览器中 响应时间要控制好,避免微信服务超时

type WxUser

type WxUser struct {
	UserInfo WxUserInfo
	// contains filtered or unexported fields
}

func NewWxUser

func NewWxUser(params *wxconf.WxParamsT) *WxUser

func (*WxUser) GetInfo

func (user *WxUser) GetInfo() error

get user info with OAuth2 API. please call this method after calling getOpenId(). this calling maybe fail if unauthorized

func (*WxUser) GetInfoByAccessToken

func (user *WxUser) GetInfoByAccessToken() (map[string]interface{}, error)

get user info by common access token please call this method after calling getOpenId(). this calling will succeed if params are valid.

func (*WxUser) GetOpenId

func (user *WxUser) GetOpenId(code string) (string, error)

其实是authorize

func (*WxUser) RefreshToken

func (user *WxUser) RefreshToken() error

func (*WxUser) TokenExpired

func (user *WxUser) TokenExpired() bool

type WxUserInfo

type WxUserInfo map[string]interface{} // 由于文档上sex是string类型,实际是整型。干脆不用struct解析了

Jump to

Keyboard shortcuts

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