Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #546

Merged
merged 10 commits into from
Sep 9, 2024
4 changes: 2 additions & 2 deletions .github/workflows/go-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: 1.16
go-version: 1.23

- name: Build
run: make build
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/go-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: 1.16
go-version: 1.23
- name: Test
run: make test
13 changes: 10 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
module github.com/ArtisanCloud/PowerWeChat/v3

go 1.17
go 1.23

//replace github.com/ArtisanCloud/PowerLibs/v3 => ../PowerLibs

//replace github.com/ArtisanCloud/PowerSocialite/v3 => ../PowerSocialite

require (
github.com/ArtisanCloud/PowerLibs/v3 v3.2.5
github.com/ArtisanCloud/PowerLibs/v3 v3.2.6
github.com/ArtisanCloud/PowerSocialite/v3 v3.0.7
github.com/go-playground/assert/v2 v2.0.1
github.com/redis/go-redis/v9 v9.0.3
github.com/stretchr/testify v1.8.0
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/otel v1.4.0
go.opentelemetry.io/otel/sdk v1.4.0
)

require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/clbanning/mxj/v2 v2.7.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/go-logr/logr v1.2.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel/trace v1.4.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.21.0 // indirect
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 // indirect
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
158 changes: 19 additions & 139 deletions go.sum

Large diffs are not rendered by default.

69 changes: 53 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
package main

import (
"context"
"fmt"
fmt2 "github.com/ArtisanCloud/PowerLibs/v3/fmt"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/power"
"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram"
"github.com/ArtisanCloud/PowerWeChat/v3/src/officialAccount"
"github.com/ArtisanCloud/PowerWeChat/v3/src/officialAccount/templateMessage/request"
"github.com/ArtisanCloud/PowerWeChat/v3/src/openPlatform"
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment"
"github.com/ArtisanCloud/PowerWeChat/v3/src/work"
"github.com/ArtisanCloud/PowerWeChat/v3/test/testLogDriver"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/sdk/trace"
"os"
"strconv"
)

func GetOfficialConfig() *officialAccount.UserConfig {
return &officialAccount.UserConfig{

AppID: os.Getenv("official_app_id"), // 小程序、公众号或者企业微信的appid
Secret: os.Getenv("official_secret"), // 商户号 appID

ResponseType: os.Getenv("array"),
Token: "",
AESKey: "",
Log: officialAccount.Log{
Driver: &testLogDriver.SimpleLogger{},
Level: "debug",
File: "./wechat/info.log",
Error: "./wechat/error.log",
Stdout: false,
},

// ResponseType: os.Getenv("response_type"),
Cache: kernel.NewRedisClient(&kernel.UniversalOptions{
Addrs: []string{"127.0.0.1:6379"},
Password: "",
//Username: "username",
DB: 1,
DB: 1,
}),
HttpDebug: true,
//Debug: true,
//"sandbox": true,

HttpDebug: false,
Debug: false,
}

}
Expand Down Expand Up @@ -168,21 +169,57 @@ func GetOpenPlatformConfig() *openPlatform.UserConfig {
}
}

func initTracer() {
tp := trace.NewTracerProvider()
// Set Global Tracer Provider
otel.SetTracerProvider(tp)
}

func init() {
initTracer()
}

