分享自己封装的微信公众号类 基于ThinkPHP5.1

分享自己封装的微信公众号类 基于ThinkPHP5.1

// An highlighted block
<?php
namespace app\util;

use think\Controller;

class Wxutil
{

    /*
    * 微信验证
    * */
    public function check(){
        $param = input('param.');
        $signature = $param['signature'];
        $timestamp = $param['timestamp'];
        $nonce = $param['nonce'];
        $echostr = $param['echostr'];
        //字典排序
        $system = FF('config/wxconfig');
        $arrayname = array($timestamp,$nonce,$system['token']);
        sort($arrayname, SORT_STRING);
        $temp = implode($arrayname);
        $temp = sha1($temp);
        if($temp==$signature){
            return $echostr;
        }else{
            return false;
            exit;
        }
    }

    /*
     * 关注后回复信息
     * */
    public function reponseMsg(){
        $postArr = file_get_contents('php://input');
        $postobj = simplexml_load_string($postArr);
        $rx_type = strtolower($postobj->MsgType);
        switch($rx_type){
            case 'event':
                $resultStr = $this->handleEvent($postobj);
            break;
            case 'text':
                $resultStr = $this->handleText($postobj);
            break;
            case 'image':
                $resultStr = $this->sendutypeal($postobj);
                break;
            case 'voice':
                $resultStr = $this->sendutypeal($postobj);
                break;
            case 'video':
                $resultStr = $this->sendutypeal($postobj);
                break;
            case 'shortvideo':
                $resultStr = $this->sendutypeal($postobj);
                break;
            case 'location':
                $resultStr = $this->sendutypeal($postobj);
                break;
            case 'link':
                $resultStr = $this->sendutypeal($postobj);
                break;
            default:
                $resultStr = "Unknow msg type: ".$rx_type;
        }
        echo $resultStr;
    }

    //关注回复
    public function handleEvent($object)
    {
        $contentStr = "";
        $rx_type = strtolower($object->Event);
        switch($rx_type){
            //首次关注
            case "subscribe":
                    $type = 2;
                    $data = $this->getfolwlist($type);
                    if($data){
                        echo  $this->sendFormUser($data['type'], $data['reply'],$object);
                    }
                    break;
            case 'unsubscribe':
                $keyword = '取消关注';
                $data = [
                    ['keyword'=>'取消关注','reply'=>'谢谢关注','type'=>'text'],
                ];
                break;
            case 'scan':
                echo $this->responseText($object,'您已经关注过楼');
            case 'click':
                return $this->checkClick($object,$object->EventKey);
                break;
            case 'location':
                file_put_contents('dlwz.json', '{"latitude": "'.$object->Latitude.'", "wd": '.$object->Longitude.'}');
                return  $this->responseText($object,'您地址的经纬度为'.$object->Latitude.','.$object->Longitude);
                break;
                default;
        }
        if($data){
            $is_keyw = $this->is_keyword($keyword,$data);
            if(!$is_keyw){
                $contentStr = $keyword;
                echo $this->responseText($object,$contentStr);
            }else{
                $is_str = $this->is_keytype($is_keyw,$data);
                switch ($is_str){
                    case 'text':
                        echo $this->responseText($object,$is_keyw);
                        break;
                    case 'imagtext':
                        echo  $this->handleImtw($object,$is_keyw);
                        break;
                    case 'voice':
                        echo  $this->handlevoice($object,$is_keyw);
                        break;
                    case 'image':
                        echo $this->responseimg($object,$is_keyw);
                        break;
                    case 'video':
                        echo $this->handlvideo($object,$is_keyw);
                        break;
                    case 'music':
                        echo $this->handmusic($object,$is_keyw);
                        break;
                    default;
                }
            }
        }
//        $resultStr = $this->responseText($object,$contentStr);
//        return $resultStr;
    }

