<?php
/*
    by GLORY QQ:276369046
*/

class class_weixin_adv
{
    var $appid = "";
    var $appsecret = "";

    //构造函数，获取Access Token
    public function __construct($appid = NULL, $appsecret = NULL)
    {
		global $db,$ass;
        if($appid){
            $this->appid = $appid;
        }
        if($appsecret){
            $this->appsecret = $appsecret;
        }
        $timeOut = false;
		if($ass->number('token','appid="'.$this->appid.'"')>0){
			$one = $ass->one('token','','appid="'.$this->appid.'" and access_token!=""');
			$this->access_token = $one['access_token'];
			$lasttime = $one['lasttime'];
			if(time() > ($lasttime+7000)){
				$timeOut = true;
			}
		}else{
			if($this->appid){
				if($ass->insert('token',array(
					'appid'		=> $this->appid,
					'access_token'		=> '',
					'lasttime'	=> time()
				))){
				}
			}
			$this->access_token = "";
			$timeOut = true;
		}
        if ($timeOut){
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->appsecret;
            $res = $this->https_request($url,'');
            $result = json_decode($res, true);
            //save to Database or Memcache
            $this->access_token = $result["access_token"];
            $this->lasttime = time();
			if($ass->update('token',array('access_token'=>$this->access_token,'lasttime'=>time()),'appid="'.$this->appid.'"')){
			}
        }
    }
	
	function recalltoken(){
		global $db,$ass;
		$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->appsecret;
		$res = $this->https_request($url,'');
		$result = json_decode($res, true);
		//save to Database or Memcache
		$this->access_token = $result["access_token"];
		$this->lasttime = time();
		if($ass->update('token',array('access_token'=>$this->access_token,'lasttime'=>time()),'appid="'.$this->appid.'"')){
		}
	}
    //获取关注者列表
    public function get_user_list($next_openid = NULL)
    {
        $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$this->access_token."&next_openid=".$next_openid;
        $res = $this->https_request($url);
        return json_decode($res, true);
    }

    //获取用户基本信息
    public function get_user_info($openid)
    {
        $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$this->access_token."&openid=".$openid."&lang=zh_CN";
        $res = $this->https_request($url);
        return json_decode($res, true);
    }

    //创建菜单
    public function create_menu($data)
    {
        $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$this->access_token;
        $res = $this->https_request($url, $data);
        return json_decode($res, true);
    }

	//发送客服消息，已实现发送文本，其他类型可扩展
    public function send_custom_message_text($touser, $type, $data)
    {
        $msg = array('touser' =>$touser);
        switch($type)
        {
            case 'text':
                $msg['msgtype'] = 'text';
                $msg['text']    = array('content'=> urlencode($data));
                break;
        }

		//var_dump($msg);
		//exit('');
        $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$this->access_token;
        return $this->https_request($url, urldecode(json_encode($msg)));
    }

    //发送客服消息，已实现发送文本，其他类型可扩展
    public function send_custom_message($touser, $type, $data,$title='',$description='')
    {
        $msg = array('touser' =>$touser);
		$msg['msgtype'] = $type;
        switch($type)
        {
            case 'text'://发送文本消息
                $msg[$type]    = array('content'=> urlencode($data));
				break;
			case 'image'://发送图片消息
			case 'voice'://发送语音消息
				$msg[$type]    = array('media_id'=> urlencode($data));
				break;
			case 'video'://发送视频消息
                $msg[$type]    = array(
									'media_id'=> $data,
									'thumb_media_id'=>$data,
									'title'=>'111',
									'description'=>'2222'
									//'title'=>urlencode($title),
									//'description'=>urlencode($description)
								);
				break;
			case 'music'://发送音乐消息
				$msg[$type]    = array(
									'thumb_media_id'=> urlencode($data),
									'musicurl'=>urlencode($musicurl),
									'hqmusicurl'=>urlencode($hqmusicurl),
									'title'=>urlencode($title),
									'description'=>urlencode($description)
								);
                break;
			case 'news'://发送图文消息（点击跳转到外链） 图文消息条数限制在8条以内，注意，如果图文数超过8，则将会无响应。75442
                $msg[$type]    = array('articles'=> $data);
				//data格式  title  description  url  picurl
                break;
			case 'mpnews'://发送图文消息（点击跳转到图文消息页面） 图文消息条数限制在8条以内，注意，如果图文数超过8，则将会无响应。
                $msg[$type]    = array('media_id'=> urlencode($data));
                break;
        }

		//var_dump($msg);
		//exit('');

        $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$this->access_token;
        return $this->https_request($url, urldecode(json_encode($msg)));
    }

