BaseShop.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace app\shop\controller;
  11. use addon\weapp\model\Config as WeappConfigModel;
  12. use app\Controller;
  13. use app\model\store\Store as StoreModel;
  14. use app\model\system\Addon;
  15. use app\model\system\Config as SystemConfig;
  16. use app\model\system\Group as GroupModel;
  17. use app\model\system\Menu;
  18. use app\model\system\Site;
  19. use app\model\system\User as UserModel;
  20. use app\model\upload\Config as UploadConfigModel;
  21. use app\model\web\Config as ConfigModel;
  22. use app\model\web\DiyView as DiyViewModel;
  23. class BaseShop extends Controller
  24. {
  25. protected $init_menu = [];
  26. protected $crumbs = [];
  27. protected $crumbs_array = [];
  28. protected $uid;
  29. protected $user_info;
  30. protected $url;
  31. protected $group_info;
  32. protected $menus;
  33. protected $menu_info;
  34. protected $site_id;
  35. protected $store_id;
  36. protected $shop_info;
  37. protected $store_info;
  38. protected $app_module = SHOP_MODULE;
  39. protected $replace = [];
  40. protected $addon = '';
  41. public function __construct()
  42. {
  43. //执行父类构造函数
  44. parent::__construct();
  45. //检测基础登录
  46. $this->site_id = request()->siteid();
  47. $user_model = new UserModel();
  48. $this->app_module = $user_model->loginModule($this->site_id);
  49. $this->uid = $user_model->uid($this->app_module, $this->site_id);
  50. if (empty($this->uid)) {
  51. $this->redirect(url("shop/login/login"));
  52. }
  53. $this->url = request()->parseUrl();
  54. $this->addon = request()->addon() ? request()->addon() : '';
  55. $this->user_info = $user_model->userInfo($this->app_module, $this->site_id);
  56. $this->assign("user_info", $this->user_info);
  57. $this->assign('app_module', $this->app_module);
  58. $this->checkLogin();
  59. //检测用户组
  60. $this->getGroupInfo();
  61. if ($this->app_module == 'store') {
  62. $this->replace = [
  63. 'STORE_CSS' => __ROOT__ . '/addon/store/store/view/public/css',
  64. 'STORE_JS' => __ROOT__ . '/addon/store/store/view/public/js',
  65. 'STORE_IMG' => __ROOT__ . '/addon/store/store/view/public/img',
  66. ];
  67. //检测用户组,通过用户组查询对应门店id
  68. $store_model = new StoreModel();
  69. $store_info = $store_model->getStoreInfo([ [ 'store_id', '=', $this->store_id ] ]);
  70. $this->store_info = $store_info[ 'data' ];
  71. if ($this->store_info[ 'is_frozen' ]) {
  72. $this->error('该门店已关闭,请联系店铺管理员开启');
  73. }
  74. $this->assign('store_info', $this->store_info);
  75. }
  76. if (!$this->checkAuth()) {
  77. if (!request()->isAjax()) {
  78. $menu_info = $user_model->getRedirectUrl($this->url, $this->app_module, $this->group_info, $this->addon);
  79. if (empty($menu_info)) {
  80. $this->error('权限不足,请联系客服');
  81. } else {
  82. $this->redirect(addon_url($menu_info[ 'url' ]));
  83. }
  84. } else {
  85. echo json_encode(error(-1, '权限不足,请联系客服'));
  86. exit;
  87. }
  88. }
  89. //获取店铺信息
  90. $site_model = new Site();
  91. $shop_info = $site_model->getSiteInfo([ [ 'site_id', '=', $this->site_id ] ], 'site_id,site_name,logo,seo_keywords,seo_description, create_time');
  92. $this->shop_info = $shop_info[ 'data' ];
  93. $this->assign("shop_info", $shop_info[ 'data' ]);
  94. if (!request()->isAjax()) {
  95. //获取菜单
  96. $this->assign("help_show", HELP_SHOW);
  97. $this->menus = $this->getMenuList();
  98. $this->initBaseInfo();
  99. }
  100. // 加载自定义图标库
  101. $diy_view = new DiyViewModel();
  102. $diy_icon_url = $diy_view->getIconUrl()[ 'data' ];
  103. $this->assign('load_diy_icon_url', $diy_icon_url);
  104. // 上传图片配置
  105. $uplode_config_model = new UploadConfigModel();
  106. $upload_config = $uplode_config_model->getUploadConfig($this->site_id);
  107. $this->assign('upload_max_filesize', $upload_config[ 'data' ][ 'value' ][ 'upload' ][ 'max_filesize' ] / 1024);
  108. if ($this->app_module == 'store') {
  109. $base = 'addon/store/store/view/base.html';
  110. $this->assign('base', $base);
  111. } else {
  112. //$config_model = new ConfigModel();
  113. //$base = $config_model->getStyle($this->site_id);
  114. $base = 'app/shop/view/base/style2.html';
  115. $this->assign('base', $base);
  116. }
  117. }
  118. /**
  119. * 加载基础信息
  120. */
  121. private function initBaseInfo()
  122. {
  123. //获取一级权限菜单
  124. $this->getTopMenu();
  125. $menu_model = new Menu();
  126. $info_result = $menu_model->getMenuInfoByUrl($this->url, $this->app_module, $this->addon);
  127. $info = [];
  128. if (!empty($info_result[ "data" ])) {
  129. $info = $info_result[ "data" ];
  130. $this->getParentMenuList($info[ 'name' ]);
  131. } elseif ($this->url == '/Index/index') {
  132. $info_result = $menu_model->getMenuInfoByUrl($this->url, $this->app_module, $this->addon);
  133. if (!empty($info_result[ "data" ])) {
  134. $info = $info_result[ "data" ];
  135. $this->getParentMenuList($info[ 'name' ]);
  136. }
  137. }
  138. $this->menu_info = $info;
  139. $this->assign("menu_info", $info);
  140. if (!empty($this->crumbs)) {
  141. $this->crumbs = array_reverse($this->crumbs);
  142. $this->assign("crumbs", $this->crumbs);
  143. }
  144. //加载菜单树
  145. $init_menu = $this->initMenu($this->menus, '');
  146. $init_menu = $this->getRealMenu($init_menu);
  147. $this->assign("url", $this->url);
  148. $this->assign("menu", $init_menu);
  149. //加载版权信息
  150. $config_model = new ConfigModel();
  151. $copyright = $config_model->getCopyright();
  152. $this->assign('copyright', $copyright[ 'data' ][ 'value' ]);
  153. // 查询小程序配置信息
  154. if (addon_is_exit('weapp', $this->site_id)) {
  155. $weapp_config_model = new WeappConfigModel();
  156. $weapp_config = $weapp_config_model->getWeappConfig($this->site_id);
  157. $weapp_config = $weapp_config[ 'data' ][ 'value' ];
  158. $this->assign('base_weapp_config', $weapp_config);
  159. }
  160. }
  161. /**
  162. * 加载构造函数信息
  163. */
  164. public function initConstructInfo()
  165. {
  166. $this->site_id = input('site_id', 0);
  167. $config_model = new ConfigModel();
  168. $base = $config_model->getStyle($this->site_id);
  169. $this->assign('base', $base);
  170. $site_model = new Site();
  171. $shop_info = $site_model->getSiteInfo([ [ 'site_id', '=', $this->site_id ] ], 'site_name,logo,seo_keywords,seo_description, create_time')[ 'data' ];
  172. $this->assign("shop_info", $shop_info);
  173. $this->assign('app_module', $this->app_module);
  174. // 加载自定义图标库
  175. $diy_view = new DiyViewModel();
  176. $diy_icon_url = $diy_view->getIconUrl()[ 'data' ];
  177. $this->assign('load_diy_icon_url', $diy_icon_url);
  178. }
  179. /**
  180. * layui化处理菜单数据
  181. * @param $menus_list
  182. * @param string $parent
  183. * @return array
  184. */
  185. public function initMenu($menus_list, $parent = "")
  186. {
  187. $temp_list = [];
  188. if (!empty($menus_list)) {
  189. foreach ($menus_list as $menu_k => $menu_v) {
  190. if (in_array($menu_v[ 'name' ], $this->crumbs_array)) {
  191. $selected = true;
  192. } else {
  193. $selected = false;
  194. }
  195. if ($menu_v[ "parent" ] == $parent && $menu_v[ "is_show" ] == 1) {
  196. $temp_item = array (
  197. 'name' => $menu_v[ 'name' ],
  198. 'level' => $menu_v[ 'level' ],
  199. 'addon' => $menu_v[ 'addon' ],
  200. 'selected' => $selected,
  201. 'url' => addon_url($menu_v[ 'url' ]),
  202. 'title' => $menu_v[ 'title' ],
  203. 'icon' => $menu_v[ 'picture' ],
  204. 'icon_selected' => $menu_v[ 'picture_select' ],
  205. 'target' => '',
  206. 'parent' => $menu_v[ 'parent' ]
  207. );
  208. $child = $this->initMenu($menus_list, $menu_v[ "name" ]);//获取下级的菜单
  209. $temp_item[ "child_list" ] = $child;
  210. $temp_list[ $menu_v[ "name" ] ] = $temp_item;
  211. }
  212. }
  213. }
  214. return $temp_list;
  215. }
  216. /**
  217. * 获取上级菜单列表
  218. * @param string $name
  219. */
  220. private function getParentMenuList($name = '')
  221. {
  222. if (!empty($name)) {
  223. $menu_model = new Menu();
  224. $menu_info_result = $menu_model->getMenuInfo([ [ 'name', "=", $name ], [ 'app_module', '=', $this->app_module ] ]);
  225. $menu_info = $menu_info_result[ "data" ];
  226. if (!empty($menu_info)) {
  227. $menu_info[ "url" ] = addon_url($menu_info[ "url" ]);
  228. $this->crumbs[] = $menu_info;
  229. $this->crumbs_array[] = $menu_info[ 'name' ];
  230. $this->getParentMenuList($menu_info[ 'parent' ]);
  231. }
  232. }
  233. }
  234. /**
  235. * 获取当前用户的用户组
  236. */
  237. private function getGroupInfo()
  238. {
  239. $group_model = new GroupModel();
  240. $group_info_result = $group_model->getGroupInfo([ [ "group_id", "=", $this->user_info[ "group_id" ] ], [ "app_module", "=", $this->app_module ] ]);
  241. $this->group_info = $group_info_result[ "data" ];
  242. if ($this->app_module == 'store') {
  243. //门店登录,用户权限对应站点id是门店id
  244. $this->store_id = $this->group_info[ 'site_id' ];
  245. }
  246. }
  247. /**
  248. * 验证登录
  249. */
  250. private function checkLogin()
  251. {
  252. //验证基础登录
  253. if (!$this->uid) {
  254. $this->redirect(url('shop/login/login'));
  255. }
  256. }
  257. /**
  258. * 检测权限
  259. */
  260. private function checkAuth()
  261. {
  262. if ($this->user_info[ 'is_admin' ] == 1) {
  263. return true;
  264. }
  265. $user_model = new UserModel();
  266. $res = $user_model->checkAuth($this->url, $this->app_module, $this->group_info, $this->addon);
  267. return $res;
  268. }
  269. /**
  270. * 获取菜单
  271. */
  272. private function getMenuList()
  273. {
  274. $menu_model = new Menu();
  275. //暂定全部权限,系统用户做完后放开
  276. if ($this->user_info[ 'is_admin' ] || $this->group_info[ 'is_system' ] == 1) {
  277. $menus = $menu_model->getMenuList([ [ 'app_module', "=", $this->app_module ] ], '*', 'level asc, sort asc');
  278. } else {
  279. $menus = $menu_model->getMenuList([ [ 'name', 'in', $this->group_info[ 'menu_array' ] ], [ 'app_module', "=", $this->app_module ] ], '*', 'level asc,sort asc');
  280. $control_menu = $menu_model->getMenuList([ [ 'is_control', '=', 0 ], [ 'app_module', "=", $this->app_module ] ], '*', 'sort asc');
  281. $menus[ 'data' ] = array_merge($control_menu[ 'data' ], $menus[ 'data' ]);
  282. $keys = array_column($menus[ 'data' ], 'sort');
  283. if (!empty($keys)) {
  284. array_multisort($keys, SORT_ASC, SORT_NUMERIC, $menus[ 'data' ]);
  285. }
  286. }
  287. //隐藏门店模块的概况
  288. $user_info = $this->user_info;
  289. if($user_info['is_admin'] ==0){
  290. $menus[ 'data' ]['1']['is_show']=0;
  291. }
  292. return $menus[ 'data' ];
  293. }
  294. /**
  295. * 获取顶级菜单
  296. */
  297. protected function getTopMenu()
  298. {
  299. $list = array_filter($this->menus, function($v) {
  300. return $v[ 'parent' ] == '0';
  301. });
  302. return $list;
  303. }
  304. /**
  305. * 四级菜单
  306. * @param array $params
  307. */
  308. protected function forthMenu($params = [])
  309. {
  310. if (!empty($this->crumbs)) {
  311. //菜单的等级有可能是四级,也可能是五级,级数不能写死,直接取最后一个就行
  312. $crumbs = $this->crumbs;
  313. $menu_info = array_pop($crumbs);
  314. $menu_model = new Menu();
  315. $menus = $menu_model->getMenuList([ [ 'app_module', "=", $this->app_module ], [ 'is_show', "=", 1 ], [ 'parent', '=', $menu_info[ 'parent' ] ] ], '*', 'sort asc');
  316. foreach ($menus[ 'data' ] as $k => $v) {
  317. $menus[ 'data' ][ $k ][ 'parse_url' ] = addon_url($menus[ 'data' ][ $k ][ 'url' ], $params);
  318. if ($menus[ 'data' ][ $k ][ 'name' ] == $menu_info[ 'name' ]) {
  319. $menus[ 'data' ][ $k ][ 'selected' ] = 1;
  320. } else {
  321. $menus[ 'data' ][ $k ][ 'selected' ] = 0;
  322. }
  323. }
  324. $this->assign('forth_menu', $menus[ 'data' ]);
  325. }
  326. }
  327. /**
  328. * 添加日志
  329. * @param $action_name
  330. * @param array $data
  331. */
  332. protected function addLog($action_name, $data = [])
  333. {
  334. $user = new UserModel();
  335. $user->addUserLog($this->uid, $this->user_info[ 'username' ], $this->site_id, $action_name, $data);
  336. }
  337. /**
  338. * 切换风格
  339. * @return array
  340. */
  341. public function checkStyle()
  342. {
  343. $style = Array (
  344. 'app/shop/view/base/style1.html',
  345. 'app/shop/view/base/style2.html'
  346. );
  347. $type = input('type', 'old');
  348. $data = [];
  349. if ($type == 'old') {
  350. $data[ 'style' ] = $style[ 0 ];
  351. } else if ($type == 'new') {
  352. $data[ 'style' ] = $style[ 1 ];
  353. }
  354. $config_model = new ConfigModel();
  355. $res = $config_model->setStyle($data, $this->site_id);
  356. return $res;
  357. }
  358. /**
  359. * 获取真实的链接
  360. * @param $menu_list
  361. * @return mixed
  362. */
  363. protected function getRealMenu($menu_list)
  364. {
  365. if (empty($this->crumbs)) {
  366. return $menu_list;
  367. }
  368. if ($this->crumbs[ 0 ][ 'name' ] != 'PROMOTION_ROOT') return $menu_list;
  369. $addon_model = new Addon();
  370. $value = $addon_model->getAddonQuickMenuConfig($this->site_id, $this->app_module)[ 'data' ][ 'value' ];
  371. $promotion_addon = $value[ 'promotion' ];
  372. $tool_addon = $value[ 'tool' ];
  373. if (isset($this->crumbs[ 0 ])) {
  374. // 处理父级菜单不在营销下的插件
  375. if (!empty($menu_list[ 'PROMOTION_ROOT' ][ 'child_list' ][ 'PROMOTION_CENTER' ])) {
  376. $all_promotion = array_filter(array_column($menu_list[ 'PROMOTION_ROOT' ][ 'child_list' ][ 'PROMOTION_CENTER' ][ 'child_list' ], 'addon'));
  377. $promotion_diff = array_filter(array_diff($promotion_addon, $all_promotion));
  378. if (!empty($promotion_diff)) {
  379. foreach ($promotion_diff as $addon) {
  380. $addon_menu = require 'addon/' . $addon . '/config/menu_' . $this->app_module . '.php';
  381. $addon_info = require 'addon/' . $addon . '/config/info.php';
  382. if (isset($addon_menu[ 0 ])) {
  383. array_push($menu_list[ 'PROMOTION_ROOT' ][ 'child_list' ][ 'PROMOTION_CENTER' ][ 'child_list' ],
  384. array_merge($addon_menu[ 0 ], [ 'title' => $addon_info[ 'title' ], 'selected' => false, 'url' => addon_url($addon_menu[ 0 ][ 'url' ]) ]));
  385. }
  386. }
  387. }
  388. }
  389. if (!empty($menu_list[ 'PROMOTION_ROOT' ][ 'child_list' ][ 'PROMOTION_TOOL' ])) {
  390. $all_tool = array_filter(array_column($menu_list[ 'PROMOTION_ROOT' ][ 'child_list' ][ 'PROMOTION_TOOL' ][ 'child_list' ], 'addon'));
  391. $tool_diff = array_filter(array_diff($tool_addon, $all_tool));
  392. if (!empty($tool_diff)) {
  393. foreach ($tool_diff as $addon) {
  394. $addon_menu = require 'addon/' . $addon . '/config/menu_' . $this->app_module . '.php';
  395. $addon_info = require 'addon/' . $addon . '/config/info.php';
  396. if (isset($addon_menu[ 0 ])) {
  397. array_push($menu_list[ 'PROMOTION_ROOT' ][ 'child_list' ][ 'PROMOTION_TOOL' ][ 'child_list' ],
  398. array_merge($addon_menu[ 0 ], [ 'title' => $addon_info[ 'title' ], 'selected' => false, 'url' => addon_url($addon_menu[ 0 ][ 'url' ]) ]));
  399. }
  400. }
  401. }
  402. }
  403. if (!empty($menu_list[ 'PROMOTION_ROOT' ][ 'child_list' ][ 'PROMOTION_CENTER' ])) {
  404. foreach ($menu_list[ 'PROMOTION_ROOT' ][ 'child_list' ][ 'PROMOTION_CENTER' ][ 'child_list' ] as $k => &$val) {
  405. if (!empty($val[ 'addon' ]) && ( $val[ 'addon' ] != $this->addon && !in_array($val[ 'addon' ], $promotion_addon) )) {
  406. unset($menu_list[ 'PROMOTION_ROOT' ][ 'child_list' ][ 'PROMOTION_CENTER' ][ 'child_list' ][ $k ]);
  407. }
  408. }
  409. }
  410. if (!empty($menu_list[ 'PROMOTION_ROOT' ][ 'child_list' ][ 'PROMOTION_TOOL' ])) {
  411. foreach ($menu_list[ 'PROMOTION_ROOT' ][ 'child_list' ][ 'PROMOTION_TOOL' ][ 'child_list' ] as $k => &$val) {
  412. if (!empty($val[ 'addon' ]) && ( $val[ 'addon' ] != $this->addon && !in_array($val[ 'addon' ], $tool_addon) )) {
  413. unset($menu_list[ 'PROMOTION_ROOT' ][ 'child_list' ][ 'PROMOTION_TOOL' ][ 'child_list' ][ $k ]);
  414. }
  415. }
  416. }
  417. }
  418. return $menu_list;
  419. }
  420. public function __call($method, $args)
  421. {
  422. return $this->fetch(app()->getRootPath() . 'public/error/error.html');
  423. }
  424. }