From c372d7a83c51686020b94fadbb485b57d16efed8 Mon Sep 17 00:00:00 2001 From: silenceper Date: Sat, 23 May 2020 12:02:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E5=BC=80=E6=94=BE=E5=B9=B3?= =?UTF-8?q?=E5=8F=B0-=E4=BB=A3=E5=A4=84=E7=90=86=E5=B0=8F=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- officialaccount/message/message.go | 1 + openplatform/miniprogram/basic/basic.go | 52 ++++++++++++++ .../miniprogram/component/component.go | 69 +++++++++++++++++++ openplatform/miniprogram/miniprogram.go | 32 +++++++++ .../officialaccount/officialaccount.go | 1 + openplatform/openplatform.go | 6 ++ 6 files changed, 161 insertions(+) create mode 100644 openplatform/miniprogram/basic/basic.go create mode 100644 openplatform/miniprogram/component/component.go create mode 100644 openplatform/miniprogram/miniprogram.go diff --git a/officialaccount/message/message.go b/officialaccount/message/message.go index 278f552..ecd798e 100644 --- a/officialaccount/message/message.go +++ b/officialaccount/message/message.go @@ -73,6 +73,7 @@ const ( const ( //微信开放平台需要用到 + // InfoTypeVerifyTicket 返回ticket InfoTypeVerifyTicket InfoType = "component_verify_ticket" // InfoTypeAuthorized 授权 diff --git a/openplatform/miniprogram/basic/basic.go b/openplatform/miniprogram/basic/basic.go new file mode 100644 index 0000000..6a5ef55 --- /dev/null +++ b/openplatform/miniprogram/basic/basic.go @@ -0,0 +1,52 @@ +package basic + +import ( + "fmt" + + openContext "github.com/silenceper/wechat/v2/openplatform/context" + "github.com/silenceper/wechat/v2/util" +) + +const ( + getAccountBasicInfoURL = "https://api.weixin.qq.com/cgi-bin/account/getaccountbasicinfo" +) + +//Basic 基础信息设置 +type Basic struct { + *openContext.Context + appID string +} + +//NewBasic new +func NewBasic(opContext *openContext.Context, appID string) *Basic { + return &Basic{Context: opContext, appID: appID} +} + +//AccountBasicInfo 基础信息 +type AccountBasicInfo struct { + util.CommonError +} + +//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) + if err != nil { + return nil, err + } + url := fmt.Sprintf("%s?access_token=%s", getAccountBasicInfoURL, ak) + data, err := util.HTTPGet(url) + if err != nil { + return nil, err + } + result := &AccountBasicInfo{} + if err := util.DecodeWithError(data, result, "account/getaccountbasicinfo"); err != nil { + return nil, err + } + return result, nil +} + +//modify_domain设置服务器域名 +//TODO +//func (basic *Basic) modifyDomain() { +//} diff --git a/openplatform/miniprogram/component/component.go b/openplatform/miniprogram/component/component.go new file mode 100644 index 0000000..8a5ff22 --- /dev/null +++ b/openplatform/miniprogram/component/component.go @@ -0,0 +1,69 @@ +package component + +import ( + "fmt" + + openContext "github.com/silenceper/wechat/v2/openplatform/context" + "github.com/silenceper/wechat/v2/util" +) + +const ( + fastregisterweappURL = "https://api.weixin.qq.com/cgi-bin/component/fastregisterweapp" +) + +//Component 快速创建小程序 +type Component struct { + *openContext.Context +} + +//NewComponent new +func NewComponent(opContext *openContext.Context) *Component { + return &Component{opContext} +} + +//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"` //第三方联系电话(方便法人与第三方联系) +} + +//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 { + return nil + } + url := fmt.Sprintf(fastregisterweappURL+"?action=create&component_access_token=%s", componentAK) + data, err := util.PostJSON(url, param) + if err != nil { + return err + } + return util.DecodeWithCommonError(data, "component/fastregisterweapp?action=create") +} + +//GetRegistrationStatusParam 查询任务创建状态 +type GetRegistrationStatusParam struct { + Name string `json:"name"` //企业名 + LegalPersonaWechat string `json:"legal_persona_wechat"` //法人微信号 + LegalPersonaName string `json:"legal_persona_name"` //法人姓名(绑定银行卡) + +} + +//GetRegistrationStatus 查询创建任务状态. +func (component *Component) GetRegistrationStatus(param *GetRegistrationStatusParam) error { + componentAK, err := component.GetComponentAccessToken() + if err != nil { + return nil + } + url := fmt.Sprintf(fastregisterweappURL+"?action=search&component_access_token=%s", componentAK) + data, err := util.PostJSON(url, param) + if err != nil { + return err + } + return util.DecodeWithCommonError(data, "component/fastregisterweapp?action=search") +} diff --git a/openplatform/miniprogram/miniprogram.go b/openplatform/miniprogram/miniprogram.go new file mode 100644 index 0000000..1f02a51 --- /dev/null +++ b/openplatform/miniprogram/miniprogram.go @@ -0,0 +1,32 @@ +package miniprogram + +import ( + openContext "github.com/silenceper/wechat/v2/openplatform/context" + "github.com/silenceper/wechat/v2/openplatform/miniprogram/basic" + "github.com/silenceper/wechat/v2/openplatform/miniprogram/component" +) + +//MiniProgram 代小程序实现业务 +type MiniProgram struct { + AppID string + openContext *openContext.Context +} + +//NewMiniProgram 实例化 +func NewMiniProgram(opCtx *openContext.Context, appID string) *MiniProgram { + return &MiniProgram{ + openContext: opCtx, + AppID: appID, + } +} + +//GetComponent get component +//快速注册小程序相关 +func (miniProgram *MiniProgram) GetComponent() *component.Component { + return component.NewComponent(miniProgram.openContext) +} + +//GetBasic 基础信息设置 +func (miniProgram *MiniProgram) GetBasic() *basic.Basic { + return basic.NewBasic(miniProgram.openContext, miniProgram.AppID) +} diff --git a/openplatform/officialaccount/officialaccount.go b/openplatform/officialaccount/officialaccount.go index 438d539..c7d4c4f 100644 --- a/openplatform/officialaccount/officialaccount.go +++ b/openplatform/officialaccount/officialaccount.go @@ -7,6 +7,7 @@ import ( opContext "github.com/silenceper/wechat/v2/openplatform/context" ) +//OfficialAccount 代公众号实现业务 type OfficialAccount struct { //授权的公众号的appID appID string diff --git a/openplatform/openplatform.go b/openplatform/openplatform.go index 230465e..b237380 100644 --- a/openplatform/openplatform.go +++ b/openplatform/openplatform.go @@ -3,6 +3,7 @@ package openplatform import ( "github.com/silenceper/wechat/v2/openplatform/config" "github.com/silenceper/wechat/v2/openplatform/context" + "github.com/silenceper/wechat/v2/openplatform/miniprogram" "github.com/silenceper/wechat/v2/openplatform/officialaccount" ) @@ -26,3 +27,8 @@ func NewOpenPlatform(cfg *config.Config) *OpenPlatform { func (openPlatform *OpenPlatform) GetOfficialAccount(appID string) *officialaccount.OfficialAccount { return officialaccount.NewOfficialAccount(openPlatform.Context, appID) } + +//GetMiniProgram 小程序代理 +func (openPlatform *OpenPlatform) GetMiniProgram(opCtx *context.Context, appID string) *miniprogram.MiniProgram { + return miniprogram.NewMiniProgram(opCtx, appID) +}