    /*
     * 发送客服消息
     * */
    public function messageToUserName($content){
        $userlist = $this->getuserlist();
        $acctoken = $this->getWxAccessToken();
        $data = [
            'touser' => 'otFQV1U8ud9-it0i67eQd223GWvU',
            'msgtype' => 'text',
            'text' => [
                'content' => iconv("ISO-8859-1","UTF-8",$content),
            ]
        ];
        $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$acctoken;
        $result = $this->http_curl($url,'post','json',json_encode($data));
        return $result;
    }

    /*
    * 处理click
    * */
    public function checkClick($postobj,$eventkey){
        $menulist = $this->getmenuclick($eventkey);
        $data = $menulist['reply'];
        return $this->handleImtw($postobj,$data);
//        switch($eventkey){
//            case 'news';
//                $data=array(
//                    array(
//                        'Title'=>'3000万的故事',
//                        'Description'=>'3000万的颠沛流离',
//                        'PicUrl'=>'http://vwzf3a.natappfree.cc/static/img/timg.jpg',
//                        'Url'=>'http://www.baidu.com',
//                    ),
//                    array(
//                        'Title'=>'3001万的故事',
//                        'Description'=>'3000万的颠沛流离',
//                        'PicUrl'=>'http://vwzf3a.natappfree.cc/static/img/2.jpg',
//                        'Url'=>'http://www.baidu.com',
//                    ),
//                    array(
//                        'Title'=>'3002万的故事',
//                        'Description'=>'3000万的颠沛流离',
//                        'PicUrl'=>'http://vwzf3a.natappfree.cc/static/img/1.png',
//                        'Url'=>'http://www.baidu.com',
//                    ),
//                );
//
//                return $this->handleImtw($postobj,$data);
//                break;
//            default;
    }

    //处理语音消息
    public function receiveVoice($postobj){
        $recogniton = $postobj->Recognition;
        return $this->responseText($postobj,$recogniton);
    }

    //处理语音图片视频小视频地理位置链接等消息
    public function sendutypeal($postobj){
        $type = 2;
        $data = $this->getautolist($type);
        echo $this->sendFormUser($data['type'],$data['reply'],$postobj);
    }

    //关键字回复
    public function handleText($postobj)
    {
        $keyword = trim($postobj->Content);
        if(!empty( $keyword )){
            $data = $this->getkeywordlist();
//            $data = [
//                ['keyword'=>'新闻','reply'=>[
//                    ['Title'=>'3000万的故事','Description'=>'一万次悲伤依然会有dream','PicUrl'=>'http://vwzf3a.natappfree.cc/static/img/timg.jpg','Url' => 'http://www.baidu.com'],
//                    ['Title'=>'3001万的故事','Description'=>'改变','PicUrl'=>'http://vwzf3a.natappfree.cc/static/img/2.jpg','Url' => 'http://www.baidu.com'],
//                    ['Title'=>'3002万的故事','Description'=>'一万道光','PicUrl'=>'http://vwzf3a.natappfree.cc/static/img/1.png','Url' => 'http://www.baidu.com']],'type'=>'imagtext'],
//                ['keyword'=>'新闻','reply'=>
//                    [['Title'=>'3000万的故事','Description'=>'一万次悲伤依然会有dream','PicUrl'=>'http://vwzf3a.natappfree.cc/static/img/timg.jpg','Url' => 'http://www.baidu.com']]
//
//
//                    ,'type'=>'imagtext'],
//                ];
//                ['keyword'=>'您好','reply'=>'哈哈哈','type'=>'text'],
//                ['keyword'=>'您好鸭','reply'=>'gxn','type'=>'text'],
//                ['keyword'=>'hih','reply'=>'hih嘻嘻嘻','type'=>'text'],
//                ['keyword'=>'新闻','reply'=>[
//                    ['Title'=>'3000万的故事','Description'=>'一万次悲伤依然会有dream','PicUrl'=>'http://vwzf3a.natappfree.cc/static/img/timg.jpg','Url' => 'http://www.baidu.com'],
//                    ['Title'=>'3001万的故事','Description'=>'改变','PicUrl'=>'http://vwzf3a.natappfree.cc/static/img/2.jpg','Url' => 'http://www.baidu.com'],
//                    ['Title'=>'3002万的故事','Description'=>'一万道光','PicUrl'=>'http://vwzf3a.natappfree.cc/static/img/1.png','Url' => 'http://www.baidu.com']],'type'=>'imagtext'],
//                ['keyword'=>'语音','reply'=>'AKby_r2npN2eJnBTnxj11blEBqCy2CaUuIN1tMSWaBpuhTkQ8serv4Bz8wX8LPv3','type'=>'voice'],
//                ['keyword'=>'图片','reply'=>'PRgUnUEaVckE9lUpL4juIyndTyssJVOzMLzRuBsXeJb51Ml4wV6U0Z1375rQbmeN','type'=>'image'],
//                ['keyword'=>'视频','reply'=>['mid'=>'Wyu5SDz0Os-4weMuNiAGrFep4bnmq0d4MV73coK7xzOe47Mmy-8zL4j6AsFJr1Bf','title'=>'测试','description'=>'测试'],'type'=>'video'],
//                ['keyword'=>'音乐','reply'=>['musicurl'=>'http://vwzf3a.natappfree.cc/static/music/1820926991.mp3','title'=>'寂寞沙洲冷','description'=>'寂寞沙洲冷'],'type'=>'music'],
                ['keyword'=>'视频','reply'=>'Wyu5SDz0Os-4weMuNiAGrFep4bnmq0d4MV73coK7xzOe47Mmy-8zL4j6AsFJr1Bf','type'=>'video'],
//                ];
            echo $this->sendofuserda($postobj,$keyword,$data);
        }
    }

