'default',2=>'pic',3=>'text']; $mode = $params['mode']; $status = $params['status']; $key = isset($params['key']) && $params['key'] ? $params['key']:'0'; $action = $params['action']; \Log::info('setReplyToCache_param:'); \Log::info($params); // 按时间 $history_key = 'content_mode_'.$mode; $replays = Redis::hgetAll('site_sub_reply:'.$distribution_channel_id); $replays = $replays?$replays:[]; \Log::info('setReplyToCache_origin_$replays:'); \Log::info($replays); $history_contents = isset($replays[$history_key]) && !empty($replays[$history_key]) ?$replays[$history_key]:''; \Log::info('$history_contents_origin');\Log::info($history_contents); $history_contents = object_to_array(json_decode($history_contents)); \Log::info('$history_contents_decode');\Log::info($history_contents); $history_content_count = count($history_contents); if($action == 'add'){ $key = $history_content_count; } $history_contents[$key]['start_time'] = $start_time; $history_contents[$key]['end_time'] = $end_time; $history_contents[$key]['content'] = $content; $history_contents[$key]['is_pic'] = $mode==2?1:0; $history_contents[$key]['status'] = $status; \Log::info('$history_contents_before_encode');\Log::info($history_contents); // 一定要原始对象基础上改 $replays[$mode] = $content; $replays['current'] = $mode; $replays[$history_key] = json_encode($history_contents); \Log::info('setReplyToCache_after_data:'.$distribution_channel_id); \Log::info($replays); Redis::hmset('site_sub_reply:'.$distribution_channel_id,$replays);//设置内容 } /** * 获取当前回复 * 微信交互使用 */ static function getCurrentReply($distribution_channel_id) { //关注回复 $replays = Redis::hgetAll('site_sub_reply:'.$distribution_channel_id); $now = date('Y-m-d H:i:s'); // $now = date('Y-m-d',strtotime('2018-11-13')); $current = isset($replays['current'])?$replays['current']:'1'; // 获取当前时间区间的回复 $current_reply = null; if($current != 1) { $history_contents = isset($replays['content_mode_'.$current])?$replays['content_mode_'.$current]:''; $history_contents = object_to_array(json_decode($history_contents)); \Log::info('$history_contents2');\Log::info($history_contents); if(!empty($history_contents)){ // 先看历史时间段有没有匹配的 foreach($history_contents as $key=>$history_content){ $start_time = isset($history_content['start_time'])?$history_content['start_time']:''; $end_time = isset($history_content['end_time'])?$history_content['end_time']:''; $status = isset($history_content['status'])?$history_content['status']:'0'; \Log::info('start_time:'.$start_time.' end_time:'.$end_time.' now:'.$now.' status:'.$status); if($start_time <= $now && $now <= $end_time && $status == 1){ \Log::info('getCurrentReply:distribution_channel_id:'.$distribution_channel_id.' get_this:'.$key.' start_time:'.$start_time.' end_time:'.$end_time.' now:'.$now); $obj = new \stdClass(); $obj->distribution_channel_id = $distribution_channel_id; $obj->content = $history_content['content']; $obj->is_pic = $history_content['is_pic']; $obj->status = 1; return $obj; } } } } return $current_reply; } /** * 初始化修复redis格式 */ static function initOriginReply($distribution_channel_id) { //关注回复 $replays = Redis::hgetAll('site_sub_reply:'.$distribution_channel_id); $origin_content = []; if(!empty($replays)){ \Log::info('initOriginReply_not_null:'.$distribution_channel_id); // 第0个默认是原版的内容 if(! isset($replays['content_mode_2'])){ \Log::info('initOriginReply:'.$distribution_channel_id.' mode:2'); $replays['2'] = isset($replays['2'])?$replays['2']:''; $origin_content[0]['start_time'] = '2018-01-01'; $origin_content[0]['end_time'] = '2030-01-01'; $origin_content[0]['content'] = json_decode($replays['2']); $origin_content[0]['status'] = 1; $origin_content[0]['is_pic'] = 1; $replays['content_mode_2'] = json_encode($origin_content); Redis::hmset('site_sub_reply:'.$distribution_channel_id,$replays); } if(! isset($replays['content_mode_3'])){ \Log::info('initOriginReply:'.$distribution_channel_id.' mode:3'); $replays['3'] = isset($replays['3']) ? $replays['3']:''; \Log::info($replays['3']);\Log::info(json_decode($replays['3'])); $origin_content[0]['start_time'] = '2018-01-01'; $origin_content[0]['end_time'] = '2030-01-01'; $origin_content[0]['content'] = $replays['3']; $origin_content[0]['status'] = 1; $origin_content[0]['is_pic'] = 0; $replays['content_mode_3'] = json_encode($origin_content); Redis::hmset('site_sub_reply:'.$distribution_channel_id,$replays); } }else{ \Log::info('initOriginReply_is_null:'.$distribution_channel_id); } } static function pushChannelCurrentSecondReply($distribution_channel_id,$appid,$openid,$category) { try{ // 判断是否有配置 $channel = ChannelService::getDistributionChannelSwitchByCategory($distribution_channel_id, $category); if(!empty($channel)){ \Log::info('pushChannelCurrentSecondReply_start:'.$distribution_channel_id.' openid:'.$openid); $second_reply = self::getCurrentSecondReply($distribution_channel_id); $is_news = isset($second_reply->is_pic)?$second_reply->is_pic:'0'; $data = $send_data = array(); // 判断是否图文 if($is_news){ $send_content = ImageNewsToArray($second_reply->content); $send_data[] = $send_content; \Log::info($send_data); $data['news_content'] = json_encode($send_data); }else{ $send_content = isset($second_reply->content)?$second_reply->content:''; $data['content'] = $send_content; } $data['openid'] = $openid; $data['appid'] = $appid; $data['type'] = 'one_task'; $data['task_id'] = 3; $data['send_time'] = date("Y-m-d H:i:s"); $send_data=array( 'send_time'=>date("Y-m-d H:i:s"), 'data' => $data ); $delay = 1; if ($is_news){ $job = (new SendNews($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_news_list'); }else{ $job = (new SendTexts($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_texts_list'); } dispatch($job); \Log::info('pushChannelCurrentSecondReply_end:'.$openid.json_encode($send_data)); }else{ \Log::info('pushChannelCurrentSecondReply_not_sencod:'.$distribution_channel_id); } }catch(\Exception $e){ \Log::info('pushChannelCurrentSecondReply_ept:'.$e->getMessage()); } } /** * 获取当前第二个回复 * 微信交互使用 */ static function getCurrentSecondReply($distribution_channel_id) { //关注回复 $replays = Redis::hgetAll('site_second_sub_reply:'.$distribution_channel_id); $now = date('Y-m-d H:i:s'); // $now = date('Y-m-d',strtotime('2018-11-13')); $current = isset($replays['current'])?$replays['current']:'1'; // 获取当前时间区间的回复 $current_reply = null; if($current != 1) { $history_contents = isset($replays['content_mode_'.$current])?$replays['content_mode_'.$current]:''; $history_contents = object_to_array(json_decode($history_contents)); \Log::info('$history_contents2');\Log::info($history_contents); if(!empty($history_contents)){ // 先看历史时间段有没有匹配的 foreach($history_contents as $key=>$history_content){ $start_time = isset($history_content['start_time'])?$history_content['start_time']:''; $end_time = isset($history_content['end_time'])?$history_content['end_time']:''; $status = isset($history_content['status'])?$history_content['status']:'0'; \Log::info('start_time:'.$start_time.' end_time:'.$end_time.' now:'.$now.' status:'.$status); if($start_time <= $now && $now <= $end_time && $status == 1){ \Log::info('getCurrentReply:distribution_channel_id:'.$distribution_channel_id.' get_this:'.$key.' start_time:'.$start_time.' end_time:'.$end_time.' now:'.$now); $obj = new \stdClass(); $obj->distribution_channel_id = $distribution_channel_id; $obj->content = $history_content['content']; $obj->is_pic = $history_content['is_pic']; $obj->status = 1; return $obj; } } } } return $current_reply; } }