|
|
|
@@ -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) |
|
|
|
} |
|
|
|
} |
|
|
|
} |