Skip to content

Commit

Permalink
Merge pull request #464 from bububa/release/3.2.0
Browse files Browse the repository at this point in the history
 重构 openWork 以应对base Client中override access_token 问题
  • Loading branch information
Matrix-X committed Apr 6, 2024
2 parents 8d35ebc + afc4d97 commit 1839fa7
Show file tree
Hide file tree
Showing 16 changed files with 346 additions and 135 deletions.
2 changes: 1 addition & 1 deletion src/kernel/baseClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ func (client *BaseClient) OverrideGetMiddlewareOfAccessToken() {
//fmt.Println("获取access token, 在请求前执行")

accessToken := (*client.App).GetAccessToken()
//accessToken := client.Token
// accessToken := client.Token

if accessToken != nil {
config := (*client.App).GetContainer().Config
Expand Down
9 changes: 4 additions & 5 deletions src/kernel/serverGuard.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"sort"
"strconv"
Expand Down Expand Up @@ -216,15 +215,15 @@ func (serverGuard *ServerGuard) ResolveEvent(request *http.Request, closure func
resultRS = (*result)["response"].(string)
}
rs = &http.Response{
Body: ioutil.NopCloser(bytes.NewBufferString(resultRS)),
Body: io.NopCloser(bytes.NewBufferString(resultRS)),
}

} else {
strBuiltResponse := serverGuard.buildResponse(request, (*result)["to"].(string), (*result)["from"].(string), (*result)["response"])
header := http.Header{}
header.Set("Content-Type", "application/xml")
rs = &http.Response{
Body: ioutil.NopCloser(bytes.NewBufferString(strBuiltResponse)),
Body: io.NopCloser(bytes.NewBufferString(strBuiltResponse)),
StatusCode: http.StatusOK,
Header: header,
}
Expand All @@ -246,15 +245,15 @@ func (serverGuard *ServerGuard) OverrideResolve() {
resultRS = (*result)["response"].(string)
}
rs = &http.Response{
Body: ioutil.NopCloser(bytes.NewBufferString(resultRS)),
Body: io.NopCloser(bytes.NewBufferString(resultRS)),
}

} else {
strBuiltResponse := serverGuard.buildResponse(request, (*result)["to"].(string), (*result)["from"].(string), (*result)["response"])
header := http.Header{}
header.Set("Content-Type", "application/xml")
rs = &http.Response{
Body: ioutil.NopCloser(bytes.NewBufferString(strBuiltResponse)),
Body: io.NopCloser(bytes.NewBufferString(strBuiltResponse)),
StatusCode: http.StatusOK,
Header: header,
}
Expand Down
2 changes: 1 addition & 1 deletion src/officialAccount/publish/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (comp *Client) PublishSubmit(ctx context.Context, mediaID string) (*respons

// 发布状态轮询接口
// https://developers.weixin.qq.com/doc/offiaccount/Publish/Get_status.html
func (comp *Client) PublishGet(ctx context.Context, publishID string) (*response.ResponsePublishGet, error) {
func (comp *Client) PublishGet(ctx context.Context, publishID uint64) (*response.ResponsePublishGet, error) {
result := &response.ResponsePublishGet{}

_, err := comp.BaseClient.HttpPostJson(ctx, "cgi-bin/freepublish/get", &object.HashMap{
Expand Down
4 changes: 2 additions & 2 deletions src/officialAccount/publish/response/responsePublish.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/response"
type ResponsePublishSubmit struct {
response.ResponseOfficialAccount

PublishId string `json:"publish_id"`
PublishId uint64 `json:"publish_id"`
}

type ArticleItem struct {
Expand All @@ -21,7 +21,7 @@ type ArticleDetail struct {
type ResponsePublishGet struct {
response.ResponseOfficialAccount

PublishId string `json:"publish_id"`
PublishId uint64 `json:"publish_id"`
PublishStatus int `json:"publish_status"`
ArticleId interface{} `json:"article_id"`
ArticleDetail *ArticleDetail `json:"article_detail"`
Expand Down
156 changes: 144 additions & 12 deletions src/openWork/application.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package openWork

import (
"github.com/ArtisanCloud/PowerLibs/v3/cache"
"github.com/ArtisanCloud/PowerLibs/v3/logger"
"github.com/ArtisanCloud/PowerLibs/v3/logger/contract"
"github.com/ArtisanCloud/PowerLibs/v3/object"

"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/providers"
"github.com/ArtisanCloud/PowerWeChat/v3/src/openWork/base"
"github.com/ArtisanCloud/PowerWeChat/v3/src/openWork/corp"
"github.com/ArtisanCloud/PowerWeChat/v3/src/openWork/externalcontact"
"github.com/ArtisanCloud/PowerWeChat/v3/src/openWork/license"
"github.com/ArtisanCloud/PowerWeChat/v3/src/openWork/provider"
"github.com/ArtisanCloud/PowerWeChat/v3/src/openWork/server"
suit "github.com/ArtisanCloud/PowerWeChat/v3/src/openWork/suitAuth"
"github.com/ArtisanCloud/PowerWeChat/v3/src/openWork/user"
"github.com/ArtisanCloud/PowerWeChat/v3/src/work"
)

type OpenWork struct {
Expand All @@ -39,6 +41,7 @@ type UserConfig struct {
AuthCode string
Token string
AESKey string
CallbackURL string

ResponseType string
Log Log
Expand Down Expand Up @@ -152,14 +155,6 @@ func (app *OpenWork) GetConfig() *kernel.Config {
return app.Config
}

func (app *OpenWork) Provider(corpID string) (*provider.Client, error) {
return provider.NewClient(app, corpID)
}

func (app *OpenWork) License(corpID string) (*license.Client, error) {
return license.NewClient(app, corpID)
}

func (app *OpenWork) GetComponent(name string) interface{} {

switch name {
Expand Down Expand Up @@ -200,11 +195,12 @@ func MapUserConfig(userConfig *UserConfig) (*object.HashMap, error) {

"app_id": userConfig.AppID,
"secret": userConfig.Secret,
"provider_corpid": userConfig.ProviderCorpID,
"provider_secret": userConfig.ProviderSecret,
"auth_code": userConfig.AuthCode,
"token": userConfig.Token,
"aes_key": userConfig.AESKey,
"provider_corpid": userConfig.ProviderCorpID,
"provider_secret": userConfig.ProviderSecret,
"callback_url": userConfig.CallbackURL,

"response_type": userConfig.ResponseType,
"http": &object.HashMap{
Expand All @@ -230,5 +226,141 @@ func MapUserConfig(userConfig *UserConfig) (*object.HashMap, error) {
}

return config, nil
}

// ProviderWork 服务商代开发用
func (app *OpenWork) ProviderClient(corpID string, permanentCode string, externConfig *work.UserConfig) (*work.Work, error) {
config := app.GetConfig()
cacheHandle := config.Get("cache", nil).(cache.CacheInterface)
log := config.Get("log", nil).(*object.HashMap)
httpCfg := config.Get("http", nil).(*object.HashMap)
workHttp := work.Http{
ProxyURI: (*httpCfg)["proxy_uri"].(string),
Timeout: (*httpCfg)["timeout"].(float64),
BaseURI: (*httpCfg)["base_uri"].(string),
}
oauth := work.OAuth{
Callback: config.GetString("callback_url", ""),
}
workLog := work.Log{
Level: (*log)["level"].(string),
File: (*log)["file"].(string),
ENV: (*log)["env"].(string),
}
debug := config.GetBool("debug", false)
httpDebug := config.GetBool("http_debug", false)
if externConfig != nil {
if externConfig.Http.Timeout > 0 {
workHttp.Timeout = externConfig.Http.Timeout
}
if externConfig.Http.BaseURI != "" {
workHttp.BaseURI = externConfig.Http.BaseURI
}
if externConfig.Http.ProxyURI != "" {
workHttp.ProxyURI = externConfig.Http.ProxyURI
}
if externConfig.OAuth.Callback != "" {
oauth.Callback = externConfig.OAuth.Callback
}
if externConfig.Log.File != "" {
workLog.File = externConfig.Log.File
}
if externConfig.Log.ENV != "" {
workLog.ENV = externConfig.Log.ENV
}
if externConfig.Log.Level != "" {
workLog.Level = externConfig.Log.Level
}
if externConfig.Log.Error != "" {
workLog.Error = externConfig.Log.Error
}
debug = externConfig.Debug
httpDebug = externConfig.HttpDebug
}
userConfig := work.UserConfig{
CorpID: corpID,
Secret: permanentCode,
Token: config.GetString("token", ""),
AESKey: config.GetString("aes_key", ""),
Cache: cacheHandle,
Http: workHttp,
OAuth: oauth,
Log: workLog,
HttpDebug: httpDebug,
Debug: debug,
}
return work.NewWork(&userConfig)
}

// ThirdpartyWork 第三方应用
func (app *OpenWork) ThirdpartyClient(corpID string, permanentCode string, externConfig *work.UserConfig) (*work.Work, error) {
config := app.GetConfig()
cacheHandle := config.Get("cache", nil).(cache.CacheInterface)
log := config.Get("log", nil).(*object.HashMap)
httpCfg := config.Get("http", nil).(*object.HashMap)
workHttp := work.Http{
ProxyURI: (*httpCfg)["proxy_uri"].(string),
Timeout: (*httpCfg)["timeout"].(float64),
BaseURI: (*httpCfg)["base_uri"].(string),
}
oauth := work.OAuth{
Callback: config.GetString("callback_url", ""),
}
workLog := work.Log{
Level: (*log)["level"].(string),
File: (*log)["file"].(string),
ENV: (*log)["env"].(string),
}
debug := config.GetBool("debug", false)
httpDebug := config.GetBool("http_debug", false)
if externConfig != nil {
if externConfig.Http.Timeout > 0 {
workHttp.Timeout = externConfig.Http.Timeout
}
if externConfig.Http.BaseURI != "" {
workHttp.BaseURI = externConfig.Http.BaseURI
}
if externConfig.Http.ProxyURI != "" {
workHttp.ProxyURI = externConfig.Http.ProxyURI
}
if externConfig.OAuth.Callback != "" {
oauth.Callback = externConfig.OAuth.Callback
}
if externConfig.Log.File != "" {
workLog.File = externConfig.Log.File
}
if externConfig.Log.ENV != "" {
workLog.ENV = externConfig.Log.ENV
}
if externConfig.Log.Level != "" {
workLog.Level = externConfig.Log.Level
}
if externConfig.Log.Error != "" {
workLog.Error = externConfig.Log.Error
}
debug = externConfig.Debug
httpDebug = externConfig.HttpDebug
}
userConfig := work.UserConfig{
CorpID: corpID,
Secret: permanentCode,
Token: config.GetString("token", ""),
AESKey: config.GetString("aes_key", ""),
Cache: cacheHandle,
Http: workHttp,
OAuth: oauth,
Log: workLog,
HttpDebug: httpDebug,
Debug: debug,
}
clt, err := work.NewWork(&userConfig)
if err != nil {
return nil, err
}
accessToken, err := corp.RegisterProvider(app, corpID, permanentCode)
if err != nil {
return nil, err
}
clt.AccessToken.AccessToken = accessToken.AccessToken
return clt, nil
}
17 changes: 9 additions & 8 deletions src/openWork/corp/accessToken.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func NewAccessToken(app *kernel.ApplicationInterface, corpId, permanentCode stri
func (accessToken *AccessToken) OverrideGetEndpoint() {

app := (*accessToken.App)
token := app.GetComponent("SuiteAccessToken").(*suit.AccessToken)
accessToken.GetEndpoint = func() (string, error) {
token := app.GetComponent("SuiteAccessToken").(*suit.AccessToken)
suiteAccessToken, _ := token.AccessToken.GetToken(false)
return accessToken.EndpointToGetToken + `?suite_access_token=` + url.QueryEscape(suiteAccessToken.AccessToken), nil
}
Expand All @@ -58,17 +58,18 @@ func (accessToken *AccessToken) OverrideGetEndpoint() {
func (accessToken *AccessToken) OverrideGetCredentials() {

app := (*accessToken.App)
config := app.GetContainer().GetConfig()
appID := (*config)["app_id"].(string)
corpID := accessToken.corpID
permanentCode := accessToken.permanentCode
accessToken.GetCredentials = func() *object.StringMap {
config := app.GetContainer().GetConfig()

appID := (*config)["app_id"].(string)

return &object.StringMap{
"auth_corpid": accessToken.corpID,
"permanent_code": accessToken.permanentCode,
"auth_corpid": corpID,
"permanent_code": permanentCode,

"appid": accessToken.corpID,
"secret": accessToken.permanentCode,
"appid": corpID,
"secret": permanentCode,
"neededText": security.HashStringData(accessToken.corpID + "-" + appID),
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/openWork/externalcontact/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ import (
"github.com/ArtisanCloud/PowerLibs/v3/object"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel"
"github.com/ArtisanCloud/PowerWeChat/v3/src/openWork/externalcontact/response"
suite "github.com/ArtisanCloud/PowerWeChat/v3/src/openWork/suitAuth"
)

type Client struct {
BaseClient *kernel.BaseClient
}

func NewClient(app *kernel.ApplicationInterface) (*Client, error) {
token := (*app).GetComponent("SuiteAccessToken").(*suite.AccessToken)
baseClient, err := kernel.NewBaseClient(app, token.AccessToken)
baseClient, err := kernel.NewBaseClient(app, nil)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 1839fa7

Please sign in to comment.