    public function sendofuserda($postobj,$keyword,$data){
        $is_keyw = $this->is_keyword($keyword,$data);
        if(!$is_keyw){
            $type = 2;
            $data = $this->getautolist($type);
            if($data){
                return $this->sendFormUser($data['type'],$data['reply'],$postobj);
            }
        }else{
            $is_str = $this->is_keytype($is_keyw,$data);
            return $this->sendFormUser($is_str,$is_keyw,$postobj);
        }
    }

    public function sendFormUser($is_str,$is_keyw,$postobj){
        switch ($is_str){
            case 'text':
                return $this->responseText($postobj,$is_keyw);
                break;
            case 'imagtext':
                echo  $this->handleImtw($postobj,$is_keyw);
                break;
            case 'voice':
                echo  $this->handlevoice($postobj,$is_keyw);
                break;
            case 'image':
                echo $this->responseimg($postobj,$is_keyw);
                break;
            case 'video':
                echo $this->handlvideo($postobj,$is_keyw);
                break;
            case 'music':
                echo $this->handmusic($postobj,$is_keyw);
                break;
            default;
        }
    }
    //关键词验证
    public function is_keyword($keyword,$data){
        foreach($data as $key=>$value){
//            if($keyword==$value['keyword']){
//                return $value['reply'];
//            }
//            if(strstr($value['keyword'],$keyword)){
//                return $value['reply'];
//            }
            if(strpos($value['keyword'],$keyword) !== false){
                return $value['reply'];
            }
        }
        return false;
    }
    //消息类型验证
    public function is_keytype($is_keyw,$data){
        foreach($data as $key=>$value){
            if($is_keyw==$value['reply']){
                return $value['type'];
            }
        }
    }
    //图文回复
    public function handleImtw($postobj,$data){
        $str = "";
        foreach ($data as $key => $value) {
            $str.="<item>
				<Title><![CDATA[".$value['Title']."]]></Title> 
				<Description><![CDATA[".$value['Description']."]]></Description>
				<PicUrl><![CDATA[".$value['PicUrl']."]]></PicUrl>
				<Url><![CDATA[".$value['Url']."]]></Url>
				</item>";
        }
        $xml='	<xml>
				<ToUserName><![CDATA[%s]]></ToUserName>
				<FromUserName><![CDATA[%s]]></FromUserName>
				<CreateTime>%d</CreateTime>
				<MsgType><![CDATA[news]]></MsgType>
				<ArticleCount>'.count($data).'</ArticleCount>
				<Articles>
				'.$str.'
				</Articles>
				</xml> ';
        echo sprintf($xml,$postobj->FromUserName,$postobj->ToUserName,time());
    }
    //图回复
    public function handleImage($postobj){
        $fromUsername = $postobj->FromUserName;
        $toUsername = $postobj->ToUserName;
        $mediaid = $postobj->MediaId;
        $textTpl = "<xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%d</CreateTime>
                    <MsgType><![CDATA[image]]></MsgType>
                    <Image>
                    <MediaId><![CDATA[%s]]></MediaId>
                    </Image>
                    </xml>";
        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(),$mediaid);
        echo $resultStr;

    }
    /*
     * 关键词消息回复
     * */
    public function responseText($object, $content, $flag=0)
    {
        $textTpl = "<xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[text]]></MsgType>
                    <Content><![CDATA[%s]]></Content>
                    <FuncFlag>%d</FuncFlag>
                    </xml>";
        $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag);
        return $resultStr;
    }
    /*
     * 关键词图片回复
     * */
    public function responseimg($object,$mid)
    {
        $texttpl = "<xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[image]]></MsgType>
                    <Image>
                    <MediaId><![CDATA[%s]]></MediaId>
                    </Image>
                    </xml>";
        $resultStr = sprintf($texttpl, $object->FromUserName, $object->ToUserName,time(),$mid);
        return $resultStr;
    }
    /*
     * 关键词语音回复
     * */
    public function handlevoice($object,$mid){
        $texttpl = "<xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[voice]]></MsgType>
                    <Voice>
                        <MediaId><![CDATA[%s]]></MediaId>
                    </Voice>
                    </xml>";
        $resultStr = sprintf($texttpl, $object->FromUserName, $object->ToUserName,time(),$mid);
        return $resultStr;
    }
    /*
     * 关键词视频回复
     * */
    public function handlvideo($object,$vodo){
        $texttpl = "<xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[video]]></MsgType>
                    <Video>
                     <MediaId><![CDATA[%s]]></MediaId>
                        <Title><![CDATA[%s]]></Title>
                        <Description><![CDATA[%s]]></Description>
                    </Video>
                    </xml>";
        $resultStr = sprintf($texttpl, $object->FromUserName, $object->ToUserName,time(),$vodo['mid'],$vodo['title'],$vodo['description']);
        return $resultStr;
    }
    /*
     * 关键词音乐回复
     * */
    public function handmusic($object,$vodo){
        $texttpl = "<xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[music]]></MsgType>
                    <Music>
                    <Title><![CDATA[%s]]></Title>
                    <Description><![CDATA[%s]]></Description>
                    <MusicUrl><![CDATA[%s]]></MusicUrl>
                    <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
                    </Music>
                    </xml>";
        file_put_contents('121212.txt',$vodo);
        $resultStr = sprintf($texttpl,$object->FromUserName,$object->ToUserName,time(),$vodo['title'],$vodo['description'],$vodo['musicurl'],$vodo['musicurl']);
        file_put_contents('121212.txt',$resultStr);
        return $resultStr;
    }

    /*
     * 获取用户信息
     * */
    public function getuserinfo($object){
        $AccessToken = $this->getWxAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$AccessToken&openid=$object->FromUserName&lang=zh_CN";
        $userinfo = $this->http_curl($url,'get','json');
        return $userinfo;
    }

    /*
     * 创建菜单
     * */
    public function  definedItem($postArr){
        //创建微信菜单
        //目前微信接口的调用方式都是通过 curl post/get
        header('content-type:text/html;charset=utf-8');
        $access_token = $this ->getWxAccessToken();
        $url ='https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$access_token;
        $postJson = urldecode(json_encode($postArr));
        $res = $this->http_curl($url,'post','json',$postJson);
        return $res;
    }

    /*
     * 查询菜单
     * */
    public function  getusermenu(){
        //创建微信菜单
        //目前微信接口的调用方式都是通过 curl post/get
        header('content-type:text/html;charset=utf-8');
        $access_token = $this ->getWxAccessToken();
        $url ='https://api.weixin.qq.com/cgi-bin/menu/get?access_token='.$access_token;
        $res = $this->http_curl($url,'post','json');
        return $res;
    }

    /*
     * 删除菜单
     * */
    public function  delusermenu(){
        //创建微信菜单
        //目前微信接口的调用方式都是通过 curl post/get
        header('content-type:text/html;charset=utf-8');
        $access_token = $this ->getWxAccessToken();
        $url ='https://api.weixin.qq.com/cgi-bin/menu/delete?access_token='.$access_token;
        $res = $this->http_curl($url,'post','json');
        return $res;
    }

    /*
     * 发送请求参数
     * */
    public function http_curl($url,$type='get',$res='json',$arr=''){
        $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);
        if($type == 'post'){
            curl_setopt($ch,CURLOPT_POST,1);
            curl_setopt($ch,CURLOPT_POSTFIELDS,$arr);
        }
        $output =curl_exec($ch);
        if($res=='json'){
            if(curl_error($ch)) {
                return curl_error($ch);
            }else{
                return json_decode($output,true);
            }
        }
        curl_close($ch);
        echo var_dump($output);
    }


    /*
     *返回二进制
     * */
    public function http_tourl($url,$type='get',$arr=''){
        $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);
        if($type == 'post'){
            curl_setopt($ch,CURLOPT_POST,1);
            curl_setopt($ch,CURLOPT_POSTFIELDS,$arr);
        }
        $output =curl_exec($ch);
        curl_close($ch);
        return $output;
