-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcallback.go
More file actions
100 lines (95 loc) · 2.53 KB
/
Copy pathcallback.go
File metadata and controls
100 lines (95 loc) · 2.53 KB
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
package main
import (
"CoolPush/tools"
"encoding/json"
"encoding/xml"
"github.com/julienschmidt/httprouter"
"io/ioutil"
"net/http"
)
type UserChangeEvent struct {
MsgType string `xml:"MsgType"`
Event string `xml:"Event"`
ToUserName string `xml:"ToUserName"`
FromUserName string `xml:"FromUserName"`
CreateTime uint32 `xml:"CreateTime"`
ChangeType string `xml:"ChangeType"`
UserID string `xml:"UserID"`
NewUserID string `xml:"NewUserID"`
Name string `xml:"Name"`
Mobile string `xml:"Mobile"`
Gender string `xml:"Gender"`
Email string `xml:"Email"`
Avatar string `xml:"Avatar"`
Status uint32 `xml:"Status"`
}
func CallbackWework(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
log.Infof("get query: %v", r.URL.Query().Encode())
msgSignature := r.URL.Query().Get("msg_signature")
timestamp := r.URL.Query().Get("timestamp")
nonce := r.URL.Query().Get("nonce")
if msgSignature == "" || timestamp == "" || nonce == "" {
body, _ := json.Marshal(&Response{
Code: StatusClientError,
Message: "Failed",
})
Write(w, body)
return
}
verifyEchoStr := r.URL.Query().Get("echostr")
// 回调检验
if verifyEchoStr != "" {
wwConf := conf.Wework
wxcpt := tools.NewWXBizMsgCrypt(wwConf.Token, wwConf.EncodingAESKey, wwConf.CorpId, tools.XmlType)
echoStr, cryptErr := wxcpt.VerifyURL(msgSignature, timestamp, nonce, verifyEchoStr)
if nil != cryptErr {
Write(w, []byte("Failed"))
return
}
Write(w, echoStr)
return
}
//正常的数据回调
rawData, err := ioutil.ReadAll(r.Body)
if err != nil {
Write(w, []byte("Failed"))
return
}
wwConf := conf.Wework
wxcpt := tools.NewWXBizMsgCrypt(wwConf.Token, wwConf.EncodingAESKey, wwConf.CorpId, tools.XmlType)
msg, cryptErr := wxcpt.DecryptMsg(msgSignature, timestamp, nonce, rawData)
if nil != cryptErr {
log.Errorf("DecryptMsg fail: %v", cryptErr)
Write(w, []byte("Failed"))
return
}
var event UserChangeEvent
err = xml.Unmarshal(msg, &event)
if nil != err {
log.Errorf("Unmarshal fail")
Write(w, []byte("Failed"))
return
}
switch event.ChangeType {
case "create_user":
err := CreateWeworkUser(event)
if err != nil {
log.Errorf("create user err: %v", err)
Write(w, []byte("Failed"))
return
}
case "update_user":
case "delete_user":
err := DeleteWeworkUser(event)
if err != nil {
log.Errorf("delete user err: %v", err)
Write(w, []byte("Failed"))
return
}
default:
Write(w, []byte("skip change_type"))
return
}
Write(w, []byte("Success"))
return
}