Skip to content

Commit 5fb0932

Browse files
author
Matrix-X
committed
feat(eCommerce): add eCommerce for channels
1 parent f6b4b09 commit 5fb0932

File tree

6 files changed

+282
-0
lines changed

6 files changed

+282
-0
lines changed

src/channels/eCommerce/application.go

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
package eCommerce
2+
3+
import (
4+
"github.com/ArtisanCloud/PowerLibs/v3/logger"
5+
"github.com/ArtisanCloud/PowerLibs/v3/logger/contract"
6+
"github.com/ArtisanCloud/PowerLibs/v3/object"
7+
"github.com/ArtisanCloud/PowerWeChat/v3/src/channels/eCommerce/store"
8+
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel"
9+
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/providers"
10+
"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/auth"
11+
"net/http"
12+
)
13+
14+
type ECommerce struct {
15+
*kernel.ServiceContainer
16+
17+
Config *kernel.Config
18+
AccessToken *auth.AccessToken
19+
20+
Store *store.Client
21+
22+
Logger *logger.Logger
23+
}
24+
25+
type UserConfig struct {
26+
AppID string
27+
Secret string
28+
29+
StableTokenMode bool
30+
ForceRefresh bool
31+
RefreshToken string
32+
ComponentAppID string
33+
ComponentAppToken string
34+
Token string
35+
AESKey string
36+
37+
ResponseType string
38+
Log Log
39+
OAuth OAuth
40+
Cache kernel.CacheInterface
41+
42+
Http Http
43+
44+
HttpDebug bool
45+
Debug bool
46+
NotifyURL string
47+
Sandbox bool
48+
}
49+
50+
type Http struct {
51+
Timeout float64
52+
BaseURI string
53+
ProxyURI string
54+
Transport http.RoundTripper
55+
}
56+
57+
type Log struct {
58+
Driver contract.LoggerInterface
59+
Level string
60+
File string
61+
Error string
62+
ENV string
63+
Stdout bool
64+
}
65+
66+
type OAuth struct {
67+
Callback string
68+
Scopes []string
69+
}
70+
71+
func NewECommerce(config *UserConfig, extraInfos ...*kernel.ExtraInfo) (*ECommerce, error) {
72+
var err error
73+
74+
userConfig, err := MapUserConfig(config)
75+
if err != nil {
76+
return nil, err
77+
}
78+
79+
var extraInfo, _ = kernel.NewExtraInfo()
80+
if len(extraInfos) > 0 {
81+
extraInfo = extraInfos[0]
82+
}
83+
84+
// init an app container
85+
container, err := kernel.NewServiceContainer(userConfig, extraInfo)
86+
if err != nil {
87+
return nil, err
88+
}
89+
container.GetConfig()
90+
91+
// init app
92+
app := &ECommerce{
93+
ServiceContainer: container,
94+
}
95+
96+
//-------------- global app config --------------
97+
// global app config
98+
app.Config = providers.RegisterConfigProvider(app)
99+
100+
app.Logger, err = logger.NewLogger(app.Config.Get("log.driver", nil), &object.HashMap{
101+
"level": app.Config.GetString("log.level", "info"),
102+
"env": app.Config.GetString("log.env", "develop"),
103+
"outputPath": app.Config.GetString("log.file", "./wechat/info.log"),
104+
"errorPath": app.Config.GetString("log.error", "./wechat/error.log"),
105+
"stdout": app.Config.GetBool("log.stdout", false),
106+
})
107+
if err != nil {
108+
return nil, err
109+
}
110+
111+
//-------------- register auth,AccessToken --------------
112+
app.AccessToken, err = auth.RegisterProvider(app)
113+
if err != nil {
114+
return nil, err
115+
}
116+
117+
return app, err
118+
}
119+
120+
func (app *ECommerce) GetContainer() *kernel.ServiceContainer {
121+
return app.ServiceContainer
122+
}
123+
124+
func (app *ECommerce) GetAccessToken() *kernel.AccessToken {
125+
return app.AccessToken.AccessToken
126+
}
127+
128+
func (app *ECommerce) GetConfig() *kernel.Config {
129+
return app.Config
130+
}
131+
132+
func (app *ECommerce) GetComponent(name string) interface{} {
133+
134+
switch name {
135+
case "AccessToken":
136+
return app.AccessToken
137+
case "Config":
138+
return app.Config
139+
140+
case "Store":
141+
return app.Store
142+
143+
case "Logger":
144+
return app.Logger
145+
146+
default:
147+
return nil
148+
}
149+
150+
}
151+
152+
func MapUserConfig(userConfig *UserConfig) (*object.HashMap, error) {
153+
154+
baseURI := "https://api.weixin.qq.com/"
155+
if userConfig.Http.BaseURI != "" {
156+
baseURI = userConfig.Http.BaseURI
157+
}
158+
159+
config := &object.HashMap{
160+
161+
"app_id": userConfig.AppID,
162+
"secret": userConfig.Secret,
163+
164+
"token": userConfig.Token,
165+
"aes_key": userConfig.AESKey,
166+
"component_app_id": userConfig.ComponentAppID,
167+
"component_app_token": userConfig.ComponentAppToken,
168+
"stable_token_mode": userConfig.StableTokenMode,
169+
"refresh_token": userConfig.RefreshToken,
170+
171+
"response_type": userConfig.ResponseType,
172+
"http": &object.HashMap{
173+
"timeout": userConfig.Http.Timeout,
174+
"base_uri": baseURI,
175+
"proxy_uri": userConfig.Http.ProxyURI,
176+
"transport": userConfig.Http.Transport,
177+
},
178+
"log": &object.HashMap{
179+
"driver": userConfig.Log.Driver,
180+
"level": userConfig.Log.Level,
181+
"file": userConfig.Log.File,
182+
"error": userConfig.Log.Error,
183+
"env": userConfig.Log.ENV,
184+
"stdout": userConfig.Log.Stdout,
185+
},
186+
"cache": userConfig.Cache,
187+
188+
"http_debug": userConfig.HttpDebug,
189+
"debug": userConfig.Debug,
190+
}
191+
192+
return config, nil
193+
194+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package store
2+
3+
import (
4+
"context"
5+
"github.com/ArtisanCloud/PowerLibs/v3/object"
6+
"github.com/ArtisanCloud/PowerWeChat/v3/src/channels/eCommerce/store/request"
7+
"github.com/ArtisanCloud/PowerWeChat/v3/src/channels/eCommerce/store/response"
8+
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel"
9+
)
10+
11+
type Client struct {
12+
BaseClient *kernel.BaseClient
13+
}
14+
15+
// 获取店铺基本信息
16+
// https://developers.weixin.qq.com/doc/store/shop/API/basics/getbasicinfo.html
17+
func (comp *Client) GetBasicInfo(ctx context.Context) (*response.ResponseGetBasicInfo, error) {
18+
19+
result := &response.ResponseGetBasicInfo{}
20+
21+
_, err := comp.BaseClient.HttpGet(ctx, "channels/ec/basics/info/get", nil, nil, result)
22+
23+
return result, err
24+
}
25+
26+
// 获取店铺二维码
27+
// https://developers.weixin.qq.com/doc/store/shop/API/basics/getshopqrcode.html
28+
func (comp *Client) GetShopQRCode(ctx context.Context, data *request.RequestGetShopQRCode) (*response.ResponseGetShopQRCode, error) {
29+
30+
result := &response.ResponseGetShopQRCode{}
31+
32+
params, err := object.StructToHashMap(data)
33+
if err != nil {
34+
return nil, err
35+
}
36+
37+
_, err = comp.BaseClient.HttpPost(ctx, "channels/ec/basics/shop/qrcode/get", params, nil, result)
38+
39+
return result, err
40+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package store
2+
3+
import "github.com/ArtisanCloud/PowerWeChat/v3/src/kernel"
4+
5+
func RegisterProvider(app kernel.ApplicationInterface) (*Client, error) {
6+
baseClient, err := kernel.NewBaseClient(app, nil)
7+
if err != nil {
8+
return nil, err
9+
}
10+
return &Client{
11+
baseClient,
12+
}, nil
13+
14+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package request
2+
3+
type RequestGetShopQRCode struct {
4+
WeComCorpId string `json:"wecom_corp_id"`
5+
WeComUserId string `json:"wecom_user_id"`
6+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package response
2+
3+
import "github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/response"
4+
5+
type Info struct {
6+
Nickname string `json:"nickname"`
7+
HeadImgUrl string `json:"headimg_url"`
8+
SubjectType string `json:"subject_type"`
9+
Status string `json:"status"`
10+
Username string `json:"username"`
11+
}
12+
13+
type ResponseGetBasicInfo struct {
14+
response.ResponseECommerce
15+
Info Info `json:"info"`
16+
}
17+
18+
type ResponseGetShopQRCode struct {
19+
response.ResponseECommerce
20+
ShopQrcode string `json:"shop_qrcode"`
21+
}

src/kernel/response/wx.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,10 @@ const (
7979
//func (res *ResponseWork) GetStatusCode() int {
8080
// return 200
8181
//}
82+
83+
type ResponseECommerce struct {
84+
ResponseBase
85+
86+
ErrCode int `json:"errcode,omitempty"`
87+
ErrMsg string `json:"errmsg,omitempty"`
88+
}

0 commit comments

Comments
 (0)