-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.go
128 lines (110 loc) · 2.5 KB
/
user.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
package dingtalk
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
)
// API地址
const (
USER_SIMPLELIST_URL = DINGTALK_API_URL + "/user/simplelist"
USER_GET_URL = DINGTALK_API_URL + "/user/get"
)
type UserRole struct {
Id int
Name string
GroupName string
}
// 用户
type User struct {
Unionid string
Remark string
Userid string
IsLeaderInDepts string
IsBoss bool
HiredDate int
IsSenior bool
Tel string
Department []int
WorkPlace string
Email string
OrderInDepts string
Mobile string
Errmsg string
Active bool
Avatar string
IsAdmin bool
IsHide bool
Jobnumber string
Name string
Extattr interface{}
StateCode string
Position string
Roles []UserRole
}
func (u *User) DeptLeaderInfo() map[int]bool {
fragments := strings.Split(u.IsLeaderInDepts[1:len(u.IsLeaderInDepts)-1], ",")
info := make(map[int]bool)
for _, fragment := range fragments {
kv := strings.Split(fragment, ":")
id, _ := strconv.Atoi(kv[0])
info[id] = kv[1] == "true"
}
json.Unmarshal([]byte(u.IsLeaderInDepts), &info)
return info
}
// 获取部门列表API的返回数据
type UserSimplelistResponse struct {
CommonResponse
HasMore bool
UserList []User
}
// 获取指定部门的子部门列表
func GetUsers(deptId int) ([]User, error) {
param := url.Values{
"access_token": {accessToken},
"department_id": {fmt.Sprintf("%d", deptId)},
}
resp, err := http.Get(USER_SIMPLELIST_URL + "?" + param.Encode())
if err != nil {
return nil, err
}
defer resp.Body.Close()
var info UserSimplelistResponse
err = json.NewDecoder(resp.Body).Decode(&info)
if err != nil {
return nil, err
}
if err := info.CommonResponse.Error(); err != nil {
return nil, err
}
return info.UserList, nil
}
// 获取用户详情的API的返回数据
type UserGetResponse struct {
CommonResponse
User
}
// 获取指定用户详情
func GetUser(userId string) (User, error) {
param := url.Values{
"access_token": {accessToken},
"userid": {userId},
}
resp, err := http.Get(USER_GET_URL + "?" + param.Encode())
if err != nil {
return User{}, err
}
defer resp.Body.Close()
var info UserGetResponse
err = json.NewDecoder(resp.Body).Decode(&info)
if err != nil {
return User{}, err
}
if err := info.CommonResponse.Error(); err != nil {
return User{}, err
}
return info.User, nil
}