func main() {

fmt.Printf("hello Wechat! \n")

tracer := otel.Tracer("example-tracer")
ctx, span := tracer.Start(context.Background(), "test")
defer span.End()

// init officialAccount app
configOfficialAccount := GetOfficialConfig()
officialAccountApp, err := officialAccount.NewOfficialAccount(configOfficialAccount)
if err != nil {
fmt.Println(err.Error())
}
officialAccountApp.Logger.Driver.Info("custom info log")
officialAccountApp.Logger.Driver.Error("custom error log")
officialAccountApp.Logger.Driver.Warn("custom warn log")
//officialAccountApp.Logger.Info("custom info log")
//officialAccountApp.Logger.Error("custom error log")
//officialAccountApp.Logger.Warn("custom warn log")

officialAccountApp.TemplateMessage.Send(ctx, &request.RequestTemlateMessage{
ToUser: "",
TemplateID: "",
MiniProgram: &request.MiniProgram{ // 上线后的小程序才可以使用
AppID: "",
PagePath: fmt.Sprintf("pages/order/details?id=%v", 111111),
},
Data: &power.HashMap{
"character_string1": &power.HashMap{
"value": "111111111",
"color": "#173177",
},
"thing3": &power.HashMap{
"value": "可口可乐大瓶装",
"color": "#173177",
},
"const2": &power.HashMap{
"value": "订单超时未支付",
"color": "#173177",
},
},
})
fmt2.Dump("officialAccount config:", officialAccountApp.GetConfig().All())

// init wecom app
configWecom := GetWorkConfig()
wecomApp, err := work.NewWork(configWecom)
Expand Down
2 changes: 2 additions & 0 deletions src/basicService/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Log struct {
File string
Error string
ENV string
Stdout bool
}

type OAuth struct {
Expand Down Expand Up @@ -156,6 +157,7 @@ func MapUserConfig(userConfig *UserConfig) (*object.HashMap, error) {
"file": userConfig.Log.File,
"error": userConfig.Log.Error,
"env": userConfig.Log.ENV,
"stdout": userConfig.Log.Stdout,
},
"cache": userConfig.Cache,
"http_debug": userConfig.HttpDebug,
Expand Down
51 changes: 24 additions & 27 deletions src/kernel/accessToken.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kernel

import (
"context"
"crypto/md5"
"encoding/hex"
"errors"
Expand All @@ -14,6 +15,7 @@ import (
contract2 "github.com/ArtisanCloud/PowerLibs/v3/logger/contract"
"github.com/ArtisanCloud/PowerLibs/v3/object"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/contract"
request2 "github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/request"
response2 "github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/response"
)

Expand Down Expand Up @@ -79,11 +81,7 @@ func NewAccessToken(app *ApplicationInterface) (*AccessToken, error) {
return token, nil
}

func (accessToken *AccessToken) GetRefreshedToken() (*response2.ResponseGetToken, error) {
return accessToken.GetToken(true)
}

func (accessToken *AccessToken) GetToken(refresh bool) (resToken *response2.ResponseGetToken, err error) {
func (accessToken *AccessToken) GetToken(ctx context.Context, refresh bool) (resToken *response2.ResponseGetToken, err error) {

cacheKey := accessToken.GetCacheKey()
cache := accessToken.GetCache()
Expand All @@ -105,7 +103,7 @@ func (accessToken *AccessToken) GetToken(refresh bool) (resToken *response2.Resp
}

// request token from power
resToken, err = accessToken.requestToken(accessToken.GetCredentials())
resToken, err = accessToken.requestToken(ctx, accessToken.GetCredentials())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -185,15 +183,19 @@ func (accessToken *AccessToken) getFormatToken(token object.HashMap) (*response2
return resToken, nil
}

func (accessToken *AccessToken) Refresh() contract.AccessTokenInterface {
accessToken.GetToken(true)
func (accessToken *AccessToken) GetRefreshedToken() (*response2.ResponseGetToken, error) {
return accessToken.GetToken(context.Background(), true)
}

func (accessToken *AccessToken) Refresh(ctx context.Context) contract.AccessTokenInterface {
accessToken.GetToken(ctx, true)

return accessToken
}

func (accessToken *AccessToken) requestToken(credentials *object.StringMap) (*response2.ResponseGetToken, error) {
func (accessToken *AccessToken) requestToken(ctx context.Context, credentials *object.StringMap) (*response2.ResponseGetToken, error) {

res, err := accessToken.sendRequest(credentials)
res, err := accessToken.sendRequest(ctx, credentials)
if err != nil {
return nil, err
}
Expand All @@ -214,7 +216,7 @@ func (accessToken *AccessToken) requestToken(credentials *object.StringMap) (*re
func (accessToken *AccessToken) ApplyToRequest(request *http.Request, requestOptions *object.HashMap) (*http.Request, error) {

// query Access Token power
mapToken, err := accessToken.getQuery()
mapToken, err := accessToken.getQuery(request.Context())
if err != nil {
return nil, err
}
Expand All @@ -227,7 +229,7 @@ func (accessToken *AccessToken) ApplyToRequest(request *http.Request, requestOpt
return request, err
}

func (accessToken *AccessToken) sendRequest(credential *object.StringMap) (*response2.ResponseGetToken, error) {
func (accessToken *AccessToken) sendRequest(ctx context.Context, credential *object.StringMap) (*response2.ResponseGetToken, error) {

key := "json"
if accessToken.RequestMethod == http.MethodGet {
Expand All @@ -244,7 +246,7 @@ func (accessToken *AccessToken) sendRequest(credential *object.StringMap) (*resp
return nil, err
}

df := accessToken.HttpHelper.Df().Uri(strEndpoint).
df := accessToken.HttpHelper.Df().WithContext(ctx).Uri(strEndpoint).
Method(accessToken.RequestMethod)

// 检查是否需要有请求参数配置
Expand Down Expand Up @@ -311,7 +313,7 @@ func (accessToken *AccessToken) GetDefaultCacheKey() string {
return cacheKey
}

func (accessToken *AccessToken) getQuery() (*object.StringMap, error) {
func (accessToken *AccessToken) getQuery(ctx context.Context) (*object.StringMap, error) {
// set the current token key
var key string
if accessToken.QueryName != "" {
Expand All @@ -321,7 +323,7 @@ func (accessToken *AccessToken) getQuery() (*object.StringMap, error) {
}

// get token string power
resToken, err := accessToken.GetToken(false)
resToken, err := accessToken.GetToken(ctx, false)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -352,29 +354,24 @@ func (accessToken *AccessToken) OverrideGetMiddlewares() {

func (accessToken *AccessToken) OverrideGetMiddlewareOfLog() {
accessToken.GetMiddlewareOfLog = func(logger contract2.LoggerInterface) contract3.RequestMiddleware {
return contract3.RequestMiddleware(func(handle contract3.RequestHandle) contract3.RequestHandle {
return func(handle contract3.RequestHandle) contract3.RequestHandle {
return func(request *http.Request) (response *http.Response, err error) {

//config := (*client.App).GetConfig()
//http_debug := config.GetBool("http_debug", false)
//// 前置中间件
//if http_debug {
// request2.LogRequest(logger, request)
//}
logger := logger.WithContext(request.Context())

// 此处请求前后日志根据 log 配置中的 level 判断是否打印
request2.LogRequest(logger, request)

response, err = handle(request)
if err != nil {
return response, err
}

// 后置中间件
//if http_debug {
//response2.LogResponse(logger, response)
//}
response2.LogResponse(logger, response)

return
}
})
}
}
}

Expand Down
Loading