Skip to content

Commit af9ce6d

Browse files
authored
Merge pull request #645 from ArtisanCloud/develop
Develop
2 parents 30a49dd + fa7417b commit af9ce6d

File tree

7 files changed

+232
-12
lines changed

7 files changed

+232
-12
lines changed

src/officialAccount/user/tag/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func (comp *Client) TagUsers(ctx context.Context, openIDs []string, tagID int) (
123123

124124
// 批量为用户取消标签
125125
// https://developers.weixin.qq.com/doc/offiaccount/User_Management/User_Tag_Management.html
126-
func (comp *Client) UntagUsers(ctx context.Context, openIDs []string, tagID string) (*response.ResponseUntagUsers, error) {
126+
func (comp *Client) UntagUsers(ctx context.Context, openIDs []string, tagID int) (*response.ResponseUntagUsers, error) {
127127
result := &response.ResponseUntagUsers{}
128128

129129
params := &object.HashMap{

src/payment/notify/costom/handler.go

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
package costom
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"errors"
7+
"fmt"
8+
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/notify"
9+
"io"
10+
"net/http"
11+
12+
"github.com/ArtisanCloud/PowerLibs/v3/object"
13+
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/support"
14+
base2 "github.com/ArtisanCloud/PowerWeChat/v3/src/payment/base"
15+
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/kernel"
16+
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/notify/request"
17+
)
18+
19+
type CustomHandler struct {
20+
App kernel.ApplicationPaymentInterface
21+
Message *request.RequestNotify
22+
fail string
23+
Attributes *object.StringMap
24+
Check bool
25+
Sign bool
26+
27+
ExternalBody io.Reader
28+
29+
// Handle func(closure func(message *request.RequestNotify, transaction *models.Transaction, fail func(groupWelcomeTemplate string)) interface{}) *http.Response
30+
}
31+
32+
func NewCustomHandler(app kernel.ApplicationPaymentInterface, body io.Reader) *CustomHandler {
33+
34+
// -------------- external request --------------
35+
var req io.Reader = &bytes.Buffer{}
36+
if body != nil {
37+
req = body
38+
}
39+
40+
return &CustomHandler{
41+
App: app,
42+
Check: true,
43+
Sign: false,
44+
ExternalBody: req,
45+
}
46+
}
47+
48+
func (handler *CustomHandler) Fail(message string) {
49+
handler.fail = message
50+
}
51+
52+
func (handler *CustomHandler) RespondWith(attributes *object.StringMap, sign bool) *CustomHandler {
53+
54+
handler.Attributes = attributes
55+
handler.Sign = sign
56+
57+
return handler
58+
}
59+
func (handler *CustomHandler) ToResponse() (response *http.Response, err error) {
60+
61+
returnCode := notify.SUCCESS
62+
returnMsg := "成功"
63+
if handler.fail != "" {
64+
returnCode = notify.FAIL
65+
returnMsg = handler.fail
66+
err = errors.New(handler.fail)
67+
}
68+
base := &object.StringMap{
69+
"code": returnCode,
70+
"uniformMessage": returnMsg,
71+
}
72+
73+
attributes := object.MergeStringMap(base, handler.Attributes)
74+
baseClient := (handler.App).GetComponent("Base").(*base2.Client)
75+
if handler.Sign {
76+
(*attributes)["sign"], err = baseClient.BaseClient.Signer.GenerateSign("")
77+
if err != nil {
78+
return nil, err
79+
}
80+
}
81+
82+
bodyBuffer, _ := json.Marshal(attributes)
83+
rs := &http.Response{
84+
StatusCode: http.StatusOK,
85+
}
86+
rs.Body = io.NopCloser(bytes.NewBuffer(bodyBuffer))
87+
88+
return rs, err
89+
}
90+
91+
func (handler *CustomHandler) GetMessage() (notify *request.RequestNotify, err error) {
92+
93+
if handler.Message != nil {
94+
return handler.Message, nil
95+
}
96+
97+
body := handler.ExternalBody
98+
99+
requestBody, err := io.ReadAll(body)
100+
if err != nil {
101+
return nil, err
102+
}
103+
handler.Message = &request.RequestNotify{}
104+
err = object.JsonDecode(requestBody, handler.Message)
105+
if err != nil {
106+
return nil, err
107+
}
108+
109+
// note: 自定义的不保存 http request
110+
// handler.Message.RawRequest = body
111+
112+
return handler.Message, nil
113+
}
114+
115+
func (handler *CustomHandler) DecryptMessage() (string, error) {
116+
message, err := handler.GetMessage()
117+
if err != nil {
118+
return "", err
119+
}
120+
if message.Resource == nil {
121+
return "", errors.New("uniformMessage doesn't have the key value")
122+
}
123+
124+
config := (handler.App).GetConfig()
125+
wxKey := config.GetString("mch_api_v3_key", "")
126+
nonce := message.Resource.Nonce
127+
associatedData := message.Resource.AssociatedData
128+
cipherText := message.Resource.Ciphertext
129+
return support.DecryptAES256GCM(
130+
wxKey,
131+
associatedData,
132+
nonce,
133+
cipherText,
134+
)
135+
136+
}
137+
138+
func (handler *CustomHandler) Strict(result interface{}) {
139+
140+
bResult := true
141+
strResult := ""
142+
switch result.(type) {
143+
case bool:
144+
bResult = result.(bool)
145+
strResult = fmt.Sprintf("%t", result)
146+
case string:
147+
strResult = result.(string)
148+
if strResult != "" {
149+
bResult = false
150+
}
151+
default:
152+
return
153+
}
154+
155+
if bResult != true && handler.fail == "" {
156+
handler.Fail(strResult)
157+
}
158+
}
159+
160+
func (handler *CustomHandler) reqInfo() (content string, err error) {
161+
162+
content, err = handler.DecryptMessage()
163+
if err != nil {
164+
return "", err
165+
}
166+
167+
// save the decoded content to message resource
168+
handler.Message.Resource.Plaintext = content
169+
170+
return content, nil
171+
}

src/payment/notify/costom/notify.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package costom
2+
3+
import (
4+
"io"
5+
"net/http"
6+
7+
"github.com/ArtisanCloud/PowerLibs/v3/object"
8+
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/kernel"
9+
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/notify/request"
10+
)
11+
12+
// CustomNotify 框架内没有实现的,可以使用这个自己实现
13+
type CustomNotify[T any] struct {
14+
*CustomHandler
15+
}
16+
17+
func NewCustomNotify[T any](app kernel.ApplicationPaymentInterface, body io.Reader) *CustomNotify[T] {
18+
return &CustomNotify[T]{
19+
NewCustomHandler(app, body),
20+
}
21+
}
22+
23+
func (comp *CustomNotify[T]) Handle(closure func(message *request.RequestNotify, transaction *T, fail func(message string)) interface{}) (*http.Response, error) {
24+
message, err := comp.GetMessage()
25+
if err != nil {
26+
return nil, err
27+
}
28+
29+
reqInfo, err := comp.reqInfo()
30+
if err != nil {
31+
return nil, err
32+
}
33+
34+
// struct the content
35+
var transaction = new(T)
36+
err = object.JsonDecode([]byte(reqInfo), transaction)
37+
if err != nil {
38+
return nil, err
39+
}
40+
41+
result := closure(message, transaction, comp.Fail)
42+
comp.Strict(result)
43+
44+
return comp.ToResponse()
45+
}

src/payment/notify/handler.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8+
"io"
9+
"net/http"
10+
811
"github.com/ArtisanCloud/PowerLibs/v3/object"
912
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/support"
1013
base2 "github.com/ArtisanCloud/PowerWeChat/v3/src/payment/base"
1114
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/kernel"
1215
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/notify/request"
13-
"io"
14-
"net/http"
1516
)
1617

1718
type Handler struct {
@@ -24,25 +25,25 @@ type Handler struct {
2425

2526
ExternalRequest *http.Request
2627

27-
//Handle func(closure func(message *request.RequestNotify, transaction *models.Transaction, fail func(groupWelcomeTemplate string)) interface{}) *http.Response
28+
// Handle func(closure func(message *request.RequestNotify, transaction *models.Transaction, fail func(groupWelcomeTemplate string)) interface{}) *http.Response
2829
}
2930

3031
const SUCCESS = "SUCCESS"
3132
const FAIL = "FAIL"
3233

3334
func NewHandler(app kernel.ApplicationPaymentInterface, r *http.Request) *Handler {
3435

35-
//-------------- external request --------------
36-
request := &http.Request{}
36+
// -------------- external request --------------
37+
req := &http.Request{}
3738
if r != nil {
38-
request = r
39+
req = r
3940
}
4041

4142
return &Handler{
4243
App: app,
4344
Check: true,
4445
Sign: false,
45-
ExternalRequest: request,
46+
ExternalRequest: req,
4647
}
4748
}
4849

src/payment/notify/paid.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package notify
22

33
import (
4+
"net/http"
5+
46
"github.com/ArtisanCloud/PowerLibs/v3/object"
57
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/models"
68
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/kernel"
79
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/notify/request"
8-
"net/http"
910
)
1011

1112
type Paid struct {

src/payment/notify/refund.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package notify
22

33
import (
4+
"net/http"
5+
46
"github.com/ArtisanCloud/PowerLibs/v3/object"
57
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/models"
68
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/kernel"
79
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/notify/request"
8-
"net/http"
910
)
1011

1112
type Refund struct {

src/payment/notify/scanned.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package notify
22

33
import (
4+
"net/http"
5+
"reflect"
6+
47
"github.com/ArtisanCloud/PowerLibs/v3/object"
58
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/kernel"
69
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/notify/request"
7-
"net/http"
8-
"reflect"
910
)
1011

1112
type Scanned struct {

0 commit comments

Comments
 (0)