MsgService.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace App\Modules\OfficialAccount\Services;
  3. use App\Modules\OfficialAccount\Models\DistributionSelfDefineContent;
  4. use Redis;
  5. class MsgService
  6. {
  7. /**
  8. * 关注回复
  9. */
  10. static function getReply($distribution_channel_id)
  11. {
  12. //关注回复
  13. return Redis::hgetAll('site_sub_reply:'.$distribution_channel_id);
  14. }
  15. /**
  16. * 设置回复
  17. */
  18. static function setReply($params)
  19. {
  20. return self::setReplyToCache($params);
  21. }
  22. //关注回复
  23. static function setReplyToCache($params)
  24. {
  25. $distribution_channel_id = $params['distribution_channel_id'];
  26. $content = $params['content'];
  27. $is_pic = $params['is_pic'];
  28. $start_time = $params['start_time'];
  29. $end_time = $params['end_time'];
  30. //$modes = [1=>'default',2=>'pic',3=>'text'];
  31. $mode = $params['mode'];
  32. $status = $params['status'];
  33. $key = isset($params['key']) && $params['key'] ? $params['key']:'0';
  34. $action = $params['action'];
  35. \Log::info('setReplyToCache_param:');
  36. \Log::info($params);
  37. // 按时间
  38. $history_key = 'content_mode_'.$mode;
  39. $replays = Redis::hgetAll('site_sub_reply:'.$distribution_channel_id);
  40. $replays = $replays?$replays:[];
  41. \Log::info('setReplyToCache_origin_$replays:');
  42. \Log::info($replays);
  43. $history_contents = isset($replays[$history_key]) && !empty($replays[$history_key]) ?$replays[$history_key]:'';
  44. \Log::info('$history_contents_origin');\Log::info($history_contents);
  45. $history_contents = object_to_array(json_decode($history_contents));
  46. \Log::info('$history_contents_decode');\Log::info($history_contents);
  47. $history_content_count = count($history_contents);
  48. if($action == 'add'){
  49. $key = $history_content_count;
  50. }
  51. $history_contents[$key]['start_time'] = $start_time;
  52. $history_contents[$key]['end_time'] = $end_time;
  53. $history_contents[$key]['content'] = $content;
  54. $history_contents[$key]['is_pic'] = $mode==2?1:0;
  55. $history_contents[$key]['status'] = $status;
  56. \Log::info('$history_contents_before_encode');\Log::info($history_contents);
  57. // 一定要原始对象基础上改
  58. $replays[$mode] = $content;
  59. $replays['current'] = $mode;
  60. $replays[$history_key] = json_encode($history_contents);
  61. \Log::info('setReplyToCache_after_data:'.$distribution_channel_id);
  62. \Log::info($replays);
  63. Redis::hmset('site_sub_reply:'.$distribution_channel_id,$replays);//设置内容
  64. }
  65. /**
  66. * 获取当前回复
  67. * 微信交互使用
  68. */
  69. static function getCurrentReply($distribution_channel_id)
  70. {
  71. //关注回复
  72. $replays = Redis::hgetAll('site_sub_reply:'.$distribution_channel_id);
  73. $now = date('Y-m-d H:i:s');
  74. // $now = date('Y-m-d',strtotime('2018-11-13'));
  75. $current = isset($replays['current'])?$replays['current']:'1';
  76. // 获取当前时间区间的回复
  77. $current_reply = null;
  78. if($current != 1)
  79. {
  80. $history_contents = isset($replays['content_mode_'.$current])?$replays['content_mode_'.$current]:'';
  81. $history_contents = object_to_array(json_decode($history_contents));
  82. \Log::info('$history_contents2');\Log::info($history_contents);
  83. if(!empty($history_contents)){
  84. // 先看历史时间段有没有匹配的
  85. foreach($history_contents as $key=>$history_content){
  86. $start_time = isset($history_content['start_time'])?$history_content['start_time']:'';
  87. $end_time = isset($history_content['end_time'])?$history_content['end_time']:'';
  88. $status = isset($history_content['status'])?$history_content['status']:'0';
  89. \Log::info('start_time:'.$start_time.' end_time:'.$end_time.' now:'.$now.' status:'.$status);
  90. if($start_time <= $now && $now <= $end_time && $status == 1){
  91. \Log::info('getCurrentReply:distribution_channel_id:'.$distribution_channel_id.' get_this:'.$key.' start_time:'.$start_time.' end_time:'.$end_time.' now:'.$now);
  92. $obj = new \stdClass();
  93. $obj->distribution_channel_id = $distribution_channel_id;
  94. $obj->content = $history_content['content'];
  95. $obj->is_pic = $history_content['is_pic'];
  96. $obj->status = 1;
  97. return $obj;
  98. }
  99. }
  100. }
  101. }
  102. return $current_reply;
  103. }
  104. /**
  105. * 初始化修复redis格式
  106. */
  107. static function initOriginReply($distribution_channel_id)
  108. {
  109. //关注回复
  110. $replays = Redis::hgetAll('site_sub_reply:'.$distribution_channel_id);
  111. $origin_content = [];
  112. if(!empty($replays)){
  113. \Log::info('initOriginReply_not_null:'.$distribution_channel_id);
  114. // 第0个默认是原版的内容
  115. if(! isset($replays['content_mode_2'])){
  116. \Log::info('initOriginReply:'.$distribution_channel_id.' mode:2');
  117. $replays['2'] = isset($replays['2'])?$replays['2']:'';
  118. $origin_content[0]['start_time'] = '2018-01-01';
  119. $origin_content[0]['end_time'] = '2030-01-01';
  120. $origin_content[0]['content'] = json_decode($replays['2']);
  121. $origin_content[0]['status'] = 1;
  122. $origin_content[0]['is_pic'] = 1;
  123. $replays['content_mode_2'] = json_encode($origin_content);
  124. Redis::hmset('site_sub_reply:'.$distribution_channel_id,$replays);
  125. }
  126. if(! isset($replays['content_mode_3'])){
  127. \Log::info('initOriginReply:'.$distribution_channel_id.' mode:3');
  128. $replays['3'] = isset($replays['3']) ? $replays['3']:'';
  129. \Log::info($replays['3']);\Log::info(json_decode($replays['3']));
  130. $origin_content[0]['start_time'] = '2018-01-01';
  131. $origin_content[0]['end_time'] = '2030-01-01';
  132. $origin_content[0]['content'] = $replays['3'];
  133. $origin_content[0]['status'] = 1;
  134. $origin_content[0]['is_pic'] = 0;
  135. $replays['content_mode_3'] = json_encode($origin_content);
  136. Redis::hmset('site_sub_reply:'.$distribution_channel_id,$replays);
  137. }
  138. }else{
  139. \Log::info('initOriginReply_is_null:'.$distribution_channel_id);
  140. }
  141. }
  142. }