diff --git a/eventSubscribe.go b/eventSubscribe.go new file mode 100644 index 0000000..5f166e0 --- /dev/null +++ b/eventSubscribe.go @@ -0,0 +1,72 @@ +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "net/http" +) + +func onSubscribe(in InWechatMsg) { + openID := in.header.FromUserName + log.Println(openID) +} + +/* +{ + "subscribe": 1, + "openid": "o6_bmjrPTlm6_2sgVt7hMZOPfL2M", + "nickname": "Band", + "sex": 1, + "language": "zh_CN", + "city": "广州", + "province": "广东", + "country": "中国", + + "headimgurl": "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0", + "subscribe_time": 1382694957, + "unionid": " o6_bmasdasdsad6_2sgVt7hMZOPfL" + "remark": "", + + "groupid": 0, + + "tagid_list":[128,2] +} +*/ +type WechatUserInfo struct { + Subscribe int32 `json:"subscribe"` + OpenID string `json:"openid"` + NickName string `json:"nickname"` + Sex int32 `json:"sex"` + Language string `json:"language"` + City string `json:"city"` + Province string `json:"province"` + Country string `json:"country"` + Avatar string `json:"headimgurl"` + SubscribeTime int32 `json:"subscribe_time"` + UnionID string `json:"unionid"` + Remark string `json:"remark"` + GroupID int32 `json:"groupid"` + TagIDList []int32 `json:"tagid_list"` +} + +func getUserInfo(openID string, lang string) (result WechatUserInfo) { + url := result.getURL(openID, lang) + resp, err := http.Get(url) + if err != nil { + } + b, err := ioutil.ReadAll(resp.Body) + err = json.Unmarshal(b, &result) + return +} + +func (m *WechatUserInfo) getURL(openID, lang string) string { + atk, _ := GetAccessToken() + u := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s&lang=%s", atk, openID, lang) + return u +} + +func getURL() { + +} diff --git a/eventSubscribe_test.go b/eventSubscribe_test.go new file mode 100644 index 0000000..0e15ba4 --- /dev/null +++ b/eventSubscribe_test.go @@ -0,0 +1,49 @@ +package main + +import ( + "encoding/json" + "log" + "testing" +) + +func TestDecodeSubscribeJson(t *testing.T) { + msg := ` +{ + "subscribe": 1, + "openid": "o6_bmjrPTlm6_2sgVt7hMZOPfL2M", + "nickname": "Band", + "sex": 1, + "language": "zh_CN", + "city": "广州", + "province": "广东", + "country": "中国", + + "headimgurl": "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0", + "subscribe_time": 1382694957, + "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL", + "remark": "rr", + + "groupid": 0, + + "tagid_list":[128,2] +}` + s := WechatUserInfo{} + err := json.Unmarshal([]byte(msg), &s) + log.Println(err) + AssertEqual(t, s.Avatar, "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0", "Avatar not right") + AssertEqual(t, s.Subscribe, int32(1), "subscribe should =1") + AssertEqual(t, s.OpenID, "o6_bmjrPTlm6_2sgVt7hMZOPfL2M", "OpenID mismatch") + AssertEqual(t, s.NickName, "Band", "NickName wrong") + AssertEqual(t, s.Sex, int32(1), "sex should = 1 ") + AssertEqual(t, s.Language, "zh_CN", "language should = zh_CN") + AssertEqual(t, s.City, "广州", "City = 广州") + AssertEqual(t, s.Province, "广东", "Province = 广东") + AssertEqual(t, s.Country, "中国", "country = 中国") + AssertEqual(t, s.SubscribeTime, int32(1382694957), "time = 1382694957") + AssertEqual(t, s.UnionID, "o6_bmasdasdsad6_2sgVt7hMZOPfL", "Union ID wrong") + AssertEqual(t, s.Remark, "rr", "Remark wrong") + AssertEqual(t, s.GroupID, int32(0), "GroupID wrong") + AssertEqual(t, s.TagIDList[0], int32(128), "TagIDList wrong") + AssertEqual(t, s.TagIDList[1], int32(2), "TagIDList wrong") + +}