update wechatminiprogram

master
李光春 2 years ago
parent c6dd7830c9
commit 3232ad0de3

@ -1,14 +1,14 @@
package wechatminiprogram package wechatminiprogram
import ( import (
"bytes"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
"go.uber.org/zap" "go.uber.org/zap"
"gopkg.in/dtapps/go-library.v3/utils/gohttp"
"gorm.io/gorm" "gorm.io/gorm"
"io/ioutil"
"net/http" "net/http"
) )
@ -24,36 +24,25 @@ type App struct {
MDb *mongo.Client // 非关系数据库服务 MDb *mongo.Client // 非关系数据库服务
} }
func (app *App) request(url string, params map[string]interface{}, method string) (resp []byte, err error) { func (app *App) request(url string, params map[string]interface{}, method string) (resp gohttp.Response, err error) {
// 请求参数 switch method {
marshal, _ := json.Marshal(params) case http.MethodGet:
var req *http.Request get, err := gohttp.Get(url, params)
req, err = http.NewRequest(method, url, bytes.NewReader(marshal)) // 日志
if err != nil { if app.ZapLog != nil {
return nil, err app.ZapLog.Sugar().Info(fmt.Sprintf("wechatminiprogram %s %s %s", url, get.Header, get.Body))
}
return get, err
case http.MethodPost:
// 请求参数
paramsStr, err := json.Marshal(params)
postJson, err := gohttp.PostJson(url, paramsStr)
// 日志
if app.ZapLog != nil {
app.ZapLog.Sugar().Info(fmt.Sprintf("wechatminiprogram %s %s %s", url, postJson.Header, postJson.Body))
}
return postJson, err
default:
return resp, errors.New("请求类型不支持")
} }
httpClient := &http.Client{}
var response *http.Response
response, err = httpClient.Do(req)
if err != nil {
return nil, err
}
// 处理成功
defer response.Body.Close()
resp, err = ioutil.ReadAll(response.Body)
// 日志
if app.ZapLog != nil {
app.ZapLog.Sugar().Info(fmt.Sprintf("%s %s", url, resp))
}
// 检查请求错误
if response.StatusCode == 200 {
return resp, err
}
return nil, err
} }

@ -3,6 +3,7 @@ package wechatminiprogram
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"gopkg.in/dtapps/go-library.v3/utils/gohttp"
"net/http" "net/http"
) )
@ -13,11 +14,12 @@ type GetCallBackIpResponse struct {
type GetCallBackIpResult struct { type GetCallBackIpResult struct {
Result GetCallBackIpResponse // 结果 Result GetCallBackIpResponse // 结果
Body []byte // 内容 Body []byte // 内容
Http gohttp.Response // 请求
Err error // 错误 Err error // 错误
} }
func NewGetCallBackIpResult(result GetCallBackIpResponse, body []byte, err error) *GetCallBackIpResult { func NewGetCallBackIpResult(result GetCallBackIpResponse, body []byte, http gohttp.Response, err error) *GetCallBackIpResult {
return &GetCallBackIpResult{Result: result, Body: body, Err: err} return &GetCallBackIpResult{Result: result, Body: body, Http: http, Err: err}
} }
// GetCallBackIp 获取微信callback IP地址 // GetCallBackIp 获取微信callback IP地址
@ -25,9 +27,9 @@ func NewGetCallBackIpResult(result GetCallBackIpResponse, body []byte, err error
// 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 // 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 { func (app *App) GetCallBackIp() *GetCallBackIpResult {
// 请求 // 请求
body, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=%s", app.AccessToken), map[string]interface{}{}, http.MethodGet) request, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=%s", app.AccessToken), map[string]interface{}{}, http.MethodGet)
// 定义 // 定义
var response GetCallBackIpResponse var response GetCallBackIpResponse
err = json.Unmarshal(body, &response) err = json.Unmarshal(request.Body, &response)
return NewGetCallBackIpResult(response, body, err) return NewGetCallBackIpResult(response, request.Body, request, err)
} }

