Skip to content

Develop #650

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

Merged
merged 9 commits into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
194 changes: 194 additions & 0 deletions src/channels/eCommerce/application.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package eCommerce

import (
"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/channels/eCommerce/store"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/providers"
"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/auth"
"net/http"
)

type ECommerce struct {
*kernel.ServiceContainer

Config *kernel.Config
AccessToken *auth.AccessToken

Store *store.Client

Logger *logger.Logger
}

type UserConfig struct {
AppID string
Secret string

StableTokenMode bool
ForceRefresh bool
RefreshToken string
ComponentAppID string
ComponentAppToken string
Token string
AESKey string

ResponseType string
Log Log
OAuth OAuth
Cache kernel.CacheInterface

Http Http

HttpDebug bool
Debug bool
NotifyURL string
Sandbox bool
}

type Http struct {
Timeout float64
BaseURI string
ProxyURI string
Transport http.RoundTripper
}

type Log struct {
Driver contract.LoggerInterface
Level string
File string
Error string
ENV string
Stdout bool
}

type OAuth struct {
Callback string
Scopes []string
}

func NewECommerce(config *UserConfig, extraInfos ...*kernel.ExtraInfo) (*ECommerce, error) {
var err error

userConfig, err := MapUserConfig(config)
if err != nil {
return nil, err
}

var extraInfo, _ = kernel.NewExtraInfo()
if len(extraInfos) > 0 {
extraInfo = extraInfos[0]
}

// init an app container
container, err := kernel.NewServiceContainer(userConfig, extraInfo)
if err != nil {
return nil, err
}
container.GetConfig()

// init app
app := &ECommerce{
ServiceContainer: container,
}

//-------------- global app config --------------
// global app config
app.Config = providers.RegisterConfigProvider(app)

app.Logger, err = logger.NewLogger(app.Config.Get("log.driver", nil), &object.HashMap{
"level": app.Config.GetString("log.level", "info"),
"env": app.Config.GetString("log.env", "develop"),
"outputPath": app.Config.GetString("log.file", "./wechat/info.log"),
"errorPath": app.Config.GetString("log.error", "./wechat/error.log"),
"stdout": app.Config.GetBool("log.stdout", false),
})
if err != nil {
return nil, err
}

//-------------- register auth,AccessToken --------------
app.AccessToken, err = auth.RegisterProvider(app)
if err != nil {
return nil, err
}

return app, err
}

func (app *ECommerce) GetContainer() *kernel.ServiceContainer {
return app.ServiceContainer
}

func (app *ECommerce) GetAccessToken() *kernel.AccessToken {
return app.AccessToken.AccessToken
}

func (app *ECommerce) GetConfig() *kernel.Config {
return app.Config
}

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

switch name {
case "AccessToken":
return app.AccessToken
case "Config":
return app.Config

case "Store":
return app.Store

case "Logger":
return app.Logger

default:
return nil
}

}

func MapUserConfig(userConfig *UserConfig) (*object.HashMap, error) {

baseURI := "https://api.weixin.qq.com/"
if userConfig.Http.BaseURI != "" {
baseURI = userConfig.Http.BaseURI
}

config := &object.HashMap{

"app_id": userConfig.AppID,
"secret": userConfig.Secret,

"token": userConfig.Token,
"aes_key": userConfig.AESKey,
"component_app_id": userConfig.ComponentAppID,
"component_app_token": userConfig.ComponentAppToken,
"stable_token_mode": userConfig.StableTokenMode,
"refresh_token": userConfig.RefreshToken,

"response_type": userConfig.ResponseType,
"http": &object.HashMap{
"timeout": userConfig.Http.Timeout,
"base_uri": baseURI,
"proxy_uri": userConfig.Http.ProxyURI,
"transport": userConfig.Http.Transport,
},
"log": &object.HashMap{
"driver": userConfig.Log.Driver,
"level": userConfig.Log.Level,
"file": userConfig.Log.File,
"error": userConfig.Log.Error,
"env": userConfig.Log.ENV,
"stdout": userConfig.Log.Stdout,
},
"cache": userConfig.Cache,

"http_debug": userConfig.HttpDebug,
"debug": userConfig.Debug,
}

return config, nil

}
40 changes: 40 additions & 0 deletions src/channels/eCommerce/store/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package store

import (
"context"
"github.com/ArtisanCloud/PowerLibs/v3/object"
"github.com/ArtisanCloud/PowerWeChat/v3/src/channels/eCommerce/store/request"
"github.com/ArtisanCloud/PowerWeChat/v3/src/channels/eCommerce/store/response"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel"
)

type Client struct {
BaseClient *kernel.BaseClient
}

// 获取店铺基本信息
// https://developers.weixin.qq.com/doc/store/shop/API/basics/getbasicinfo.html
func (comp *Client) GetBasicInfo(ctx context.Context) (*response.ResponseGetBasicInfo, error) {

result := &response.ResponseGetBasicInfo{}

_, err := comp.BaseClient.HttpGet(ctx, "channels/ec/basics/info/get", nil, nil, result)

return result, err
}

// 获取店铺二维码
// https://developers.weixin.qq.com/doc/store/shop/API/basics/getshopqrcode.html
func (comp *Client) GetShopQRCode(ctx context.Context, data *request.RequestGetShopQRCode) (*response.ResponseGetShopQRCode, error) {

result := &response.ResponseGetShopQRCode{}

params, err := object.StructToHashMap(data)
if err != nil {
return nil, err
}

_, err = comp.BaseClient.HttpPost(ctx, "channels/ec/basics/shop/qrcode/get", params, nil, result)

return result, err
}
14 changes: 14 additions & 0 deletions src/channels/eCommerce/store/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package store

import "github.com/ArtisanCloud/PowerWeChat/v3/src/kernel"

func RegisterProvider(app kernel.ApplicationInterface) (*Client, error) {
baseClient, err := kernel.NewBaseClient(app, nil)
if err != nil {
return nil, err
}
return &Client{
baseClient,
}, nil

}
6 changes: 6 additions & 0 deletions src/channels/eCommerce/store/request/request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package request

type RequestGetShopQRCode struct {
WeComCorpId string `json:"wecom_corp_id"`
WeComUserId string `json:"wecom_user_id"`
}
21 changes: 21 additions & 0 deletions src/channels/eCommerce/store/response/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package response

import "github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/response"

type Info struct {
Nickname string `json:"nickname"`
HeadImgUrl string `json:"headimg_url"`
SubjectType string `json:"subject_type"`
Status string `json:"status"`
Username string `json:"username"`
}

type ResponseGetBasicInfo struct {
response.ResponseECommerce
Info Info `json:"info"`
}

type ResponseGetShopQRCode struct {
response.ResponseECommerce
ShopQrcode string `json:"shop_qrcode"`
}
7 changes: 7 additions & 0 deletions src/kernel/response/wx.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,10 @@ const (
//func (res *ResponseWork) GetStatusCode() int {
// return 200
//}

type ResponseECommerce struct {
ResponseBase

ErrCode int `json:"errcode,omitempty"`
ErrMsg string `json:"errmsg,omitempty"`
}
Loading