diff --git a/crmLead.go b/crmLead.go index 8da83c3..dc85870 100644 --- a/crmLead.go +++ b/crmLead.go @@ -4,7 +4,10 @@ import ( "encoding/json" "errors" "fmt" + "io/ioutil" "log" + "net/http" + "time" ) type crmdLead struct { @@ -129,3 +132,50 @@ func crmLeadSetStatus(id string, status string) { newLead.Status = status crmPatchLeadInfo(newLead) } + +//WechatUserList 超过一万个也可能呢。 +//Decode Wechat user List {"total":2,"count":2,"data":{"openid":["","OPENID1","OPENID2"]},"next_openid":"NEXT_OPENID"} +type WechatUserList struct { + Total int `json:"total,omitempty"` + Count int `json:"count,omitempty"` + Data struct { + OpenID []string `json:"openid"` + } `json:"data,omitempty"` + NextOpenID string `json:"next_openid,omitempty"` +} + +func importExistingWechatUserAsLead() { + //当公众号关注者数量超过10000时,可通过填写next_openid的值,从而多次拉取列表的方式来满足需求。 + atk, _ := GetAccessToken() + nextOpenID := "" + url := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/user/get?access_token=%s&next_openid=%s", atk, nextOpenID) + + req, err := http.NewRequest("GET", url, nil) + var myClient = &http.Client{Timeout: 20 * time.Second} + + r, err := myClient.Do(req) + if err != nil { + log.Printf("Fail to get URL: %s", req.RequestURI) + log.Println(err) + } + defer r.Body.Close() + + jsonB, err := ioutil.ReadAll(r.Body) + + users := WechatUserList{} + err = json.Unmarshal(jsonB, &users) + if err != nil { + return + } + + for _, openID := range users.Data.OpenID { + log.Println(openID) + _, found, err := crmFindLeadByOpenID(openID) + + if !found && err == nil { + info := WechatUserInfo{} + info.getUserInfo(openID, "zh_CN") + info.registerNewLeadWithInfo(openID) + } + } +} diff --git a/crmLead_test.go b/crmLead_test.go index d1c3ce7..407b830 100644 --- a/crmLead_test.go +++ b/crmLead_test.go @@ -410,3 +410,7 @@ func TestGetLead(t *testing.T) { deleted, _ := crmDeleteEntity("Lead", id) AssertEqual(t, deleted, true, "test record shold be deleted correctly") } + +func TestRegisterExistingUser(t *testing.T) { + importExistingWechatUserAsLead() +}