123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- <?php
- namespace app\admin\library;
- use fast\Http;
- use think\Config;
- class DingTalk
- {
- protected $app_key = 'dingug0hx6zmvmxa3awj';
- protected $app_secret = 'J7iJSrSkwMpCJfXvFsnNug0Y5zYOAYZObTfOlgEeTD2J4ei_eMHTLPP9mWszXx9i';
- protected $get_token_url = null;
- protected $get_user_id_url = null;
- protected $get_user_detail = null;
- protected $get_query_on_job_url = null;
- protected $send_notice_url = null;
- protected $agent_id = '1122860674';
- protected static $error = '';
- protected static $instance;
- public function __construct()
- {
- $dingdingurl = Config::get('dingding_url');
- $this->get_token_url = $dingdingurl.'/gettoken';
- $this->get_user_id_url = $dingdingurl.'/user/getuserinfo';
- $this->get_user_detail = $dingdingurl.'/topapi/v2/user/get';
- $this->get_query_on_job_url = $dingdingurl.'/topapi/smartwork/hrm/employee/queryonjob';
- $this->send_notice_url = $dingdingurl.'/topapi/message/corpconversation/asyncsend_v2';
- }
- /**
- * 初始化
- * @param array $options
- * @return object|static
- * @author matielong
- */
- public static function instance($options = [])
- {
- if (is_null(self::$instance)) {
- self::$instance = new static($options);
- }
- return self::$instance;
- }
- protected function setError($msg)
- {
- self::$error = $msg;
- return false;
- }
- public function getError()
- {
- return self::$error;
- }
- public function getToken()
- {
- $params = [
- 'appkey' => $this->app_key,
- 'appsecret' => $this->app_secret
- ];
- $res = Http::get($this->get_token_url, $params);
- if(!$res){
- return false;
- }
- $arr = json_decode($res ,true);
- if(isset($arr['errcode']) && $arr['errcode'] !== 0){
- return $this->setError($arr['errmsg'] ?? '');
- }
- return $arr['access_token'];
- }
- public function getUserId($code, $access_token)
- {
- $params = [
- 'code' => $code,
- 'access_token' => $access_token
- ];
- $res = Http::get($this->get_user_id_url, $params);
- if(!$res){
- return false;
- }
- $arr = json_decode($res ,true);
- if(isset($arr['errcode']) && $arr['errcode'] !== 0){
- return $this->setError($arr['errmsg'] ?? '');
- }
- return $arr['userid'];
- }
- public function getUserDetail($user_id, $access_token)
- {
- $params = [
- 'access_token' => $access_token,
- 'userid' => $user_id,
- 'language' => 'zh_CN'
- ];
- $res = Http::post($this->get_user_detail, $params);
- if(!$res){
- return false;
- }
- $arr = json_decode($res ,true);
- if(isset($arr['errcode']) && $arr['errcode'] !== 0){
- return $this->setError($arr['errmsg'] ?? '');
- }
- return $arr['result'];
- }
- public function getQueryOnJob($access_token)
- {
- $ids = [];
- $offset = 0;
- $size = 50;
- while (true) {
- $params = [
- 'access_token' => $access_token,
- 'status_list' => '2,3,5,-1',
- 'offset' => $offset,
- 'size' => $size,
- ];
- $res = Http::post($this->get_query_on_job_url, $params);
- if (!$res) {
- return false;
- }
- $arr = json_decode($res, true);
- if (isset($arr['errcode']) && $arr['errcode'] !== 0) {
- return $this->setError($arr['errmsg'] ?? '');
- }
- $data = $arr['result']['data_list'];
- if(is_array($data) && empty($data)){
- return $ids;
- }
- $ids = array_merge($ids, $data);
- $offset+=$size;
- }
- }
- public function sendWorkNotice($access_token, $params, $user_ids)
- {
- $text = "## " . $params['exam_title'] . " \n - 开始时间:" . $params['start_time'] . " \n - 结束时间:" . $params['end_time'] . " \n - 合格分数:" . $params['qualified'] . " 分";
- $msg_arr = [
- 'msgtype' => 'action_card',
- 'action_card' => [
- 'markdown' => $text,
- 'title' => $params['exam_title'],
- 'single_title' => $params['single_title'],
- 'single_url' => 'eapp://pages/ad/ad'
- ]
- ];
- $params = [
- 'access_token' => $access_token,
- 'agent_id' => $this->agent_id,
- 'userid_list' => $user_ids,
- 'msg' => json_encode($msg_arr, JSON_UNESCAPED_UNICODE)
- ];
- $res = Http::post($this->send_notice_url, $params);
- if (!$res) {
- return false;
- }
- $arr = json_decode($res, true);
- if (isset($arr['errcode']) && $arr['errcode'] !== 0) {
- return $this->setError($arr['errmsg'] ?? '');
- }
- return true;
- }
- public function sendInstitutionNotice($ins_name, $datetime, $phone = [])
- {
- $base_url = "https://oapi.dingtalk.com/robot/send?access_token=68dc416c50326f1d110d8c430538637f5ee1d60d5434b0201c14343d7465ca25";
- $time = time()* 1000;
- $secret = 'SECd4c09ea00ac0a0879d4fa4efe778d8167359bb3621ee00cc7a4f7ecd1ebb9435';
- $m = $time."\n".$secret;
- $s = hash_hmac('sha256', $m, $secret, true);
- $a = base64_encode($s);
- $b = urlencode($a);
- $url = $base_url . "×tamp=$time&sign=$b";
- $data = [
- 'msgtype' => 'text',
- 'text' => [
- 'content' => "异常:\n$ins_name\n影像最后上传时间:$datetime"
- ]
- ];
- $data_string = json_encode($data);
- $res = $this->request_by_curl($url, $data_string);
- return $res;
- }
- public function request_by_curl($remote_server, $post_string) {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $remote_server);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Content-Type: application/json;charset=utf-8'));
- curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- // 线下环境不用开启curl证书验证, 未调通情况可尝试添加该代码
- curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
- curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
- $data = curl_exec($ch);
- curl_close($ch);
- return $data;
- }
- }
|