DingTalk.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace app\admin\library;
  3. use fast\Http;
  4. use think\Config;
  5. class DingTalk
  6. {
  7. protected $app_key = 'dingug0hx6zmvmxa3awj';
  8. protected $app_secret = 'J7iJSrSkwMpCJfXvFsnNug0Y5zYOAYZObTfOlgEeTD2J4ei_eMHTLPP9mWszXx9i';
  9. protected $get_token_url = null;
  10. protected $get_user_id_url = null;
  11. protected $get_user_detail = null;
  12. protected $get_query_on_job_url = null;
  13. protected $send_notice_url = null;
  14. protected $agent_id = '1122860674';
  15. protected static $error = '';
  16. protected static $instance;
  17. public function __construct()
  18. {
  19. $dingdingurl = Config::get('dingding_url');
  20. $this->get_token_url = $dingdingurl.'/gettoken';
  21. $this->get_user_id_url = $dingdingurl.'/user/getuserinfo';
  22. $this->get_user_detail = $dingdingurl.'/topapi/v2/user/get';
  23. $this->get_query_on_job_url = $dingdingurl.'/topapi/smartwork/hrm/employee/queryonjob';
  24. $this->send_notice_url = $dingdingurl.'/topapi/message/corpconversation/asyncsend_v2';
  25. }
  26. /**
  27. * 初始化
  28. * @param array $options
  29. * @return object|static
  30. * @author matielong
  31. */
  32. public static function instance($options = [])
  33. {
  34. if (is_null(self::$instance)) {
  35. self::$instance = new static($options);
  36. }
  37. return self::$instance;
  38. }
  39. protected function setError($msg)
  40. {
  41. self::$error = $msg;
  42. return false;
  43. }
  44. public function getError()
  45. {
  46. return self::$error;
  47. }
  48. public function getToken()
  49. {
  50. $params = [
  51. 'appkey' => $this->app_key,
  52. 'appsecret' => $this->app_secret
  53. ];
  54. $res = Http::get($this->get_token_url, $params);
  55. if(!$res){
  56. return false;
  57. }
  58. $arr = json_decode($res ,true);
  59. if(isset($arr['errcode']) && $arr['errcode'] !== 0){
  60. return $this->setError($arr['errmsg'] ?? '');
  61. }
  62. return $arr['access_token'];
  63. }
  64. public function getUserId($code, $access_token)
  65. {
  66. $params = [
  67. 'code' => $code,
  68. 'access_token' => $access_token
  69. ];
  70. $res = Http::get($this->get_user_id_url, $params);
  71. if(!$res){
  72. return false;
  73. }
  74. $arr = json_decode($res ,true);
  75. if(isset($arr['errcode']) && $arr['errcode'] !== 0){
  76. return $this->setError($arr['errmsg'] ?? '');
  77. }
  78. return $arr['userid'];
  79. }
  80. public function getUserDetail($user_id, $access_token)
  81. {
  82. $params = [
  83. 'access_token' => $access_token,
  84. 'userid' => $user_id,
  85. 'language' => 'zh_CN'
  86. ];
  87. $res = Http::post($this->get_user_detail, $params);
  88. if(!$res){
  89. return false;
  90. }
  91. $arr = json_decode($res ,true);
  92. if(isset($arr['errcode']) && $arr['errcode'] !== 0){
  93. return $this->setError($arr['errmsg'] ?? '');
  94. }
  95. return $arr['result'];
  96. }
  97. public function getQueryOnJob($access_token)
  98. {
  99. $ids = [];
  100. $offset = 0;
  101. $size = 50;
  102. while (true) {
  103. $params = [
  104. 'access_token' => $access_token,
  105. 'status_list' => '2,3,5,-1',
  106. 'offset' => $offset,
  107. 'size' => $size,
  108. ];
  109. $res = Http::post($this->get_query_on_job_url, $params);
  110. if (!$res) {
  111. return false;
  112. }
  113. $arr = json_decode($res, true);
  114. if (isset($arr['errcode']) && $arr['errcode'] !== 0) {
  115. return $this->setError($arr['errmsg'] ?? '');
  116. }
  117. $data = $arr['result']['data_list'];
  118. if(is_array($data) && empty($data)){
  119. return $ids;
  120. }
  121. $ids = array_merge($ids, $data);
  122. $offset+=$size;
  123. }
  124. }
  125. public function sendWorkNotice($access_token, $params, $user_ids)
  126. {
  127. $text = "## " . $params['exam_title'] . " \n - 开始时间:" . $params['start_time'] . " \n - 结束时间:" . $params['end_time'] . " \n - 合格分数:" . $params['qualified'] . " 分";
  128. $msg_arr = [
  129. 'msgtype' => 'action_card',
  130. 'action_card' => [
  131. 'markdown' => $text,
  132. 'title' => $params['exam_title'],
  133. 'single_title' => $params['single_title'],
  134. 'single_url' => 'eapp://pages/ad/ad'
  135. ]
  136. ];
  137. $params = [
  138. 'access_token' => $access_token,
  139. 'agent_id' => $this->agent_id,
  140. 'userid_list' => $user_ids,
  141. 'msg' => json_encode($msg_arr, JSON_UNESCAPED_UNICODE)
  142. ];
  143. $res = Http::post($this->send_notice_url, $params);
  144. if (!$res) {
  145. return false;
  146. }
  147. $arr = json_decode($res, true);
  148. if (isset($arr['errcode']) && $arr['errcode'] !== 0) {
  149. return $this->setError($arr['errmsg'] ?? '');
  150. }
  151. return true;
  152. }
  153. public function sendInstitutionNotice($ins_name, $datetime, $phone = [])
  154. {
  155. $base_url = "https://oapi.dingtalk.com/robot/send?access_token=68dc416c50326f1d110d8c430538637f5ee1d60d5434b0201c14343d7465ca25";
  156. $time = time()* 1000;
  157. $secret = 'SECd4c09ea00ac0a0879d4fa4efe778d8167359bb3621ee00cc7a4f7ecd1ebb9435';
  158. $m = $time."\n".$secret;
  159. $s = hash_hmac('sha256', $m, $secret, true);
  160. $a = base64_encode($s);
  161. $b = urlencode($a);
  162. $url = $base_url . "&timestamp=$time&sign=$b";
  163. $data = [
  164. 'msgtype' => 'text',
  165. 'text' => [
  166. 'content' => "异常:\n$ins_name\n影像最后上传时间:$datetime"
  167. ]
  168. ];
  169. $data_string = json_encode($data);
  170. $res = $this->request_by_curl($url, $data_string);
  171. return $res;
  172. }
  173. public function request_by_curl($remote_server, $post_string) {
  174. $ch = curl_init();
  175. curl_setopt($ch, CURLOPT_URL, $remote_server);
  176. curl_setopt($ch, CURLOPT_POST, 1);
  177. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  178. curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Content-Type: application/json;charset=utf-8'));
  179. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
  180. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  181. // 线下环境不用开启curl证书验证, 未调通情况可尝试添加该代码
  182. curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
  183. curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
  184. $data = curl_exec($ch);
  185. curl_close($ch);
  186. return $data;
  187. }
  188. }