ThinkPHP 3.2 微信公众号模板消息发送,多种模板消息,带参数加密解密

发布时间:2017年12月15日 17:11:46编辑:admin阅读(1326)评论(0)

    微信发送模板消息,应用在各种需要的地方,每次都写一大堆比较麻烦

    虽然程序不怕麻烦

    但是谁敢说又不麻烦,真的是不怕被打屎。

    活在客户的无理,项目经理的不麻烦下,不仅要代码敲得快,还需要刀防身吧(作战吧)

    哈哈哈哈


    好了,模板消息不难,公众号申请了自定义消息之后,添加模板就可以使用了

    我使用的事 ThinkPHP 3.2 放在function.php 里面,放在公共模块里,就是一创口贴

    哪里需要,贴!哪!里!


    <?php  
    /*
    	@author 逍客 <1078576480@qq.com> 
    */
    //***********************************检测是否带sql语句攻击**************************************
    function get_strstr($set_sql){
    	if(stristr($set_sql,'drop')){
    		return '含有drop';
    	}else if(stristr($set_sql,'truncate')){
    		return '含有truncate';
    	}else if(stristr($set_sql,'delete')){
    		return '含有delete';
    	}else{
    		return 1;
    	}
    }
    //***********************************加密解密****************************************************
    /* 加密
     * $string 		  加密解密字符串
     * C('code_skey') 加密解密密匙
     * C('code_type') 加密解密字符替换
     * C('code_num')  加密解密数字替换
     */
     //加密
    function get_encode($string) {
    	$strArr = str_split(base64_encode($string));
    	$strCount = count($strArr);
    	foreach (str_split(C('code_skey')) as $key => $value)
    		$key < $strCount && $strArr[$key].=$value;
    	return str_replace(C('code_type'), C('code_num'), join('', $strArr));
    }
    //解密
    function get_decode($string) {
    	$strArr = str_split(str_replace(C('code_num'), C('code_type'), $string), 2);
    	$strCount = count($strArr);
    	foreach (str_split(C('code_skey')) as $key => $value)
    		$key <= $strCount  && isset($strArr[$key]) && $strArr[$key][1] === $value && $strArr[$key] = $strArr[$key][0];
    	return base64_decode(join('', $strArr));
    }
    //获取用户信息
    /*
      $id   加密的用户id 
      $type  需要返回的数据
    */
    function get_user_info($id,$type){
        $usda=M("wx_users")->where("id=".get_decode($id))->find();
    	if(!$usda){
    		return '平台';
    	}else if ($type == "n") {
           if($usda["nickname"]){
    			return $usda["nickname"];
    		}else{
    			return $usda["tel"];
    		}
        }else if ($type == "t") {
           return '20'.date('y-m-d H:i:s',$usda["create_time"]);
        }else{
    		return $usda[$type];
        }
    }
    /*
    	$uid 加密的用户id
    	$type  发送那条模板消息
    	$orderid  订单id  我这是根据订单发送模板消息,及后续操作 参数可以自定义,看需要修改
    */
    //模板消息分类列表
    function template_type($uid,$type,$orderid){
    	if($type=='newbuy'){
    		$arr=[
    			'name'=>['value'=>'购买'.orderid($orderid,'n').'成功', 'color'=>'#FF0000'],
    			'remark'=>['value'=>'查看详细信息','color'=>'#000000']
    		];
    		template_home(
    			'ELmPgTF6TxPS8w8DfZxxxxxxxxxxxxxxxxxxxxxx',
    			get_user_info($uid,'openid'),
    			'http://'.$_SERVER['HTTP_HOST'].'/Home/User/us_detail?id='.$orderid,
    			$arr
    		);
    	}else if($type=='new'){
    		$arr=[
    			'tradeDateTime'=>['value'=>date("Y年m月d日 H:i",time()), 'color'=>'#FF0000'],
    			'orderType'=>['value'=>'消费订单', 'color'=>'#FF0000'],
    			'customerInfo'=>['value'=>get_user_info(orderid($orderid,'u'),'n'), 'color'=>'#FF0000'],
    			'orderItemName'=>['value'=>'购买回报', 'color'=>'#FF0000'],
    			'orderItemData'=>['value'=>orderid($orderid,'n'), 'color'=>'#FF0000'],
    			'remark'=>['value'=>'查看详细信息','color'=>'#000000']
    		];
    		template_home(
    			'uMeZSwL0kN6sU_wgHd52eGHsxxxxxxxxxxxxxxxxxx',
    			get_user_info($uid,'openid'),
    			'http://'.$_SERVER['HTTP_HOST'].'/Home/User/us_detail?id='.$orderid,
    			$arr
    		);
    	}else if($type=='fahuo'){
    		$arr=[
    			'delivername'=>['value'=>orderid($orderid,'w'), 'color'=>'#FF0000'],
    			'ordername'=>['value'=>orderid($orderid,'g'), 'color'=>'#FF0000'],
    			'remark'=>['value'=>orderid($orderid,'n'), 'color'=>'#FF0000'],
    			'remark'=>['value'=>'点击查看物流详情','color'=>'#000000']
    		];
    		template_home(
    			'nJ3WBjVZfJ9MCaj_jNY_shGxxxxxxxxxxxxxxx',
    			get_user_info($uid,'openid'),
    			'http://'.$_SERVER['HTTP_HOST'].'/Home/User/us_detail?id='.$orderid,
    			$arr
    		);
    	}
    }
    
    /*
    	$temp  		模板id 公众号自定义消息获取
    	$openid   	接收模板消息的用户openID  (必须要关注了该公众号的)
    	$url      	点击模板消息跳转的连接地址
    	$contend	模板消息内容,具体看微信公号自定义消息的参数
    */
    //模板消息
    function template_home($temp,$openid,$url,$contend){
    	$wx=C("WX_PAY_CONFIG");
    	$appid = $wx["appid"];
    	$appsecret = $wx["appsecret"];
        $ch = curl_init();
    	curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    	// 我们在POST数据哦!
    	curl_setopt($ch, CURLOPT_POST, 1);
    	// 把post的变量加上
    	curl_setopt($ch, CURLOPT_POSTFIELDS, $data="");
    	$json_token = curl_exec($ch);
    	curl_close($ch);
    	$access_token=json_decode($json_token,true);
    	
        //模板消息
        $template=array(
            'touser'=>$openid,
            'template_id'=>$temp,
            'url'=>$url,
            'topcolor'=>"#ff6699",
            'data'=>$contend,
        );
        $json_template=json_encode($template);
        $url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token["access_token"];
        $res=httprequest($url,urldecode($json_template));
        return $res;
    }
    //CURL 请求
    function httprequest($url,$data=array()){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        // 我们在POST数据哦!
        curl_setopt($ch, CURLOPT_POST, 1);
        // 把post的变量加上
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
    ?>