12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- <?php
- declare (strict_types = 1);
- namespace app\command;
- use app\bug\model\WechatPush;use think\console\Command;
- use think\console\Input;
- use think\console\input\Argument;
- use think\console\input\Option;
- use think\console\Output;use think\facade\Cache;use think\facade\Log;
- class WxPush extends Command
- {
- protected function configure()
- {
- // 指令配置
- $this->setName('wxpush')
- ->setDescription('the wxpush command');
- }
- protected function execute(Input $input, Output $output)
- {
- $redis = Cache::store('redis')->handler();
- $isLock = $redis->set("wxpush_lock", time() + 600, array('nx', 'ex'=>600));
- if(!$isLock)return;
- try{
- $appid = env('WECHAT.APPID');
- $appsecret = env('WECHAT.APPSECRET');
- $wechat = new \Wechat(['appid' => $appid, 'appsecret' => $appsecret]);
- $que= Cache::store('redis')->handler()->lpop('wxpush_queue');
- while ($que){
- $data=json_decode($que,true);
- $temp=[];
- $res=false;
- if($data['openId']!=''){
- $temp['touser'] =$data['openId'];
- $temp['template_id'] = $data['template_id'];
- $temp['data']=$data['template_data'];
- $res= $wechat->sendTemplateMessage($temp);
- if($res==false && $wechat->errCode==40001){
- $wechat->resetAuth(env('WECHAT.APPID'));
- $wechat->sendTemplateMessage($temp);
- }
- }
- $add= [
- 'bugNo'=>$data['code'],
- 'belong'=>$data['belong'],
- 'action_name'=>$data['action_name'],
- 'template_id'=>$data['template_id'],
- 'template_name'=>$data['template_name'],
- 'content'=>json_encode($temp,JSON_UNESCAPED_UNICODE),
- 'push_id'=>$data['uid'],
- 'pusher'=>$data['uname'],
- 'status'=>$res?1:0,
- 'remark'=>$res?'':$wechat->errMsg
- ];
- WeChatPush::create($add);
- Log::write('data:'.json_encode($add,JSON_UNESCAPED_UNICODE),'info');
- $output->writeln(json_encode($add,JSON_UNESCAPED_UNICODE));
- $que= Cache::store('redis')->handler()->lpop('wxpush_queue');
- }
- }catch (\Exception $e){
- Log::write('wxpush:'.$e->getMessage(),'error');
- $output->writeln($e->getMessage());
- }
- $redis->delete("wxpush_lock");
- }
- }
|