瀏覽代碼

import existing Wechat User

master
Patrick Peng Sun 8 年之前
父節點
當前提交
819404ed17
共有 2 個文件被更改,包括 54 次插入0 次删除
  1. +50
    -0
      crmLead.go
  2. +4
    -0
      crmLead_test.go

+ 50
- 0
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)
}
}
}

+ 4
- 0
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()
}

Loading…
取消
儲存