@ -3,19 +3,10 @@ package wechatminiprogram
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"gopkg.in/dtapps/go-library.v3/utils/gohttp"
"net/http" "net/http"
) )
// SubscribeMessageSend 入参
type SubscribeMessageSend struct {
Touser string `json:"touser"` // 接收者(用户)的 openid
TemplateId string `json:"template_id"` // 所需下发的订阅模板id
Page string `json:"page,omitempty"` // 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,示例index?foo=bar。该字段不填则模板无跳转。
Data map[string]interface{} `json:"data"` // 模板内容,格式形如 { "key1": { "value": any }, "key2": { "value": any } }
MiniprogramState string `json:"miniprogram_state,omitempty"` // 跳转小程序类型developer为开发版trial为体验版formal为正式版默认为正式版
Lang string `json:"lang,omitempty"` // 进入小程序查看”的语言类型支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文)默认为zh_CN
}
type SubscribeMessageSendResponse struct { type SubscribeMessageSendResponse struct {
Errcode int // 错误码 Errcode int // 错误码
Errmsg string // 错误信息 Errmsg string // 错误信息
@ -24,11 +15,12 @@ type SubscribeMessageSendResponse struct {
type SubscribeMessageSendResult struct { type SubscribeMessageSendResult struct {
Result SubscribeMessageSendResponse // 结果 Result SubscribeMessageSendResponse // 结果
Body []byte // 内容 Body []byte // 内容
Http gohttp.Response // 请求
Err error // 错误 Err error // 错误
} }
func NewSubscribeMessageSendResult(result SubscribeMessageSendResponse, body []byte, err error) *SubscribeMessageSendResult { func NewSubscribeMessageSendResult(result SubscribeMessageSendResponse, body []byte, http gohttp.Response, err error) *SubscribeMessageSendResult {
return &SubscribeMessageSendResult{Result: result, Body: body, Err: err} return &SubscribeMessageSendResult{Result: result, Body: body, Http: http, Err: err}
} }
// SubscribeMessageSend 发送订阅消息 // SubscribeMessageSend 发送订阅消息
@ -37,9 +29,9 @@ func (app *App) SubscribeMessageSend(notMustParams ...Params) *SubscribeMessageS
// 参数 // 参数
params := app.NewParamsWith(notMustParams...) params := app.NewParamsWith(notMustParams...)
// 请求 // 请求
body, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=%s", app.AccessToken), params, http.MethodPost) request, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=%s", app.AccessToken), params, http.MethodPost)
// 定义 // 定义
var response SubscribeMessageSendResponse var response SubscribeMessageSendResponse
err = json.Unmarshal(body, &response) err = json.Unmarshal(request.Body, &response)
return NewSubscribeMessageSendResult(response, body, err) return NewSubscribeMessageSendResult(response, request.Body, request, err)
} }

