github.com/chanxuehong/wechat@v0.0.0-20230222024006-36f0325263cd/mp/menu/menu.go (about)

     1  package menu
     2  
     3  const (
     4  	// 下面6个类型(包括view类型)的按钮是在公众平台官网发布的菜单按钮类型
     5  	ButtonTypeText  = "text"
     6  	ButtonTypeImage = "img"
     7  	ButtonTypePhoto = "photo"
     8  	ButtonTypeVideo = "video"
     9  	ButtonTypeVoice = "voice"
    10  
    11  	// 上面5个类型的按钮不能通过API设置
    12  
    13  	ButtonTypeView        = "view"        // 跳转URL
    14  	ButtonTypeClick       = "click"       // 点击推事件
    15  	ButtonTypeMiniProgram = "miniprogram" // 小程序
    16  
    17  	// 下面的按钮类型仅支持微信 iPhone5.4.1 以上版本, 和 Android5.4 以上版本的微信用户,
    18  	// 旧版本微信用户点击后将没有回应, 开发者也不能正常接收到事件推送.
    19  	ButtonTypeScanCodePush    = "scancode_push"      // 扫码推事件
    20  	ButtonTypeScanCodeWaitMsg = "scancode_waitmsg"   // 扫码带提示
    21  	ButtonTypePicSysPhoto     = "pic_sysphoto"       // 系统拍照发图
    22  	ButtonTypePicPhotoOrAlbum = "pic_photo_or_album" // 拍照或者相册发图
    23  	ButtonTypePicWeixin       = "pic_weixin"         // 微信相册发图
    24  	ButtonTypeLocationSelect  = "location_select"    // 发送位置
    25  
    26  	// 下面的按钮类型专门给第三方平台旗下未微信认证(具体而言, 是资质认证未通过)的订阅号准备的事件类型,
    27  	// 它们是没有事件推送的, 能力相对受限, 其他类型的公众号不必使用.
    28  	ButtonTypeMediaId     = "media_id"     // 下发消息
    29  	ButtonTypeViewLimited = "view_limited" // 跳转图文消息URL
    30  )
    31  
    32  type Menu struct {
    33  	Buttons   []Button   `json:"button,omitempty"`
    34  	MatchRule *MatchRule `json:"matchrule,omitempty"`
    35  	MenuId    int64      `json:"menuid,omitempty"` // 有个性化菜单时查询接口返回值包含这个字段
    36  }
    37  
    38  type MatchRule struct {
    39  	GroupId            string `json:"group_id,omitempty"`
    40  	Sex                string `json:"sex,omitempty"`
    41  	Country            string `json:"country,omitempty"`
    42  	Province           string `json:"province,omitempty"`
    43  	City               string `json:"city,omitempty"`
    44  	ClientPlatformType string `json:"client_platform_type,omitempty"`
    45  	Language           string `json:"language,omitempty"`
    46  	TagId              string `json:"tag_id,omitempty"`
    47  }
    48  
    49  type Button struct {
    50  	Type       string   `json:"type,omitempty"`       // 非必须; 菜单的响应动作类型
    51  	Name       string   `json:"name,omitempty"`       // 必须;  菜单标题
    52  	Key        string   `json:"key,omitempty"`        // 非必须; 菜单KEY值, 用于消息接口推送
    53  	URL        string   `json:"url,omitempty"`        // 非必须; 网页链接, 用户点击菜单可打开链接
    54  	MediaId    string   `json:"media_id,omitempty"`   // 非必须; 调用新增永久素材接口返回的合法media_id
    55  	AppId      string   `json:"appid,omitempty"`      // 非必须; 跳转到小程序的appid
    56  	PagePath   string   `json:"pagepath,omitempty"`   // 非必须; 跳转到小程序的path
    57  	SubButtons []Button `json:"sub_button,omitempty"` // 非必须; 二级菜单数组
    58  }
    59  
    60  // 设置 btn 指向的 Button 为 子菜单 类型按钮.
    61  func (btn *Button) SetAsSubMenuButton(name string, subButtons []Button) {
    62  	btn.Name = name
    63  	btn.SubButtons = subButtons
    64  
    65  	btn.Type = ""
    66  	btn.Key = ""
    67  	btn.URL = ""
    68  	btn.MediaId = ""
    69  }
    70  
    71  // 设置 btn 指向的 Button 为 click 类型按钮.
    72  func (btn *Button) SetAsClickButton(name, key string) {
    73  	btn.Type = ButtonTypeClick
    74  	btn.Name = name
    75  	btn.Key = key
    76  
    77  	btn.URL = ""
    78  	btn.MediaId = ""
    79  	btn.SubButtons = nil
    80  }
    81  
    82  // 设置 btn 指向的 Button 为 view 类型按钮.
    83  func (btn *Button) SetAsViewButton(name, url string) {
    84  	btn.Type = ButtonTypeView
    85  	btn.Name = name
    86  	btn.URL = url
    87  
    88  	btn.Key = ""
    89  	btn.MediaId = ""
    90  	btn.SubButtons = nil
    91  }
    92  
    93  // 设置 btn 指向的 Button 为 扫码推事件 类型按钮.
    94  func (btn *Button) SetAsScanCodePushButton(name, key string) {
    95  	btn.Type = ButtonTypeScanCodePush
    96  	btn.Name = name
    97  	btn.Key = key
    98  
    99  	btn.URL = ""
   100  	btn.MediaId = ""
   101  	btn.SubButtons = nil
   102  }
   103  
   104  // 设置 btn 指向的 Button 为 扫码推事件且弹出"消息接收中"提示框 类型按钮.
   105  func (btn *Button) SetAsScanCodeWaitMsgButton(name, key string) {
   106  	btn.Type = ButtonTypeScanCodeWaitMsg
   107  	btn.Name = name
   108  	btn.Key = key
   109  
   110  	btn.URL = ""
   111  	btn.MediaId = ""
   112  	btn.SubButtons = nil
   113  }
   114  
   115  // 设置 btn 指向的 Button 为 弹出系统拍照发图 类型按钮.
   116  func (btn *Button) SetAsPicSysPhotoButton(name, key string) {
   117  	btn.Type = ButtonTypePicSysPhoto
   118  	btn.Name = name
   119  	btn.Key = key
   120  
   121  	btn.URL = ""
   122  	btn.MediaId = ""
   123  	btn.SubButtons = nil
   124  }
   125  
   126  // 设置 btn 指向的 Button 为 弹出拍照或者相册发图 类型按钮.
   127  func (btn *Button) SetAsPicPhotoOrAlbumButton(name, key string) {
   128  	btn.Type = ButtonTypePicPhotoOrAlbum
   129  	btn.Name = name
   130  	btn.Key = key
   131  
   132  	btn.URL = ""
   133  	btn.MediaId = ""
   134  	btn.SubButtons = nil
   135  }
   136  
   137  // 设置 btn 指向的 Button 为 弹出微信相册发图器 类型按钮.
   138  func (btn *Button) SetAsPicWeixinButton(name, key string) {
   139  	btn.Type = ButtonTypePicWeixin
   140  	btn.Name = name
   141  	btn.Key = key
   142  
   143  	btn.URL = ""
   144  	btn.MediaId = ""
   145  	btn.SubButtons = nil
   146  }
   147  
   148  // 设置 btn 指向的 Button 为 弹出地理位置选择器 类型按钮.
   149  func (btn *Button) SetAsLocationSelectButton(name, key string) {
   150  	btn.Type = ButtonTypeLocationSelect
   151  	btn.Name = name
   152  	btn.Key = key
   153  
   154  	btn.URL = ""
   155  	btn.MediaId = ""
   156  	btn.SubButtons = nil
   157  }
   158  
   159  // 设置 btn 指向的 Button 为 下发消息(除文本消息) 类型按钮.
   160  func (btn *Button) SetAsMediaIdButton(name, mediaId string) {
   161  	btn.Type = ButtonTypeMediaId
   162  	btn.Name = name
   163  	btn.MediaId = mediaId
   164  
   165  	btn.Key = ""
   166  	btn.URL = ""
   167  	btn.SubButtons = nil
   168  }
   169  
   170  // 设置 btn 指向的 Button 为 跳转图文消息URL 类型按钮.
   171  func (btn *Button) SetAsViewLimitedButton(name, mediaId string) {
   172  	btn.Type = ButtonTypeViewLimited
   173  	btn.Name = name
   174  	btn.MediaId = mediaId
   175  
   176  	btn.Key = ""
   177  	btn.URL = ""
   178  	btn.SubButtons = nil
   179  }
   180  
   181  // 设置 btn 指向的 Button 为 打开小程序.
   182  func (btn *Button) SetAsMiniProgramButton(name, appId, pagePath, url string) {
   183  	btn.Type = ButtonTypeMiniProgram
   184  	btn.Name = name
   185  	btn.URL = url
   186  	btn.AppId = appId
   187  	btn.PagePath = pagePath
   188  }