以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  钉钉接口  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=173464)

--  作者:BG小白
--  发布时间:2021/12/1 2:04:00
--  钉钉接口
Dim client As DingTalk.Api.IDingTalkClient = New DingTalk.Api.DefaultDingTalkClient("https://oapi.dingtalk.com/department/list")
Dim req As DingTalk.Api.Request.OapiDepartmentListRequest = New DingTalk.Api.Request.OapiDepartmentListRequest()
req.SetHttpMethod("GET")
Dim rsp As DingTalk.Api.Response.OapiDepartmentListResponse = client.Execute(req, access_token)
DataTables("表B").DataRows.Clear
For i As Integer = 0 To rsp.Department.count -1 
Dim r As Row
r = Tables("表B").AddNew
r("第一列") = rsp.Department.Item(I).id
r("第二列") = rsp.Department.Item(I).name
r("第三列") = rsp.Department.Item(I).Parentid
Next
DataTables("表B").Save
MessageBox.Show("读取完成")

上面的是用钉钉提供的sdk获取到的所有部门信息

Dim ul As String = "https://oapi.dingtalk.com/department/list?access_token={0}"
Dim hc As New HttpClient(CExp(ul,Functions.Execute("access_token")))
Dim jo As JObject = JObject.Parse(hc.GetData())
If jo("errcode") = "0" Then
    MessageBox.Show("成功")
Else
    MessageBox.Show(jo.ToString)
End If

怎么用上面这种获取所有部门信息呢?红色的部分不知道该怎么实现?获取到的只有根部门,不是所有部门,要获得所有部门必须提供父部门dept_id
[此贴子已经被作者于2021/12/1 2:09:40编辑过]

--  作者:z769036165
--  发布时间:2021/12/1 8:28:00
--  
建立一个内部函数-同步部门,用递归循环调用
Functions.Execute("同步部门",1)

同步部门:
Dim id As Integer = args(0)
Dim ur As String = "https://oapi.dingtalk.com/topapi/v2/department/listsub"
Dim hc As New HttpClient(ur)
hc.FormData.Add("access_token",Functions.Execute("access_token"))
hc.FormData.Add("dept_id",id)
hc.FormData.Add("language","zh_CN")
Dim jo As JObject = JObject.Parse(hc.GetData())
Dim dt As DataTable = DataTables("dept")
Dim w As DataRow
If jo("errcode") = "0" Then
    For Each jt As JToken In jo("result")
            w = dt.Find("dept_id = \'"& jt("dept_id").tostring &"\'")
            If w IsNot Nothing Then
                w("name") = jt("name").tostring
                w("parent_id") = jt("parent_id").tostring
                Functions.Execute("同步部门",jt("dept_id").tostring)
            Else
                w = dt.AddNew
                w("dept_id") = jt("dept_id").tostring
                w("name") = jt("name").tostring
                w("parent_id") = jt("parent_id").tostring
                Functions.Execute("同步部门",jt("dept_id").tostring)
            End If
    Next
else
    Output.show(jo("errmsg").Tostring)
End If
[此贴子已经被作者于2021/12/1 8:30:01编辑过]