- update service/wechatunion

master
李光春 2 years ago
parent d5c9af6d82
commit 6f61ee1793

@ -1,73 +0,0 @@
package wechatunion
import (
"go.dtapp.net/library/utils/golog"
"go.dtapp.net/library/utils/goredis"
"go.dtapp.net/library/utils/gorequest"
"gorm.io/gorm"
)
const (
UnionUrl = "https://api.weixin.qq.com/union"
)
// App 微信小程序联盟
type App struct {
appId string // 小程序唯一凭证,即 appId
appSecret string // 小程序唯一凭证密钥,即 appSecret
accessToken string // 接口调用凭证
pid string // 推广位PID
Redis goredis.App // 缓存数据库服务
pgsql *gorm.DB // pgsql数据库
client *gorequest.App // 请求客户端
log *golog.Api // 日志服务
logTableName string // 日志表名
logStatus bool // 日志状态
}
func NewApp(appId string, appSecret string, pid string, redis goredis.App, pgsql *gorm.DB) *App {
app := &App{appId: appId, appSecret: appSecret, pid: pid, Redis: redis}
app.client = gorequest.NewHttp()
if pgsql != nil {
app.pgsql = pgsql
app.logStatus = true
app.logTableName = "wechatunion"
app.log = golog.NewApi(&golog.ApiConfig{
Db: pgsql,
TableName: app.logTableName,
})
}
return app
}
// 请求
func (app *App) request(url string, params map[string]interface{}, method string) (resp gorequest.Response, err error) {
// 创建请求
client := app.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 app.logStatus == true {
go app.postgresqlLog(request)
}
return request, err
}

