/
contactorApi.go
66 lines (60 loc) · 1.65 KB
/
contactorApi.go
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
package boss
import (
"bytes"
"encoding/json"
"net/http"
"github.com/Jeffail/gabs"
"github.com/emirpasic/gods/sets/hashset"
"github.com/fwtpe/owl-backend/modules/fe/g"
log "github.com/sirupsen/logrus"
)
func QueryContact(platformName string) (contactList []Contactor, err error) {
config := g.Config()
fcname := config.Api.Name
fctoken := getFctoken()
url := config.Api.Contact
params := map[string]string{
"fcname": fcname,
"fctoken": fctoken,
"platform_key": platformName,
}
paramstr, _ := json.Marshal(params)
log.Debugf("contact get url: %s", url)
log.Debugf("contact get params: %v", params)
reqPost, _ := http.NewRequest("POST", url, bytes.NewBuffer([]byte(paramstr)))
reqPost.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(reqPost)
if err != nil {
return
}
defer resp.Body.Close()
parsedJSON, err := gabs.ParseJSONBuffer(resp.Body)
// contactList = parsedJSON.Data().(map[string]interface{})
if err != nil {
return
}
contacts, err := parsedJSON.Search("result", platformName).Children()
contactList = []Contactor{}
for _, con := range contacts {
contact := Contactor{
con.Search("cell").Data().(string),
con.Search("email").Data().(string),
con.Search("realname").Data().(string),
}
contactList = append(contactList, contact)
}
return
}
func GetPlatfromContactInfo(platList *hashset.Set) (contactorMap map[string][]Contactor, err error) {
contactorMap = map[string][]Contactor{}
for _, name := range platList.Values() {
sname := name.(string)
res, err := QueryContact(sname)
if err != nil {
continue
}
contactorMap[sname] = res
}
return
}