@ -3,6 +3,7 @@ package wechatminiprogram
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"gopkg.in/dtapps/go-library.v3/utils/gohttp"
"net/http" "net/http"
) )
@ -16,11 +17,12 @@ type AuthGetAccessTokenResponse struct {
type AuthGetAccessTokenResult struct { type AuthGetAccessTokenResult struct {
Result AuthGetAccessTokenResponse // 结果 Result AuthGetAccessTokenResponse // 结果
Body []byte // 内容 Body []byte // 内容
Http gohttp.Response // 请求
Err error // 错误 Err error // 错误
} }
func NewAuthGetAccessTokenResult(result AuthGetAccessTokenResponse, body []byte, err error) *AuthGetAccessTokenResult { func NewAuthGetAccessTokenResult(result AuthGetAccessTokenResponse, body []byte, http gohttp.Response, err error) *AuthGetAccessTokenResult {
return &AuthGetAccessTokenResult{Result: result, Body: body, Err: err} return &AuthGetAccessTokenResult{Result: result, Body: body, Http: http, Err: err}
} }
// AuthGetAccessToken // AuthGetAccessToken
@ -28,9 +30,9 @@ func NewAuthGetAccessTokenResult(result AuthGetAccessTokenResponse, body []byte,
// https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html // https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html
func (app *App) AuthGetAccessToken() *AuthGetAccessTokenResult { func (app *App) AuthGetAccessToken() *AuthGetAccessTokenResult {
// 请求 // 请求
body, 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 := 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)
// 定义 // 定义
var response AuthGetAccessTokenResponse var response AuthGetAccessTokenResponse
err = json.Unmarshal(body, &response) err = json.Unmarshal(request.Body, &response)
return NewAuthGetAccessTokenResult(response, body, err) return NewAuthGetAccessTokenResult(response, request.Body, request, err)
} }

@ -3,6 +3,7 @@ package wechatminiprogram
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"gopkg.in/dtapps/go-library.v3/utils/gohttp"
"net/http" "net/http"
) )
@ -16,11 +17,12 @@ type CgiBinWxaAppCreateWxaQrCodeResponse struct {
type CgiBinWxaAppCreateWxaQrCodeResult struct { type CgiBinWxaAppCreateWxaQrCodeResult struct {
Result CgiBinWxaAppCreateWxaQrCodeResponse // 结果 Result CgiBinWxaAppCreateWxaQrCodeResponse // 结果
Body []byte // 内容 Body []byte // 内容
Http gohttp.Response // 请求
Err error // 错误 Err error // 错误
} }
func NewCgiBinWxaAppCreateWxaQrCodeResult(result CgiBinWxaAppCreateWxaQrCodeResponse, body []byte, err error) *CgiBinWxaAppCreateWxaQrCodeResult { func NewCgiBinWxaAppCreateWxaQrCodeResult(result CgiBinWxaAppCreateWxaQrCodeResponse, body []byte, http gohttp.Response, err error) *CgiBinWxaAppCreateWxaQrCodeResult {
return &CgiBinWxaAppCreateWxaQrCodeResult{Result: result, Body: body, Err: err} return &CgiBinWxaAppCreateWxaQrCodeResult{Result: result, Body: body, Http: http, Err: err}
} }
// CgiBinWxaAppCreateWxaQrCode 获取小程序二维码,适用于需要的码数量较少的业务场景。通过该接口生成的小程序码,永久有效,有数量限制 // CgiBinWxaAppCreateWxaQrCode 获取小程序二维码,适用于需要的码数量较少的业务场景。通过该接口生成的小程序码,永久有效,有数量限制
@ -32,9 +34,9 @@ func (app *App) CgiBinWxaAppCreateWxaQrCode(path string, width int) *CgiBinWxaAp
param.Set("width", width) param.Set("width", width)
params := app.NewParamsWith(param) params := app.NewParamsWith(param)
// 请求 // 请求
body, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=%s", app.AccessToken), params, http.MethodPost) request, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=%s", app.AccessToken), params, http.MethodPost)
// 定义 // 定义
var response CgiBinWxaAppCreateWxaQrCodeResponse var response CgiBinWxaAppCreateWxaQrCodeResponse
err = json.Unmarshal(body, &response) err = json.Unmarshal(request.Body, &response)
return NewCgiBinWxaAppCreateWxaQrCodeResult(response, body, err) return NewCgiBinWxaAppCreateWxaQrCodeResult(response, request.Body, request, err)
} }

@ -3,6 +3,7 @@ package wechatminiprogram
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"gopkg.in/dtapps/go-library.v3/utils/gohttp"
"net/http" "net/http"
) )
@ -17,18 +18,19 @@ type AuthCode2SessionResponse struct {
type AuthCode2SessionResult struct { type AuthCode2SessionResult struct {
Result AuthCode2SessionResponse // 结果 Result AuthCode2SessionResponse // 结果
Body []byte // 内容 Body []byte // 内容
Http gohttp.Response // 请求
Err error // 错误 Err error // 错误
} }
func NewAuthCode2SessionResult(result AuthCode2SessionResponse, body []byte, err error) *AuthCode2SessionResult { func NewAuthCode2SessionResult(result AuthCode2SessionResponse, body []byte, http gohttp.Response, err error) *AuthCode2SessionResult {
return &AuthCode2SessionResult{Result: result, Body: body, Err: err} return &AuthCode2SessionResult{Result: result, Body: body, Http: http, Err: err}
} }
func (app *App) AuthCode2Session(jsCode string) *AuthCode2SessionResult { func (app *App) AuthCode2Session(jsCode string) *AuthCode2SessionResult {
// 请求 // 请求
body, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code", app.AppId, app.AppSecret, jsCode), map[string]interface{}{}, http.MethodGet) request, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code", app.AppId, app.AppSecret, jsCode), map[string]interface{}{}, http.MethodGet)
// 定义 // 定义
var response AuthCode2SessionResponse var response AuthCode2SessionResponse
err = json.Unmarshal(body, &response) err = json.Unmarshal(request.Body, &response)
return NewAuthCode2SessionResult(response, body, err) return NewAuthCode2SessionResult(response, request.Body, request, err)
} }

