/
work.go
141 lines (125 loc) · 3.65 KB
/
work.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package work
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"strings"
"github.com/33cn/chat33/pkg/http"
. "github.com/33cn/chat33/pkg/work/model"
"github.com/33cn/chat33/utility"
"github.com/inconshreveable/log15"
)
var defaultHost = "http://127.0.0.1:8088"
var l = log15.New("library", "work")
func Init(cfg *Config) {
if cfg.Host != "" {
defaultHost = cfg.Host
}
l.Info("init work service", "host", defaultHost)
}
func GetUser(appId, uid string) (*UserInfo, error) {
params := url.Values{}
params.Set("app_id", appId)
params.Set("uid", uid)
byte, err := http.HTTPPostForm(defaultHost+"/work/checkUser", nil, strings.NewReader(params.Encode()))
if err != nil {
//屏蔽请求超时错误详情,打印详情日志 dld-v2.8.4 2019年7月2日16:22:53
l.Warn(fmt.Sprintf("http client request %v /work/checkUser failed", defaultHost), "err", err.Error())
return nil, errors.New("请求账户信息超时")
}
/*
{
"code": 200,
"message": "请求成功",
"data": {
"unique_id":"NQrqWISRghmMB9DkVHRjPNFnBYWvZIJh"
}
}
*/
var resp map[string]interface{}
err = json.Unmarshal(byte, &resp)
if err != nil {
return nil, err
}
if utility.ToInt(resp["result"]) != 0 {
return nil, errors.New(resp["message"].(string))
}
data, ok := resp["data"]
if !ok {
return nil, errors.New("no 'data' info")
}
var ret *UserInfo
if d, ok := data.(map[string]interface{}); !ok {
return nil, errors.New("account result err")
} else {
ret = &UserInfo{
AppId: utility.ToString(d["appId"]),
Uid: utility.ToString(d["uid"]),
Name: utility.ToString(d["name"]),
Code: utility.ToString(d["code"]),
EnterpriseCode: utility.ToString(d["enterpriseCode"]),
EnterpriseName: utility.ToString(d["enterpriseName"]),
}
}
return ret, nil
}
func GetUsers(appId, uid string) (*UsersList, error) {
params := url.Values{}
params.Set("app_id", appId)
params.Set("uid", uid)
byte, err := http.HTTPPostForm(defaultHost+"/work/users", nil, strings.NewReader(params.Encode()))
if err != nil {
//屏蔽请求超时错误详情,打印详情日志 dld-v2.8.4 2019年7月2日16:22:53
l.Warn(fmt.Sprintf("http client request %v /work/users failed", defaultHost), "err", err.Error())
return nil, errors.New("请求账户信息超时")
}
/*
{
"code": 200,
"message": "请求成功",
"data": {
"unique_id":"NQrqWISRghmMB9DkVHRjPNFnBYWvZIJh"
}
}
*/
var resp map[string]interface{}
err = json.Unmarshal(byte, &resp)
if err != nil {
return nil, err
}
if utility.ToInt(resp["result"]) != 0 {
return nil, errors.New(resp["message"].(string))
}
data, ok := resp["data"]
if !ok {
return nil, errors.New("no 'data' info")
}
var ret UsersList
if d, ok := data.(map[string]interface{}); !ok {
return nil, errors.New("account result err")
} else {
if enterprise, ok := d["enterprise"].(map[string]interface{}); ok {
ret.Enterprise.Code = utility.ToString(enterprise["code"])
ret.Enterprise.Name = utility.ToString(enterprise["name"])
ret.Enterprise.Remark = utility.ToString(enterprise["remark"])
}
if users, ok := d["users"].([]interface{}); ok {
us := make([]*UserInfo, 0)
for _, user := range users {
if u, ok := user.(map[string]interface{}); ok {
us = append(us, &UserInfo{
AppId: utility.ToString(u["appId"]),
Uid: utility.ToString(u["uid"]),
Name: utility.ToString(u["name"]),
Code: utility.ToString(u["code"]),
EnterpriseCode: utility.ToString(u["enterpriseCode"]),
EnterpriseName: utility.ToString(u["enterpriseName"]),
})
}
}
ret.Users = us
}
}
return &ret, nil
}