123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- <?php
- namespace App\Modules\OfficialAccount\Services;
- use App\Modules\OfficialAccount\Models\DistributionSelfDefineContent;
- use Redis;
- use App\Modules\Channel\Services\ChannelService;
- use App\Jobs\SendNews;
- use App\Jobs\SendTexts;
- class MsgService
- {
- /**
- * 关注回复
- */
- static function getReply($distribution_channel_id)
- {
- //关注回复
- return Redis::hgetAll('site_sub_reply:'.$distribution_channel_id);
- }
- /**
- * 设置回复
- */
- static function setReply($params)
- {
- return self::setReplyToCache($params);
- }
- //关注回复
- static function setReplyToCache($params)
- {
- $distribution_channel_id = $params['distribution_channel_id'];
-
- $content = $params['content'];
- $is_pic = $params['is_pic'];
- $start_time = $params['start_time'];
- $end_time = $params['end_time'];
- //$modes = [1=>'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 = 5;
-
- 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;
- }
- }
|