@ -3,6 +3,7 @@ package wechatminiprogram
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"gopkg.in/dtapps/go-library.v3/utils/gohttp"
"net/http" "net/http"
) )
@ -44,11 +45,12 @@ type BusinessGetLiveInfoResponse struct {
type BusinessGetLiveInfoResult struct { type BusinessGetLiveInfoResult struct {
Result BusinessGetLiveInfoResponse // 结果 Result BusinessGetLiveInfoResponse // 结果
Body []byte // 内容 Body []byte // 内容
Http gohttp.Response // 请求
Err error // 错误 Err error // 错误
} }
func NewBusinessGetLiveInfoResult(result BusinessGetLiveInfoResponse, body []byte, err error) *BusinessGetLiveInfoResult { func NewBusinessGetLiveInfoResult(result BusinessGetLiveInfoResponse, body []byte, http gohttp.Response, err error) *BusinessGetLiveInfoResult {
return &BusinessGetLiveInfoResult{Result: result, Body: body, Err: err} return &BusinessGetLiveInfoResult{Result: result, Body: body, Http: http, Err: err}
} }
// BusinessGetLiveInfo 获取直播间列表 // BusinessGetLiveInfo 获取直播间列表
@ -58,9 +60,9 @@ func (app *App) BusinessGetLiveInfo(notMustParams ...Params) *BusinessGetLiveInf
// 参数 // 参数
params := app.NewParamsWith(notMustParams...) params := app.NewParamsWith(notMustParams...)
// 请求 // 请求
body, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/wxa/business/getliveinfo?access_token=%s", app.AccessToken), params, http.MethodPost) request, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/wxa/business/getliveinfo?access_token=%s", app.AccessToken), params, http.MethodPost)
// 定义 // 定义
var response BusinessGetLiveInfoResponse var response BusinessGetLiveInfoResponse
err = json.Unmarshal(body, &response) err = json.Unmarshal(request.Body, &response)
return NewBusinessGetLiveInfoResult(response, body, err) return NewBusinessGetLiveInfoResult(response, request.Body, request, err)
} }

@ -3,6 +3,7 @@ package wechatminiprogram
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"gopkg.in/dtapps/go-library.v3/utils/gohttp"
"net/http" "net/http"
) )
@ -15,11 +16,12 @@ type WxaGenerateUrlLinkResponse struct {
type WxaGenerateUrlLinkResult struct { type WxaGenerateUrlLinkResult struct {
Result WxaGenerateUrlLinkResponse // 结果 Result WxaGenerateUrlLinkResponse // 结果
Body []byte // 内容 Body []byte // 内容
Http gohttp.Response // 请求
Err error // 错误 Err error // 错误
} }
func NewWxaGenerateUrlLinkResult(result WxaGenerateUrlLinkResponse, body []byte, err error) *WxaGenerateUrlLinkResult { func NewWxaGenerateUrlLinkResult(result WxaGenerateUrlLinkResponse, body []byte, http gohttp.Response, err error) *WxaGenerateUrlLinkResult {
return &WxaGenerateUrlLinkResult{Result: result, Body: body, Err: err} return &WxaGenerateUrlLinkResult{Result: result, Body: body, Http: http, Err: err}
} }
// WxaGenerateUrlLink 获取小程序 URL Link适用于短信、邮件、网页、微信内等拉起小程序的业务场景。通过该接口可以选择生成到期失效和永久有效的小程序链接有数量限制目前仅针对国内非个人主体的小程序开放 // WxaGenerateUrlLink 获取小程序 URL Link适用于短信、邮件、网页、微信内等拉起小程序的业务场景。通过该接口可以选择生成到期失效和永久有效的小程序链接有数量限制目前仅针对国内非个人主体的小程序开放
@ -28,9 +30,9 @@ func (app *App) WxaGenerateUrlLink(notMustParams ...Params) *WxaGenerateUrlLinkR
// 参数 // 参数
params := app.NewParamsWith(notMustParams...) params := app.NewParamsWith(notMustParams...)
// 请求 // 请求
body, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/wxa/generate_urllink?access_token=%s", app.AccessToken), params, http.MethodPost) request, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/wxa/generate_urllink?access_token=%s", app.AccessToken), params, http.MethodPost)
// 定义 // 定义
var response WxaGenerateUrlLinkResponse var response WxaGenerateUrlLinkResponse
err = json.Unmarshal(body, &response) err = json.Unmarshal(request.Body, &response)
return NewWxaGenerateUrlLinkResult(response, body, err) return NewWxaGenerateUrlLinkResult(response, request.Body, request, err)
} }

