129 lines
2.8 KiB
Go
129 lines
2.8 KiB
Go
package internal
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"gitea.cdlsxd.cn/sdk/plugin/proto"
|
|
"net/http"
|
|
)
|
|
|
|
const (
|
|
Tag = "lsxd_wechat_cpn"
|
|
Version = 1
|
|
CookieKey = "lsxd_wechat_cpn"
|
|
CookieValue = "lsxd_wechat_cpn"
|
|
)
|
|
|
|
const (
|
|
orderMethod = "/wechat/coupon/issue" // 支持幂等
|
|
queryMethod = "/wechat/coupon/query"
|
|
configMethod = "/wechat/coupon/config"
|
|
)
|
|
|
|
// LSXDWeChatCpnService
|
|
// 钉钉文档 https://alidocs.dingtalk.com/i/nodes/Y1OQX0akWm6LKDO4iOo1xE0QVGlDd3mE?utm_scene=person_space
|
|
type LSXDWeChatCpnService struct{}
|
|
|
|
func (p *LSXDWeChatCpnService) Order(ctx context.Context, request *proto.OrderRequest) (resp2 *proto.OrderResponse, respErr error) {
|
|
|
|
defer func() {
|
|
if err := recover(); err != nil {
|
|
respErr = proto.ErrorPanic(fmt.Sprintf("panic: %v", err))
|
|
}
|
|
}()
|
|
|
|
config, err := transConfig(request.Config)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
bizContent, err := config.orderReq(request.Order, request.Product)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
reqBody, err := bizContent.Json()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
bodyBytes, _, err := config.Post(ctx, orderMethod, reqBody)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return orderResp(request.GetOrder(), bodyBytes)
|
|
}
|
|
|
|
func (p *LSXDWeChatCpnService) Query(ctx context.Context, request *proto.QueryRequest) (resp2 *proto.QueryResponse, respErr error) {
|
|
|
|
defer func() {
|
|
if err := recover(); err != nil {
|
|
respErr = proto.ErrorPanic(fmt.Sprintf("panic: %v", err))
|
|
}
|
|
}()
|
|
|
|
config, err := transConfig(request.Config)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
bizContent, err := config.queryReq(request.Order)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
reqBody, err := bizContent.Json()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
bodyBytes, _, err := config.Post(ctx, queryMethod, reqBody)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return queryResp(request, bodyBytes)
|
|
}
|
|
|
|
func (p *LSXDWeChatCpnService) Notify(ctx context.Context, request *proto.NotifyRequest) (resp2 *proto.NotifyResponse, respErr error) {
|
|
|
|
defer func() {
|
|
if err := recover(); err != nil {
|
|
respErr = proto.ErrorPanic(fmt.Sprintf("panic: %v", err))
|
|
}
|
|
}()
|
|
|
|
config, err := transConfig(request.Config)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var h http.Header
|
|
if err = json.Unmarshal(request.Headers, &h); err != nil {
|
|
return nil, proto.ErrorParamFail("header参数解析失败:" + err.Error())
|
|
}
|
|
|
|
if err = config.verify(request.Body, h.Get("Verify")); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return notifyResp(request.Body)
|
|
}
|
|
|
|
func (p *LSXDWeChatCpnService) Label(ctx context.Context, request *proto.QueryRequest) (resp2 string, respErr error) {
|
|
|
|
config, err := transConfig(request.Config)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
bodyBytes, _, err := config.Post(ctx, configMethod, []byte(`{}`))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return string(bodyBytes), nil
|
|
}
|