1.生成job文件
php artisan make:job SendMessage
2.编辑文件
//接收参数
public function __construct($cid,$data)
{
//$data['title']
//$data['url']
//$data['description']
//$data['picurl']
//$data['userid']
$this->cid=$cid;
$this->info=$data;
}
/**
* Execute the job. 逻辑处理
*
* @return void
*/
public function handle()
{
//查询是否绑定了企业微信
$info=WeWorkAction::model()->getBindInfo($this->cid);
if(empty($info)) return true;
$obj = new WorkUser();
$data = $this->info; //组合数据
//获取url进行拼接处理
if($info->type==1){
$query = parse_url($data['url'], PHP_URL_QUERY);
parse_str($query, $url_info);
$url_info['login_cid'] = $info->comid;
$buid_query=http_build_query($url_info);
$data['url']=parse_url($data['url'], PHP_URL_SCHEME).'://'.parse_url($data['url'], PHP_URL_HOST).'?'.$buid_query;
}
//组装userid
$arr=array_chunk($data['userid'],100);
foreach ($arr as $val){
//获取userid
$userid = DB::table('user_info')->select('wx_id')->where(['comid' => 213])->whereIn('id', $val)->where('wx_id', '!=', ' ')->get()->toArray();
if(empty($userid)) break;
$users = array_column($userid,'wx_id');
$data['userid'] = $users;
$data['type'] = 'news'; //默认是新闻的形式
$data['comid'] = $info->comid;
$obj->sendWeworkMsg(formatToArray($info),$data);
}
}
- 正常文件中调用队列
SendMessage::dispatch(1,['title'=>'松伟真帅','userid'=>[12052], 'description'=>'这是一条来自未来的消息',
'url'=>'https://testent.toujianyun.com/detail/97','picurl'=>'https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1194131577,2954769920&fm=26&gp=0.jpg'])
->onQueue('{sendWeWorkMsg}');
4.消耗队列
php artisan queue:work --queue={sendWeWorkMsg}
注意 centos 中将会把队列服务的job 信息等输出到 日志中 /var/log/messages 中 可以通过降低日志的级别避免写入
vi /etc/rsyslog.conf
找到关于 messages的配置信息:
*.info;mail.none;authpriv.none;cron.none /var/log/messages
将其改为 级别改变了除了系统的报错不会写入了就
*.notice;mail.none;authpriv.none;cron.none /var/log/messages (不写日志)
网友评论