@ -3,6 +3,7 @@ package wechatminiprogram
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"gopkg.in/dtapps/go-library.v3/utils/gohttp"
"net/http" "net/http"
) )
@ -15,11 +16,12 @@ type WxaGenerateSchemeResponse struct {
type WxaGenerateSchemeResult struct { type WxaGenerateSchemeResult struct {
Result WxaGenerateSchemeResponse // 结果 Result WxaGenerateSchemeResponse // 结果
Body []byte // 内容 Body []byte // 内容
Http gohttp.Response // 请求
Err error // 错误 Err error // 错误
} }
func NewWxaGenerateSchemeResult(result WxaGenerateSchemeResponse, body []byte, err error) *WxaGenerateSchemeResult { func NewWxaGenerateSchemeResult(result WxaGenerateSchemeResponse, body []byte, http gohttp.Response, err error) *WxaGenerateSchemeResult {
return &WxaGenerateSchemeResult{Result: result, Body: body, Err: err} return &WxaGenerateSchemeResult{Result: result, Body: body, Http: http, Err: err}
} }
// WxaGenerateScheme 获取小程序 scheme 码,适用于短信、邮件、外部网页、微信内等拉起小程序的业务场景。通过该接口,可以选择生成到期失效和永久有效的小程序码,有数量限制,目前仅针对国内非个人主体的小程序开放 // WxaGenerateScheme 获取小程序 scheme 码,适用于短信、邮件、外部网页、微信内等拉起小程序的业务场景。通过该接口,可以选择生成到期失效和永久有效的小程序码,有数量限制,目前仅针对国内非个人主体的小程序开放
@ -28,9 +30,9 @@ func (app *App) WxaGenerateScheme(notMustParams ...Params) *WxaGenerateSchemeRes
// 参数 // 参数
params := app.NewParamsWith(notMustParams...) params := app.NewParamsWith(notMustParams...)
// 请求 // 请求
body, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/wxa/generatescheme?access_token=%s", app.AccessToken), params, http.MethodPost) request, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/wxa/generatescheme?access_token=%s", app.AccessToken), params, http.MethodPost)
// 定义 // 定义
var response WxaGenerateSchemeResponse var response WxaGenerateSchemeResponse
err = json.Unmarshal(body, &response) err = json.Unmarshal(request.Body, &response)
return NewWxaGenerateSchemeResult(response, body, err) return NewWxaGenerateSchemeResult(response, request.Body, request, err)
} }

@ -3,6 +3,7 @@ package wechatminiprogram
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"gopkg.in/dtapps/go-library.v3/utils/gohttp"
"net/http" "net/http"
) )
@ -16,11 +17,12 @@ type WxaGetWxaCodeResponse struct {
type WxaGetWxaCodeResult struct { type WxaGetWxaCodeResult struct {
Result WxaGetWxaCodeResponse // 结果 Result WxaGetWxaCodeResponse // 结果
Body []byte // 内容 Body []byte // 内容
Http gohttp.Response // 请求
Err error // 错误 Err error // 错误
} }
func NewWxaGetWxaCodeResult(result WxaGetWxaCodeResponse, body []byte, err error) *WxaGetWxaCodeResult { func NewWxaGetWxaCodeResult(result WxaGetWxaCodeResponse, body []byte, http gohttp.Response, err error) *WxaGetWxaCodeResult {
return &WxaGetWxaCodeResult{Result: result, Body: body, Err: err} return &WxaGetWxaCodeResult{Result: result, Body: body, Http: http, Err: err}
} }
// WxaGetWxaCode 获取小程序码,适用于需要的码数量较少的业务场景。通过该接口生成的小程序码,永久有效,有数量限制 // WxaGetWxaCode 获取小程序码,适用于需要的码数量较少的业务场景。通过该接口生成的小程序码,永久有效,有数量限制
@ -29,9 +31,9 @@ func (app *App) WxaGetWxaCode(notMustParams ...Params) *WxaGetWxaCodeResult {
// 参数 // 参数
params := app.NewParamsWith(notMustParams...) params := app.NewParamsWith(notMustParams...)
// 请求 // 请求
body, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/wxa/getwxacode?access_token=%s", app.AccessToken), params, http.MethodPost) request, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/wxa/getwxacode?access_token=%s", app.AccessToken), params, http.MethodPost)
// 定义 // 定义
var response WxaGetWxaCodeResponse var response WxaGetWxaCodeResponse
err = json.Unmarshal(body, &response) err = json.Unmarshal(request.Body, &response)
return NewWxaGetWxaCodeResult(response, body, err) return NewWxaGetWxaCodeResult(response, request.Body, request, err)
} }

