php redis微信发红包,高阶篇二 使用Redis队列发送微信模版消息
# 高階篇二 使用Redis隊列發(fā)送微信模版消息
> 此命令行執(zhí)行任務(wù)的方法類比較復(fù)雜 他需要命令行運行才會有效
>
命令行源碼以及創(chuàng)建方法 參見上節(jié)
https://www.kancloud.cn/mikkle/thinkphp5_study/396283
> 總體的邏輯是通過Redis作為隊列媒介 命令行 根據(jù)分析reids數(shù)據(jù)并執(zhí)行指定方法
> 本類的靜態(tài)方法add 是往redis隊列中追加數(shù)據(jù)
> 本類的靜態(tài)方法run方法 是命令行程序執(zhí)行的處理隊列信息的方法
>
Redis類庫 參見 附件十
https://www.kancloud.cn/mikkle/thinkphp5_study/374537
~~~
/**
* 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);
}
/**
* 檢測命令行是否執(zhí)行中
* Power: Mikkle
* Email:776329498@qq.com
* @return bool
*/
static public function checkCommandRun(){
return self::redis()->get("command") ? true :false;
}
/**
* 快速添加模版消息任務(wù)
*
* 當(dāng)命令行未運行 直接執(zhí)行
* 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);
}
}
/**
* 命令行執(zhí)行的方法
* 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 "執(zhí)行了{(lán)$i}次任務(wù)".PHP_EOL;
}catch (Exception $e){
Log::error($e);
$instance->clearWorker();
die($e->getMessage());
}
}
/**
* 發(fā)送模版消息的方法
* Power: Mikkle
* Email:776329498@qq.com
* @param $data
* @return bool
*/
protected function sendMessage($data){
$no = $this->message->sendTemplateMessage($data);
if ($no){
Log::notice("發(fā)送成功[{$no}]");
return true ;
}else{
Log::notice("發(fā)送失敗[{$no}]");
$this->failed($data);
};
}
/**
* 出錯執(zhí)行的回調(diào)方法
* Power: Mikkle
* Email:776329498@qq.com
* @param $data
*/
protected function failed($data){
}
}
~~~

* 調(diào)用Work類的方法 你可以循環(huán)插入
~~~
$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"=>"點擊查看具體內(nèi)容"
];
$template = $this->assignTemplate("mission",$assign_data);
//添加異步命令行發(fā)送模版消息
$re = WechatMessage::add($template,"erp_options");
//dump($re);
Log::notice("發(fā)送{$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;
}
}
~~~
感謝大家關(guān)注 交流請加QQ群 321449759

總結(jié)
以上是生活随笔為你收集整理的php redis微信发红包,高阶篇二 使用Redis队列发送微信模版消息的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mvc模式缺点 php,mvc模式有哪些
- 下一篇: php通知websocket,php实现