-
Notifications
You must be signed in to change notification settings - Fork 37
/
api_user.go
151 lines (124 loc) · 6 KB
/
api_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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright 2020 zhaoyunxing.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dingtalk
import (
"fmt"
"net/http"
"net/url"
"strconv"
"time"
"github.com/pkg/errors"
"github.com/zhaoyunxing92/dingtalk/v2/constant"
"github.com/zhaoyunxing92/dingtalk/v2/crypto"
"github.com/zhaoyunxing92/dingtalk/v2/request"
"github.com/zhaoyunxing92/dingtalk/v2/response"
)
// CreateUser 创建用户
func (ding *DingTalk) CreateUser(user *request.CreateUser) (req response.CreateUser, err error) {
return req, ding.Request(http.MethodPost, constant.CreateUserKey, nil, user, &req)
}
// UpdateUser 更新用户信息
func (ding *DingTalk) UpdateUser(user *request.UpdateUser) (req response.Response, err error) {
return req, ding.Request(http.MethodPost, constant.UpdateUserKey, nil, user, &req)
}
// DeleteUser 删除用户
func (ding *DingTalk) DeleteUser(userId string) (req response.Response, err error) {
return req, ding.Request(http.MethodPost, constant.DeleteUserKey, nil, request.NewDeleteUser(userId), &req)
}
// GetUserDetail 根据userid获取用户详情
func (ding *DingTalk) GetUserDetail(user *request.UserDetail) (req response.UserDetail, err error) {
return req, ding.Request(http.MethodPost, constant.GetUserDetailKey, nil, user, &req)
}
// GetUserIdByUnionId 根据unionid获取用户userid
func (ding *DingTalk) GetUserIdByUnionId(res *request.UnionIdGetUserId) (req response.UnionIdGetUserId, err error) {
return req, ding.Request(http.MethodPost, constant.GetUserIdByUnionIdKey, nil, res, &req)
}
// GetUserIdByMobile 根据手机号获取userid
func (ding *DingTalk) GetUserIdByMobile(res *request.MobileGetUserId) (req response.MobileGetUserId, err error) {
return req, ding.Request(http.MethodPost, constant.GetUserIdByMobileKey, nil, res, &req)
}
// GetOrgAdminUser 获取管理员列表
func (ding *DingTalk) GetOrgAdminUser() (req response.OrgAdminUser, err error) {
return req, ding.Request(http.MethodPost, constant.GetOrgAdminUserKey, nil, nil, &req)
}
// GetOrgAdminScope 获取管理员通讯录权限范围
func (ding *DingTalk) GetOrgAdminScope(res *request.AdminUserScope) (req response.AdminUserScope, err error) {
return req, ding.Request(http.MethodPost, constant.GetOrgAdminScopeKey, nil, res, &req)
}
// GetUserCanAccessApplet 获取管理员的应用管理权限
func (ding *DingTalk) GetUserCanAccessApplet(appId int, userId string) (req response.UserCanAccessApplet, err error) {
if !ding.isv() {
return response.UserCanAccessApplet{}, errors.New("应用必须是产品方案商所开发")
}
query := url.Values{}
query.Set("appId", strconv.Itoa(appId))
query.Set("userId", userId)
return req, ding.Request(http.MethodGet, constant.GetUserCanAccessAppletKey, query, nil, &req)
}
// GetUserCount 获取员工人数
func (ding *DingTalk) GetUserCount(res *request.UserCount) (req response.UserCont, err error) {
return req, ding.Request(http.MethodPost, constant.GetUserCountKey, nil, res, &req)
}
// GetInactiveUser 获取未登录钉钉的员工列表
func (ding *DingTalk) GetInactiveUser(res *request.InactiveUser) (req response.InactiveUser, err error) {
return req, ding.Request(http.MethodPost, constant.GetInactiveUserKey, nil, res, &req)
}
// GetUserInfoByCode 通过免登码获取用户信息
func (ding *DingTalk) GetUserInfoByCode(code string) (req response.CodeGetUserInfo, err error) {
return req, ding.Request(http.MethodPost, constant.GetUserInfoByCodeKey, nil,
request.NewCodeGetUserInfo(code), &req)
}
// GetSSOUserInfo 获取应用管理员的身份信息
func (ding *DingTalk) GetSSOUserInfo(code string) (req response.SSOUserInfo, err error) {
var (
corpId = ding.corpId
secret = ding.ssoSecret
token string
)
if len(corpId) <= 0 || len(secret) <= 0 {
return response.SSOUserInfo{}, errors.New("corpId和SSOSecret不能为空")
}
if token, err = ding.GetSSOToken(corpId, secret); err != nil {
return response.SSOUserInfo{}, err
}
query := url.Values{}
query.Set("code", code)
query.Set("access_token", token)
return req, ding.Request(http.MethodGet, constant.GetSSOUserInfoKey, query, nil, &req)
}
// GetSnsUserInfo 根据sns临时授权码获取用户信息
// 该接口获取的用户信息仅用于扫码登录第三方网站、钉钉内免登第三方网站和使用钉钉账号登录第三方网站的场景。
func (ding *DingTalk) GetSnsUserInfo(code string) (req response.SnsUserInfo, err error) {
timestamp := strconv.FormatInt(time.Now().UnixNano()/1e6, 10)
sign := crypto.GetAvoidLoginSignature(timestamp, ding.secret)
query := url.Values{}
query.Set("accessKey", ding.key)
query.Set("timestamp", timestamp)
query.Set("signature", sign)
return req, ding.Request(http.MethodPost, constant.GetSNSUserInfoKey, query, request.NewSnsUserInfo(code), &req)
}
// GetContactUser 获取用户通讯录个人信息
// 调用本接口获取企业用户通讯录中的个人信息。
// @see https://open.dingtalk.com/document/orgapp/dingtalk-retrieve-user-information?spm=ding_open_doc.document.0.0.58b9492dZxH66f
func (ding *DingTalk) GetContactUser(unionId string) (rsp response.ContactUser, err error) {
return rsp, ding.Request(http.MethodGet, fmt.Sprintf(constant.GetContactUser, unionId), nil, nil, &rsp)
}
// GetCurrentUserByAccessToken 通UserAccessToken获取当前授权人的信息
func (ding *DingTalk) GetCurrentUserByAccessToken(userAccessToken string) (rsp response.ContactUser, err error) {
query := url.Values{}
query.Set("access_token", userAccessToken)
return rsp, ding.Request(http.MethodGet, fmt.Sprintf(constant.GetContactUser, "me"), query, nil, &rsp)
}