dingapi.class.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. class dingapi
  3. {
  4. public $apiUrl = 'https://oapi.dingtalk.com/';
  5. private $appKey;
  6. private $appSecret;
  7. private $token;
  8. private $expires;
  9. private $errors = array();
  10. /**
  11. * Construct
  12. *
  13. * @param string $appKey
  14. * @param string $appSecret
  15. * @param string $agentId
  16. * @param string $apiUrl
  17. * @access public
  18. * @return void
  19. */
  20. public function __construct($appKey, $appSecret, $agentId, $apiUrl = '')
  21. {
  22. $this->appKey = $appKey;
  23. $this->appSecret = $appSecret;
  24. $this->agentId = $agentId;
  25. if($apiUrl) $this->apiUrl = rtrim($apiUrl, '/') . '/';
  26. if(!$this->getToken()) return array('result' => 'fail', 'message' => $this->errors);
  27. }
  28. /**
  29. * Get token.
  30. *
  31. * @access public
  32. * @return string
  33. */
  34. public function getToken()
  35. {
  36. if($this->token and (time() - $this->expires) >= 0) return $this->token;
  37. $response = $this->queryAPI($this->apiUrl . "gettoken?appkey={$this->appKey}&appsecret={$this->appSecret}");
  38. if($this->isError()) return false;
  39. $this->token = $response->access_token;
  40. $this->expires = time() + $response->expires_in;
  41. return $this->token;
  42. }
  43. /**
  44. * Get users.
  45. *
  46. * @param string $selectedDepts
  47. * @access public
  48. * @return array
  49. */
  50. public function getUsers($selectedDepts = '')
  51. {
  52. $depts = trim($selectedDepts);
  53. if(empty($depts)) return array('result' => 'fail', 'message' => 'nodept');
  54. set_time_limit(0);
  55. $users = array();
  56. foreach(explode(',', $depts) as $deptID)
  57. {
  58. if(empty($deptID)) continue;
  59. $response = $this->queryAPI($this->apiUrl . "user/simplelist?access_token={$this->token}&department_id={$deptID}");
  60. if($this->isError())
  61. {
  62. $this->getErrors();
  63. continue;
  64. }
  65. foreach($response->userlist as $user) $users[$user->name] = $user->userid;
  66. }
  67. $response = $this->queryAPI($this->apiUrl . "auth/scopes?access_token={$this->token}");
  68. if(!empty($response->auth_org_scopes->authed_user))
  69. {
  70. foreach($response->auth_org_scopes->authed_user as $userid)
  71. {
  72. $user = $this->queryAPI($this->apiUrl . "user/get?access_token={$this->token}&userid={$userid}");
  73. if($this->isError())
  74. {
  75. $this->getErrors();
  76. continue;
  77. }
  78. $users[$user->name] = $user->userid;
  79. }
  80. }
  81. return array('result' => 'success', 'data' => $users);
  82. }
  83. /**
  84. * Get dept tree.
  85. *
  86. * @access public
  87. * @return array
  88. */
  89. public function getDeptTree()
  90. {
  91. $response = $this->queryAPI($this->apiUrl . "auth/scopes?access_token={$this->token}");
  92. if(isset($response->auth_org_scopes->authed_dept) and $response->auth_org_scopes->authed_dept[0] != 1)
  93. {
  94. $selectedDepts = array();
  95. foreach($response->auth_org_scopes->authed_dept as $deptID)
  96. {
  97. $selectedDepts[$deptID] = $deptID;
  98. $response = $this->queryAPI($this->apiUrl . "department/list?access_token={$this->token}&id=$deptID");
  99. foreach($response->department as $dept) $selectedDepts[$dept->id] = $dept->id;
  100. }
  101. return array('result' => 'selected', 'data' => $selectedDepts);
  102. }
  103. else
  104. {
  105. $response = $this->queryAPI($this->apiUrl . "department/list?access_token={$this->token}");
  106. if($this->isError()) return array('result' => 'fail', 'message' => $this->errors);
  107. $parentDepts = array();
  108. foreach($response->department as $dept)
  109. {
  110. $parentID = isset($dept->parentid) ? $dept->parentid : 0;
  111. $parentDepts[$parentID][$dept->id] = $dept->name;
  112. }
  113. }
  114. $tree = array();
  115. foreach($parentDepts as $parentID => $depts)
  116. {
  117. foreach($depts as $deptID => $deptName)
  118. {
  119. $node = array();
  120. $node['id'] = $deptID;
  121. $node['pId'] = $parentID;
  122. $node['name'] = $deptName;
  123. $node['open'] = true;
  124. $tree[] = $node;
  125. }
  126. }
  127. return array('result' => 'success', 'data' => $tree);
  128. }
  129. /**
  130. * Send message
  131. *
  132. * @param string $userList
  133. * @param string $message
  134. * @access public
  135. * @return array
  136. */
  137. public function send($userList, $message)
  138. {
  139. $postData = array();
  140. $postData['agent_id'] = $this->agentId;
  141. $postData['userid_list'] = $userList;
  142. $postData['msg'] = $message;
  143. $url = $this->apiUrl . 'topapi/message/corpconversation/asyncsend_v2?access_token=' . $this->token;
  144. $response = common::http($url, $postData);
  145. $errors = commonModel::$requestErrors;
  146. $response = json_decode($response);
  147. if(isset($response->errcode) and $response->errcode == 0) return array('result' => 'success');
  148. if(empty($response)) $this->errors = $errors;
  149. if(isset($response->errcode)) $this->errors[$response->errcode] = "Errcode:{$response->errcode}, Errmsg:{$response->errmsg}";
  150. return array('result' => 'fail', 'message' => $this->errors);
  151. }
  152. /**
  153. * Query API.
  154. *
  155. * @param string $url
  156. * @access public
  157. * @return string
  158. */
  159. public function queryAPI($url)
  160. {
  161. $response = common::http($url);
  162. $errors = commonModel::$requestErrors;
  163. $response = json_decode($response);
  164. if(isset($response->errcode) and $response->errcode == 0) return $response;
  165. if(empty($response)) $this->errors = $errors;
  166. if(isset($response->result) and $response->result == 'fail') $this->errors['curl'] = $response->message;
  167. if(isset($response->errcode)) $this->errors[$response->errcode] = "Errcode:{$response->errcode}, Errmsg:{$response->errmsg}";
  168. return false;
  169. }
  170. /**
  171. * 根据open_id列表,获取用户。
  172. * Batch get users by open_id list.
  173. *
  174. * @param array $userIdList
  175. * @access public
  176. * @return array
  177. */
  178. public function batchGetUsers($userIdList)
  179. {
  180. $useridPairs = array();
  181. $userGroup = array_chunk($userIdList, 49);
  182. foreach($userGroup as $userIdList)
  183. {
  184. $urls = array();
  185. foreach($userIdList as $userID) $urls[] = $this->apiUrl . "topapi/v2/user/get?access_token={$this->token}&userid={$userID}";
  186. $datas = $this->multiRequest($urls);
  187. foreach($datas as $response)
  188. {
  189. $response = json_decode($response);
  190. if(empty($response->result)) continue;
  191. $user = $response->result;
  192. $useridPairs[$user->userid] = $user->name;
  193. }
  194. }
  195. return $useridPairs;
  196. }
  197. /**
  198. * Handle the concurrency of requests.
  199. *
  200. * @param array $urls
  201. * @access public
  202. * @return array
  203. */
  204. public function multiRequest($urls)
  205. {
  206. $curl = curl_multi_init();
  207. $urlHandlers = array();
  208. $urlData = array();
  209. /* Set request header information. */
  210. /* Initialize multiple request handles to one. */
  211. foreach($urls as $url)
  212. {
  213. $ch = curl_init();
  214. curl_setopt($ch, CURLOPT_URL, $url);
  215. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  216. if(!isset($_SERVER['HTTPS']) or $_SERVER['HTTPS'] != 'on')
  217. {
  218. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  219. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  220. }
  221. $urlHandlers[] = $ch;
  222. curl_multi_add_handle($curl, $ch);
  223. }
  224. $active = null;
  225. do
  226. {
  227. $mrc = curl_multi_exec($curl, $active);
  228. }
  229. while($mrc == CURLM_CALL_MULTI_PERFORM);
  230. while($active and $mrc == CURLM_OK)
  231. {
  232. usleep(50000);
  233. if(curl_multi_select($curl) != -1)
  234. {
  235. do
  236. {
  237. $mrc = curl_multi_exec($curl, $active);
  238. }
  239. while($mrc == CURLM_CALL_MULTI_PERFORM);
  240. }
  241. }
  242. foreach($urlHandlers as $index => $ch)
  243. {
  244. $urlData[$index] = curl_multi_getcontent($ch);
  245. curl_multi_remove_handle($curl, $ch);
  246. }
  247. curl_multi_close($curl);
  248. return $urlData;
  249. }
  250. /**
  251. * Check for errors.
  252. *
  253. * @access public
  254. * @return bool
  255. */
  256. public function isError()
  257. {
  258. return !empty($this->errors);
  259. }
  260. /**
  261. * Get errors.
  262. *
  263. * @access public
  264. * @return array
  265. */
  266. public function getErrors()
  267. {
  268. $errors = $this->errors;
  269. $this->errors = array();
  270. return $errors;
  271. }
  272. }