//        echo var_dump($output);
    }


    /*
     * 获取accesstoken
     * */
    public function getWxAccessToken(){
        $res = file_get_contents('access_token.json');
        $result = json_decode($res, true);
        return $result["access_token"];
    }

    /*
    * 清除接口缓存
     */
    public function clear_quea(){
        header('content-type:text/html;charset=utf-8');
        $access_token = $this ->getWxAccessToken();
        $url ='https://api.weixin.qq.com/cgi-bin/clear_quota?access_token='.$access_token;
        $appid = ['appid' => 'wxe6c34af4726ebd5b'];
        $arr = json_encode($appid,true);
        $res = $this->http_curl($url,'post','json',$arr);
        var_dump($res);
    }

    /*
     * 获取用户列表
     * */
    public function getuserlist(){
        $getWxAccessToken = $this->getWxAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$getWxAccessToken;
        $res = $this->http_curl($url);
        return $res['data'];
    }

    /*
     * 群发文本
     * */
    public function sendText($content){
        $access_token = $this->getWxAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=$access_token";
        $getuserlist = $this->getuserlist();
        $arr = array(
            'touser'=> $getuserlist['openid'],
            'msgtype'=>'text',
            'text'=>array('content'=> iconv("ISO-8859-1","UTF-8",$content)),
        );
        $dataJson = json_encode($arr);
        $res = $this->http_curl($url,'post','json',$dataJson);
        return $res;
    }

    /*
     * 群发图文
     * */
    public function sendtimg($content){
        $access_token = $this->getWxAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=$access_token";
        $getuserlist = $this->getuserlist();
        $arr = array(
            'touser'=> $getuserlist['openid'],
            'msgtype'=>'mpnews',
            'send_ignore_reprint' => 0,
            'mpnews'=>array('media_id'=> $content),
        );
        $dataJson = json_encode($arr);
        $res = $this->http_curl($url,'post','json',$dataJson);
        return $res;
    }

    /*
     * 群发图片
     * */
    public function sendImage($content){
        $access_token = $this->getWxAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=$access_token";
        $getuserlist = $this->getuserlist();
        $arr = array(
            'touser'=> $getuserlist['openid'],
            'msgtype'=>'image',
            'image'=>array('media_id' => $content),
        );
        $dataJson = json_encode($arr);
        $res = $this->http_curl($url,'post','json',$dataJson);
        return $res;
    }

    /*
     * 群发语音
     * */
    public function sendvoice($content){
        $access_token = $this->getWxAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=$access_token";
        $getuserlist = $this->getuserlist();
        $arr = array(
            'touser'=> $getuserlist['openid'],
            'msgtype'=>'voice',
            'voice'=>array('media_id' => $content),
        );
        $dataJson = json_encode($arr);
        $res = $this->http_curl($url,'post','json',$dataJson);
        return $res;
    }

    /*
     * 群发视频
     * */
    public function sendvideo($content){
        $access_token = $this->getWxAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=$access_token";
        $getuserlist = $this->getuserlist();
        $arr = array(
            'touser'=> $getuserlist['openid'],
            'msgtype'=>'mpvideo',
            'mpvideo'=>array('media_id' => $content,'title'=>'test','description'=>'test'),
        );
        $dataJson = json_encode($arr);
        $res = $this->http_curl($url,'post','json',$dataJson);
        return $res;
    }

    /*
     * 上传群发视频
     * */
    public function uploadsendvideo($content){
        $access_token = $this->getWxAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/media/uploadvideo?access_token=$access_token";
        $dataJson = json_encode($content);
        $res = $this->http_curl($url,'post','json',$dataJson);
        return $res;
    }

    /*
     * 添加用户消息记录
     * */
    public function  userRecord(){

    }

    /*
     *添加关注用户
     * */
    public function addmember(){

    }

    /*
     *新增临时素材
     * @ type  上传类型 如image voice
     * @ weburl 文件路径 文件在服务器上的绝对路径 如 './static/img/timg.jpg'
     * */
    public function temporarydata($type,$weburl){
        $accesstoken = $this->getWxAccessToken();
        $url = 'https://api.weixin.qq.com/cgi-bin/media/upload?access_token='.$accesstoken.'&type='.$type;
        $path =  array('media' => new \CURLFile(realpath($weburl['tmp_name']),$weburl['type'],$weburl['name']));
        $resulf = $this->http_curl($url,'post','json',$path);
        return $resulf;
    }

    /*
     *获取临时素材
     * @ mediaid 素材id 新增时会返回
     * */
    public function gettemporarylist($mediaid){
        $accesstoken = $this->getWxAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=".$accesstoken."&media_id=".$mediaid;
        return $url;
    }

    /*
     * 高清语音素材获取接口
     * @ mediaid 素材id 新增时会返回
     * */
    public function gettemporaryVoice($mediaid){
        $accesstoken = $this->getWxAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/media/get/jssdk?access_token=".$accesstoken."&media_id=".$mediaid;
        return $url;
    }


    /*
     *新增永久图文
     * */
    public function permanenimgtex($json){
        $accesstoken = $this->getWxAccessToken();
        $url = 'https://api.weixin.qq.com/cgi-bin/material/add_news?access_token='.$accesstoken;
        $resulf = $this->http_curl($url,'post','json',$json);
        return $resulf;
    }

    /*
     *新增永久素材
     * */
    public function permanendata($path,$type,$title="",$dis=""){
        $accesstoken = $this->getWxAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=".$accesstoken."&type=".$type;
        if($type=="video"){
            $b = json_encode(array('title'=>$title, 'introduction'=>$dis));
            $res = array('media' => new \CURLFile(realpath($path['tmp_name']),$path['type'],$path['name']),'description'=>$b);
            $is_send = $this->http_curl($url,'post','json',$res);
        }else{
            $data = array('media' => new \CURLFile(realpath($path['tmp_name']),$path['type'],$path['name']));
            $is_send = $this->http_curl($url,'post','json',$data);
        }
        // 判断是否调用成功
        return $is_send;
    }

    /*
     *获取永久素材
     * */
    public function permanentlist($media){
        $accesstoken = $this->getWxAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=" .$accesstoken;
        $arr = json_encode(array('media_id' => $media));
        $res = $this->http_tourl($url,'post',$arr);
        // 判断是否调用成功
        return $res;
    }

    /*
     *删除永久素材
     * */
    public function permanentdel($media){
        $accesstoken = $this->getWxAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/material/del_material?access_token=".$accesstoken;
        $arr = json_encode(array('media_id' => $media),true);
        $res = $this->http_curl($url,'post','json',$arr);
        // 判断是否调用成功
        return $res;
    }

    /*
     *修改永久素材
     * */
    public function parmanentedit($json){
        $accesstoken = $this->getWxAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/material/update_news?access_token=" .$accesstoken;
        $resulf = $this->http_curl($url,'post','json',$json);
        return $resulf;
    }

    /*
     *获取素材总数
     * */
    public function parmanentcount(){
        $accesstoken = $this->getWxAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token=" .$accesstoken;
        $res = $this->http_curl($url);
        // 判断是否调用成功
        return $res;
    }

    /*
     *获取素材列表
     * */
    public function materiallist($param){
        $accesstoken = $this->getWxAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" .$accesstoken;
        $arr = json_encode(array('type' => $param['type'],'offset'=>$param['offset'],'count'=>$param['count']));
        $res = $this->http_curl($url,'post','json',$arr);
        return $res;
    }

    /*
     * 获取微信自动回复规则
     * */
    public function autoreply(){
        $accesstoken = $this->getWxAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/get_current_autoreply_info?access_token=".$accesstoken;
        $res = $this->http_curl($url);
    }

    /*
     * 获取当前关键词
     * */
    public function getkeywordlist(){
        $keywordarr = db('hinge_reply')->field('keyword,reply,type')->select();
        foreach($keywordarr as $key => $value){
            if($value['type']=='imagtext' || $value['type']=='video' || $value['type']=='music'){
                $keywordarr[$key]['reply'] = json_decode($value['reply'],true);
            }
        }
        return $keywordarr;
    }

    /*
     * 获取消息自动回复
     * */
    public function getautolist($type){
        if($type==2){
            $keywordarr = db('auto_reply')->where(['is_open'=>1])->field('reply,type')->find();
            if(!$keywordarr){
                $keywordarr = db('auto_reply')->where(['is_open'=>2])->field('reply,type')->find();
            }
            if($keywordarr['type']=='imagtext' || $keywordarr['type']=='video' || $keywordarr['type']=='music'){
                $keywordarr['reply'] = json_decode($keywordarr['reply'],true);
            }
        }else{
            $keywordarr = db('auto_reply')->select();
        }
        return $keywordarr;
    }

    /*
     * 获取关注后自动回复
     * */
    public function getfolwlist($type){
        if($type==2){
            $keywordarr = db('follow_reply')->where(['is_open'=>1])->field('reply,type')->find();

            if($keywordarr['type']=='imagtext' || $keywordarr['type']=='video' || $keywordarr['type']=='music'){
                $keywordarr['reply'] = json_decode($keywordarr['reply'],true);
            }
        }else{
            $keywordarr = db('follow_reply')->select();
        }
        return $keywordarr;
    }

    /*
     * 处理菜单click事件
     * */
    public function getmenuclick($even){
        $menulist = db('menu_list')->where(['dentif'=>$even,'is_default'=>0])->field('dentif,reply,type')->find();
        if(!$menulist){
            $menulist = db('menu_list')->where(['dentif'=>$even,'is_default'=>1])->field('dentif,reply,type')->find();
        }
        $menulist['reply'] = json_decode($menulist['reply'],true);
//        foreach($menulist as $key=>$value)
//            $menulist[$key]['reply'] = json_decode($value['reply'],true);
//        }
        return $menulist;
    }

    /*
     * 上传图文消息内的图片获取URL
     * */
    public function uploadimgs($data,$dataname){
        $accesstoken = $this->getWxAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=".$accesstoken;
        $res = $this->http_curl($url,'post','json',array('media' => new \CURLFile($data,'image/jpeg',$dataname)));
        return $res;
    }

    /*
     *上传图文消息素材
     * */
    public function uploadimgtext($data){
        $accesstoken = $this->getWxAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=".$accesstoken;
        $res = $this->http_curl($url,'post','json',$data);
        return $res;
    }

    /*
     * 删除群发
     * */
    public function delmasscl($data){
        $accesstoken = $this->getWxAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/message/mass/delete?access_token=".$accesstoken;
        $urarr = json_encode($data);
        $res = $this->http_curl($url,'post','json',$urarr);
        return $res;
    }

    /*
     * 查询群发状态
     * */
    public function getmasscl($data){
        $accesstoken = $this->getWxAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/message/mass/delete?access_token=".$accesstoken;
        $urarr = json_encode($data);
        $res = $this->http_curl($url,'post','json',$urarr);
        return $res;
    }

    /*
     * 创建分组
     * */
    public function creategroup($data){
        $accesstoken = $this->getWxAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/tags/create?access_token=".$accesstoken;
        $dataarr = [
            'tag' => [
                'name' => $data,
            ]
        ];
        $res = $this->http_curl($url,'post','json',json_encode($dataarr));
        return $res;
    }

    /*
     * 获取公众号已创建的标签
     * */
    public function getgroup(){
        $accesstoken = $this->getWxAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/tags/get?access_token=".$accesstoken;
        $res = $this->http_curl($url,'get','json');
        return $res;
    }

    /*
     * 修改公众号已创建的标签
     * */
    public function editgroup($data){
        $accesstoken = $this->getWxAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/tags/update?access_token=".$accesstoken;
        $dataarr = [
            'tag' => [
                'id' => $data['id'],
                'name' => $data['name'],
            ]
        ];
        $res = $this->http_curl($url,'post','json',json_encode($dataarr));
        return $res;
    }

    /*
     * 删除公众号已创建的标签
     * */
    public function delgroup($data){
        $accesstoken = $this->getWxAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/tags/delete?access_token=".$accesstoken;
        $dataarr = [
            'tag' => [
                'id' => $data['id'],
            ]
        ];
        $res = $this->http_curl($url,'post','json',json_encode($dataarr));
        return $res;
    }

    /*
     * 获取标签下粉丝列表
     * */
    public function getusergroup($data){
        $accesstoken = $this->getWxAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/user/tag/get?access_token=".$accesstoken;
        $dataarr = [
            'tagid' => $data['tagid'],
            'next_openid' => $data['next_openid'],
        ];
        $res = $this->http_curl($url,'post','json',json_encode($dataarr));
        return $res;
    }

    /*
     * 用户批量分组
     * */
    public function createtagging($data){
        $accesstoken = $this->getWxAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token=".$accesstoken;
        $data1 = [
            'openid_list' => json_decode($data['openid_list']),
            'tagid' => $data['tagid'],
        ];
        $data1  =  json_encode($data1);
        $res = $this->http_curl($url,'post','json',$data1);
        return $res;
    }

    /*
     * 用户批量取消分组
     * */
    public function deltagging($data){
        $accesstoken = $this->getWxAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/tags/members/batchuntagging?access_token=".$accesstoken;
        $data1 = [
            'openid_list' => json_decode($data['openid_list']),
            'tagid' => $data['tagid'],
        ];
        $data1  =  json_encode($data1);
        $res = $this->http_curl($url,'post','json',$data1);
        return $res;
    }

    /*
     * 获取用户身上的标签
     * */
    public function getidlistuser($data){
        $accesstoken = $this->getWxAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/tags/getidlist?access_token=".$accesstoken;
        $dataarr = [
            'openid' => $data,
        ];
        $res = $this->http_curl($url,'post','json',json_encode($dataarr));
        return $res;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值