Notice.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace addons\notice;
  3. use addons\notice\library\GatewayTool;
  4. use addons\notice\library\NoticeClient;
  5. use app\common\library\Menu;
  6. use think\Addons;
  7. use think\Request;
  8. /**
  9. * 插件
  10. */
  11. class Notice extends Addons
  12. {
  13. /**
  14. * 应用初始化
  15. */
  16. public function appInit()
  17. {
  18. // 公共方法
  19. require_once __DIR__ . '/library/common/helper.php';
  20. if ($this->getConfig()['admin_real'] == 2 || $this->getConfig()['user_real'] == 2) {
  21. require_once __DIR__ . '/library/libs/GatewayClient/Gateway.php';
  22. GatewayTool::init();
  23. }
  24. if (request()->isCli()) {
  25. \think\Console::addDefaultCommands([
  26. 'addons\notice\library\libs\GatewayWorker\server'
  27. ]);
  28. }
  29. }
  30. /**
  31. * @param $params
  32. */
  33. public function configInit(&$params)
  34. {
  35. $config = $this->getConfig();
  36. if ($config['wss_switch'] == 1) {
  37. $protocol = 'wss://';
  38. } else {
  39. $protocol = 'ws://';
  40. }
  41. $wsurl = $protocol .\request()->host() . ':' . $config['websocket_port'];
  42. $params['notice'] = [
  43. 'admin_real' => $config['admin_real'],
  44. 'wsurl' => $wsurl,
  45. 'user_real_url' => $config['user_real_url'],
  46. 'user_real' => $config['user_real']
  47. ];
  48. }
  49. /**
  50. * 插件安装方法
  51. *
  52. * @return bool
  53. */
  54. public function install()
  55. {
  56. $config_file = ADDON_PATH . "notice" . DS . 'config' . DS . "menu.php";
  57. $menu = include $config_file;
  58. Menu::create($menu);
  59. return true;
  60. }
  61. /**
  62. * 插件卸载方法
  63. *
  64. * @return bool
  65. */
  66. public function uninstall()
  67. {
  68. Menu::delete("notice");
  69. Menu::delete("notice/admin");
  70. return true;
  71. }
  72. /**
  73. * 插件启用方法
  74. *
  75. * @return bool
  76. */
  77. public function enable()
  78. {
  79. Menu::enable("notice");
  80. Menu::enable("notice/admin");
  81. return true;
  82. }
  83. /**
  84. * 插件禁用方法
  85. *
  86. * @return bool
  87. */
  88. public function disable()
  89. {
  90. Menu::disable("notice");
  91. Menu::disable("notice/admin");
  92. return true;
  93. }
  94. /**
  95. * 会员中心边栏后
  96. *
  97. * @return mixed
  98. * @throws \Exception
  99. */
  100. public function userSidenavAfter()
  101. {
  102. $request = Request::instance();
  103. $actionname = strtolower($request->action());
  104. $data = [
  105. 'actionname' => $actionname,
  106. ];
  107. return $this->fetch('view/hook/user_sidenav_after', $data);
  108. }
  109. /**
  110. * 发送消息
  111. */
  112. public function sendNotice($params)
  113. {
  114. return NoticeClient::instance()->trigger($params['event'], $params['params']);
  115. }
  116. }