<?php
namespace Home\Controller;
use Think\Controller;
class PushwarnController extends Controller {
	
	
public function s_curl($url){
		$ch = curl_init();
		curl_setopt ($ch, CURLOPT_URL, $url);
		curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
		$file_contents = curl_exec($ch);
		curl_close($ch);
		$arr = json_decode($file_contents,true);
		return $arr;
    }

 public function curl($url,$data=''){
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    $info = curl_exec($ch);
    if (curl_errno($ch)) {return 'Errno'.curl_error($ch);}else{ return $info;}
    curl_close($ch);
  } 
  
/**
 * [array_column1 ] 多维数组改成关联数组转字符串，
 * @param  $rows      [description]必需。多维数组。
 * @param  [type] $column_key [description]必需。可以是索引数组的列的整数索引，或者是关联数组的列的字符串键值。
 * @param  [type] $index_key  [description]可选。用作返回数组的索引/键的列。
 * @return [type]             [description]（两个参数）返回用逗号分割的字符串，（三个参数）返回以第三个参数为列的索引数组
 */
public function array_column1($rows, $column_key, $index_key = null) {
    $data = array();
    if (empty($index_key)) {
      foreach ($rows as $row) {
        $data[] = $row[$column_key];
      }
     $data=implode(",",$data);
    } else {
      foreach ($rows as $row) {
        $data[$row[$index_key]] = $row[$column_key];
      }
    }
    return $data;
  }


public function SendDeviceAlarm($access_token,$openid,$address,$deviceid,$type,$time,$remark,$ljurl,$url){
      $data = array(
        "touser"=> $openid,
        "template_id"=>"9cYn9IdoLE7NhCAPTIc1Ip4dV5JPrbbATxQT-v944Vw",
          "url" =>$ljurl,
        //"url" =>"http://zt-iot.com/ztxf/weixin.php/Home/Fire/water",         
        "data"=> array(
               "first"=> array(
                   "value"=> $address,
                   "color"=>"#44b549"
               ),
               "keyword1"=>array(
                   "value"=> $deviceid,
                   "color"=>"#173177"
               ),
               "keyword2"=> array(
                   "value"=> $type,
                   "color"=>"#173177"
               ),
               "keyword3"=> array(
                   "value"=> $time,
                   "color"=>"#173177"
               ),
               "remark"=>array(
                   "value"=> $remark,
                   "color"=>"#aaaaaa"
               )
        )
      );
      //{{first.DATA}}
      //设备号：{{keyword1.DATA}}
      //报警类型：{{keyword2.DATA}}
      //时间：{{keyword3.DATA}}
      //{{remark.DATA}}
      $data = json_encode($data);
      $QUEST_URL = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
      $res = $this->curl($QUEST_URL,$data);
      return json_decode($res,true);
  }

    public function SendDeviceAlarm1($access_token,$openid,$content,$type,$title,$people,$remark,$url){
        $data = array(
            "touser"=> $openid,
            "template_id"=>"N4PQZ9AmO2mjWVRjfZzYgLFrNq69WcLBW1KKjP1jbDs",
            "url" =>$url,
            "data"=> array(
                "first"=> array(
                    "value"=> $content,
                    "color"=>"#44b549"
                ),
                "keyword1"=>array(
                    "value"=> $type,
                    "color"=>"#173177"
                ),
                "keyword2"=> array(
                    "value"=> $title,
                    "color"=>"#173177"
                ),
                "keyword3"=> array(
                    "value"=> $people,
                    "color"=>"#173177"
                ),
                "remark"=>array(
                    "value"=> $remark,
                    "color"=>"#173177"
                )
            )
        );
        //{{first.DATA}}
        //设备号：{{keyword1.DATA}}
        //报警类型：{{keyword2.DATA}}
        //时间：{{keyword3.DATA}}
        //{{remark.DATA}}
        $data = json_encode($data);
        $QUEST_URL = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
        $res = $this->curl($QUEST_URL,$data);
        return json_decode($res,true);
//        return "ceshi";
    }

