mirror of
https://github.com/silenceper/wechat.git
synced 2026-02-23 13:42:25 +08:00
ADD 获取红包封面
This commit is contained in:
@@ -15,6 +15,7 @@ import (
|
|||||||
"github.com/silenceper/wechat/v2/miniprogram/order"
|
"github.com/silenceper/wechat/v2/miniprogram/order"
|
||||||
"github.com/silenceper/wechat/v2/miniprogram/privacy"
|
"github.com/silenceper/wechat/v2/miniprogram/privacy"
|
||||||
"github.com/silenceper/wechat/v2/miniprogram/qrcode"
|
"github.com/silenceper/wechat/v2/miniprogram/qrcode"
|
||||||
|
"github.com/silenceper/wechat/v2/miniprogram/redpacketcover"
|
||||||
"github.com/silenceper/wechat/v2/miniprogram/riskcontrol"
|
"github.com/silenceper/wechat/v2/miniprogram/riskcontrol"
|
||||||
"github.com/silenceper/wechat/v2/miniprogram/security"
|
"github.com/silenceper/wechat/v2/miniprogram/security"
|
||||||
"github.com/silenceper/wechat/v2/miniprogram/shortlink"
|
"github.com/silenceper/wechat/v2/miniprogram/shortlink"
|
||||||
@@ -155,3 +156,8 @@ func (miniProgram *MiniProgram) GetShipping() *order.Shipping {
|
|||||||
func (miniProgram *MiniProgram) GetMiniDrama() *minidrama.MiniDrama {
|
func (miniProgram *MiniProgram) GetMiniDrama() *minidrama.MiniDrama {
|
||||||
return minidrama.NewMiniDrama(miniProgram.ctx)
|
return minidrama.NewMiniDrama(miniProgram.ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetRedPacketCover 小程序微信红包封面 API
|
||||||
|
func (miniProgram *MiniProgram) GetRedPacketCover() *redpacketcover.RedPacketCover {
|
||||||
|
return redpacketcover.NewRedPacketCover(miniProgram.ctx)
|
||||||
|
}
|
||||||
|
|||||||
69
miniprogram/redpacketcover/redpacketcover.go
Normal file
69
miniprogram/redpacketcover/redpacketcover.go
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
package redpacketcover
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/silenceper/wechat/v2/miniprogram/context"
|
||||||
|
"github.com/silenceper/wechat/v2/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
getRedPacketCoverUrl = "https://api.weixin.qq.com/redpacketcover/wxapp/cover_url/get_by_token?access_token=%s"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RedPacketCover struct
|
||||||
|
type RedPacketCover struct {
|
||||||
|
*context.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewRedPacketCover 实例
|
||||||
|
func NewRedPacketCover(context *context.Context) *RedPacketCover {
|
||||||
|
redPacketCover := new(RedPacketCover)
|
||||||
|
redPacketCover.Context = context
|
||||||
|
return redPacketCover
|
||||||
|
}
|
||||||
|
|
||||||
|
// CoverParma 小程序码参数
|
||||||
|
type CoverParma struct {
|
||||||
|
// openid 可领取用户的openid
|
||||||
|
OpenID string `json:"openid"`
|
||||||
|
// ctoken 在红包封面平台获取发放ctoken(需要指定可以发放的appid)
|
||||||
|
CToken string `json:"ctoken"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// fetchCode 请求并返回二维码二进制数据
|
||||||
|
func (qrCode *RedPacketCover) fetchCode(urlStr string, body interface{}) (response []byte, err error) {
|
||||||
|
var accessToken string
|
||||||
|
accessToken, err = qrCode.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
urlStr = fmt.Sprintf(urlStr, accessToken)
|
||||||
|
var contentType string
|
||||||
|
response, contentType, err = util.PostJSONWithRespContentType(urlStr, body)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(contentType, "application/json") {
|
||||||
|
// 返回错误信息
|
||||||
|
var result util.CommonError
|
||||||
|
err = json.Unmarshal(response, &result)
|
||||||
|
if err == nil && result.ErrCode != 0 {
|
||||||
|
err = fmt.Errorf("error : errcode=%v , errmsg=%v", result.ErrCode, result.ErrMsg)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return response, nil
|
||||||
|
}
|
||||||
|
err = fmt.Errorf("error : unknown response content type - %v", contentType)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetRedPacketCoverUrl 获得指定用户可以领取的红包封面链接。获取参数ctoken参考微信红包封面开放平台
|
||||||
|
// 文档地址: https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/red-packet-cover/getRedPacketCoverUrl.html
|
||||||
|
func (qrCode *RedPacketCover) GetRedPacketCoverUrl(coderParams CoverParma) (response []byte, err error) {
|
||||||
|
return qrCode.fetchCode(getRedPacketCoverUrl, coderParams)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user