-
Notifications
You must be signed in to change notification settings - Fork 156
/
message.go
133 lines (111 loc) · 3.24 KB
/
message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package messages
import (
"github.com/ArtisanCloud/PowerLibs/v3/object"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/contract"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/power"
)
const VOID = 0
const TEXT = 2
const IMAGE = 4
const VOICE = 8
const VIDEO = 16
const SHORT_VIDEO = 32
const LOCATION = 64
const LINK = 128
const DEVICE_EVENT = 256
const DEVICE_TEXT = 512
const FILE = 1024
const TEXT_CARD = 2048
const TRANSFER = 4096
const EVENT = 1048576
const MINIPROGRAM_PAGE = 2097152
const MINIPROGRAM_NOTICE = 4194304
const ALL = TEXT | IMAGE | VOICE | VIDEO |
SHORT_VIDEO | LOCATION | LINK | DEVICE_EVENT |
DEVICE_TEXT | FILE | TEXT_CARD | TRANSFER |
EVENT | MINIPROGRAM_PAGE | MINIPROGRAM_NOTICE
type Message struct {
contract.MessageInterface
*object.Attribute
Type string
ID int
To string
From string
Properties []string
JsonAliases *object.HashMap
PropertiesToArray func(data *object.HashMap, aliases *object.HashMap) (*object.HashMap, error)
ToXmlArray func() *object.HashMap
}
func NewMessage(attributes *power.HashMap) *Message {
objAttribute, _ := power.PowerHashMapToObjectHashMap(attributes)
m := &Message{
Attribute: &object.Attribute{},
}
m.SetAttributes(objAttribute)
m.ToXmlArray = func() *object.HashMap {
return m.toXmlArray()
}
m.PropertiesToArray = func(data *object.HashMap, aliases *object.HashMap) (*object.HashMap, error) {
err := m.CheckRequiredAttributes()
if err != nil {
return nil, err
}
for property, value := range m.Attributes {
if value == nil && !m.IsRequired(property) {
continue
}
has, alias := object.InHash(property, aliases)
if has {
(*data)[alias] = m.Get(property, nil)
} else {
(*data)[property] = m.Get(property, nil)
}
}
return data, nil
}
return m
}
func (msg *Message) GetType() string {
return msg.Type
}
func (msg *Message) SetType(strType string) {
msg.Type = strType
}
func (msg *Message) TransformForJsonRequestWithoutType(appends *object.HashMap) (*object.HashMap, error) {
return msg.TransformForJsonRequest(appends, false)
}
func (msg *Message) TransformForJsonRequest(appends *object.HashMap, withType bool) (*object.HashMap, error) {
if !withType {
return msg.PropertiesToArray(&object.HashMap{}, msg.JsonAliases)
}
messageType := msg.GetType()
data := object.MergeHashMap(&object.HashMap{"msgtype": messageType}, appends)
arrayType := &object.HashMap{}
if (*data)[messageType] != nil {
arrayType = (*data)[messageType].(*object.HashMap)
}
arrayFromProperties, err := msg.PropertiesToArray(&object.HashMap{}, msg.JsonAliases)
if err != nil {
return nil, err
}
(*data)[messageType] = object.MergeHashMap(arrayType, arrayFromProperties)
return data, nil
}
// return string or hashmap
func (msg *Message) TransformToXml(appends *object.HashMap, returnAsArray bool) (interface{}, error) {
data := object.MergeHashMap(&object.HashMap{"MsgType": msg.GetType()}, msg.ToXmlArray(), appends)
if returnAsArray {
return data, nil
} else {
strXML := object.Map2Xml(data, false)
return strXML, nil
//buffer, err := xml2.Marshal(data)
//if err != nil {
// return "", err
//}
//return string(buffer), nil
}
}
func (msg *Message) toXmlArray() *object.HashMap {
return &object.HashMap{}
}