api/app/library/NoticeCenter.php
2023-04-07 19:05:18 +08:00

112 lines
3.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* 通过企业号消息提示报警
*/
class QySend
{
public $access_token = "";
public $redis = null;
public function __construct($di)
{
$this->redis = $di->get("redis");
$this->access_token = $this->__qy_access_token(QY_CORPID, QY_SECRET);
}
/**
* POST方式执行页面
*/
function __http_post_request($url, $postArr){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSLVERSION, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 60);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postArr);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
/**
* 获取企业号acces_token
* $corpid,$corpsecret为权限组的id和密钥
*/
function __qy_access_token($corpid,$corpsecret)
{
$this->redis->select(REDIS_DB_NUMBER);
$rediskey = 'qy_' . $corpid . '_' . $corpsecret;
$qy_acces_token = $this->redis->get($rediskey);
if(!empty($qy_acces_token)){
return $qy_acces_token;
}else{
$url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$corpid&corpsecret=$corpsecret";
$info = $this->__https_request($url);
$tokenArr = json_decode($info,true);
$qy_acces_token = $tokenArr['access_token'];
$this->redis->setex($rediskey,7000, $qy_acces_token);
return $qy_acces_token;
}
}
/**
* GET方式执行页面
*/
function __https_request($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_SSLVERSION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
if(curl_errno($curl)){
return 'ERROR '.curl_error($curl);
}
curl_close($curl);
return $data;
}
/**
* we企业号消息提醒自定义发送人群
*/
function send_user_remind($userArr, $content)
{
if(strstr($_SERVER['HTTP_HOST'], 'weapp'))
{
return true;
}elseif(strstr($_SERVER['HTTP_HOST'], '127.0.0.1'))
{
return true;
}
if(is_array($userArr)){
$user_str = implode("|", $userArr);
}else{
$user_str = $userArr;
}
$str = '{
"touser": "'.$user_str.'",
"toparty": "",
"totag": "",
"msgtype": "text",
"agentid": '.QY_AGENTID.',
"text": {
"content": "'.$content.'"
},
"safe":0
}';
$url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token='.$this->access_token;
return $this->__http_post_request($url, $str);
}
}