    //生成参数二维码
    public function create_qrcode($scene_type, $scene_id)
    {
        switch($scene_type)
        {
            case 'QR_LIMIT_SCENE': //永久
                $data = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": '.$scene_id.'}}}';
                break;
            case 'QR_SCENE':       //临时
                $data = '{"expire_seconds": 1800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": '.$scene_id.'}}}';
                break;
        }
        $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$this->access_token;
        $res = $this->https_request($url, $data);
        $result = json_decode($res, true);
        return "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($result["ticket"]);
    }
    
    //创建分组
    public function create_group($name)
    {
        $data = '{"group": {"name": "'.$name.'"}}';
        $url = "https://api.weixin.qq.com/cgi-bin/groups/create?access_token=".$this->access_token;
        $res = $this->https_request($url, $data);
        return json_decode($res, true);
    }
    
    //移动用户分组
    public function update_group($openid, $to_groupid)
    {
        $data = '{"openid":"'.$openid.'","to_groupid":'.$to_groupid.'}';
        $url = "https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token=".$this->access_token;
        $res = $this->https_request($url, $data);
        return json_decode($res, true);
    }
    //上传临时素材
	public function upload_temp($type,$file)
	{
		$data = array("media"  => "@".dirname(__FILE__).'\\'.$file);
        $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=".$this->access_token."&type=".$type;
        $res = $this->https_request($url, $data);
        return json_decode($res, true);
	}
    //上传多媒体文件 
    public function upload_media($type, $file)
    {
        $data = array("media"  => "@".dirname(__FILE__).'\\'.$file);
        $url = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=".$this->access_token."&type=".$type;
        $res = $this->https_request($url, $data);
        return json_decode($res, true);
    }

	//执行群发函数有连个，群发对象可以是用户组，也可以是OpenID列表。
	//1.对用户组群发
	public function mass_send_group($groupid,$type,$data)
	{
		$msg = array('filter'=>array('group_id'=>$groupid));
		$msg['msgtype'] = $type;
		switch($type)
		{
			case 'text':
				$msg[$type] = array('content'=>urlencode($data));
				break;
			case 'image':
			case 'voice':
			case 'mpvideo':
			case 'mpnews':
				$msg[$type] = array('media_id'=>urlencode($data));
				break;
		}
		$url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=".$this->access_token;
		$res = $this->https_request($url, urldecode(json_encode($msg)));
        return json_decode($res, true);
	}

	//2.对OpenID列表群发
	public function mass_send_openid($openid,$type,$data)
	{
		$msg = array('touser'=>$openid);
		$msg['msgtype'] = $type;
		switch($type)
		{
			case 'msgtype':
				$msg[$type] = array('msgtype'=>urlencode($data));
				break;
			case 'image':
			case 'voice':
			case 'mpvideo':
			case 'mpnews':
				$msg[$type] = array('media_id'=>urlencode($data));
				break;
		}
		$url = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=".$this->access_token;
		$res = $this->https_request($url, urldecode(json_encode($msg)));
        return json_decode($res, true);
	}

