BackendMsg.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\common\service\msg;
  4. use app\common\service\MsgService;
  5. use app\common\model\Msg;
  6. class BackendMsg extends MsgService{
  7. protected $msg_type='backend';
  8. //消息icon
  9. private $icon='fa fa-file';
  10. //消息头部标题
  11. private $title='通知';
  12. //消息样式,包括primary,warning,success,error,info
  13. private $style='primary';
  14. protected function sendEvent(Msg $msg):bool
  15. {
  16. return true;
  17. }
  18. public function setMessageStyle(string $title,string $icon,string $style)
  19. {
  20. $this->icon=$icon;
  21. $this->title=$title;
  22. $this->style=$style;
  23. return $this;
  24. }
  25. /**
  26. * 发送普通消息
  27. * @param string $content 消息内容
  28. * @param int $send_to 接收者admin_id
  29. * @return void
  30. */
  31. public function sendNormalMessage(int $send_to,string $content)
  32. {
  33. $arr=[
  34. 'style'=>$this->style,
  35. 'icon'=>$this->icon,
  36. 'title'=>$this->title,
  37. 'content'=>$content
  38. ];
  39. $this->sendTo($send_to);
  40. $this->setContent(json_encode($arr,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES));
  41. $this->create();
  42. }
  43. /**
  44. * 发送一条打开新窗口的消息
  45. * @param string $content 消息内容
  46. * @param int $send_to 接收者admin_id
  47. * @param string $url 打开的新窗口的url
  48. * @return void
  49. */
  50. public function sendBlankMessage(int $send_to,string $content,string $url)
  51. {
  52. $arr=[
  53. 'style'=>$this->style,
  54. 'icon'=>$this->icon,
  55. 'title'=>$this->title,
  56. 'action'=>'link',
  57. 'url'=>$url,
  58. 'content'=>$content
  59. ];
  60. $this->sendTo($send_to);
  61. $this->setContent(json_encode($arr,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES));
  62. $this->create();
  63. }
  64. /**
  65. * 发送一条打开新选项卡消息
  66. * @param string $content 消息内容
  67. * @param int $send_to 接收者admin_id
  68. * @param string $url 打开的新窗口的url
  69. * @param array|string 当为字符串时为选项卡的url,当为array时为选项卡参数,url为必填,其他的可以省略,包括如:
  70. * [
  71. * //当id等于菜单id时,会激活菜单
  72. * 'id'=>9,
  73. * 'title'=>'标题',
  74. * 'icon'=>'fa fa-envira',
  75. * 'url'=>'auth/admin'
  76. * ]
  77. *
  78. * @return void
  79. */
  80. public function sendAddTabsMessage(int|array $send_to,string $content,array|string $options=[],)
  81. {
  82. if(is_string($options)){
  83. $options=['url'=>$options];
  84. }
  85. $tabs=[
  86. 'id'=>'',
  87. 'title'=>$this->title,
  88. 'icon'=>$this->icon,
  89. ];
  90. $options=array_merge($tabs,$options);
  91. if(!isset($options['url'])){
  92. throw new \Exception(__('选项卡url不能为空'));
  93. }
  94. $arr=[
  95. 'style'=>$this->style,
  96. 'icon'=>$this->icon,
  97. 'title'=>$this->title,
  98. 'action'=>'tab',
  99. 'options'=>$options,
  100. 'content'=>$content
  101. ];
  102. $this->sendTo($send_to);
  103. $this->setContent(json_encode($arr,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES));
  104. $this->create();
  105. }
  106. /**
  107. * 发送一条打开弹窗消息
  108. * @param string $content 消息内容
  109. * @param int $send_to 接收者admin_id
  110. * @param string $url 打开的新窗口的url
  111. * @param array|string 当为字符串时为选项卡的url,当为array时为弹窗参数,url为必填,其他的可以省略,包括如:
  112. * [
  113. * //当id等于菜单id时,会激活菜单
  114. * 'id'=>9,
  115. * 'title'=>'fastadmin',
  116. * 'icon'=>'fa fa-facebook',
  117. * 'url'=>'general/profile',
  118. * 'width'=>800,
  119. * 'height'=>500,
  120. * 'expand'=>false
  121. * ]
  122. *
  123. * @return void
  124. */
  125. public function sendLayerMessage(int $send_to,string $content,array|string $options=[],)
  126. {
  127. if(is_string($options)){
  128. $options=['url'=>$options];
  129. }
  130. $layer=[
  131. 'id'=>'',
  132. 'title'=>$this->title,
  133. 'icon'=>$this->icon,
  134. 'width'=>800,
  135. 'height'=>500,
  136. 'expand'=>false
  137. ];
  138. $options=array_merge($layer,$options);
  139. if(!isset($options['url'])){
  140. throw new \Exception(__('选项卡url不能为空'));
  141. }
  142. $arr=[
  143. 'style'=>$this->style,
  144. 'icon'=>$this->icon,
  145. 'title'=>$this->title,
  146. 'action'=>'layer',
  147. 'options'=>$options,
  148. 'content'=>$content
  149. ];
  150. $this->sendTo($send_to);
  151. $this->setContent(json_encode($arr,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES));
  152. $this->create();
  153. }
  154. }