# 高阶篇二 使用Redis队列发送微信模版消息 > 此命令行执行任务的方法类比较复杂 他需要命令行运行才会有效 > 命令行源码以及创建方法 参见上节 https://www.kancloud.cn/mikkle/thinkphp5_study/396283 > 总体的逻辑是通过Redis作为队列媒介 命令行 根据分析reids数据并执行指定方法 > 本类的静态方法add 是往redis队列中追加数据 > 本类的静态方法run方法 是命令行程序执行的处理队列信息的方法 > Redis类库 参见 附件十 https://www.kancloud.cn/mikkle/thinkphp5_study/374537 ~~~ <?php /** * Created by PhpStorm. * Power By Mikkle * Email:776329498@qq.com * Date: 2017/7/4 * Time: 13:56 */ namespace app\worker\controller; use mikkle\tp_wechat\Wechat; use think\Config; use think\Exception; use think\Log; class WechatMessage extends Base { protected $options=[]; protected $wechat; protected $listName; public function __construct($options=[]) { $this->options = empty($options) ? Config::get("wechat.erp_options") : $options; parent::__construct($options); $this->message=Wechat::message($this->options); $this->listName = md5($this->workerName); } /** * 检测命令行是否执行中 * Power: Mikkle * Email:776329498@qq.com * @return bool */ static public function checkCommandRun(){ return self::redis()->get("command") ? true :false; } /** * 快速添加模版消息任务 * * 当命令行未运行 直接执行 * Power: Mikkle * Email:776329498@qq.com * @param $data * @param array $options */ static public function add($data,$options=[]){ $instance = self::instance($options); switch (true){ case (self::checkCommandRun()): $instance->redis->lpush($instance->listName,$data); $instance->runWorker(); break; default: $instance->message->sendTemplateMessage($data); } } /** * 命令行执行的方法 * Power: Mikkle * Email:776329498@qq.com */ static public function run(){ $instance = self::instance(); try{ $i = 0; while(true){ $data = $instance->redis->rpop($instance->listName); if ($data){ $re=$instance->sendMessage($data); Log::notice($re); }else{ break; } $i++; sleep(1); } $instance->clearWorker(); echo "执行了{$i}次任务".PHP_EOL; }catch (Exception $e){ Log::error($e); $instance->clearWorker(); die($e->getMessage()); } } /** * 发送模版消息的方法 * Power: Mikkle * Email:776329498@qq.com * @param $data * @return bool */ protected function sendMessage($data){ $no = $this->message->sendTemplateMessage($data); if ($no){ Log::notice("发送成功[{$no}]"); return true ; }else{ Log::notice("发送失败[{$no}]"); $this->failed($data); }; } /** * 出错执行的回调方法 * Power: Mikkle * Email:776329498@qq.com * @param $data */ protected function failed($data){ } } ~~~ ![](https://box.kancloud.cn/3d03b7f1d9950795c8b1f45d16dfef3a_862x335.png) * 调用Work类的方法 你可以循环插入 ~~~ $assign_data= [ "ToUserName"=>$value, "url"=> Url::build($type["url"],[],"",true), "first"=> "温馨提示,你有一个[{$type["title"]}]需要审核". (empty($uuid_name)?"":",提交人:$uuid_name"), "keyword1"=>"{$type["title"]}审核", "keyword2"=>date("Y-m-d H:i:s",time()), "remark"=>"点击查看具体内容" ]; $template = $this->assignTemplate("mission",$assign_data); //添加异步命令行发送模版消息 $re = WechatMessage::add($template,"erp_options"); //dump($re); Log::notice("发送{$uuid_name}提交的{$type["title"]}审核模版消息成功!$re"); } ~~~ * 本类用到的部分方法 ~~~ static public function assignTemplate($name, $data = '') { $template = self::getTemplate($name); if (empty($data)){ return $template; }else{ try{ return View::instance()->display($template,$data); }catch (Exception $e){ Log::error($e->getMessage()); ob_get_clean(); throw $e; return false; } } } static public function getTemplate($name) { if (is_null(self::$instance)) { self::$instance = new static(); } if(isset(self::$instance->$name)){ return self::$instance->$name; }else{ return false; } } ~~~ 感谢大家关注 交流请加QQ群 321449759 ![](https://box.kancloud.cn/3499008a08e64306c68873288092a057_286x340.png)