	//生成带参数的二维码
	//times有效时间  2592000 为30天  604800 为7天 
	//type 0临时二维码 1永久二维码
	public function get_parameter_qrcode($glide,$times=604800,$type=0)
	{
		if($type){//永久二维码
			//$qrcode = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": '.$glide.'}}}';
			$qrcode = '{"action_name": "QR_LIMIT_STR_SCENE", "action_info": {"scene": {"scene_str": "'.$glide.'"}}}';
		}else{//临时二维码
			$qrcode = '{"expire_seconds": '.$times.', "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": '.$glide.'}}}';
		}
		$url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$this->access_token;
		$res = $this->https_request($url, $qrcode);
        return json_decode($res, true);
	}
	//获取二维码的路径
	public function get_qrcode_url($ticket)
	{
		return "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".$ticket;		
	}
	//添加客服
	public function add_customservice($account,$nickname,$password)
	{
		$data = '{"kf_account":"'.$account.'","nickname":"'.$nickname.'","password":"'.md5($password).'"}';
		//$data = '{"kf_account":"wang@minan-119","nickname":"glory","password":"e10adc3949ba59abbe56e057f20f883e"}';

		$url = "https://api.weixin.qq.com/customservice/kfaccount/add?access_token=".$this->access_token;
		$res = $this->https_request($url, $data);
        return json_decode($res, true);
	}
	//修改客服帐号
	public function update_kfaccount($account,$nickname,$password)
	{
		$data = '{"kf_account":"'.$account.'","nickname":"'.$nickname.'","password":"'.md5($password).'"}';
		$url = "https://api.weixin.qq.com/customservice/kfaccount/update?access_token=".$this->access_token;
		$res = $this->https_request($url, $data);
        return json_decode($res, true);

	}
	//删除客服帐号
	public function del_fkccount($account,$nickname,$password)
	{
		$data = '{"kf_account":"'.$account.'","nickname":"'.$nickname.'","password":"'.md5($password).'"}';
		$url = "https://api.weixin.qq.com/customservice/kfaccount/del?access_token=".$this->access_token;
		$res = $this->https_request($url, $data);
        return json_decode($res, true);
	}
	//设置客服帐号的头像
	public function uploadheadimg()
	{

	}
	//获取所有客服
	public function add_getkflist()
	{
		$url = "https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=".$this->access_token;
		$res = $this->https_request($url);
        return json_decode($res, true);

	}
	//发送模板消费给用户
	public function send_template_message($data)
	{
		$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$this->access_token;

		$res = $this->https_request($url,$data);
        return json_decode($res, true);
	}
	/*
		send_code:模板编号
		openid:用户微信ID
		urls:跳转URL
		temp:数据
		batch:批次，如果一次发送多个，批次大于0，单个为0
		shop_id:门店
		
	*/
	/*
		模版格式发送数据
		移车提醒 send_movecar
		$wxdata = array(
			'first'	=>'尊敬的车主您好，您的爱车需要移车，请前往查看！',
			'keyword1'	=>$location,
			'keyword2'	=>$time,
			'keyword3'	=>$mem['mem_name'],
			'remark'=>'尊敬的车主您好，您的爱车需要移车，请前往查看！',
		);

		工作提醒 work_remind
		$wxdata = array(
			'first'	=>'明日回收工作计划',
			'keyword1'	=>'10月27日回收工作',
			'keyword2'	=>'王二，张三',
			'keyword3'	=>'2017-10-27 10点',
			'keyword3'	=>'花园里小区',
			'remark'=>'请携带好工作电脑、电子秤等其他工具',
		);
		合同到期提醒 pact_fellin
		$wxdata = array(
			'first'	=>'尊敬的XX用户',
			'keyword1'	=>'GOV-CC-1311-01',
			'keyword2'	=>'2015年10月30日',
			'keyword3'	=>'2017-10-27 10点',
			'remark'=>'您的合同即将到期，如需继续使用该服务，请联系客服。',
		);
		
	*/
	public function send_wxmessage($send_code,$openid,$urls,$temp,$batch,$shop){
		global $db,$ass;
		$one = $ass->one('shop_wxmessage','','message_shop='.$shop['shop_id'].' and message_code="'.$send_code.'"');
		if($one['message_audit']=='Y'){
			if(!$temp['first']){
				$temp['first'] = $one['message_first'];
			}
			if(!$temp['remark']){
				$temp['remark'] = $one['message_remark'];
			}
			$sendArr = array();
			foreach($temp as $key =>$line){
				$sendArr[$key]= $this->encodeWxMess($line);
			}
			//if($one['message_callback']=='N'){
			//	$urls = '';
			//}
			if(trim($urls)=='')$urls = '';
			if($this->send_temp_wxmessage($one['message_template'],$openid,$urls,$sendArr)){
				$send_text = json_encode($sendArr);
				return $this->save_wxmessage($one,$openid,$send_text,$batch,$shop);
			}else{
				//如果发送不成功，插入数据库
				$this->save_wxmessage($one,$openid,$send_text,-1,$shop);
				return false;
			}
		}
	}
	//插入发送消息
	function save_wxmessage($message,$send_openid,$send_text,$batch,$shop){
		global $ass;
		if($batch>0){
			$send_do = 'N';
			$send_audit = 'N';
		}else if($batch==-1){
			$send_do = 'Y';
			$send_audit = 'N';
		}else{
			$send_do = 'N';
			$send_audit = 'N';
		}
		$data=array(
			'send_name'		=> $message['message_name'],
			'send_openid'	=> $send_openid,
			'send_text'		=> urldecode($send_text),
			'send_time'		=> date('Y-m-d H:i:s'),
			'send_leave'	=> date('Y-m-d H:i:s',strtotime('+'.$message['message_leave'].' day')),
			'send_do'		=> $send_do,
			'send_audit'	=> $send_audit,
			'send_batch'	=> $send_batch,
			'send_message'	=> $message['message_id'],
			'send_tempid'	=> $message['message_temp'],
			'send_template'	=> $message['send_template'],
			//'send_village'	=> $send_village,
			'send_shop'		=> $shop['shop_id'],
			'send_comp'		=> $shop['comp_id']
		);
		if($ass->insert('wx_send',$data)){
			return true;
		}else{
			return false;
		}
	}
	
