BaseShop.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 山西牛酷信息科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com.cn
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
  9. * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
  10. * =========================================================
  11. */
  12. namespace app\shop\controller;
  13. use app\Controller;
  14. use app\model\shop\Shop;
  15. use app\model\shop\ShopGroup;
  16. use app\model\system\Addon;
  17. use app\model\system\Group as GroupModel;
  18. use app\model\system\Menu;
  19. use app\model\system\Promotion as PrmotionModel;
  20. use app\model\system\User as UserModel;
  21. use app\model\web\Config as ConfigModel;
  22. use app\model\web\WebSite;
  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 $site_id;
  34. protected $website_id;
  35. protected $shop_info;
  36. protected $app_module = "shop";
  37. protected $addon = '';
  38. public function __construct()
  39. {
  40. //执行父类构造函数
  41. parent::__construct();
  42. //检测基础登录
  43. $user_model = new UserModel();
  44. $this->uid = $user_model->uid($this->app_module);
  45. $this->url = request()->parseUrl();
  46. $this->addon = request()->addon() ? request()->addon() : '';
  47. $this->user_info = $user_model->userInfo($this->app_module);
  48. $this->assign("user_info", $this->user_info);
  49. $this->site_id = $this->user_info["site_id"];
  50. if (empty($this->uid)) {
  51. $this->redirect(url("shop/login/login"));
  52. exit();
  53. }
  54. if ($this->site_id == 0) {
  55. $this->redirect(url("shop/apply/index"));
  56. exit();
  57. }
  58. $this->checkLogin();
  59. //检测用户组
  60. $this->getGroupInfo();
  61. if (!$this->checkAuth()) {
  62. if (!request()->isAjax()) {
  63. $this->error('权限不足');
  64. } else {
  65. echo json_encode(error('', '权限不足'));
  66. exit;
  67. }
  68. }
  69. //获取店铺信息
  70. $shop = new Shop();
  71. $shop_info = $shop->getShopInfo([ [ 'site_id', '=', $this->site_id ] ], 'website_id, site_name,logo,is_own,level_id,category_id,group_id,seo_keywords,seo_description,expire_time,shop_status');
  72. $this->website_id = $shop_info['data']['website_id'];
  73. $this->shop_info = $shop_info['data'];
  74. $this->assign("shop_info", $shop_info['data']);
  75. if (!request()->isAjax()) {
  76. //获取菜单
  77. $this->menus = $this->getMenuList();
  78. $this->initBaseInfo();
  79. }
  80. }
  81. /**
  82. * 加载基础信息
  83. */
  84. private function initBaseInfo()
  85. {
  86. //获取一级权限菜单
  87. $this->getTopMenu();
  88. $menu_model = new Menu();
  89. $info_result = $menu_model->getMenuInfoByUrl($this->url, $this->app_module, $this->addon);
  90. $info = [];
  91. if (!empty($info_result["data"])) {
  92. $info = $info_result["data"];
  93. $this->getParentMenuList($info['name']);
  94. }
  95. $this->assign("menu_info", $info);
  96. //加载菜单树
  97. $init_menu = $this->initMenu($this->menus, '');
  98. // 应用下的菜单特殊处理
  99. if (!empty($this->crumbs) && $this->crumbs[0]['name'] == 'TOOL_ROOT') {
  100. //如果当前选择了【应用管理】,则只保留【应用管理】菜单
  101. if ($this->crumbs[1]['name'] == 'PROMOTION_TOOL') {
  102. foreach ($init_menu as $k => $v) {
  103. if ($v['selected']) {
  104. $init_menu[ $k ]['child_list'] = [ $v['child_list']['PROMOTION_TOOL'] ];
  105. break;
  106. }
  107. }
  108. } else {
  109. //选择了应用下的某个插件,则移除【应用管理】菜单,显示该插件下的菜单,并且标题名称改为插件名称
  110. $addon_model = new Addon();
  111. $addon_info = $addon_model->getAddonInfo([ [ 'name', '=', request()->addon() ] ], 'name,title');
  112. $addon_info = $addon_info['data'];
  113. foreach ($init_menu as $k => $v) {
  114. if ($v['selected']) {
  115. $this->crumbs[0]['title'] = $addon_info['title'];
  116. unset($init_menu[ $k ]['child_list']['PROMOTION_TOOL']);
  117. foreach ($init_menu[ $k ]['child_list'] as $ck => $cv) {
  118. if ($cv['addon'] != $addon_info['name']) {
  119. unset($init_menu[ $k ]['child_list'][ $ck ]);
  120. }
  121. }
  122. break;
  123. }
  124. }
  125. }
  126. }
  127. $this->assign("url", $this->url);
  128. $this->assign("menu", $init_menu);
  129. $this->assign("crumbs", $this->crumbs);
  130. //加载网站基础信息
  131. $website = new WebSite();
  132. $website_info = $website->getWebSite([ [ 'site_id', '=', 0 ] ], 'title,logo,desc,keywords,web_status,close_reason,web_qrcode,web_phone');
  133. $this->assign("website_info", $website_info['data']);
  134. //加载版权信息
  135. $config_model = new ConfigModel();
  136. $copyright = $config_model->getCopyright();
  137. $this->assign('copyright', $copyright['data']['value']);
  138. }
  139. /**
  140. * layui化处理菜单数据
  141. */
  142. public function initMenu($menus_list, $parent = "")
  143. {
  144. $temp_list = [];
  145. if (!empty($menus_list)) {
  146. foreach ($menus_list as $menu_k => $menu_v) {
  147. if (in_array($menu_v['name'], $this->crumbs_array)) {
  148. $selected = true;
  149. } else {
  150. $selected = false;
  151. }
  152. if ($menu_v["parent"] == $parent && $menu_v["is_show"] == 1) {
  153. $temp_item = array(
  154. 'addon' => $menu_v['addon'],
  155. 'selected' => $selected,
  156. 'url' => addon_url($menu_v['url']),
  157. 'title' => $menu_v['title'],
  158. 'icon' => $menu_v['picture'],
  159. 'icon_selected' => $menu_v['picture_select'],
  160. 'target' => ''
  161. );
  162. $child = $this->initMenu($menus_list, $menu_v["name"]);//获取下级的菜单
  163. $temp_item["child_list"] = $child;
  164. $temp_list[ $menu_v["name"] ] = $temp_item;
  165. }
  166. }
  167. }
  168. return $temp_list;
  169. }
  170. /**
  171. * 获取上级菜单列表
  172. * @param number $menu_id
  173. */
  174. private function getParentMenuList($name = '')
  175. {
  176. if (!empty($name)) {
  177. $menu_model = new Menu();
  178. $menu_info_result = $menu_model->getMenuInfo([ [ 'name', "=", $name ], [ 'app_module', '=', $this->app_module ] ]);
  179. $menu_info = $menu_info_result["data"];
  180. if (!empty($menu_info)) {
  181. $this->getParentMenuList($menu_info['parent']);
  182. $menu_info["url"] = addon_url($menu_info["url"]);
  183. $this->crumbs[] = $menu_info;
  184. $this->crumbs_array[] = $menu_info['name'];
  185. }
  186. }
  187. }
  188. /**
  189. * 获取当前用户的用户组
  190. */
  191. private function getGroupInfo()
  192. {
  193. $group_model = new GroupModel();
  194. $group_info_result = $group_model->getGroupInfo([ [ "group_id", "=", $this->user_info["group_id"] ], [ "site_id", "=", $this->site_id ], [ "app_module", "=", $this->app_module ] ]);
  195. $this->group_info = $group_info_result["data"];
  196. }
  197. /**
  198. * 验证登录
  199. */
  200. private function checkLogin()
  201. {
  202. //验证基础登录
  203. if (!$this->uid) {
  204. $this->redirect(url('shop/login/login'));
  205. }
  206. }
  207. /**
  208. * 检测权限
  209. */
  210. private function checkAuth()
  211. {
  212. $user_model = new UserModel();
  213. echo $this->url."<br />";
  214. echo $this->app_module."<br />";
  215. echo $this->group_info."<br />";
  216. die;
  217. $res = $user_model->checkAuth($this->url, $this->app_module, $this->group_info);
  218. return $res;
  219. }
  220. /**
  221. * 获取菜单
  222. */
  223. private function getMenuList()
  224. {
  225. $menu_model = new Menu();
  226. if ($this->group_info['is_system'] == 1) {
  227. $shop_group_model = new ShopGroup();
  228. $shop_group_info_result = $shop_group_model->getGroupInfo([ [ 'group_id', '=', $this->user_info['app_group'] ] ], 'addon_array');
  229. $shop_group_info = $shop_group_info_result['data'];
  230. if (empty($shop_group_info)) {
  231. $this->error('用户组缺失');
  232. }
  233. //获取除营销插件以外的shop菜单
  234. $promotion_model = new PrmotionModel();
  235. $promotions = $promotion_model->getPromotions();
  236. $all_promotion_name = array_column($promotions['shop'], 'name');
  237. $menus = $menu_model->getMenuList([[ 'app_module', '=', $this->app_module ], [ 'addon', 'NOT IN', $all_promotion_name ] , [ 'is_show', '=', 1 ]], '*', 'sort ASC');
  238. $addon_menus = $menu_model->getMenuList([[ 'addon', 'in', $shop_group_info['addon_array'] ], [ 'app_module', '=', $this->app_module ], [ 'is_show', '=', 1 ] ], '*', 'sort ASC');
  239. if (!empty($addon_menus)) {
  240. $menus['data'] = array_merge($menus['data'], $addon_menus['data']);
  241. }
  242. } else {
  243. $addon_array = $this->group_info['addon_array'];
  244. $addon_menus = $menu_model->getMenuList([[ 'addon', 'in', $addon_array ], [ 'app_module', '=', $this->app_module ], [ 'is_show', '=', 1 ] ], '*', 'sort asc');
  245. $addon_menus = $addon_menus['data'];
  246. $menus = $menu_model->getMenuList([[ 'name', 'in', $this->group_info['menu_array'] ], [ 'is_show', '=', 1 ], [ 'app_module', '=', $this->app_module ] ], '*', 'sort ASC');
  247. if (!empty($addon_menus)) {
  248. $menus['data'] = array_merge($menus['data'], $addon_menus);
  249. }
  250. }
  251. return $menus['data'];
  252. }
  253. /**
  254. * 获取顶级菜单
  255. */
  256. protected function getTopMenu()
  257. {
  258. $list = array_filter($this->menus, function ($v) {
  259. return $v['parent'] == '0';
  260. });
  261. return $list;
  262. }
  263. protected function forthMenu($params = [])
  264. {
  265. $url = strtolower($this->url);
  266. $menu_model = new Menu();
  267. $menu_info = $menu_model->getMenuInfo([ [ 'url', "=", $url ], [ 'level', '=', 4 ] ], 'parent');
  268. if (!empty($menu_info['data'])) {
  269. $menus = $menu_model->getMenuList([ [ 'app_module', "=", $this->app_module ], [ 'is_show', "=", 1 ], [ 'parent', '=', $menu_info['data']['parent'] ] ], '*', 'sort asc');
  270. foreach ($menus['data'] as $k => $v) {
  271. $menus['data'][ $k ]['parse_url'] = addon_url($menus['data'][ $k ]['url'], $params);
  272. if ($menus['data'][ $k ]['url'] == $url) {
  273. $menus['data'][ $k ]['selected'] = 1;
  274. } else {
  275. $menus['data'][ $k ]['selected'] = 0;
  276. }
  277. }
  278. $this->assign('forth_menu', $menus['data']);
  279. }
  280. }
  281. /**
  282. * 添加日志
  283. * @param unknown $action_name
  284. * @param unknown $data
  285. */
  286. protected function addLog($action_name, $data = [])
  287. {
  288. $user = new UserModel();
  289. $user->addUserLog($this->uid, $this->user_info['username'], $this->site_id, $action_name, $data);
  290. }
  291. }