@ -3,6 +3,7 @@ package wechatminiprogram
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"gopkg.in/dtapps/go-library.v3/utils/gohttp"
"net/http" "net/http"
) )
@ -16,11 +17,12 @@ type WxaGetWxaCodeUnLimitResponse struct {
type WxaGetWxaCodeUnLimitResult struct { type WxaGetWxaCodeUnLimitResult struct {
Result WxaGetWxaCodeUnLimitResponse // 结果 Result WxaGetWxaCodeUnLimitResponse // 结果
Body []byte // 内容 Body []byte // 内容
Http gohttp.Response // 请求
Err error // 错误 Err error // 错误
} }
func NewWxaGetWxaCodeUnLimitResult(result WxaGetWxaCodeUnLimitResponse, body []byte, err error) *WxaGetWxaCodeUnLimitResult { func NewWxaGetWxaCodeUnLimitResult(result WxaGetWxaCodeUnLimitResponse, body []byte, http gohttp.Response, err error) *WxaGetWxaCodeUnLimitResult {
return &WxaGetWxaCodeUnLimitResult{Result: result, Body: body, Err: err} return &WxaGetWxaCodeUnLimitResult{Result: result, Body: body, Http: http, Err: err}
} }
// WxaGetWxaCodeUnLimit 获取小程序码,适用于需要的码数量极多的业务场景。通过该接口生成的小程序码,永久有效,数量暂无限制 // WxaGetWxaCodeUnLimit 获取小程序码,适用于需要的码数量极多的业务场景。通过该接口生成的小程序码,永久有效,数量暂无限制
@ -29,9 +31,9 @@ func (app *App) WxaGetWxaCodeUnLimit(notMustParams ...Params) *WxaGetWxaCodeUnLi
// 参数 // 参数
params := app.NewParamsWith(notMustParams...) params := app.NewParamsWith(notMustParams...)
// 请求 // 请求
body, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=%s", app.AccessToken), params, http.MethodPost) request, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=%s", app.AccessToken), params, http.MethodPost)
// 定义 // 定义
var response WxaGetWxaCodeUnLimitResponse var response WxaGetWxaCodeUnLimitResponse
err = json.Unmarshal(body, &response) err = json.Unmarshal(request.Body, &response)
return NewWxaGetWxaCodeUnLimitResult(response, body, err) return NewWxaGetWxaCodeUnLimitResult(response, request.Body, request, err)
} }

@ -3,6 +3,7 @@ package wechatminiprogram
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"gopkg.in/dtapps/go-library.v3/utils/gohttp"
"net/http" "net/http"
) )
@ -33,11 +34,12 @@ type WxaQueryUrlLinkResponse struct {
type WxaQueryUrlLinkResult struct { type WxaQueryUrlLinkResult struct {
Result WxaQueryUrlLinkResponse // 结果 Result WxaQueryUrlLinkResponse // 结果
Body []byte // 内容 Body []byte // 内容
Http gohttp.Response // 请求
Err error // 错误 Err error // 错误
} }
func NewWxaQueryUrlLinkResult(result WxaQueryUrlLinkResponse, body []byte, err error) *WxaQueryUrlLinkResult { func NewWxaQueryUrlLinkResult(result WxaQueryUrlLinkResponse, body []byte, http gohttp.Response, err error) *WxaQueryUrlLinkResult {
return &WxaQueryUrlLinkResult{Result: result, Body: body, Err: err} return &WxaQueryUrlLinkResult{Result: result, Body: body, Http: http, Err: err}
} }
// WxaQueryUrlLink 查询小程序 url_link 配置,及长期有效 quota // WxaQueryUrlLink 查询小程序 url_link 配置,及长期有效 quota
@ -48,9 +50,9 @@ func (app *App) WxaQueryUrlLink(urlLink string) *WxaQueryUrlLinkResult {
param.Set("url_link", urlLink) param.Set("url_link", urlLink)
params := app.NewParamsWith(param) params := app.NewParamsWith(param)
// 请求 // 请求
body, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/wxa/query_urllink?access_token=%s", app.AccessToken), params, http.MethodPost) request, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/wxa/query_urllink?access_token=%s", app.AccessToken), params, http.MethodPost)
// 定义 // 定义
var response WxaQueryUrlLinkResponse var response WxaQueryUrlLinkResponse
err = json.Unmarshal(body, &response) err = json.Unmarshal(request.Body, &response)
return NewWxaQueryUrlLinkResult(response, body, err) return NewWxaQueryUrlLinkResult(response, request.Body, request, err)
} }