	function encodeWxMess($str){
		return array('value'=>urlencode($str),'color'=>'#000000');
	}
	//通用发送模板
	public function send_temp_wxmessage($template_id,$openid,$urls,$temp){
		$data=array(
			'touser'=>$openid,
			'template_id'=>$template_id,
			'url'=>$urls,
			'topcolor'=>'#7B68EE',
			'data'=> $temp
		);
		return $this->send_template_message(urldecode(json_encode($data)));
	}
	/*
		1.积分变动提醒
		openid:用户微信ID
		urls:跳转路径
		mem_name:会员名称
		time:获得时间
		获得积分:cent
		info:获得原因
		nowcent:当前积分
		memo:备注信息
	*/
	public function send_cent($template_id,$openid,$urls,$mem_name,$time,$cent,$info,$nowcent,$memo='感谢您的使用'){
		$data=array(
			'touser'=>$openid,
			//'template_id'=>'N63s7jhm-MzZmYQXJiOk0BQp59OYDT2ppNGyv4Ua7A0',
			'template_id'=>$template_id,
			'url'=>$urls,
			'topcolor'=>'#7B68EE',
			'data'=> array(
				'first'=>array('value'=>urlencode('亲爱的'.$mem_name.'，您的积分账户有新的变动，具体内容如下：'),'color'=>'#000000'),
				'keyword1'=>array('value'=>urlencode($time),'color'=>'#000000'),
				'keyword2'=>array('value'=>urlencode($cent),'color'=>'#000000'),
				'keyword3'=>array('value'=>urlencode($info),'color'=>'#000000'),
				'keyword4'=>array('value'=>urlencode($nowcent),'color'=>'#000000'),
				'remark'=>array('value'=>urlencode($memo),'color'=>'#000000'),
			)
		);
		return $this->send_template_message(urldecode(json_encode($data)));
	}
	/*
		2.预约成功提醒
		openid:用户微信ID
		urls:跳转路径
		goods_name:预约项目
		time:预约时间
		memo:备注信息
	*/
	public function send_reserve($template_id,$openid,$urls,$goods_name,$time,$memo='为了完美的服务体验，请提前安排好时间'){
		$data=array(
			'touser'=>$openid,
			//'template_id'=>'Ngy6YA99uFow40rGo3kRJDY3xVdNrZtFb4ZfPE4StdY',
			'template_id'=>$template_id,
			'url'=>$urls,
			'topcolor'=>'#7B68EE',
			'data'=> array(
				'first'=>array('value'=>urlencode('您已成功预约！'),'color'=>'#000000'),
				'keyword1'=>array('value'=>urlencode($goods_name),'color'=>'#000000'),
				'keyword2'=>array('value'=>urlencode($time),'color'=>'#000000'),
				'remark'=>array('value'=>urlencode($memo),'color'=>'#000000'),
			)
		);
		return $this->send_template_message(urldecode(json_encode($data)));
	}
	/*
		3.购买成功通知
		openid:用户微信ID
		urls:跳转路径
		goods_name:商品名称
		price:商品价格
		time:购买时间
		memo:备注信息
	*/
	public function send_buysucceed($template_id,$openid,$urls,$goods_name,$price,$time,$memo='祝您生活愉快！'){
		$data=array(
			'touser'=>$openid,
			//'template_id'=>'QrQTdO8PJEIjEoRgLWzB43HfP5_m3gBX-s_xSxFYa08',
			'template_id'=>$template_id,
			'url'=>$urls,
			'topcolor'=>'#7B68EE',
			'data'=> array(
				'first'=>array('value'=>urlencode('您已成功预约！'),'color'=>'#000000'),
				'product'=>array('value'=>urlencode($goods_name),'color'=>'#000000'),
				'price'=>array('value'=>urlencode($price),'color'=>'#000000'),
				'time'=>array('value'=>urlencode($time),'color'=>'#000000'),
				'remark'=>array('value'=>urlencode($memo),'color'=>'#000000'),
			)
		);
		return $this->send_template_message(urldecode(json_encode($data)));
	}
	/*
		4.移车通知
		openid:用户微信ID
		urls:跳转路径
		car_brand:车牌号
		time:时间
		reason:原因
		memo:备注信息
	*/
	public function send_movecar1($template_id,$openid,$urls,$car_brand,$time,$reason,$memo='请速处理！'){
		$data=array(
			'touser'=>$openid,
			//'template_id'=>'UEuhCy_hPhP36lMIA8T78fXxbJk9rkBQQzV_JQYThcI',
			'template_id'=>$template_id,
			'url'=>$urls,
			'topcolor'=>'#7B68EE',
			'data'=> array(
				'first'=>array('value'=>urlencode('你好，请前去移车'),'color'=>'#000000'),
				'keyword1'=>array('value'=>urlencode($car_brand),'color'=>'#000000'),
				'keyword2'=>array('value'=>urlencode($time),'color'=>'#000000'),
				'keyword3'=>array('value'=>urlencode($reason),'color'=>'#000000'),
				'remark'=>array('value'=>urlencode($memo),'color'=>'#000000'),
			)
		);
		return $this->send_template_message(urldecode(json_encode($data)));
	}

