Shop.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace addons\shop;
  3. use addons\shop\library\FulltextSearch;
  4. use addons\shop\library\Service;
  5. use app\common\library\Menu;
  6. use think\Addons;
  7. use think\Config;
  8. use think\Db;
  9. use think\Request;
  10. use think\Loader;
  11. /**
  12. * Shop插件
  13. */
  14. class Shop extends Addons
  15. {
  16. /**
  17. * 插件安装方法
  18. * @return bool
  19. */
  20. public function install()
  21. {
  22. $menu = include ADDON_PATH . 'shop' . DS . 'data' . DS . 'menu.php';
  23. Menu::create($menu);
  24. return true;
  25. }
  26. /**
  27. * 插件卸载方法
  28. * @return bool
  29. */
  30. public function uninstall()
  31. {
  32. Menu::delete('shop');
  33. return true;
  34. }
  35. /**
  36. * 插件启用方法
  37. */
  38. public function enable()
  39. {
  40. $menu = include ADDON_PATH . 'shop' . DS . 'data' . DS . 'menu.php';
  41. Menu::upgrade('shop', $menu);
  42. Menu::enable('shop');
  43. }
  44. /**
  45. * 插件禁用方法
  46. */
  47. public function disable()
  48. {
  49. Menu::disable('shop');
  50. }
  51. /**
  52. * 插件升级方法
  53. */
  54. public function upgrade()
  55. {
  56. $menu = include ADDON_PATH . 'shop' . DS . 'data' . DS . 'menu.php';
  57. Menu::upgrade('shop', $menu);
  58. }
  59. /**
  60. * 应用初始化
  61. */
  62. public function appInit()
  63. {
  64. //添加命名空间
  65. if (!class_exists('\Hashids\Hashids')) {
  66. Loader::addNamespace('Hashids', ADDON_PATH . 'shop' . DS . 'library' . DS . 'hashids' . DS);
  67. }
  68. // 自定义路由变量规则
  69. // \think\Route::pattern([
  70. // 'diyname' => "/[a-zA-Z0-9\-_\x{4e00}-\x{9fa5}]+/u",
  71. // 'id' => "\d+",
  72. // ]);
  73. $config = get_addon_config('shop');
  74. $taglib = Config::get('template.taglib_pre_load');
  75. Config::set('template.taglib_pre_load', ($taglib ? $taglib . ',' : '') . 'addons\\shop\\taglib\\Shop');
  76. Config::set('shop', $config);
  77. }
  78. /**
  79. * 脚本替换
  80. */
  81. public function viewFilter(& $content)
  82. {
  83. $request = \think\Request::instance();
  84. $dispatch = $request->dispatch();
  85. if (!$dispatch) {
  86. return;
  87. }
  88. if ($request->module() || !isset($dispatch['method'][0]) || $dispatch['method'][0] != '\think\addons\Route') {
  89. return;
  90. }
  91. $addon = isset($dispatch['var']['addon']) ? $dispatch['var']['addon'] : $request->param('addon');
  92. if ($addon != 'shop') {
  93. return;
  94. }
  95. $style = '';
  96. $script = '';
  97. $result = preg_replace_callback("/<(script|style)\s(data\-render=\"(script|style)\")([\s\S]*?)>([\s\S]*?)<\/(script|style)>/i", function ($match) use (&$style, &$script) {
  98. if (isset($match[1]) && in_array($match[1], ['style', 'script'])) {
  99. ${$match[1]} .= str_replace($match[2], '', $match[0]);
  100. }
  101. return '';
  102. }, $content);
  103. $content = preg_replace_callback('/^\s+(\{__STYLE__\}|\{__SCRIPT__\})\s+$/m', function ($matches) use ($style, $script) {
  104. return $matches[1] == '{__STYLE__}' ? $style : $script;
  105. }, $result ? $result : $content);
  106. }
  107. /**
  108. * 会员中心边栏后
  109. * @return mixed
  110. * @throws \Exception
  111. */
  112. public function userSidenavAfter()
  113. {
  114. $request = Request::instance();
  115. $controllername = strtolower($request->controller());
  116. $actionname = strtolower($request->action());
  117. $config = get_addon_config('shop');
  118. $usersidebar = explode(',', $config['usersidenav']);
  119. if (!$usersidebar) {
  120. return '';
  121. }
  122. $data = [
  123. 'controllername' => $controllername,
  124. 'actionname' => $actionname,
  125. 'usersidebar' => $usersidebar
  126. ];
  127. return $this->fetch('view/hook/user_sidenav_after', $data);
  128. }
  129. }