- update wechatunion

master
李光春 2 years ago
parent 9fbf6ba6ca
commit efe77317be

@ -7,10 +7,6 @@ import (
"gorm.io/gorm"
)
const (
UnionUrl = "https://api.weixin.qq.com/union"
)
type ConfigClient struct {
AppId string // 小程序唯一凭证,即 appId
AppSecret string // 小程序唯一凭证密钥,即 appSecret
@ -22,57 +18,28 @@ type ConfigClient struct {
// Client 微信小程序联盟
type Client struct {
client *gorequest.App // 请求客户端
log *golog.Api // 日志服务
logTableName string // 日志表名
logStatus bool // 日志状态
config *ConfigClient // 配置
client *gorequest.App // 请求客户端
log *golog.ApiClient // 日志服务
logStatus bool // 日志状态
config *ConfigClient // 配置
}
func NewClient(config *ConfigClient) *Client {
func NewClient(config *ConfigClient) (*Client, error) {
var err error
c := &Client{config: config}
c.client = gorequest.NewHttp()
if c.config.PgsqlDb != nil {
c.logStatus = true
c.logTableName = "wechatunion"
c.log = golog.NewApi(&golog.ApiConfig{
c.log, err = golog.NewApiClient(&golog.ConfigApiClient{
Db: c.config.PgsqlDb,
TableName: c.logTableName,
TableName: logTable,
})
if err != nil {
return nil, err
}
}
return c
}
// 请求
func (c *Client) request(url string, params map[string]interface{}, method string) (resp gorequest.Response, err error) {
// 创建请求
client := c.client
// 设置请求地址
client.SetUri(url)
// 设置请求方式
client.SetMethod(method)
// 设置FORM格式
client.SetContentTypeForm()
// 设置参数
client.SetParams(params)
// 发起请求
request, err := client.Request()
if err != nil {
return gorequest.Response{}, err
}
// 日志
if c.logStatus == true {
go c.postgresqlLog(request)
}
return request, err
return c, nil
}

@ -0,0 +1,9 @@
package wechatunion
const (
apiUrl = "https://api.weixin.qq.com/union"
)
const (
logTable = "wechatunion"
)

@ -2,6 +2,7 @@ package wechatunion
import (
"errors"
"go.dtapp.net/library/utils/gorequest"
)
type OrderSearch struct {
@ -50,7 +51,7 @@ type OrderSearchResult struct {
}
// OrderSearch 根据订单支付时间、订单分佣状态拉取订单详情 https://developers.weixin.qq.com/doc/ministore/union/access-guidelines/promoter/api/order/order-info.html
func (c *Client) OrderSearch(notMustParams ...Params) (result OrderSearchResult, err error) {
func (c *Client) OrderSearch(notMustParams ...gorequest.Params) (result OrderSearchResult, err error) {
if len(c.getAccessToken()) <= 0 {
return result, errors.New("调用凭证异常")
}

@ -1,27 +0,0 @@
package wechatunion
// Params 请求参数
type Params map[string]interface{}
func NewParams() Params {
p := make(Params)
return p
}
func (c *Client) NewParamsWith(params ...Params) Params {
p := make(Params)
for _, v := range params {
p.SetParams(v)
}
return p
}
func (p Params) Set(key string, value interface{}) {
p[key] = value
}
func (p Params) SetParams(params Params) {
for key, value := range params {
p[key] = value
}
}

@ -57,14 +57,14 @@ func NewPromoterOrderInfoResult(result PromoterOrderInfoResponse, body []byte, h
// https://developers.weixin.qq.com/doc/ministore/union/access-guidelines/promoter/api/order/order-info.html#_1-%E6%A0%B9%E6%8D%AE%E8%AE%A2%E5%8D%95ID%E6%9F%A5%E8%AF%A2%E8%AE%A2%E5%8D%95%E8%AF%A6%E6%83%85
func (c *Client) PromoterOrderInfo(orderId ...string) *PromoterOrderInfoResult {
// 参数
params := c.NewParamsWith()
params := gorequest.NewParamsWith()
var orderIdList []any
for _, v := range orderId {
orderIdList = append(orderIdList, v)
}
params.Set("orderIdList", orderIdList)
// 请求
request, err := c.request(UnionUrl+fmt.Sprintf("/promoter/order/info?access_token=%s", c.getAccessToken()), params, http.MethodPost)
request, err := c.request(apiUrl+fmt.Sprintf("/promoter/order/info?access_token=%s", c.getAccessToken()), params, http.MethodPost)
// 定义
var response PromoterOrderInfoResponse
err = json.Unmarshal(request.ResponseBody, &response)

@ -57,11 +57,11 @@ func NewPromoterOrderSearchResult(result PromoterOrderSearchResponse, body []byt
// PromoterOrderSearch 根据订单支付时间、订单分佣状态拉取订单详情
// https://developers.weixin.qq.com/doc/ministore/union/access-guidelines/promoter/api/order/order-info.html#_2-%E6%A0%B9%E6%8D%AE%E8%AE%A2%E5%8D%95%E6%94%AF%E4%BB%98%E6%97%B6%E9%97%B4%E3%80%81%E8%AE%A2%E5%8D%95%E5%88%86%E4%BD%A3%E7%8A%B6%E6%80%81%E6%8B%89%E5%8F%96%E8%AE%A2%E5%8D%95%E8%AF%A6%E6%83%85
func (c *Client) PromoterOrderSearch(notMustParams ...Params) *PromoterOrderSearchResult {
func (c *Client) PromoterOrderSearch(notMustParams ...gorequest.Params) *PromoterOrderSearchResult {
// 参数
params := c.NewParamsWith(notMustParams...)
params := gorequest.NewParamsWith(notMustParams...)
// 请求
request, err := c.request(UnionUrl+fmt.Sprintf("/promoter/order/search?access_token=%s", c.getAccessToken()), params, http.MethodGet)
request, err := c.request(apiUrl+fmt.Sprintf("/promoter/order/search?access_token=%s", c.getAccessToken()), params, http.MethodGet)
// 定义
var response PromoterOrderSearchResponse
err = json.Unmarshal(request.ResponseBody, &response)

@ -31,7 +31,7 @@ func NewPromoterProductCategoryResult(result PromoterProductCategoryResponse, bo
// https://developers.weixin.qq.com/doc/ministore/union/access-guidelines/promoter/api/product/category.html#_1-%E8%8E%B7%E5%8F%96%E8%81%94%E7%9B%9F%E5%95%86%E5%93%81%E7%B1%BB%E7%9B%AE%E5%88%97%E8%A1%A8%E5%8F%8A%E7%B1%BB%E7%9B%AEID
func (c *Client) PromoterProductCategory() *PromoterProductCategoryResult {
// 请求
request, err := c.request(UnionUrl+fmt.Sprintf("/promoter/product/category?access_token=%s", c.getAccessToken()), map[string]interface{}{}, http.MethodGet)
request, err := c.request(apiUrl+fmt.Sprintf("/promoter/product/category?access_token=%s", c.getAccessToken()), map[string]interface{}{}, http.MethodGet)
// 定义
var response PromoterProductCategoryResponse
err = json.Unmarshal(request.ResponseBody, &response)

@ -55,12 +55,12 @@ func NewPromoterProductGenerateResult(result PromoterProductGenerateResponse, bo
// PromoterProductGenerate 获取商品推广素材
// https://developers.weixin.qq.com/doc/ministore/union/access-guidelines/promoter/api/product/category.html#_4-%E8%8E%B7%E5%8F%96%E5%95%86%E5%93%81%E6%8E%A8%E5%B9%BF%E7%B4%A0%E6%9D%90
func (c *Client) PromoterProductGenerate(notMustParams ...Params) *PromoterProductGenerateResult {
func (c *Client) PromoterProductGenerate(notMustParams ...gorequest.Params) *PromoterProductGenerateResult {
// 参数
params := c.NewParamsWith(notMustParams...)
params := gorequest.NewParamsWith(notMustParams...)
params.Set("pid", c.getPid())
// 请求
request, err := c.request(UnionUrl+fmt.Sprintf("/promoter/product/generate?access_token=%s", c.getAccessToken()), params, http.MethodPost)
request, err := c.request(apiUrl+fmt.Sprintf("/promoter/product/generate?access_token=%s", c.getAccessToken()), params, http.MethodPost)
// 定义
var response PromoterProductGenerateResponse
err = json.Unmarshal(request.ResponseBody, &response)

@ -139,11 +139,11 @@ func NewPromoterProductListResult(result PromoterProductListResponse, body []byt
// PromoterProductList 查询全量商品
// https://developers.weixin.qq.com/doc/ministore/union/access-guidelines/promoter/api/product/category.html#_2-%E6%9F%A5%E8%AF%A2%E5%85%A8%E9%87%8F%E5%95%86%E5%93%81
func (c *Client) PromoterProductList(notMustParams ...Params) *PromoterProductListResult {
func (c *Client) PromoterProductList(notMustParams ...gorequest.Params) *PromoterProductListResult {
// 参数
params := c.NewParamsWith(notMustParams...)
params := gorequest.NewParamsWith(notMustParams...)
// 请求
request, err := c.request(UnionUrl+fmt.Sprintf("/promoter/product/list?access_token=%s", c.getAccessToken()), params, http.MethodGet)
request, err := c.request(apiUrl+fmt.Sprintf("/promoter/product/list?access_token=%s", c.getAccessToken()), params, http.MethodGet)
// 定义
var response PromoterProductListResponse
err = json.Unmarshal(request.ResponseBody, &response)

@ -140,11 +140,11 @@ func NewPromoterProductSelectResult(result PromoterProductSelectResponse, body [
// 查询联盟精选商品
// 支持开发者根据多种筛选条件获取联盟精选的商品列表及详情,筛选条件包括商品价格、商品佣金、商品累计销量、佣金比例、是否含有联盟券、配送方式、发货地区
// https://developers.weixin.qq.com/doc/ministore/union/access-guidelines/promoter/api/product/category.html#3.%E6%9F%A5%E8%AF%A2%E8%81%94%E7%9B%9F%E7%B2%BE%E9%80%89%E5%95%86%E5%93%81
func (c *Client) PromoterProductSelect(notMustParams ...Params) *PromoterProductSelectResult {
func (c *Client) PromoterProductSelect(notMustParams ...gorequest.Params) *PromoterProductSelectResult {
// 参数
params := c.NewParamsWith(notMustParams...)
params := gorequest.NewParamsWith(notMustParams...)
// 请求
request, err := c.request(UnionUrl+fmt.Sprintf("/promoter/product/select?access_token=%s", c.getAccessToken()), params, http.MethodGet)
request, err := c.request(apiUrl+fmt.Sprintf("/promoter/product/select?access_token=%s", c.getAccessToken()), params, http.MethodGet)
// 定义
var response PromoterProductSelectResponse
err = json.Unmarshal(request.ResponseBody, &response)

@ -28,10 +28,10 @@ func NewPromotionAddResult(result PromotionAddResponse, body []byte, http gorequ
// https://developers.weixin.qq.com/doc/ministore/union/access-guidelines/promoter/api/promotion.html#_1-%E6%B7%BB%E5%8A%A0%E6%8E%A8%E5%B9%BF%E4%BD%8D
func (c *Client) PromotionAdd(promotionSourceName string) *PromotionAddResult {
// 参数
params := NewParams()
params := gorequest.NewParams()
params.Set("promotionSourceName", promotionSourceName) // 推广位名称
// 请求
request, err := c.request(UnionUrl+fmt.Sprintf("/promoter/promotion/add?access_token%s", c.getAccessToken()), params, http.MethodPost)
request, err := c.request(apiUrl+fmt.Sprintf("/promoter/promotion/add?access_token%s", c.getAccessToken()), params, http.MethodPost)
// 定义
var response PromotionAddResponse
err = json.Unmarshal(request.ResponseBody, &response)

@ -27,11 +27,11 @@ func NewPromotionDelResult(result PromotionDelResponse, body []byte, http gorequ
// https://developers.weixin.qq.com/doc/ministore/union/access-guidelines/promoter/api/promotion.html#_3-%E7%BC%96%E8%BE%91%E6%8E%A8%E5%B9%BF%E4%BD%8D
func (c *Client) PromotionDel(promotionSourcePid, promotionSourceName string) *PromotionDelResult {
// 参数
params := NewParams()
params := gorequest.NewParams()
params.Set("promotionSourcePid", promotionSourcePid) // 推广位PID
params.Set("promotionSourceName", promotionSourceName) // 推广位名称
// 请求
request, err := c.request(UnionUrl+fmt.Sprintf("/promoter/promotion/del?access_token%s", c.getAccessToken()), params, http.MethodPost)
request, err := c.request(apiUrl+fmt.Sprintf("/promoter/promotion/del?access_token%s", c.getAccessToken()), params, http.MethodPost)
// 定义
var response PromotionDelResponse
err = json.Unmarshal(request.ResponseBody, &response)

@ -35,11 +35,11 @@ func NewPromotionListResult(result PromotionListResponse, body []byte, http gore
// https://developers.weixin.qq.com/doc/ministore/union/access-guidelines/promoter/api/promotion.html#_4-%E8%8E%B7%E5%8F%96%E6%8E%A8%E5%B9%BF%E4%BD%8D%E5%88%97%E8%A1%A8
func (c *Client) PromotionList(start int, limit int) *PromotionListResult {
// 参数
params := NewParams()
params := gorequest.NewParams()
params.Set("start", start) // 偏移
params.Set("limit", limit) // 每页条数
// 请求
request, err := c.request(UnionUrl+fmt.Sprintf("/promoter/promotion/list?access_token%s", c.getAccessToken()), params, http.MethodGet)
request, err := c.request(apiUrl+fmt.Sprintf("/promoter/promotion/list?access_token%s", c.getAccessToken()), params, http.MethodGet)
// 定义
var response PromotionListResponse
err = json.Unmarshal(request.ResponseBody, &response)

@ -25,11 +25,11 @@ func NewPromotionUpdResult(result PromotionUpdResponse, body []byte, http gorequ
// PromotionUpd 编辑推广位
// https://developers.weixin.qq.com/doc/ministore/union/access-guidelines/promoter/api/promotion.html#_3-%E7%BC%96%E8%BE%91%E6%8E%A8%E5%B9%BF%E4%BD%8D
func (c *Client) PromotionUpd(notMustParams ...Params) *PromotionUpdResult {
func (c *Client) PromotionUpd(notMustParams ...gorequest.Params) *PromotionUpdResult {
// 参数
params := c.NewParamsWith(notMustParams...)
params := gorequest.NewParamsWith(notMustParams...)
// 请求
request, err := c.request(UnionUrl+fmt.Sprintf("/promoter/promotion/upd?access_token%s", c.getAccessToken()), params, http.MethodPost)
request, err := c.request(apiUrl+fmt.Sprintf("/promoter/promotion/upd?access_token%s", c.getAccessToken()), params, http.MethodPost)
// 定义
var response PromotionUpdResponse
err = json.Unmarshal(request.ResponseBody, &response)

@ -0,0 +1,35 @@
package wechatunion
import "go.dtapp.net/library/utils/gorequest"
// 请求
func (c *Client) request(url string, params map[string]interface{}, method string) (gorequest.Response, error) {
// 创建请求
client := c.client
// 设置请求地址
client.SetUri(url)
// 设置请求方式
client.SetMethod(method)
// 设置FORM格式
client.SetContentTypeForm()
// 设置参数
client.SetParams(params)
// 发起请求
request, err := client.Request()
if err != nil {
return gorequest.Response{}, err
}
// 日志
if c.logStatus == true {
go c.postgresqlLog(request)
}
return request, err
}
Loading…
Cancel
Save