@ -3,18 +3,47 @@ package wechatminiprogram
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"gopkg.in/dtapps/go-library.v3/utils/gohttp"
"net/http" "net/http"
) )
type WxaQuerySchemeResponse struct {
Errcode int `json:"errcode"`
Errmsg string `json:"errmsg"`
SchemeInfo struct {
Appid string `json:"appid"`
Path string `json:"path"`
Query string `json:"query"`
CreateTime int `json:"create_time"`
ExpireTime int `json:"expire_time"`
EnvVersion string `json:"env_version"`
} `json:"scheme_info"`
SchemeQuota struct {
LongTimeUsed int `json:"long_time_used"`
LongTimeLimit int `json:"long_time_limit"`
} `json:"scheme_quota"`
}
type WxaQuerySchemeResult struct {
Result WxaQuerySchemeResponse // 结果
Body []byte // 内容
Http gohttp.Response // 请求
Err error // 错误
}
func NewWxaQuerySchemeResult(result WxaQuerySchemeResponse, body []byte, http gohttp.Response, err error) *WxaQuerySchemeResult {
return &WxaQuerySchemeResult{Result: result, Body: body, Http: http, Err: err}
}
// WxaQueryScheme 查询小程序 scheme 码,及长期有效 quota // WxaQueryScheme 查询小程序 scheme 码,及长期有效 quota
// https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/url-scheme/urlscheme.query.html // https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/url-scheme/urlscheme.query.html
func (app *App) WxaQueryScheme(notMustParams ...Params) *BusinessGetLiveInfoResult { func (app *App) WxaQueryScheme(notMustParams ...Params) *WxaQuerySchemeResult {
// 参数 // 参数
params := app.NewParamsWith(notMustParams...) params := app.NewParamsWith(notMustParams...)
// 请求 // 请求
body, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/wxa/queryscheme?access_token=%s", app.AccessToken), params, http.MethodPost) request, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/wxa/queryscheme?access_token=%s", app.AccessToken), params, http.MethodPost)
// 定义 // 定义
var response BusinessGetLiveInfoResponse var response WxaQuerySchemeResponse
err = json.Unmarshal(body, &response) err = json.Unmarshal(request.Body, &response)
return NewBusinessGetLiveInfoResult(response, body, err) return NewWxaQuerySchemeResult(response, request.Body, request, err)
} }

@ -2,6 +2,7 @@ package wechatunion
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
@ -22,21 +23,24 @@ type App struct {
} }
func (app *App) request(url string, params map[string]interface{}, method string) (resp []byte, err error) { func (app *App) request(url string, params map[string]interface{}, method string) (resp []byte, err error) {
if method == http.MethodGet { switch method {
case http.MethodGet:
get, err := gohttp.Get(url, params) get, err := gohttp.Get(url, params)
// 日志 // 日志
if app.ZapLog != nil { if app.ZapLog != nil {
app.ZapLog.Sugar().Info(fmt.Sprintf("%s %s %s", url, get.Header, get.Body)) app.ZapLog.Sugar().Info(fmt.Sprintf("wechatunion %s %s %s", url, get.Header, get.Body))
} }
return get.Body, err return get.Body, err
} else { case http.MethodPost:
// 请求参数 // 请求参数
paramsStr, err := json.Marshal(params) paramsStr, err := json.Marshal(params)
postJson, err := gohttp.PostJson(url, paramsStr) postJson, err := gohttp.PostJson(url, paramsStr)
// 日志 // 日志
if app.ZapLog != nil { if app.ZapLog != nil {
app.ZapLog.Sugar().Info(fmt.Sprintf("%s %s %s", url, postJson.Header, postJson.Body)) app.ZapLog.Sugar().Info(fmt.Sprintf("wechatunion %s %s %s", url, postJson.Header, postJson.Body))
} }
return postJson.Body, err return postJson.Body, err
default:
return nil, errors.New("请求类型不支持")
} }
} }

Loading…
Cancel
Save