<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/3
 * Time: 15:38
 */
class ModelWechat
{
    private $_appId = 'wx921fc9b6c72d721a';
    private $_appSecrect = 'd9008607995a060c90436637c6d87a85';
    private $_sendCustomUrl = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=';
    private $_sendTempUrl = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=';
    private $_openidList = [
        "oEa2vt_6YKtZNnMH0-02fpcsZUpM",   //Jacob
        "oEa2vtzA_0yi0N-w8mxKsccc-uB0",    //宁
        "oEa2vt7-4gGX0mGtNS6e2VIagWdo",    //幽幽
        "oEa2vtxA0WcsUtiNrXn8y4z-5urE",     //悟空
        "oEa2vt-JwFYIAJeWoHpeHUAt9kWE",    //菲利普
        "oEa2vt2YP_gRekosIo1owogNW9MY",    //小莫子
        "oEa2vt7MuIRi0WnplndYbZ7gS-L8",    //杨波
        "oEa2vt3Bwn1HuywNBNTZMa3vTdHc",    //傻白
        "oEa2vt34l41_BasBR6dglM8nx61w"     //joan
    ];

    public function __construct()
    {
        include_once PATH_LIB . 'class.wechatutil.php';
    }


    /**
     *服务器崩溃报警
     */
    public function serverAlarm()
    {
        $onlineNum = shell_exec('netstat  -apn | egrep "11001|11002" | grep ESTABLISHED  | wc -l');
        if($onlineNum <= 30) {
            $this->sendTempMessage();
        }
    }

    /**
     * 推送模板消息
     * @return bool
     */
    public function sendTempMessage()
    {
        $wechatUtil = new WechatUtil($this->_appId, $this->_appSecrect);
        $accessToken = $wechatUtil->getAccessToken();
        if(!empty($accessToken)) {
            foreach ($this->_openidList as $openid) {
                $postData = '{
                    "touser":"'.$openid.'",
                    "template_id":"UN3cXcr7kYtt11AJGe-YpCOdndnQ7w3lDYdyOV3J3IQ",
                    "url":"",
                    "topcolor":"#FF683F",
                    "data":{
                        "first":{
                            "value":"警告提醒！游戏在线人数小于30人，请打开游戏确认能否正常登录！",
                            "color":"#ff510"
                        },
                        "keyword1":{
                            "value":"管理员",
                            "color":"#ff510"
                        },
                        "keyword2":{
                            "value":"'.date('Y-m-d H:i:s',time()).'",
                            "color":"#ff510"
                        },
                        "remark":{
                            "value":"服务器、数据库、php或者nginx运行是否异常",
                            "color":"#ff510"
                        }
                    }
                }';
                $response = $this->curlJson($this->_sendTempUrl.$accessToken, $postData);
                oo::logs()->todebug('response:'.json_encode($response) , 'wechat.php');
            }
        }
        return true;
    }



    /**
     * 发送客服消息提醒
     * @return bool
     */
    public function sendCustomMessage()
    {
        $wechatUtil = new WechatUtil($this->_appId, $this->_appSecrect);
        $accessToken = $wechatUtil->getAccessToken();
        if(!empty($accessToken)) {
            foreach ($this->_openidList as $openid) {
                $postData = '{
                    "touser":"'.$openid.'",
                    "msgtype":"text",
                    "text":
                    {
                         "content":"警告提醒！游戏在线人数小于30人，请打开游戏确认能否正常登录！"
                    }
                }';
                $response = $this->curlJson($this->_sendCustomUrl.$accessToken, $postData);
                oo::logs()->todebug('response:'.json_encode($response) , 'wechat.php');
            }
        }
        return true;
    }


    public function curlJson($url, $dataJson)
    {
        $ch = curl_init($url); //请求的URL地址
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $dataJson);//$data JSON类型字符串
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($dataJson)));
        $data = curl_exec($ch);
        return $data;
    }

}