wechat.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2018/5/3
  6. * Time: 15:38
  7. */
  8. class ModelWechat
  9. {
  10. private $_appId = 'wx921fc9b6c72d721a';
  11. private $_appSecrect = 'd9008607995a060c90436637c6d87a85';
  12. private $_sendCustomUrl = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=';
  13. private $_sendTempUrl = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=';
  14. private $_openidList = [
  15. "oEa2vt_6YKtZNnMH0-02fpcsZUpM", //Jacob
  16. "oEa2vtzA_0yi0N-w8mxKsccc-uB0", //宁
  17. "oEa2vt7-4gGX0mGtNS6e2VIagWdo", //幽幽
  18. "oEa2vtxA0WcsUtiNrXn8y4z-5urE", //悟空
  19. "oEa2vt-JwFYIAJeWoHpeHUAt9kWE", //菲利普
  20. "oEa2vt2YP_gRekosIo1owogNW9MY", //小莫子
  21. "oEa2vt7MuIRi0WnplndYbZ7gS-L8", //杨波
  22. "oEa2vt3Bwn1HuywNBNTZMa3vTdHc", //傻白
  23. "oEa2vt34l41_BasBR6dglM8nx61w" //joan
  24. ];
  25. public function __construct()
  26. {
  27. include_once PATH_LIB . 'class.wechatutil.php';
  28. }
  29. /**
  30. *服务器崩溃报警
  31. */
  32. public function serverAlarm()
  33. {
  34. $onlineNum = shell_exec('netstat -apn | egrep "11001|11002" | grep ESTABLISHED | wc -l');
  35. if($onlineNum <= 30) {
  36. $this->sendTempMessage();
  37. }
  38. }
  39. /**
  40. * 推送模板消息
  41. * @return bool
  42. */
  43. public function sendTempMessage()
  44. {
  45. $wechatUtil = new WechatUtil($this->_appId, $this->_appSecrect);
  46. $accessToken = $wechatUtil->getAccessToken();
  47. if(!empty($accessToken)) {
  48. foreach ($this->_openidList as $openid) {
  49. $postData = '{
  50. "touser":"'.$openid.'",
  51. "template_id":"UN3cXcr7kYtt11AJGe-YpCOdndnQ7w3lDYdyOV3J3IQ",
  52. "url":"",
  53. "topcolor":"#FF683F",
  54. "data":{
  55. "first":{
  56. "value":"警告提醒!游戏在线人数小于30人,请打开游戏确认能否正常登录!",
  57. "color":"#ff510"
  58. },
  59. "keyword1":{
  60. "value":"管理员",
  61. "color":"#ff510"
  62. },
  63. "keyword2":{
  64. "value":"'.date('Y-m-d H:i:s',time()).'",
  65. "color":"#ff510"
  66. },
  67. "remark":{
  68. "value":"服务器、数据库、php或者nginx运行是否异常",
  69. "color":"#ff510"
  70. }
  71. }
  72. }';
  73. $response = $this->curlJson($this->_sendTempUrl.$accessToken, $postData);
  74. oo::logs()->todebug('response:'.json_encode($response) , 'wechat.php');
  75. }
  76. }
  77. return true;
  78. }
  79. /**
  80. * 发送客服消息提醒
  81. * @return bool
  82. */
  83. public function sendCustomMessage()
  84. {
  85. $wechatUtil = new WechatUtil($this->_appId, $this->_appSecrect);
  86. $accessToken = $wechatUtil->getAccessToken();
  87. if(!empty($accessToken)) {
  88. foreach ($this->_openidList as $openid) {
  89. $postData = '{
  90. "touser":"'.$openid.'",
  91. "msgtype":"text",
  92. "text":
  93. {
  94. "content":"警告提醒!游戏在线人数小于30人,请打开游戏确认能否正常登录!"
  95. }
  96. }';
  97. $response = $this->curlJson($this->_sendCustomUrl.$accessToken, $postData);
  98. oo::logs()->todebug('response:'.json_encode($response) , 'wechat.php');
  99. }
  100. }
  101. return true;
  102. }
  103. public function curlJson($url, $dataJson)
  104. {
  105. $ch = curl_init($url); //请求的URL地址
  106. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  107. curl_setopt($ch, CURLOPT_POSTFIELDS, $dataJson);//$data JSON类型字符串
  108. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  109. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($dataJson)));
  110. $data = curl_exec($ch);
  111. return $data;
  112. }
  113. }