github.com/chanxuehong/wechat@v0.0.0-20230222024006-36f0325263cd/mp/message/callback/request/event.go (about)

     1  package request
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/chanxuehong/wechat/mp/core"
     8  )
     9  
    10  const (
    11  	// 普通事件类型
    12  	EventTypeSubscribe   core.EventType = "subscribe"   // 关注事件, 包括点击关注和扫描二维码(公众号二维码和公众号带参数二维码)关注
    13  	EventTypeUnsubscribe core.EventType = "unsubscribe" // 取消关注事件
    14  	EventTypeScan        core.EventType = "SCAN"        // 已经关注的用户扫描带参数二维码事件
    15  	EventTypeLocation    core.EventType = "LOCATION"    // 上报地理位置事件
    16  )
    17  
    18  // 关注事件
    19  type SubscribeEvent struct {
    20  	XMLName struct{} `xml:"xml" json:"-"`
    21  	core.MsgHeader
    22  	EventType core.EventType `xml:"Event" json:"Event"` // subscribe
    23  
    24  	// 下面两个字段只有在扫描带参数二维码进行关注时才有值, 否则为空值!
    25  	EventKey string `xml:"EventKey,omitempty" json:"EventKey,omitempty"` // 事件KEY值, 格式为: qrscene_二维码的参数值
    26  	Ticket   string `xml:"Ticket,omitempty"   json:"Ticket,omitempty"`   // 二维码的ticket, 可用来换取二维码图片
    27  }
    28  
    29  func GetSubscribeEvent(msg *core.MixedMsg) *SubscribeEvent {
    30  	return &SubscribeEvent{
    31  		MsgHeader: msg.MsgHeader,
    32  		EventType: msg.EventType,
    33  		EventKey:  msg.EventKey,
    34  		Ticket:    msg.Ticket,
    35  	}
    36  }
    37  
    38  // 获取二维码参数
    39  func (event *SubscribeEvent) Scene() (scene string, err error) {
    40  	const prefix = "qrscene_"
    41  	if !strings.HasPrefix(event.EventKey, prefix) {
    42  		err = fmt.Errorf("EventKey 应该以 %s 为前缀: %s", prefix, event.EventKey)
    43  		return
    44  	}
    45  	scene = event.EventKey[len(prefix):]
    46  	return
    47  }
    48  
    49  // 取消关注事件
    50  type UnsubscribeEvent struct {
    51  	XMLName struct{} `xml:"xml" json:"-"`
    52  	core.MsgHeader
    53  	EventType core.EventType `xml:"Event"              json:"Event"`              // unsubscribe
    54  	EventKey  string         `xml:"EventKey,omitempty" json:"EventKey,omitempty"` // 事件KEY值, 空值
    55  }
    56  
    57  func GetUnsubscribeEvent(msg *core.MixedMsg) *UnsubscribeEvent {
    58  	return &UnsubscribeEvent{
    59  		MsgHeader: msg.MsgHeader,
    60  		EventType: msg.EventType,
    61  		EventKey:  msg.EventKey,
    62  	}
    63  }
    64  
    65  // 用户已关注时, 扫描带参数二维码的事件
    66  type ScanEvent struct {
    67  	XMLName struct{} `xml:"xml" json:"-"`
    68  	core.MsgHeader
    69  	EventType core.EventType `xml:"Event"    json:"Event"`    // SCAN
    70  	EventKey  string         `xml:"EventKey" json:"EventKey"` // 事件KEY值, 二维码的参数值(scene_id, scene_str)
    71  	Ticket    string         `xml:"Ticket"   json:"Ticket"`   // 二维码的ticket, 可用来换取二维码图片
    72  }
    73  
    74  func GetScanEvent(msg *core.MixedMsg) *ScanEvent {
    75  	return &ScanEvent{
    76  		MsgHeader: msg.MsgHeader,
    77  		EventType: msg.EventType,
    78  		EventKey:  msg.EventKey,
    79  		Ticket:    msg.Ticket,
    80  	}
    81  }
    82  
    83  // 上报地理位置事件
    84  type LocationEvent struct {
    85  	XMLName struct{} `xml:"xml" json:"-"`
    86  	core.MsgHeader
    87  	EventType core.EventType `xml:"Event"     json:"Event"`     // LOCATION
    88  	Latitude  float64        `xml:"Latitude"  json:"Latitude"`  // 地理位置纬度
    89  	Longitude float64        `xml:"Longitude" json:"Longitude"` // 地理位置经度
    90  	Precision float64        `xml:"Precision" json:"Precision"` // 地理位置精度(整数? 但是微信推送过来是浮点数形式)
    91  }
    92  
    93  func GetLocationEvent(msg *core.MixedMsg) *LocationEvent {
    94  	return &LocationEvent{
    95  		MsgHeader: msg.MsgHeader,
    96  		EventType: msg.EventType,
    97  		Latitude:  msg.Latitude,
    98  		Longitude: msg.Longitude,
    99  		Precision: msg.Precision,
   100  	}
   101  }