QiWeiTool.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace Tool\MayouTool;
  3. use Tool\MayouTool\Exception\HttpCustomeException;
  4. use Tool\MayouTool\Exception\QiWeiException;
  5. /**
  6. * 企业微信工具
  7. * Class QiWeiTool
  8. * @package Tool\MayouTool
  9. */
  10. class QiWeiTool
  11. {
  12. /**
  13. * 企业微信基础url
  14. * @var string $baseUrl
  15. */
  16. protected static $baseUrl = "https://qyapi.weixin.qq.com/cgi-bin/";
  17. /**
  18. * 获取企业微信报警acess_token
  19. */
  20. public static function getBaoJingAccessToken()
  21. {
  22. $accessTokenUrl = env("QIWEI_BAOJING_ACCESSTOKEN_URL");
  23. if(!$accessTokenUrl){
  24. throw new QiWeiException("请先配置企微token获取地址");
  25. }
  26. //获取accesstToken
  27. $res = HttpCurl::getCurl($accessTokenUrl);
  28. if($res["status"]){
  29. return $res["data"]["token"];
  30. }
  31. throw new QiWeiException("企微token获取失败");
  32. }
  33. /**
  34. * 推送报警信息到自家应用
  35. * @param string $message 报警信息
  36. * @param string $chartId 群聊ID
  37. */
  38. public static function sendMessageToBaoJing(string $message,$chartId="")
  39. {
  40. //获取群聊ID
  41. if(!$chartId){
  42. $chartId = env("QIWEI_BAOJING_CHART_ID");
  43. }
  44. if(!$chartId){
  45. throw new QiWeiException("请先配置企微群聊ID");
  46. }
  47. $accessToken = self::getBaoJingAccessToken();
  48. $url = self::$baseUrl."appchat/send?access_token=".$accessToken;
  49. //组装报警信息
  50. $data = [
  51. "chatid"=>$chartId,
  52. "msgtype"=>"text",
  53. "text"=>[
  54. "content"=>$message
  55. ],
  56. "safe"=>0
  57. ];
  58. return HttpCurl::postCurl($url,$data);
  59. }
  60. /**
  61. * 发送图片到企微
  62. * @param string $imgPath 图片地址
  63. * @param string $chartId 群聊ID
  64. */
  65. public static function sendImgToBaoJing(string $imgPath,$chartId="")
  66. {
  67. //获取群聊ID
  68. if(!$chartId){
  69. $chartId = env("QIWEI_BAOJING_CHART_ID");
  70. }
  71. if(!$chartId){
  72. throw new QiWeiException("请先配置企微群聊ID");
  73. }
  74. $accessToken = self::getBaoJingAccessToken();
  75. $res = self::uploadTmpSource($accessToken,$imgPath);
  76. $url = self::$baseUrl."appchat/send?access_token=".$accessToken;
  77. //组装报警信息
  78. $data = [
  79. "chatid"=>$chartId,
  80. "msgtype"=>"image",
  81. "image"=>[
  82. "media_id"=>$res
  83. ],
  84. "safe"=>0
  85. ];
  86. return HttpCurl::postCurl($url,$data);
  87. }
  88. /**
  89. * @param string $imgPath
  90. * @param string $chartId
  91. * @return mixed
  92. * @throws QiWeiException
  93. */
  94. public static function sendImgTxtToBaoJing($coverPath,$contentUrl,$desc,$title,$chartId="")
  95. {
  96. //获取群聊ID
  97. if(!$chartId){
  98. $chartId = env("QIWEI_BAOJING_CHART_ID");
  99. }
  100. if(!$chartId){
  101. throw new QiWeiException("请先配置企微群聊ID");
  102. }
  103. $accessToken = self::getBaoJingAccessToken();
  104. $url = self::$baseUrl."appchat/send?access_token=".$accessToken;
  105. //组装报警信息
  106. $data = [
  107. "chatid"=>$chartId,
  108. "msgtype"=>"news",
  109. "news"=>[
  110. "articles"=>[
  111. [
  112. "title"=>$title,
  113. "description"=>$desc,
  114. "url"=>$contentUrl,
  115. "picurl"=>$coverPath
  116. ]
  117. ]
  118. ],
  119. "safe"=>0
  120. ];
  121. return HttpCurl::postCurl($url,$data);
  122. }
  123. /**
  124. * 上传图片到临时素材
  125. * @param string $accessToken token
  126. * @param string $filepath 图片地址
  127. */
  128. private static function uploadTmpSource(string $accessToken,$filepath)
  129. {
  130. $url = self::$baseUrl."media/upload?access_token=".$accessToken."&type=image";
  131. $file = new \CURLFile($filepath,"application/octet-stream");
  132. $data = [
  133. "media"=>$file
  134. ];
  135. $ch = curl_init($url);
  136. curl_setopt($ch,CURLOPT_POST,1);
  137. curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
  138. curl_setopt($ch,CURLOPT_HTTPHEADER,["multipart/form-data"]);
  139. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  140. curl_setopt($ch,CURLOPT_BINARYTRANSFER,true);
  141. $result = curl_exec($ch);
  142. if(curl_errno($ch)){
  143. curl_close($ch);
  144. throw new HttpCustomeException("文件上传失败");
  145. }
  146. curl_close($ch);
  147. $res = json_decode($result,1);
  148. if($res["status"]){
  149. return $res;
  150. }
  151. throw new HttpCustomeException("文件上传失败");
  152. }
  153. }