123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- <?php
- namespace Tool\MayouTool;
- use Tool\MayouTool\Exception\HttpCustomeException;
- use Tool\MayouTool\Exception\QiWeiException;
- /**
- * 企业微信工具
- * Class QiWeiTool
- * @package Tool\MayouTool
- */
- class QiWeiTool
- {
- /**
- * 企业微信基础url
- * @var string $baseUrl
- */
- protected static $baseUrl = "https://qyapi.weixin.qq.com/cgi-bin/";
- /**
- * 获取企业微信报警acess_token
- */
- public static function getBaoJingAccessToken()
- {
- $accessTokenUrl = env("QIWEI_BAOJING_ACCESSTOKEN_URL");
- if(!$accessTokenUrl){
- throw new QiWeiException("请先配置企微token获取地址");
- }
- //获取accesstToken
- $res = HttpCurl::getCurl($accessTokenUrl);
- if($res["status"]){
- return $res["data"]["token"];
- }
- throw new QiWeiException("企微token获取失败");
- }
- /**
- * 推送报警信息到自家应用
- * @param string $message 报警信息
- * @param string $chartId 群聊ID
- */
- public static function sendMessageToBaoJing(string $message,$chartId="")
- {
- //获取群聊ID
- if(!$chartId){
- $chartId = env("QIWEI_BAOJING_CHART_ID");
- }
- if(!$chartId){
- throw new QiWeiException("请先配置企微群聊ID");
- }
- $accessToken = self::getBaoJingAccessToken();
- $url = self::$baseUrl."appchat/send?access_token=".$accessToken;
- //组装报警信息
- $data = [
- "chatid"=>$chartId,
- "msgtype"=>"text",
- "text"=>[
- "content"=>$message
- ],
- "safe"=>0
- ];
- return HttpCurl::postCurl($url,$data);
- }
- /**
- * 发送图片到企微
- * @param string $imgPath 图片地址
- * @param string $chartId 群聊ID
- */
- public static function sendImgToBaoJing(string $imgPath,$chartId="")
- {
- //获取群聊ID
- if(!$chartId){
- $chartId = env("QIWEI_BAOJING_CHART_ID");
- }
- if(!$chartId){
- throw new QiWeiException("请先配置企微群聊ID");
- }
- $accessToken = self::getBaoJingAccessToken();
- $res = self::uploadTmpSource($accessToken,$imgPath);
- $url = self::$baseUrl."appchat/send?access_token=".$accessToken;
- //组装报警信息
- $data = [
- "chatid"=>$chartId,
- "msgtype"=>"image",
- "image"=>[
- "media_id"=>$res
- ],
- "safe"=>0
- ];
- return HttpCurl::postCurl($url,$data);
- }
- /**
- * @param string $imgPath
- * @param string $chartId
- * @return mixed
- * @throws QiWeiException
- */
- public static function sendImgTxtToBaoJing($coverPath,$contentUrl,$desc,$title,$chartId="")
- {
- //获取群聊ID
- if(!$chartId){
- $chartId = env("QIWEI_BAOJING_CHART_ID");
- }
- if(!$chartId){
- throw new QiWeiException("请先配置企微群聊ID");
- }
- $accessToken = self::getBaoJingAccessToken();
- $url = self::$baseUrl."appchat/send?access_token=".$accessToken;
- //组装报警信息
- $data = [
- "chatid"=>$chartId,
- "msgtype"=>"news",
- "news"=>[
- "articles"=>[
- [
- "title"=>$title,
- "description"=>$desc,
- "url"=>$contentUrl,
- "picurl"=>$coverPath
- ]
- ]
- ],
- "safe"=>0
- ];
- return HttpCurl::postCurl($url,$data);
- }
- /**
- * 上传图片到临时素材
- * @param string $accessToken token
- * @param string $filepath 图片地址
- */
- private static function uploadTmpSource(string $accessToken,$filepath)
- {
- $url = self::$baseUrl."media/upload?access_token=".$accessToken."&type=image";
- $file = new \CURLFile($filepath,"application/octet-stream");
- $data = [
- "media"=>$file
- ];
- $ch = curl_init($url);
- curl_setopt($ch,CURLOPT_POST,1);
- curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
- curl_setopt($ch,CURLOPT_HTTPHEADER,["multipart/form-data"]);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch,CURLOPT_BINARYTRANSFER,true);
- $result = curl_exec($ch);
- if(curl_errno($ch)){
- curl_close($ch);
- throw new HttpCustomeException("文件上传失败");
- }
- curl_close($ch);
- $res = json_decode($result,1);
- if($res["status"]){
- return $res;
- }
- throw new HttpCustomeException("文件上传失败");
- }
- }
|