From 1138a7db0eb6a619782017092851878cee33abfb Mon Sep 17 00:00:00 2001 From: ccfish Date: Tue, 23 Apr 2024 10:03:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E8=BF=BD=E5=8A=A0=E5=8A=A8=E6=80=81?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E6=8E=A5=E5=8F=A3=20(#775)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 小程序发货信息管理 * 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返回值类型 --- miniprogram/message/updatable_msg.go | 102 +++++++++++++++++++++++++++ miniprogram/miniprogram.go | 5 ++ 2 files changed, 107 insertions(+) create mode 100644 miniprogram/message/updatable_msg.go diff --git a/miniprogram/message/updatable_msg.go b/miniprogram/message/updatable_msg.go new file mode 100644 index 00000000..cf5cdd18 --- /dev/null +++ b/miniprogram/message/updatable_msg.go @@ -0,0 +1,102 @@ +package message + +import ( + "fmt" + + "github.com/silenceper/wechat/v2/miniprogram/context" + "github.com/silenceper/wechat/v2/util" +) + +const ( + // createActivityURL 创建activity_id + createActivityURL = "https://api.weixin.qq.com/cgi-bin/message/wxopen/activityid/create?access_token=%s" + // SendUpdatableMsgURL 修改动态消息 + setUpdatableMsgURL = "https://api.weixin.qq.com/cgi-bin/message/wxopen/updatablemsg/send?access_token=%s" +) + +// UpdatableTargetState 动态消息状态 +type UpdatableTargetState int + +const ( + // TargetStateNotStarted 未开始 + TargetStateNotStarted UpdatableTargetState = 0 + // TargetStateStarted 已开始 + TargetStateStarted UpdatableTargetState = 1 + // TargetStateFinished 已结束 + TargetStateFinished UpdatableTargetState = 2 +) + +// UpdatableMessage 动态消息 +type UpdatableMessage struct { + *context.Context +} + +// NewUpdatableMessage 实例化 +func NewUpdatableMessage(ctx *context.Context) *UpdatableMessage { + return &UpdatableMessage{ + Context: ctx, + } +} + +// CreateActivityID 创建activity_id +func (updatableMessage *UpdatableMessage) CreateActivityID() (res CreateActivityIDResponse, err error) { + accessToken, err := updatableMessage.GetAccessToken() + if err != nil { + return + } + + uri := fmt.Sprintf(createActivityURL, accessToken) + response, err := util.HTTPGet(uri) + if err != nil { + return + } + err = util.DecodeWithError(response, &res, "CreateActivityID") + return +} + +// SetUpdatableMsg 修改动态消息 +func (updatableMessage *UpdatableMessage) SetUpdatableMsg(activityID string, targetState UpdatableTargetState, template UpdatableMsgTemplate) (err error) { + accessToken, err := updatableMessage.GetAccessToken() + if err != nil { + return + } + + uri := fmt.Sprintf(setUpdatableMsgURL, accessToken) + data := SendUpdatableMsgReq{ + ActivityID: activityID, + TargetState: targetState, + TemplateInfo: template, + } + + response, err := util.PostJSON(uri, data) + if err != nil { + return + } + return util.DecodeWithCommonError(response, "SendUpdatableMsg") +} + +// CreateActivityIDResponse 创建activity_id 返回 +type CreateActivityIDResponse struct { + util.CommonError + + ActivityID string `json:"activity_id"` + ExpirationTime int64 `json:"expiration_time"` +} + +// UpdatableMsgTemplate 动态消息模板 +type UpdatableMsgTemplate struct { + ParameterList []UpdatableMsgParameter `json:"parameter_list"` +} + +// UpdatableMsgParameter 动态消息参数 +type UpdatableMsgParameter struct { + Name string `json:"name"` + Value string `json:"value"` +} + +// SendUpdatableMsgReq 修改动态消息参数 +type SendUpdatableMsgReq struct { + ActivityID string `json:"activity_id"` + TemplateInfo UpdatableMsgTemplate `json:"template_info"` + TargetState UpdatableTargetState `json:"target_state"` +} diff --git a/miniprogram/miniprogram.go b/miniprogram/miniprogram.go index 29889010..8adbb00d 100644 --- a/miniprogram/miniprogram.go +++ b/miniprogram/miniprogram.go @@ -161,3 +161,8 @@ func (miniProgram *MiniProgram) GetMiniDrama() *minidrama.MiniDrama { func (miniProgram *MiniProgram) GetRedPacketCover() *redpacketcover.RedPacketCover { return redpacketcover.NewRedPacketCover(miniProgram.ctx) } + +// GetUpdatableMessage 小程序动态消息 +func (miniProgram *MiniProgram) GetUpdatableMessage() *message.UpdatableMessage { + return message.NewUpdatableMessage(miniProgram.ctx) +}