Skip to content

Commit 1138a7d

Browse files
authored
feat:追加动态消息接口 (#775)
* 小程序发货信息管理 * fix golang lint * fix miss * fix lint * fix lint * fix lint * 修复查询参数last_index=“”时异常 * 小程序消息推送 * fix lint errors * fix lint * fix lint * fix lint, * 简化写法 * fix 简化写法 * fix name in comments * Update miniprogram.go * add events * change GoodsInfo type * change statements to 50 * 追加xml支持 * fix cl lint * 追加动态消息 * 删除多余空格 * 修复命名 * 修复createActivityId返回值类型
1 parent 1842f08 commit 1138a7d

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

miniprogram/message/updatable_msg.go

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package message
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/silenceper/wechat/v2/miniprogram/context"
7+
"github.com/silenceper/wechat/v2/util"
8+
)
9+
10+
const (
11+
// createActivityURL 创建activity_id
12+
createActivityURL = "https://api.weixin.qq.com/cgi-bin/message/wxopen/activityid/create?access_token=%s"
13+
// SendUpdatableMsgURL 修改动态消息
14+
setUpdatableMsgURL = "https://api.weixin.qq.com/cgi-bin/message/wxopen/updatablemsg/send?access_token=%s"
15+
)
16+
17+
// UpdatableTargetState 动态消息状态
18+
type UpdatableTargetState int
19+
20+
const (
21+
// TargetStateNotStarted 未开始
22+
TargetStateNotStarted UpdatableTargetState = 0
23+
// TargetStateStarted 已开始
24+
TargetStateStarted UpdatableTargetState = 1
25+
// TargetStateFinished 已结束
26+
TargetStateFinished UpdatableTargetState = 2
27+
)
28+
29+
// UpdatableMessage 动态消息
30+
type UpdatableMessage struct {
31+
*context.Context
32+
}
33+
34+
// NewUpdatableMessage 实例化
35+
func NewUpdatableMessage(ctx *context.Context) *UpdatableMessage {
36+
return &UpdatableMessage{
37+
Context: ctx,
38+
}
39+
}
40+
41+
// CreateActivityID 创建activity_id
42+
func (updatableMessage *UpdatableMessage) CreateActivityID() (res CreateActivityIDResponse, err error) {
43+
accessToken, err := updatableMessage.GetAccessToken()
44+
if err != nil {
45+
return
46+
}
47+
48+
uri := fmt.Sprintf(createActivityURL, accessToken)
49+
response, err := util.HTTPGet(uri)
50+
if err != nil {
51+
return
52+
}
53+
err = util.DecodeWithError(response, &res, "CreateActivityID")
54+
return
55+
}
56+
57+
// SetUpdatableMsg 修改动态消息
58+
func (updatableMessage *UpdatableMessage) SetUpdatableMsg(activityID string, targetState UpdatableTargetState, template UpdatableMsgTemplate) (err error) {
59+
accessToken, err := updatableMessage.GetAccessToken()
60+
if err != nil {
61+
return
62+
}
63+
64+
uri := fmt.Sprintf(setUpdatableMsgURL, accessToken)
65+
data := SendUpdatableMsgReq{
66+
ActivityID: activityID,
67+
TargetState: targetState,
68+
TemplateInfo: template,
69+
}
70+
71+
response, err := util.PostJSON(uri, data)
72+
if err != nil {
73+
return
74+
}
75+
return util.DecodeWithCommonError(response, "SendUpdatableMsg")
76+
}
77+
78+
// CreateActivityIDResponse 创建activity_id 返回
79+
type CreateActivityIDResponse struct {
80+
util.CommonError
81+
82+
ActivityID string `json:"activity_id"`
83+
ExpirationTime int64 `json:"expiration_time"`
84+
}
85+
86+
// UpdatableMsgTemplate 动态消息模板
87+
type UpdatableMsgTemplate struct {
88+
ParameterList []UpdatableMsgParameter `json:"parameter_list"`
89+
}
90+
91+
// UpdatableMsgParameter 动态消息参数
92+
type UpdatableMsgParameter struct {
93+
Name string `json:"name"`
94+
Value string `json:"value"`
95+
}
96+
97+
// SendUpdatableMsgReq 修改动态消息参数
98+
type SendUpdatableMsgReq struct {
99+
ActivityID string `json:"activity_id"`
100+
TemplateInfo UpdatableMsgTemplate `json:"template_info"`
101+
TargetState UpdatableTargetState `json:"target_state"`
102+
}

miniprogram/miniprogram.go

+5
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,8 @@ func (miniProgram *MiniProgram) GetMiniDrama() *minidrama.MiniDrama {
161161
func (miniProgram *MiniProgram) GetRedPacketCover() *redpacketcover.RedPacketCover {
162162
return redpacketcover.NewRedPacketCover(miniProgram.ctx)
163163
}
164+
165+
// GetUpdatableMessage 小程序动态消息
166+
func (miniProgram *MiniProgram) GetUpdatableMessage() *message.UpdatableMessage {
167+
return message.NewUpdatableMessage(miniProgram.ctx)
168+
}

0 commit comments

Comments
 (0)