MsgService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. namespace App\Modules\OfficialAccount\Services;
  3. use App\Modules\OfficialAccount\Models\DistributionSelfDefineContent;
  4. use Redis;
  5. use App\Modules\Channel\Services\ChannelService;
  6. use App\Jobs\SendNews;
  7. use App\Jobs\SendTexts;
  8. class MsgService
  9. {
  10. /**
  11. * 关注回复
  12. */
  13. static function getReply($distribution_channel_id)
  14. {
  15. //关注回复
  16. return Redis::hgetAll('site_sub_reply:'.$distribution_channel_id);
  17. }
  18. /**
  19. * 设置回复
  20. */
  21. static function setReply($params)
  22. {
  23. return self::setReplyToCache($params);
  24. }
  25. //关注回复
  26. static function setReplyToCache($params)
  27. {
  28. $distribution_channel_id = $params['distribution_channel_id'];
  29. $content = $params['content'];
  30. $is_pic = $params['is_pic'];
  31. $start_time = $params['start_time'];
  32. $end_time = $params['end_time'];
  33. //$modes = [1=>'default',2=>'pic',3=>'text'];
  34. $mode = $params['mode'];
  35. $status = $params['status'];
  36. $key = isset($params['key']) && $params['key'] ? $params['key']:'0';
  37. $action = $params['action'];
  38. \Log::info('setReplyToCache_param:');
  39. \Log::info($params);
  40. // 按时间
  41. $history_key = 'content_mode_'.$mode;
  42. $replays = Redis::hgetAll('site_sub_reply:'.$distribution_channel_id);
  43. $replays = $replays?$replays:[];
  44. \Log::info('setReplyToCache_origin_$replays:');
  45. \Log::info($replays);
  46. $history_contents = isset($replays[$history_key]) && !empty($replays[$history_key]) ?$replays[$history_key]:'';
  47. \Log::info('$history_contents_origin');\Log::info($history_contents);
  48. $history_contents = object_to_array(json_decode($history_contents));
  49. \Log::info('$history_contents_decode');\Log::info($history_contents);
  50. $history_content_count = count($history_contents);
  51. if($action == 'add'){
  52. $key = $history_content_count;
  53. }
  54. $history_contents[$key]['start_time'] = $start_time;
  55. $history_contents[$key]['end_time'] = $end_time;
  56. $history_contents[$key]['content'] = $content;
  57. $history_contents[$key]['is_pic'] = $mode==2?1:0;
  58. $history_contents[$key]['status'] = $status;
  59. \Log::info('$history_contents_before_encode');\Log::info($history_contents);
  60. // 一定要原始对象基础上改
  61. $replays[$mode] = $content;
  62. $replays['current'] = $mode;
  63. $replays[$history_key] = json_encode($history_contents);
  64. \Log::info('setReplyToCache_after_data:'.$distribution_channel_id);
  65. \Log::info($replays);
  66. Redis::hmset('site_sub_reply:'.$distribution_channel_id,$replays);//设置内容
  67. }
  68. /**
  69. * 获取当前回复
  70. * 微信交互使用
  71. */
  72. static function getCurrentReply($distribution_channel_id)
  73. {
  74. //关注回复
  75. $replays = Redis::hgetAll('site_sub_reply:'.$distribution_channel_id);
  76. $now = date('Y-m-d H:i:s');
  77. // $now = date('Y-m-d',strtotime('2018-11-13'));
  78. $current = isset($replays['current'])?$replays['current']:'1';
  79. // 获取当前时间区间的回复
  80. $current_reply = null;
  81. if($current != 1)
  82. {
  83. $history_contents = isset($replays['content_mode_'.$current])?$replays['content_mode_'.$current]:'';
  84. $history_contents = object_to_array(json_decode($history_contents));
  85. \Log::info('$history_contents2');\Log::info($history_contents);
  86. if(!empty($history_contents)){
  87. // 先看历史时间段有没有匹配的
  88. foreach($history_contents as $key=>$history_content){
  89. $start_time = isset($history_content['start_time'])?$history_content['start_time']:'';
  90. $end_time = isset($history_content['end_time'])?$history_content['end_time']:'';
  91. $status = isset($history_content['status'])?$history_content['status']:'0';
  92. \Log::info('start_time:'.$start_time.' end_time:'.$end_time.' now:'.$now.' status:'.$status);
  93. if($start_time <= $now && $now <= $end_time && $status == 1){
  94. \Log::info('getCurrentReply:distribution_channel_id:'.$distribution_channel_id.' get_this:'.$key.' start_time:'.$start_time.' end_time:'.$end_time.' now:'.$now);
  95. $obj = new \stdClass();
  96. $obj->distribution_channel_id = $distribution_channel_id;
  97. $obj->content = $history_content['content'];
  98. $obj->is_pic = $history_content['is_pic'];
  99. $obj->status = 1;
  100. return $obj;
  101. }
  102. }
  103. }
  104. }
  105. return $current_reply;
  106. }
  107. /**
  108. * 初始化修复redis格式
  109. */
  110. static function initOriginReply($distribution_channel_id)
  111. {
  112. //关注回复
  113. $replays = Redis::hgetAll('site_sub_reply:'.$distribution_channel_id);
  114. $origin_content = [];
  115. if(!empty($replays)){
  116. \Log::info('initOriginReply_not_null:'.$distribution_channel_id);
  117. // 第0个默认是原版的内容
  118. if(! isset($replays['content_mode_2'])){
  119. \Log::info('initOriginReply:'.$distribution_channel_id.' mode:2');
  120. $replays['2'] = isset($replays['2'])?$replays['2']:'';
  121. $origin_content[0]['start_time'] = '2018-01-01';
  122. $origin_content[0]['end_time'] = '2030-01-01';
  123. $origin_content[0]['content'] = json_decode($replays['2']);
  124. $origin_content[0]['status'] = 1;
  125. $origin_content[0]['is_pic'] = 1;
  126. $replays['content_mode_2'] = json_encode($origin_content);
  127. Redis::hmset('site_sub_reply:'.$distribution_channel_id,$replays);
  128. }
  129. if(! isset($replays['content_mode_3'])){
  130. \Log::info('initOriginReply:'.$distribution_channel_id.' mode:3');
  131. $replays['3'] = isset($replays['3']) ? $replays['3']:'';
  132. \Log::info($replays['3']);\Log::info(json_decode($replays['3']));
  133. $origin_content[0]['start_time'] = '2018-01-01';
  134. $origin_content[0]['end_time'] = '2030-01-01';
  135. $origin_content[0]['content'] = $replays['3'];
  136. $origin_content[0]['status'] = 1;
  137. $origin_content[0]['is_pic'] = 0;
  138. $replays['content_mode_3'] = json_encode($origin_content);
  139. Redis::hmset('site_sub_reply:'.$distribution_channel_id,$replays);
  140. }
  141. }else{
  142. \Log::info('initOriginReply_is_null:'.$distribution_channel_id);
  143. }
  144. }
  145. static function pushChannelCurrentSecondReply($distribution_channel_id,$appid,$openid,$category)
  146. {
  147. try{
  148. // 判断是否有配置
  149. $channel = ChannelService::getDistributionChannelSwitchByCategory($distribution_channel_id, $category);
  150. if(!empty($channel)){
  151. \Log::info('pushChannelCurrentSecondReply_start:'.$distribution_channel_id.' openid:'.$openid);
  152. $second_reply = self::getCurrentSecondReply($distribution_channel_id);
  153. $is_news = isset($second_reply->is_pic)?$second_reply->is_pic:'0';
  154. $data = $send_data = array();
  155. // 判断是否图文
  156. if($is_news){
  157. $send_content = ImageNewsToArray($second_reply->content);
  158. $send_data[] = $send_content;
  159. \Log::info($send_data);
  160. $data['news_content'] = json_encode($send_data);
  161. }else{
  162. $send_content = isset($second_reply->content)?$second_reply->content:'';
  163. $data['content'] = $send_content;
  164. }
  165. $data['openid'] = $openid;
  166. $data['appid'] = $appid;
  167. $data['type'] = 'one_task';
  168. $data['task_id'] = 3;
  169. $data['send_time'] = date("Y-m-d H:i:s");
  170. $send_data=array(
  171. 'send_time'=>date("Y-m-d H:i:s"),
  172. 'data' => $data
  173. );
  174. $delay = 5;
  175. if ($is_news){
  176. $job = (new SendNews($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_news_list');
  177. }else{
  178. $job = (new SendTexts($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_texts_list');
  179. }
  180. dispatch($job);
  181. \Log::info('pushChannelCurrentSecondReply_end:'.$openid.json_encode($send_data));
  182. }else{
  183. \Log::info('pushChannelCurrentSecondReply_not_sencod:'.$distribution_channel_id);
  184. }
  185. }catch(\Exception $e){
  186. \Log::info('pushChannelCurrentSecondReply_ept:'.$e->getMessage());
  187. }
  188. }
  189. /**
  190. * 获取当前第二个回复
  191. * 微信交互使用
  192. */
  193. static function getCurrentSecondReply($distribution_channel_id)
  194. {
  195. //关注回复
  196. $replays = Redis::hgetAll('site_second_sub_reply:'.$distribution_channel_id);
  197. $now = date('Y-m-d H:i:s');
  198. // $now = date('Y-m-d',strtotime('2018-11-13'));
  199. $current = isset($replays['current'])?$replays['current']:'1';
  200. // 获取当前时间区间的回复
  201. $current_reply = null;
  202. if($current != 1)
  203. {
  204. $history_contents = isset($replays['content_mode_'.$current])?$replays['content_mode_'.$current]:'';
  205. $history_contents = object_to_array(json_decode($history_contents));
  206. \Log::info('$history_contents2');\Log::info($history_contents);
  207. if(!empty($history_contents)){
  208. // 先看历史时间段有没有匹配的
  209. foreach($history_contents as $key=>$history_content){
  210. $start_time = isset($history_content['start_time'])?$history_content['start_time']:'';
  211. $end_time = isset($history_content['end_time'])?$history_content['end_time']:'';
  212. $status = isset($history_content['status'])?$history_content['status']:'0';
  213. \Log::info('start_time:'.$start_time.' end_time:'.$end_time.' now:'.$now.' status:'.$status);
  214. if($start_time <= $now && $now <= $end_time && $status == 1){
  215. \Log::info('getCurrentReply:distribution_channel_id:'.$distribution_channel_id.' get_this:'.$key.' start_time:'.$start_time.' end_time:'.$end_time.' now:'.$now);
  216. $obj = new \stdClass();
  217. $obj->distribution_channel_id = $distribution_channel_id;
  218. $obj->content = $history_content['content'];
  219. $obj->is_pic = $history_content['is_pic'];
  220. $obj->status = 1;
  221. return $obj;
  222. }
  223. }
  224. }
  225. }
  226. return $current_reply;
  227. }
  228. }