Skip to content

Commit ae5ecf5

Browse files
author
Athurg Feng
committed
增加获取用户详情的功能
1 parent eda493a commit ae5ecf5

File tree

1 file changed

+78
-2
lines changed

1 file changed

+78
-2
lines changed

user.go

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,61 @@ import (
55
"fmt"
66
"net/http"
77
"net/url"
8+
"strconv"
9+
"strings"
810
)
911

1012
// API地址
1113
const (
1214
USER_SIMPLELIST_URL = DINGTALK_API_URL + "/user/simplelist"
15+
USER_GET_URL = DINGTALK_API_URL + "/user/get"
1316
)
1417

18+
type UserRole struct {
19+
Id int
20+
Name string
21+
GroupName string
22+
}
23+
1524
// 用户
1625
type User struct {
17-
Userid string
18-
Name string
26+
Unionid string
27+
Remark string
28+
Userid string
29+
IsLeaderInDepts string
30+
IsBoss bool
31+
HiredDate int
32+
IsSenior bool
33+
Tel string
34+
Department []int
35+
WorkPlace string
36+
Email string
37+
OrderInDepts string
38+
Mobile string
39+
Errmsg string
40+
Active bool
41+
Avatar string
42+
IsAdmin bool
43+
IsHide bool
44+
Jobnumber string
45+
Name string
46+
Extattr interface{}
47+
StateCode string
48+
Position string
49+
Roles []UserRole
50+
}
51+
52+
func (u *User) DeptLeaderInfo() map[int]bool {
53+
fragments := strings.Split(u.IsLeaderInDepts[1:len(u.IsLeaderInDepts)-1], ",")
54+
55+
info := make(map[int]bool)
56+
for _, fragment := range fragments {
57+
kv := strings.Split(fragment, ":")
58+
id, _ := strconv.Atoi(kv[0])
59+
info[id] = kv[1] == "true"
60+
}
61+
json.Unmarshal([]byte(u.IsLeaderInDepts), &info)
62+
return info
1963
}
2064

2165
// 获取部门列表API的返回数据
@@ -50,3 +94,35 @@ func GetUsers(deptId int) ([]User, error) {
5094

5195
return info.UserList, nil
5296
}
97+
98+
// 获取用户详情的API的返回数据
99+
type UserGetResponse struct {
100+
CommonResponse
101+
User
102+
}
103+
104+
// 获取指定用户详情
105+
func GetUser(userId string) (User, error) {
106+
param := url.Values{
107+
"access_token": {accessToken},
108+
"userid": {userId},
109+
}
110+
111+
resp, err := http.Get(USER_GET_URL + "?" + param.Encode())
112+
if err != nil {
113+
return User{}, err
114+
}
115+
defer resp.Body.Close()
116+
117+
var info UserGetResponse
118+
err = json.NewDecoder(resp.Body).Decode(&info)
119+
if err != nil {
120+
return User{}, err
121+
}
122+
123+
if err := info.CommonResponse.Error(); err != nil {
124+
return User{}, err
125+
}
126+
127+
return info.User, nil
128+
}

0 commit comments

Comments
 (0)