noticeUtil.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace common\components;
  3. //通知系统
  4. use Yii;
  5. use yii\helpers\Json;
  6. use linslin\yii2\curl;
  7. class noticeUtil
  8. {
  9. public static function push($message, $mobile_list = [])
  10. {
  11. if (getenv('YII_ENV') == 'production') {
  12. $url = "https://oapi.dingtalk.com/robot/send?access_token=0856d031e07890f1902496747a0576a49c3df51dcf1e16427168cce12892bedb";
  13. } else {
  14. $mobile_list = [];
  15. $url = 'https://oapi.dingtalk.com/robot/send?access_token=0809902bdb8fa5cfdddbbe36cf3d061558c87769a38c03168571a41913e0d238';
  16. }
  17. $data = [
  18. 'msgtype' => 'text',
  19. 'text' => ['content' => '【花卉宝】' . $message]
  20. ];
  21. if (!empty($mobile_list)) {
  22. $mobile_list = is_array($mobile_list) == false ? [(string)$mobile_list] : $mobile_list;
  23. $data['at']['atMobiles'] = $mobile_list;
  24. }
  25. $data_string = json_encode($data);
  26. self::request($url, $data_string);
  27. }
  28. public static function request($remote_server, $post_string)
  29. {
  30. $ch = curl_init();
  31. curl_setopt($ch, CURLOPT_URL, $remote_server);
  32. curl_setopt($ch, CURLOPT_POST, 1);
  33. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  34. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=utf-8'));
  35. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
  36. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  37. if (getenv('YII_ENV') == 'local') {
  38. // 线下环境不用开启curl证书验证, 未调通情况可尝试添加该代码
  39. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  40. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  41. }
  42. $data = curl_exec($ch);
  43. curl_close($ch);
  44. return $data;
  45. }
  46. }