1
0
mirror of https://github.com/silenceper/wechat.git synced 2026-02-04 12:52:27 +08:00

[feature] Format the code and improve Mini Program authorization to o… (#473)

* [feature] Format the code and improve Mini Program authorization to obtain openid(miniprogram/auth/auth.go Code2Session)

* [feature] CheckEncryptedData (https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/user-info/auth.checkEncryptedData.html)

* upgrade json error

* upgrade json error

Co-authored-by: houseme <houseme@outlook.com>
This commit is contained in:
houseme
2021-09-08 11:03:23 +08:00
committed by GitHub
parent 47adf42208
commit 96c1f98944
90 changed files with 787 additions and 760 deletions

View File

@@ -2,33 +2,33 @@ package account
import "github.com/silenceper/wechat/v2/openplatform/context"
//Account 开放平台张哈管理
//TODO 实现方法
// Account 开放平台张哈管理
// TODO 实现方法
type Account struct {
*context.Context
}
//NewAccount new
// NewAccount new
func NewAccount(ctx *context.Context) *Account {
return &Account{ctx}
}
//Create 创建开放平台帐号并绑定公众号/小程序
// Create 创建开放平台帐号并绑定公众号/小程序
func (account *Account) Create(appID string) (string, error) {
return "", nil
}
//Bind 将公众号/小程序绑定到开放平台帐号下
// Bind 将公众号/小程序绑定到开放平台帐号下
func (account *Account) Bind(appID string) error {
return nil
}
//Unbind 将公众号/小程序从开放平台帐号下解绑
// Unbind 将公众号/小程序从开放平台帐号下解绑
func (account *Account) Unbind(appID string, openAppID string) error {
return nil
}
//Get 获取公众号/小程序所绑定的开放平台帐号
// Get 获取公众号/小程序所绑定的开放平台帐号
func (account *Account) Get(appID string) (string, error) {
return "", nil
}

View File

@@ -4,11 +4,11 @@ import (
"github.com/silenceper/wechat/v2/cache"
)
//Config config for 微信开放平台
// Config .config for 微信开放平台
type Config struct {
AppID string `json:"app_id"` //appid
AppSecret string `json:"app_secret"` //appsecret
Token string `json:"token"` //token
EncodingAESKey string `json:"encoding_aes_key"` //EncodingAESKey
AppID string `json:"app_id"` // appid
AppSecret string `json:"app_secret"` // appsecret
Token string `json:"token"` // token
EncodingAESKey string `json:"encoding_aes_key"` // EncodingAESKey
Cache cache.Cache
}

View File

@@ -1,4 +1,4 @@
//Package context 开放平台相关context
// Package context 开放平台相关context
package context
import (
@@ -18,10 +18,10 @@ const (
getComponentInfoURL = "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token=%s"
componentLoginURL = "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=%s&pre_auth_code=%s&redirect_uri=%s&auth_type=%d&biz_appid=%s"
bindComponentURL = "https://mp.weixin.qq.com/safe/bindcomponent?action=bindcomponent&auth_type=%d&no_scan=1&component_appid=%s&pre_auth_code=%s&redirect_uri=%s&biz_appid=%s#wechat_redirect"
//TODO 获取授权方选项信息
//getComponentConfigURL = "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_option?component_access_token=%s"
//TODO 获取已授权的账号信息
//getuthorizerListURL = "POST https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_list?component_access_token=%s"
// TODO 获取授权方选项信息
// getComponentConfigURL = "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_option?component_access_token=%s"
// TODO 获取已授权的账号信息
// getuthorizerListURL = "POST https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_list?component_access_token=%s"
)
// ComponentAccessToken 第三方平台

View File

@@ -11,23 +11,23 @@ const (
getAccountBasicInfoURL = "https://api.weixin.qq.com/cgi-bin/account/getaccountbasicinfo"
)
//Basic 基础信息设置
// Basic 基础信息设置
type Basic struct {
*openContext.Context
appID string
}
//NewBasic new
// NewBasic new
func NewBasic(opContext *openContext.Context, appID string) *Basic {
return &Basic{Context: opContext, appID: appID}
}
//AccountBasicInfo 基础信息
// AccountBasicInfo 基础信息
type AccountBasicInfo struct {
util.CommonError
}
//GetAccountBasicInfo 获取小程序基础信息
// GetAccountBasicInfo 获取小程序基础信息
//reference:https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/Mini_Programs/Mini_Program_Information_Settings.html
func (basic *Basic) GetAccountBasicInfo() (*AccountBasicInfo, error) {
ak, err := basic.GetAuthrAccessToken(basic.AppID)
@@ -46,7 +46,7 @@ func (basic *Basic) GetAccountBasicInfo() (*AccountBasicInfo, error) {
return result, nil
}
//modify_domain设置服务器域名
//TODO
//func (encryptor *Basic) modifyDomain() {
//}
// modify_domain设置服务器域名
// TODO
// func (encryptor *Basic) modifyDomain() {
// }

View File

@@ -11,28 +11,28 @@ const (
fastregisterweappURL = "https://api.weixin.qq.com/cgi-bin/component/fastregisterweapp"
)
//Component 快速创建小程序
// Component 快速创建小程序
type Component struct {
*openContext.Context
}
//NewComponent new
// NewComponent new
func NewComponent(opContext *openContext.Context) *Component {
return &Component{opContext}
}
//RegisterMiniProgramParam 快速注册小程序参数
// RegisterMiniProgramParam 快速注册小程序参数
type RegisterMiniProgramParam struct {
Name string `json:"name"` //企业名
Code string `json:"code"` //企业代码
CodeType string `json:"code_type"` //企业代码类型 1统一社会信用代码18 位) 2组织机构代码9 位 xxxxxxxx-x 3营业执照注册号(15 位)
LegalPersonaWechat string `json:"legal_persona_wechat"` //法人微信号
LegalPersonaName string `json:"legal_persona_name"` //法人姓名(绑定银行卡)
ComponentPhone string `json:"component_phone"` //第三方联系电话(方便法人与第三方联系)
Name string `json:"name"` // 企业名
Code string `json:"code"` // 企业代码
CodeType string `json:"code_type"` // 企业代码类型 1统一社会信用代码18 位) 2组织机构代码9 位 xxxxxxxx-x 3营业执照注册号(15 位)
LegalPersonaWechat string `json:"legal_persona_wechat"` // 法人微信号
LegalPersonaName string `json:"legal_persona_name"` // 法人姓名(绑定银行卡)
ComponentPhone string `json:"component_phone"` // 第三方联系电话(方便法人与第三方联系)
}
//RegisterMiniProgram 快速创建小程
//reference: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/Mini_Programs/Fast_Registration_Interface_document.html
// RegisterMiniProgram 快速创建小程
// reference: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/Mini_Programs/Fast_Registration_Interface_document.html
func (component *Component) RegisterMiniProgram(param *RegisterMiniProgramParam) error {
componentAK, err := component.GetComponentAccessToken()
if err != nil {
@@ -46,15 +46,15 @@ func (component *Component) RegisterMiniProgram(param *RegisterMiniProgramParam)
return util.DecodeWithCommonError(data, "component/fastregisterweapp?action=create")
}
//GetRegistrationStatusParam 查询任务创建状态
// GetRegistrationStatusParam 查询任务创建状态
type GetRegistrationStatusParam struct {
Name string `json:"name"` //企业名
LegalPersonaWechat string `json:"legal_persona_wechat"` //法人微信号
LegalPersonaName string `json:"legal_persona_name"` //法人姓名(绑定银行卡)
Name string `json:"name"` // 企业名
LegalPersonaWechat string `json:"legal_persona_wechat"` // 法人微信号
LegalPersonaName string `json:"legal_persona_name"` // 法人姓名(绑定银行卡)
}
//GetRegistrationStatus 查询创建任务状态.
// GetRegistrationStatus 查询创建任务状态.
func (component *Component) GetRegistrationStatus(param *GetRegistrationStatusParam) error {
componentAK, err := component.GetComponentAccessToken()
if err != nil {

View File

@@ -6,13 +6,13 @@ import (
"github.com/silenceper/wechat/v2/openplatform/miniprogram/component"
)
//MiniProgram 代小程序实现业务
// MiniProgram 代小程序实现业务
type MiniProgram struct {
AppID string
openContext *openContext.Context
}
//NewMiniProgram 实例化
// NewMiniProgram 实例化
func NewMiniProgram(opCtx *openContext.Context, appID string) *MiniProgram {
return &MiniProgram{
openContext: opCtx,
@@ -20,13 +20,13 @@ func NewMiniProgram(opCtx *openContext.Context, appID string) *MiniProgram {
}
}
//GetComponent get component
//快速注册小程序相关
// GetComponent get component
// 快速注册小程序相关
func (miniProgram *MiniProgram) GetComponent() *component.Component {
return component.NewComponent(miniProgram.openContext)
}
//GetBasic 基础信息设置
// GetBasic 基础信息设置
func (miniProgram *MiniProgram) GetBasic() *basic.Basic {
return basic.NewBasic(miniProgram.openContext, miniProgram.AppID)
}

View File

@@ -15,7 +15,7 @@ type Js struct {
credential.JsTicketHandle
}
//NewJs init
// NewJs init
func NewJs(context *context.Context, appID string) *Js {
js := new(Js)
js.Context = context
@@ -24,13 +24,13 @@ func NewJs(context *context.Context, appID string) *Js {
return js
}
//SetJsTicketHandle 自定义js ticket取值方式
// SetJsTicketHandle 自定义js ticket取值方式
func (js *Js) SetJsTicketHandle(ticketHandle credential.JsTicketHandle) {
js.JsTicketHandle = ticketHandle
}
//GetConfig 第三方平台 - 获取jssdk需要的配置参数
//uri 为当前网页地址
// GetConfig 第三方平台 - 获取jssdk需要的配置参数
// uri 为当前网页地址
func (js *Js) GetConfig(uri, appid string) (config *officialJs.Config, err error) {
config = new(officialJs.Config)
var accessToken string

View File

@@ -28,14 +28,14 @@ func NewOauth(context *context.Context) *Oauth {
return auth
}
//GetRedirectURL 第三方平台 - 获取跳转的url地址
// GetRedirectURL 第三方平台 - 获取跳转的url地址
func (oauth *Oauth) GetRedirectURL(redirectURI, scope, state, appID string) (string, error) {
//url encode
// url encode
urlStr := url.QueryEscape(redirectURI)
return fmt.Sprintf(platformRedirectOauthURL, appID, urlStr, scope, state, oauth.AppID), nil
}
//Redirect 第三方平台 - 跳转到网页授权
// Redirect 第三方平台 - 跳转到网页授权
func (oauth *Oauth) Redirect(writer http.ResponseWriter, req *http.Request, redirectURI, scope, state, appID string) error {
location, err := oauth.GetRedirectURL(redirectURI, scope, state, appID)
if err != nil {

View File

@@ -9,15 +9,15 @@ import (
"github.com/silenceper/wechat/v2/openplatform/officialaccount/oauth"
)
//OfficialAccount 代公众号实现业务
// OfficialAccount 代公众号实现业务
type OfficialAccount struct {
//授权的公众号的appID
// 授权的公众号的appID
appID string
*officialaccount.OfficialAccount
}
//NewOfficialAccount 实例化
//appID :为授权方公众号 APPID非开放平台第三方平台 APPID
// NewOfficialAccount 实例化
// appID :为授权方公众号 APPID非开放平台第三方平台 APPID
func NewOfficialAccount(opCtx *opContext.Context, appID string) *OfficialAccount {
officialAccount := officialaccount.NewOfficialAccount(&offConfig.Config{
AppID: opCtx.AppID,
@@ -25,7 +25,7 @@ func NewOfficialAccount(opCtx *opContext.Context, appID string) *OfficialAccount
Token: opCtx.Token,
Cache: opCtx.Cache,
})
//设置获取access_token的函数
// 设置获取access_token的函数
officialAccount.SetAccessTokenHandle(NewDefaultAuthrAccessToken(opCtx, appID))
return &OfficialAccount{appID: appID, OfficialAccount: officialAccount}
}
@@ -40,13 +40,13 @@ func (officialAccount *OfficialAccount) PlatformJs() *js.Js {
return js.NewJs(officialAccount.GetContext(), officialAccount.appID)
}
//DefaultAuthrAccessToken 默认获取授权ak的方法
// DefaultAuthrAccessToken 默认获取授权ak的方法
type DefaultAuthrAccessToken struct {
opCtx *opContext.Context
appID string
}
//NewDefaultAuthrAccessToken New
// NewDefaultAuthrAccessToken New
func NewDefaultAuthrAccessToken(opCtx *opContext.Context, appID string) credential.AccessTokenHandle {
return &DefaultAuthrAccessToken{
opCtx: opCtx,
@@ -54,7 +54,7 @@ func NewDefaultAuthrAccessToken(opCtx *opContext.Context, appID string) credenti
}
}
//GetAccessToken 获取ak
// GetAccessToken 获取ak
func (ak *DefaultAuthrAccessToken) GetAccessToken() (string, error) {
return ak.opCtx.GetAuthrAccessToken(ak.appID)
}

View File

@@ -11,12 +11,12 @@ import (
"github.com/silenceper/wechat/v2/openplatform/officialaccount"
)
//OpenPlatform 微信开放平台相关api
// OpenPlatform 微信开放平台相关api
type OpenPlatform struct {
*context.Context
}
//NewOpenPlatform new openplatform
// NewOpenPlatform new openplatform
func NewOpenPlatform(cfg *config.Config) *OpenPlatform {
if cfg.Cache == nil {
panic("cache 未设置")
@@ -27,24 +27,24 @@ func NewOpenPlatform(cfg *config.Config) *OpenPlatform {
return &OpenPlatform{ctx}
}
//GetServer get server
// GetServer get server
func (openPlatform *OpenPlatform) GetServer(req *http.Request, writer http.ResponseWriter) *server.Server {
off := officialaccount.NewOfficialAccount(openPlatform.Context, "")
return off.GetServer(req, writer)
}
//GetOfficialAccount 公众号代处理
// GetOfficialAccount 公众号代处理
func (openPlatform *OpenPlatform) GetOfficialAccount(appID string) *officialaccount.OfficialAccount {
return officialaccount.NewOfficialAccount(openPlatform.Context, appID)
}
//GetMiniProgram 小程序代理
// GetMiniProgram 小程序代理
func (openPlatform *OpenPlatform) GetMiniProgram(appID string) *miniprogram.MiniProgram {
return miniprogram.NewMiniProgram(openPlatform.Context, appID)
}
//GetAccountManager 账号管理
//TODO
// GetAccountManager 账号管理
// TODO
func (openPlatform *OpenPlatform) GetAccountManager() *account.Account {
return account.NewAccount(openPlatform.Context)
}