	/*
		4.移车通知
		openid:用户微信ID
		urls:跳转路径
		location:车辆位置
		time:发起时间
		initiator:发起人
		reason:原因
		memo:备注信息
	*/
	public function send_movecar($template_id,$openid,$urls,$location,$time,$initiator,$reason,$memo='请速处理！'){
		$data=array(
			'touser'=>$openid,
			//'template_id'=>'2AjTJ_W1TTL8C9K-1Vs4bZN4HgFvPhYlciJfRPO0a6A',
			'template_id'=>$template_id,
			'url'=>$urls,
			'topcolor'=>'#7B68EE',
			'data'=> array(
				'first'=>array('value'=>urlencode('你好，请前去移车'),'color'=>'#000000'),
				'keyword1'=>array('value'=>urlencode($location),'color'=>'#000000'),
				'keyword2'=>array('value'=>urlencode($time),'color'=>'#000000'),
				'keyword3'=>array('value'=>urlencode($initiator),'color'=>'#000000'),
				'remark'=>array('value'=>urlencode($reason.$memo),'color'=>'#000000'),
			)
		);
		return $this->send_template_message(urldecode(json_encode($data)));
	}
	/*
		5.成为会员通知
		openid:用户微信ID
		urls:跳转路径
		cardNumber:会员号
		shopname:店铺名称
		address:地址
		VIPName:登记姓名
		VIPPhone:登记手机号
		expDate:有效期
		memo:备注信息
	*/
	public function send_joinmember($template_id,$openid,$urls,$cardNumber,$shopname,$address,$VIPName,$VIPPhone,$expDate,$memo=''){
		$data=array(
			'touser'=>$openid,
			//'template_id'=>'YlQhEXgxmWjN6MrZjM6D5B6vx8Iof8tc1Q6osXbHbzA',
			'template_id'=>$template_id,
			'url'=>$urls,
			'topcolor'=>'#7B68EE',
			'data'=> array(
				'first'=>array('value'=>urlencode('您好，您已成为'.$shopname.'会员。'),'color'=>'#000000'),
				'cardNumber'=>array('value'=>urlencode($cardNumber),'color'=>'#000000'),
				'shopname'=>array('value'=>urlencode($shopname),'color'=>'#000000'),
				'address'=>array('value'=>urlencode($address),'color'=>'#000000'),
				'VIPName'=>array('value'=>urlencode($VIPName),'color'=>'#000000'),
				'VIPPhone'=>array('value'=>urlencode($VIPPhone),'color'=>'#000000'),
				'expDate'=>array('value'=>urlencode($expDate),'color'=>'#000000'),
				'remark'=>array('value'=>urlencode($memo),'color'=>'#000000'),
			)
		);
		return $this->send_template_message(urldecode(json_encode($data)));
	}
	/*
		6.订单支付成功
		openid:用户微信ID
		urls:跳转路径
		orderMoneySum:支付金额
		orderProductName:商品信息
		memo:备注信息  如有问题请致电400-828-1878或直接在微信留言，小易将第一时间为您服务！
	*/
	public function send_paysucceed1($template_id,$openid,$urls,$orderMoneySum,$orderProductName,$memo=''){
		$data=array(
			'touser'=>$openid,
			//'template_id'=>'ZFVY0T1PaJ8cTeFTVFsQ7dYPMKaSr6ai4RubbKU-0B0',
			'template_id'=>$template_id,
			'url'=>$urls,
			'topcolor'=>'#7B68EE',
			'data'=> array(
				'first'=>array('value'=>urlencode('我们已收到您的货款！'),'color'=>'#000000'),
				'orderMoneySum'=>array('value'=>urlencode($orderMoneySum),'color'=>'#000000'),
				'orderProductName'=>array('value'=>urlencode($orderProductName),'color'=>'#000000'),
				'remark'=>array('value'=>urlencode($memo),'color'=>'#000000'),
			)
		);
		return $this->send_template_message(urldecode(json_encode($data)));
	}
	/*
		7.订单取消通知
		openid:用户微信ID
		urls:跳转路径
		shop_name:店铺名
		orderProductPrice:订单金额
		orderProductName:商品详情
		orderAddress:收货信息
		orderName:订单编号
		memo:备注信息  点击“详情”查看详细处理信息，如有疑问可直接回复此公众号联系京东客服。 
		详情 &gt;
	*/
	public function send_billcancel($template_id,$openid,$urls,$shop_name,$orderProductPrice,$orderProductName,$orderAddress,$orderName,$memo=''){
		$data=array(
			'touser'=>$openid,
			//'template_id'=>'bGHb5O1flLDkbsKP6KWlITmcAc-AirgmNxE5DA2N_8U',
			'template_id'=>$template_id,
			'url'=>$urls,
			'topcolor'=>'#7B68EE',
			'data'=> array(
				'first'=>array('value'=>urlencode('您的'.$shop_name.'订单已取消'),'color'=>'#000000'),
				'orderProductPrice'=>array('value'=>urlencode($orderProductPrice),'color'=>'#000000'),
				'orderProductName'=>array('value'=>urlencode($orderProductName),'color'=>'#000000'),
				'orderAddress'=>array('value'=>urlencode($orderAddress),'color'=>'#000000'),
				'orderName'=>array('value'=>urlencode($orderName),'color'=>'#000000'),
				'remark'=>array('value'=>urlencode($memo),'color'=>'#000000'),
			)
		);
		return $this->send_template_message(urldecode(json_encode($data)));
	}
	/*
		8.服务完成通知
		openid:用户微信ID
		urls:跳转路径
		Content1:内容
		Good:商品名称
		contentType:服务措施
		price:收费金额
		menu:收费标准
		memo:备注信息
	*/
	public function send_serversucceed($template_id,$openid,$urls,$Content1,$Good,$contentType,$price,$menu,$memo=''){
		$data=array(
			'touser'=>$openid,
			//'template_id'=>'nGR7gnA9iRNoDpLSDbub-WPxMV0szmov732lFrxkhak',
			'template_id'=>$template_id,
			'url'=>$urls,
			'topcolor'=>'#7B68EE',
			'data'=> array(
				'first'=>array('value'=>urlencode('尊敬的用户，您好'),'color'=>'#000000'),
				'Content1'=>array('value'=>urlencode($Content1),'color'=>'#000000'),
				'Good'=>array('value'=>urlencode($Good),'color'=>'#000000'),
				'contentType'=>array('value'=>urlencode($contentType),'color'=>'#000000'),
				'price'=>array('value'=>urlencode($price),'color'=>'#000000'),
				'menu'=>array('value'=>urlencode($menu),'color'=>'#000000'),
				'remark'=>array('value'=>urlencode($memo),'color'=>'#000000'),
			)
		);
		return $this->send_template_message(urldecode(json_encode($data)));
	}
	/*
		9.支付成功通知
		openid:用户微信ID
		urls:跳转路径
		keyword1:订单编号
		keyword2:消费金额
		keyword3:消费门店
		keyword4:消费时间
		memo:备注信息
	*/
	public function send_paysucceed2($template_id,$openid,$urls,$keyword1,$keyword2,$keyword3,$keyword4,$memo=''){
		$data=array(
			'touser'=>$openid,
			//'template_id'=>'pDTV_ASWBvTvZOrhREAYN1Sm5VWVl3XH4Pg80v64FQA',
			'template_id'=>$template_id,
			'url'=>$urls,
			'topcolor'=>'#7B68EE',
			'data'=> array(
				'first'=>array('value'=>urlencode('您好，您的微信支付已成功'),'color'=>'#000000'),
				'keyword1'=>array('value'=>urlencode($keyword1),'color'=>'#000000'),
				'keyword2'=>array('value'=>urlencode($keyword2),'color'=>'#000000'),
				'keyword3'=>array('value'=>urlencode($keyword3),'color'=>'#000000'),
				'keyword4'=>array('value'=>urlencode($keyword4),'color'=>'#000000'),
				'remark'=>array('value'=>urlencode($memo),'color'=>'#000000'),
			)
		);
		return $this->send_template_message(urldecode(json_encode($data)));
	}
	/*
		10.预约提醒
		openid:用户微信ID
		urls:跳转路径
		mem_name:车会员名称牌号
		shop_name:预约门店
		time:预约时间
		info:预约类型
		memo:备注信息
	*/
	public function send_assign($template_id,$openid,$urls,$mem_name,$shop_name,$time,$info,$memo){
		$data=array(
			'touser'=>$openid,
			//'template_id'=>'pEn4NW4QYF8FEl8inzDe5Zq-P-iMsRgItDqRyWl1Jmc',
			'template_id'=>$template_id,
			'url'=>$urls,
			'topcolor'=>'#7B68EE',
			'data'=> array(
				'first'=>array('value'=>urlencode('你好。您已预定成功'),'color'=>'#000000'),
				'keyword1'=>array('value'=>urlencode($mem_name),'color'=>'#000000'),
				'keyword2'=>array('value'=>urlencode($shop_name),'color'=>'#000000'),
				'keyword3'=>array('value'=>urlencode($time),'color'=>'#000000'),
				'keyword4'=>array('value'=>urlencode($info),'color'=>'#000000'),
				'remark'=>array('value'=>urlencode($memo),'color'=>'#000000'),
			)
		);
		return $this->send_template_message(urldecode(json_encode($data)));
	}
	/*
		11.会员消费提醒
		openid:用户微信ID
		urls:跳转路径
		mem_name:会员名称
		keyword1:会员卡号
		keyword2:店铺名称
		keyword3:消费金额
		memo:备注信息
	*/
	public function send_save($template_id,$openid,$urls,$mem_name,$keyword1,$keyword2,$keyword3,$memo=''){
		$data=array(
			'touser'=>$openid,
			//'template_id'=>'rd1xeDGubL0W63vN7rm_Z6Alm-zxVgrAV_WmPGr2Obk',
			'template_id'=>$template_id,
			'url'=>$urls,
			'topcolor'=>'#7B68EE',
			'data'=> array(
				'first'=>array('value'=>urlencode('尊敬的'.$mem_name.'，您已消费成功'),'color'=>'#000000'),
				'keyword1'=>array('value'=>urlencode($keyword1),'color'=>'#000000'),
				'keyword2'=>array('value'=>urlencode($keyword2),'color'=>'#000000'),
				'keyword3'=>array('value'=>urlencode($keyword3),'color'=>'#000000'),
				'remark'=>array('value'=>urlencode($memo),'color'=>'#000000'),
			)
		);
		return $this->send_template_message(urldecode(json_encode($data)));
	}
	/*
		12.会员充值通知
		openid:用户微信ID
		urls:跳转路径
		accountType:卡类型
		account:卡号
		amount:充值金额
		result:充值状态

		{{accountType.DATA}}：{{account.DATA}}
		充值金额：{{amount.DATA}}
		充值状态：{{result.DATA}}
		memo:备注信息
	*/
	public function send_charge($template_id,$openid,$urls,$mem_name,$accountType,$account,$amount,$result,$memo=''){
		$data=array(
			'touser'=>$openid,
			//'template_id'=>'thWhTH5z80A27cILiDwjFza0i3gDkduzdhmXcQTsbKg',
			'template_id'=>$template_id,
			'url'=>$urls,
			'topcolor'=>'#7B68EE',
			'data'=> array(
				'first'=>array('value'=>urlencode('尊敬的'.$mem_name.'，您已成功进行会员卡充值。'),'color'=>'#000000'),
				'accountType'=>array('value'=>urlencode($accountType),'color'=>'#000000'),
				'account'=>array('value'=>urlencode($account),'color'=>'#000000'),
				'amount'=>array('value'=>urlencode($amount),'color'=>'#000000'),
				'result'=>array('value'=>urlencode($result),'color'=>'#000000'),
				'remark'=>array('value'=>urlencode($memo),'color'=>'#000000'),
			)
		);
		return $this->send_template_message(urldecode(json_encode($data)));
	}
    //https请求（支持GET和POST）
    protected function https_request($url, $data = null)
    {	
        $curl = curl_init();
		$timeout = 5;
        
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // 跳过证书检查  
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);  // 从证书中检查SSL加密算法是否存在
		curl_setopt($curl, CURLOPT_URL, $url);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		//curl_setopt ($curl, CURLOPT_CONNECTTIMEOUT, $timeout); 
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
		
    }
}
?>