ChangeMenuService.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace app\main\service;
  3. use app\main\constants\MqConstants;
  4. use app\main\InternalInterface\SingletonService;
  5. class ChangeMenuService extends BaseService {
  6. const EXCHANGE_NAME ='cps.ex.topic';
  7. private $map = [
  8. 'is_push' => true,
  9. 'find' => null,
  10. 'channel' => null,
  11. 'replace' => null,
  12. 'params' => null,
  13. ];
  14. /**
  15. * @var ChangeMenuService
  16. */
  17. protected static $self = null;
  18. /**
  19. * @return BaseService|ChangeMenuService
  20. */
  21. public static function instance()
  22. {
  23. if (self::$self == NULL) {
  24. self::$self = new self();
  25. }
  26. return self::$self;
  27. }
  28. /**
  29. * 不推送到微信
  30. */
  31. public function noPushWeChat(){
  32. $this->map['is_push'] = false;
  33. return $this;
  34. }
  35. /**
  36. * 设置要查找替换的域名
  37. * @param $host
  38. * @return $this
  39. * @throws \Exception
  40. */
  41. public function setFindHost($host){
  42. if(!is_string($host)){
  43. throw new \Exception("参数错误");
  44. }
  45. $this->map['find'] = $host;
  46. return $this;
  47. }
  48. /**
  49. * 根据业务域名ID查找替换
  50. * @param int $host_id
  51. * @return $this
  52. * @throws \Exception
  53. */
  54. public function setFindHostById($host_id){
  55. if(!is_numeric($host_id)){
  56. throw new \Exception("参数错误");
  57. }
  58. $host_info = model('Ophost')->getInfoById($host_id);
  59. $host = $host_info['host'] ?? null;
  60. $this->map['find'] = $host;
  61. return $this;
  62. }
  63. /**
  64. * 设置要替换的域名ID
  65. * @param int $host_id
  66. * @return $this
  67. * @throws \Exception
  68. */
  69. public function setReplaceHostById($host_id){
  70. if(!is_numeric($host_id)){
  71. throw new \Exception("参数错误");
  72. }
  73. $host_info = model('Ophost')->getInfoById($host_id);
  74. $host = $host_info['host'] ?? null;
  75. $this->map['replace'] = $host;
  76. return $this;
  77. }
  78. /**
  79. * 设置要替换的域名
  80. * @param $host
  81. * @return $this
  82. * @throws \Exception
  83. */
  84. public function setReplaceHost($host){
  85. if(!is_string($host)){
  86. throw new \Exception("参数错误");
  87. }
  88. $this->map['replace'] = $host;
  89. return $this;
  90. }
  91. /**
  92. * 设置要替换的渠道ID
  93. * @param $channel_id
  94. * @return ChangeMenuService
  95. */
  96. public function setChannelId($channel_id){
  97. $this->map['channel'] = $channel_id;
  98. return $this;
  99. }
  100. /**
  101. * 设置要追加的参数
  102. * @param array $params
  103. * @return $this
  104. */
  105. public function setAppendParams(array $params){
  106. $this->map['params'] = $params;
  107. return $this;
  108. }
  109. /**
  110. * 推送MQ消息
  111. */
  112. public function push(){
  113. $mq = MqService::instance()->getDotMqInstance();
  114. $mq->transferExchange($this->map, self::EXCHANGE_NAME, MqConstants::CHANNEL_CHANGE_MENU, 'topic');
  115. }
  116. }