快乐每一天

自己选择的路,跪着也要走下去...

标签 redis 下的文章

TP5微信redis列队群发模板消息Sendmsg

<?php
/**
 * Created by PhpStorm.
 * User: 项羽
 * Date: 2019/1/16
 * Time: 16:08
 * QQ群:310325131
 */

namespace app\api\controller;
use think\cache\driver\Redis;

class Sendmsg
{
    /*获取全局的token*/
    public function ccs(){
        $dd=$this->getAccessToken();
        echo $dd;
    }
    /*存accesstoken*/
    public function getAccessToken()
    {
        $isExpires = $this->isExpires();
        if($isExpires === false){
            //到期,获取新的
            $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' .config('appid') . '&secret=' . config('secret');

            $res = $this->curl($url);
            // dump($res);
            $arr = json_decode($res,true);
//            return json($arr);

            if($arr && !isset($arr['errcode'])){
                $arr['time'] = time();
                file_put_contents(APP_PATH . '../access_token.json', json_encode($arr));
//                var_dump($arr);
                return $arr['access_token'];
            }else{
                echo 'error on get access_token';die;
            }
        }else{
            return $isExpires;
        }

    }
    /*检测是否过期*/
    public function isExpires(){
        if(!file_exists(APP_PATH . '../access_token.json')){
            return false;
        }
        $res = file_get_contents(APP_PATH . '../access_token.json');
        $arr = json_decode($res,true);
        if($arr && time()<(intval($arr['time'])+intval($arr['expires_in']))){
            //未过期
            return $arr['access_token'];
        }else{
            return false;
        }
    }

    /*curl请求*/
    public function curl($url)
    {
        //初始化
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL,$url);
        // 执行后不直接打印出来
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        // 跳过证书检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        // 不从证书中检查SSL加密算法是否存在
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

        //执行并获取HTML文档内容
        $output = curl_exec($ch);

        //释放curl句柄
        curl_close($ch);

        return $output;
    }


//    发送模板消息↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙

    /*获取所有openid*/
    public function sendall(){
        //获取access_token
        $access_token = $this->getAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token={$access_token}&next_openid=";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
        $data = curl_exec($ch);
        $data = json_decode($data,true);
        return $data['data']['openid'];
    }

    /**
     * 发送模板消息
     */
    public function send_notice($openid){
        //获取access_token
        $access_token2=$this->getAccessToken();
        //模板消息
        $json_template = $this->json_tempalte($openid);
        $url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token2;
        $res=$this->curl_post($url,urldecode($json_template));

        return $res;
    }

    /**
     * 将模板消息json格式化
     */
    public function json_tempalte($openid){
        //模板消息
        $template=array(
            'touser'=>$openid,    //用户openid
            'template_id'=>"zTi9Y8omU9JovEI8yrX4LSMo8Yrm2EBpXD-rDU1T_tc",  //在公众号下配置的模板id
            'url'=>"https://www.iqiyi.com/v_19rrmucyoc.html?vfm=2008_aldbd",  //点击模板消息会跳转的链接
            'topcolor'=>"#7B68EE",
            'data'=>array(
                'first'=>array('value'=>urlencode("通知通知:小区发起全民选举投票了"),'color'=>"#4B0082"),
                'keyword1'=>array('value'=>urlencode('2019年人人家智慧小区投票提醒'),'color'=>'#00BFFF'),   //keyword需要与配置的模板消息对应
                'keyword2'=>array('value'=>urlencode(date("Y-m-d H:i:s")),'color'=>'#00BFFF'),
                'keyword3'=>array('value'=>urlencode('点击详情进入投票展示页面'),'color'=>'#00BFFF'),
                'remark'  =>array('value'=>urlencode('备注:这是测试'),'color'=>'#B23AEE'), )
        );
        $json_template=json_encode($template);
        return $json_template;
    }


    /**
     * @param $url
     * @param array $data
     * @return mixed
     * curl请求
     */
    public function curl_post($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;
    }



    /*获取所有openid并存入redis*/
    public function sendall_redis(){
        $redis = new Redis();
        //获取access_token
        $access_token = $this->getAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token={$access_token}&next_openid=";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
        $data = curl_exec($ch);
        $data = json_decode($data,true);

        foreach($data['data']['openid'] as $k=>$v){
            $redis->rpush("openidlist1",$v);
        }
    }

    /*每次取出一个redis队列中的值来进行操作*/
    public function sendmsg_redis(){
        $redis = new Redis();

        $all_openid=array();
        for ($i=1;$i<=300;$i++){
            $val = $redis->lpop('openidlist1');

            if ($val){
                array_push($all_openid,$val);
            }
        }

//        echo json_encode($all_openid);     /*查看所有的查出的opendi*/

        foreach ($all_openid as $value) {

            $data= $this->send_notice(trim($value));
            $data= json_decode($data,true);
            if ($data['errcode']==0){
                return '发送成功';
            }else{
                return '发送失败';
            }
        }

    }

    /*TODO 这个地方需要加一个定时任务  另外配置好  appid  secret     还需要在 think\cache\driver\Redis; 中添加以下几个函数*/
/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
    /**
     * 将一个或多个值插入到列表头部
     * @param $key
     * @param $value
     * @return int
     */

   /* public function lpush($key, $step){
        return $this->handler->lPush($key, $step);
    }*/

    /**
     * 将一个或多个值插入到列表尾部
     * @param $key
     * @param $value
     * @return int
     */

    /*public function rpush($key, $step){
        return $this->handler->rPush($key, $step);
    }*/

    /**
     * 移出并获取列表的第一个元素
     * @param string $key
     * @return string
     */

    /*public function lpop($name){
        $key = $this->getCacheKey($name);
        return $this->handler->lPop($key);
    }*/

}

推荐观看这个: https://www.cnblogs.com/hui9527/p/8513336.html

还不错哦

阅读全文