    public function SendDeviceAlarm2($access_token,$openid,$siteName,$deviceName,$evtName,$alarmStatus,$url){
        $data = array(
            "touser"=> $openid,
            "template_id"=>"xUfkQBUbTZ0TKIKuxeJaiqdLmCA541o97j2hAZN331Y",
            "url" =>$url,
            //"url" =>"http://zt-iot.com/ztxf/weixin.php/Home/Fire/water",
            "data"=> array(
                "first"=> array(
                    "value"=> '',
                    "color"=>"#44b549"
                ),
                "keyword1"=>array(
                    "value"=> $siteName,
                    "color"=>"#173177"
                ),
                "keyword2"=> array(
                    "value"=> $deviceName,
                    "color"=>"#173177"
                ),
                "keyword3"=> array(
                    "value"=> $evtName,
                    "color"=>"#173177"
                ),
                "keyword4"=> array(
                    "value"=> $alarmStatus,
                    "color"=>"#173177"
                ),
                "remark"=>array(
                    "value"=> '',
                    "color"=>"#aaaaaa"
                )
            )
        );
        //{{first.DATA}}
        //设备号：{{keyword1.DATA}}
        //报警类型：{{keyword2.DATA}}
        //时间：{{keyword3.DATA}}
        //{{remark.DATA}}
        $data = json_encode($data);
        $QUEST_URL = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
        $res = $this->curl($QUEST_URL,$data);
        return json_decode($res,true);
    }

    public function SendDeviceAlarm4($access_token,$openid,$siteName,$deviceName,$evtName,$alarmStatus,$time,$url){
        $data = array(
            "touser"=> $openid,
            "template_id"=>"I0BmMW5s8FTENbwmX1s4tPNpFT39U2qFaTCNoYcuLIo",
            "url" =>$url,
            //"url" =>"http://zt-iot.com/ztxf/weixin.php/Home/Fire/water",
            "data"=> array(
                "first"=> array(
                    "value"=> '',
                    "color"=>"#44b549"
                ),
                "keyword1"=>array(
                    "value"=> $siteName,
                    "color"=>"#173177"
                ),
                "keyword2"=> array(
                    "value"=> $deviceName,
                    "color"=>"#173177"
                ),
                "keyword3"=> array(
                    "value"=> $evtName,
                    "color"=>"#173177"
                ),
                "keyword4"=> array(
                    "value"=> $alarmStatus,
                    "color"=>"#173177"
                ),
                "keyword5"=> array(
                    "value"=> $time,
                    "color"=>"#173177"
                ),
                "remark"=>array(
                    "value"=> '',
                    "color"=>"#aaaaaa"
                )
            )
        );
        //{{first.DATA}}
        //设备号：{{keyword1.DATA}}
        //报警类型：{{keyword2.DATA}}
        //时间：{{keyword3.DATA}}
        //{{remark.DATA}}
        $data = json_encode($data);
        $QUEST_URL = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
        $res = $this->curl($QUEST_URL,$data);
        return json_decode($res,true);
    }

    public function SendDeviceAlarm3($access_token,$openid,$address,$deviceid,$type,$time,$remark,$url){
        $data = array(
            "touser"=> $openid,
            "template_id"=>"kMIOm99qgqAQ-nqsSxWlUToz4Krm-_TopRBJolXNBB8",
            "url" =>$url,
            //"url" =>"http://zt-iot.com/ztxf/weixin.php/Home/Fire/water",
            "data"=> array(
                "first"=> array(
                    "value"=> $address,
                    "color"=>"#44b549"
                ),
                "keyword1"=>array(
                    "value"=> $deviceid,
                    "color"=>"#173177"
                ),
                "keyword2"=> array(
                    "value"=> $type,
                    "color"=>"#173177"
                ),
                "keyword3"=> array(
                    "value"=> $time,
                    "color"=>"#173177"
                ),
                "remark"=>array(
                    "value"=> $remark,
                    "color"=>"#aaaaaa"
                )
            )
        );
        //{{first.DATA}}
        //设备号：{{keyword1.DATA}}
        //报警类型：{{keyword2.DATA}}
        //时间：{{keyword3.DATA}}
        //{{remark.DATA}}
        $data = json_encode($data);
        $QUEST_URL = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
        $res = $this->curl($QUEST_URL,$data);
        return json_decode($res,true);
    }


}