@ -6,15 +6,15 @@ import (
"time"
)
func (app *App) GetAccessTokenMonitor() (string, error) {
if app.Redis.Db == nil {
func (c *Client) GetAccessTokenMonitor() (string, error) {
if c.config.RedisClient.Db == nil {
return "", errors.New("驱动没有初始化")
}
result := app.GetCallBackIp()
result := c.GetCallBackIp()
if len(result.Result.IpList) <= 0 {
token := app.CgiBinToken()
app.Redis.Db.Set(context.Background(), app.getAccessTokenCacheKeyName(), token.Result.AccessToken, time.Second*7000)
token := c.CgiBinToken()
c.config.RedisClient.Db.Set(context.Background(), c.getAccessTokenCacheKeyName(), token.Result.AccessToken, time.Second*7000)
return token.Result.AccessToken, nil
}
return app.accessToken, nil
return c.config.AccessToken, nil
}

@ -5,18 +5,18 @@ import (
"time"
)
func (app *App) GetAccessToken() string {
if app.Redis.Db == nil {
return app.accessToken
func (c *Client) GetAccessToken() string {
if c.config.RedisClient.Db == nil {
return c.config.AccessToken
}
newCache := app.Redis.NewSimpleStringCache(app.Redis.NewStringOperation(), time.Second*7000)
newCache := c.config.RedisClient.NewSimpleStringCache(c.config.RedisClient.NewStringOperation(), time.Second*7000)
newCache.DBGetter = func() string {
token := app.CgiBinToken()
token := c.CgiBinToken()
return token.Result.AccessToken
}
return newCache.GetCache(app.getAccessTokenCacheKeyName())
return newCache.GetCache(c.getAccessTokenCacheKeyName())
}
func (app *App) getAccessTokenCacheKeyName() string {
return fmt.Sprintf("wechat_access_token:%v", app.appId)
func (c *Client) getAccessTokenCacheKeyName() string {
return fmt.Sprintf("wechat_access_token:%v", c.getAppId())
}

@ -28,9 +28,9 @@ func NewCgiBinTokenResult(result CgiBinTokenResponse, byte []byte, http goreques
// CgiBinToken
// 接口调用凭证
// https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html
func (app *App) CgiBinToken() *CgiBinTokenResult {
func (c *Client) CgiBinToken() *CgiBinTokenResult {
// 请求
request, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", app.appId, app.appSecret), map[string]interface{}{}, http.MethodGet)
request, err := c.request(fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", c.getAppId(), c.getAppSecret()), map[string]interface{}{}, http.MethodGet)
// 定义
var response CgiBinTokenResponse
err = json.Unmarshal(request.ResponseBody, &response)

@ -0,0 +1,78 @@
package wechatunion
import (
"go.dtapp.net/library/utils/dorm"
"go.dtapp.net/library/utils/golog"
"go.dtapp.net/library/utils/gorequest"
"gorm.io/gorm"
)
const (
UnionUrl = "https://api.weixin.qq.com/union"
)
type ConfigClient struct {
AppId string // 小程序唯一凭证,即 appId
AppSecret string // 小程序唯一凭证密钥,即 appSecret
AccessToken string // 接口调用凭证
Pid string // 推广位PID
RedisClient *dorm.RedisClient // 缓存数据库
PgsqlDb *gorm.DB // pgsql数据库
}
// Client 微信小程序联盟
type Client struct {
client *gorequest.App // 请求客户端
log *golog.Api // 日志服务
logTableName string // 日志表名
logStatus bool // 日志状态
config *ConfigClient // 配置
}
func NewClient(config *ConfigClient) *Client {
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{
Db: c.config.PgsqlDb,
TableName: c.logTableName,
})
}
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
}

@ -0,0 +1,18 @@
package wechatunion
func (c *Client) getAppId() string {
return c.config.AppId
}
func (c *Client) getAppSecret() string {
return c.config.AppSecret
}
func (c *Client) getAccessToken() string {
c.config.AccessToken = c.GetAccessToken()
return c.config.AccessToken
}
func (c *Client) getPid() string {
return c.config.Pid
}

@ -24,10 +24,9 @@ func NewGetCallBackIpResult(result GetCallBackIpResponse, byte []byte, http gore
// GetCallBackIp 获取微信callback IP地址
// callback IP即微信调用开发者服务器所使用的出口IP。
// https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_the_WeChat_server_IP_address.html#2.%20%E8%8E%B7%E5%8F%96%E5%BE%AE%E4%BF%A1callback%20IP%E5%9C%B0%E5%9D%80
func (app *App) GetCallBackIp() *GetCallBackIpResult {
app.accessToken = app.GetAccessToken()
func (c *Client) GetCallBackIp() *GetCallBackIpResult {
// 请求
request, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=%s", app.accessToken), map[string]interface{}{}, "GET")
request, err := c.request(fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=%s", c.getAccessToken()), map[string]interface{}{}, "GET")
// 定义
var response GetCallBackIpResponse
err = json.Unmarshal(request.ResponseBody, &response)

@ -50,19 +50,19 @@ type OrderSearchResult struct {
}
// OrderSearch 根据订单支付时间、订单分佣状态拉取订单详情 https://developers.weixin.qq.com/doc/ministore/union/access-guidelines/promoter/api/order/order-info.html
func (app *App) OrderSearch(notMustParams ...Params) (result OrderSearchResult, err error) {
if len(app.accessToken) <= 0 {
func (c *Client) OrderSearch(notMustParams ...Params) (result OrderSearchResult, err error) {
if len(c.getAccessToken()) <= 0 {
return result, errors.New("调用凭证异常")
}
// 参数
//params := app.NewParamsWith(notMustParams...)
//params := c.NewParamsWith(notMustParams...)
//if len(orderIdList) <= 0 || len(orderIdList) > 200 {
// return result, errors.New("未传入 orderIdList 或 orderIdList 超过上限 200")
//}
//body, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/union/promoter/order/info?access_token=%s", app.accessToken), map[string]interface{}{
//body, err := c.request(fmt.Sprintf("https://api.weixin.qq.com/union/promoter/order/info?access_token=%s", c.accessToken), map[string]interface{}{
// "orderIdList": orderIdList,
//}, http.MethodPost)
//if err != nil {

@ -8,7 +8,7 @@ func NewParams() Params {
return p
}
func (app *App) NewParamsWith(params ...Params) Params {
func (c *Client) NewParamsWith(params ...Params) Params {
p := make(Params)
for _, v := range params {
p.SetParams(v)

@ -8,8 +8,8 @@ import (
)
// 记录日志
func (app *App) postgresqlLog(request gorequest.Response) {
app.log.Record(golog.ApiPostgresqlLog{
func (c *Client) postgresqlLog(request gorequest.Response) {
c.log.Record(golog.ApiPostgresqlLog{
RequestTime: golog.TimeString{Time: request.RequestTime}, //【请求】时间
RequestUri: request.RequestUri, //【请求】链接
RequestUrl: gorequest.UriParse(request.RequestUri).Url, //【请求】链接

@ -55,17 +55,16 @@ func NewPromoterOrderInfoResult(result PromoterOrderInfoResponse, body []byte, h
// PromoterOrderInfo 根据订单ID查询订单详情
// 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 (app *App) PromoterOrderInfo(orderId ...string) *PromoterOrderInfoResult {
app.accessToken = app.GetAccessToken()
func (c *Client) PromoterOrderInfo(orderId ...string) *PromoterOrderInfoResult {
// 参数
params := app.NewParamsWith()
params := c.NewParamsWith()
var orderIdList []any
for _, v := range orderId {
orderIdList = append(orderIdList, v)
}
params.Set("orderIdList", orderIdList)
// 请求
request, err := app.request(UnionUrl+fmt.Sprintf("/promoter/order/info?access_token=%s", app.accessToken), params, http.MethodPost)
request, err := c.request(UnionUrl+fmt.Sprintf("/promoter/order/info?access_token=%s", c.getAccessToken()), params, http.MethodPost)
// 定义
var response PromoterOrderInfoResponse
err = json.Unmarshal(request.ResponseBody, &response)

@ -57,12 +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 (app *App) PromoterOrderSearch(notMustParams ...Params) *PromoterOrderSearchResult {
app.accessToken = app.GetAccessToken()
func (c *Client) PromoterOrderSearch(notMustParams ...Params) *PromoterOrderSearchResult {
// 参数
params := app.NewParamsWith(notMustParams...)
params := c.NewParamsWith(notMustParams...)
// 请求
request, err := app.request(UnionUrl+fmt.Sprintf("/promoter/order/search?access_token=%s", app.accessToken), params, http.MethodGet)
request, err := c.request(UnionUrl+fmt.Sprintf("/promoter/order/search?access_token=%s", c.getAccessToken()), params, http.MethodGet)
// 定义
var response PromoterOrderSearchResponse
err = json.Unmarshal(request.ResponseBody, &response)

@ -29,10 +29,9 @@ func NewPromoterProductCategoryResult(result PromoterProductCategoryResponse, bo
// PromoterProductCategory 获取联盟商品类目列表及类目ID
// 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 (app *App) PromoterProductCategory() *PromoterProductCategoryResult {
app.accessToken = app.GetAccessToken()
func (c *Client) PromoterProductCategory() *PromoterProductCategoryResult {
// 请求
request, err := app.request(UnionUrl+fmt.Sprintf("/promoter/product/category?access_token=%s", app.accessToken), map[string]interface{}{}, http.MethodGet)
request, err := c.request(UnionUrl+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,13 +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 (app *App) PromoterProductGenerate(notMustParams ...Params) *PromoterProductGenerateResult {
app.accessToken = app.GetAccessToken()
func (c *Client) PromoterProductGenerate(notMustParams ...Params) *PromoterProductGenerateResult {
// 参数
params := app.NewParamsWith(notMustParams...)
params.Set("pid", app.pid)
params := c.NewParamsWith(notMustParams...)
params.Set("pid", c.getPid())
// 请求
request, err := app.request(UnionUrl+fmt.Sprintf("/promoter/product/generate?access_token=%s", app.accessToken), params, http.MethodPost)
request, err := c.request(UnionUrl+fmt.Sprintf("/promoter/product/generate?access_token=%s", c.getAccessToken()), params, http.MethodPost)
// 定义
var response PromoterProductGenerateResponse
err = json.Unmarshal(request.ResponseBody, &response)

@ -139,12 +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 (app *App) PromoterProductList(notMustParams ...Params) *PromoterProductListResult {
app.accessToken = app.GetAccessToken()
func (c *Client) PromoterProductList(notMustParams ...Params) *PromoterProductListResult {
// 参数
params := app.NewParamsWith(notMustParams...)
params := c.NewParamsWith(notMustParams...)
// 请求
request, err := app.request(UnionUrl+fmt.Sprintf("/promoter/product/list?access_token=%s", app.accessToken), params, http.MethodGet)
request, err := c.request(UnionUrl+fmt.Sprintf("/promoter/product/list?access_token=%s", c.getAccessToken()), params, http.MethodGet)
// 定义
var response PromoterProductListResponse
err = json.Unmarshal(request.ResponseBody, &response)

@ -140,12 +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 (app *App) PromoterProductSelect(notMustParams ...Params) *PromoterProductSelectResult {
app.accessToken = app.GetAccessToken()
func (c *Client) PromoterProductSelect(notMustParams ...Params) *PromoterProductSelectResult {
// 参数
params := app.NewParamsWith(notMustParams...)
params := c.NewParamsWith(notMustParams...)
// 请求
request, err := app.request(UnionUrl+fmt.Sprintf("/promoter/product/select?access_token=%s", app.accessToken), params, http.MethodGet)
request, err := c.request(UnionUrl+fmt.Sprintf("/promoter/product/select?access_token=%s", c.getAccessToken()), params, http.MethodGet)
// 定义
var response PromoterProductSelectResponse
err = json.Unmarshal(request.ResponseBody, &response)

@ -26,13 +26,12 @@ func NewPromotionAddResult(result PromotionAddResponse, body []byte, http gorequ
// PromotionAdd 添加推广位
// 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 (app *App) PromotionAdd(promotionSourceName string) *PromotionAddResult {
app.accessToken = app.GetAccessToken()
func (c *Client) PromotionAdd(promotionSourceName string) *PromotionAddResult {
// 参数
params := NewParams()
params.Set("promotionSourceName", promotionSourceName) // 推广位名称
// 请求
request, err := app.request(UnionUrl+fmt.Sprintf("/promoter/promotion/add?access_token%s", app.accessToken), params, http.MethodPost)
request, err := c.request(UnionUrl+fmt.Sprintf("/promoter/promotion/add?access_token%s", c.getAccessToken()), params, http.MethodPost)
// 定义
var response PromotionAddResponse
err = json.Unmarshal(request.ResponseBody, &response)

@ -25,14 +25,13 @@ func NewPromotionDelResult(result PromotionDelResponse, body []byte, http gorequ
// PromotionDel 删除某个推广位
// 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 (app *App) PromotionDel(promotionSourcePid, promotionSourceName string) *PromotionDelResult {
app.accessToken = app.GetAccessToken()
func (c *Client) PromotionDel(promotionSourcePid, promotionSourceName string) *PromotionDelResult {
// 参数
params := NewParams()
params.Set("promotionSourcePid", promotionSourcePid) // 推广位PID
params.Set("promotionSourceName", promotionSourceName) // 推广位名称
// 请求
request, err := app.request(UnionUrl+fmt.Sprintf("/promoter/promotion/del?access_token%s", app.accessToken), params, http.MethodPost)
request, err := c.request(UnionUrl+fmt.Sprintf("/promoter/promotion/del?access_token%s", c.getAccessToken()), params, http.MethodPost)
// 定义
var response PromotionDelResponse
err = json.Unmarshal(request.ResponseBody, &response)

@ -33,14 +33,13 @@ func NewPromotionListResult(result PromotionListResponse, body []byte, http gore
// PromotionList 获取推广位列表
// 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 (app *App) PromotionList(start int, limit int) *PromotionListResult {
app.accessToken = app.GetAccessToken()
func (c *Client) PromotionList(start int, limit int) *PromotionListResult {
// 参数
params := NewParams()
params.Set("start", start) // 偏移
params.Set("limit", limit) // 每页条数
// 请求
request, err := app.request(UnionUrl+fmt.Sprintf("/promoter/promotion/list?access_token%s", app.accessToken), params, http.MethodGet)
request, err := c.request(UnionUrl+fmt.Sprintf("/promoter/promotion/list?access_token%s", c.getAccessToken()), params, http.MethodGet)
// 定义
var response PromotionListResponse
err = json.Unmarshal(request.ResponseBody, &response)

@ -25,12 +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 (app *App) PromotionUpd(notMustParams ...Params) *PromotionUpdResult {
app.accessToken = app.GetAccessToken()
func (c *Client) PromotionUpd(notMustParams ...Params) *PromotionUpdResult {
// 参数
params := app.NewParamsWith(notMustParams...)
params := c.NewParamsWith(notMustParams...)
// 请求
request, err := app.request(UnionUrl+fmt.Sprintf("/promoter/promotion/upd?access_token%s", app.accessToken), params, http.MethodPost)
request, err := c.request(UnionUrl+fmt.Sprintf("/promoter/promotion/upd?access_token%s", c.getAccessToken()), params, http.MethodPost)
// 定义
var response PromotionUpdResponse
err = json.Unmarshal(request.ResponseBody, &response)

Loading…
Cancel
Save