BaseAdmin.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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\admin\controller;
  13. use app\Controller;
  14. use app\model\system\Addon;
  15. use app\model\system\Group;
  16. use app\model\system\Menu;
  17. use app\model\system\User as UserModel;
  18. use app\model\web\Config as ConfigModel;
  19. use app\model\web\WebSite;
  20. use app\model\system\User;
  21. class BaseAdmin extends Controller
  22. {
  23. protected $init_menu = [];
  24. protected $crumbs = [];
  25. protected $crumbs_array = [];
  26. protected $uid;
  27. protected $user_info;
  28. protected $url;
  29. protected $group_info;
  30. protected $menus;
  31. protected $site_id = 0;
  32. protected $app_module = "admin";
  33. protected $addon = '';
  34. public function __construct()
  35. {
  36. //执行父类构造函数
  37. parent::__construct();
  38. $user = new UserModel();
  39. //检测基础登录
  40. $this->uid = $user->uid($this->app_module);
  41. $this->url = request()->parseUrl();
  42. $this->addon = request()->addon() ? request()->addon() : '';
  43. $this->user_info = $user->userInfo($this->app_module);
  44. $this->assign("user_info", $this->user_info);
  45. $this->checkLogin();
  46. //检测用户组
  47. $this->getGroupInfo();
  48. if (!$this->checkAuth()) {
  49. if (!request()->isAjax()) {
  50. $this->error('权限不足');
  51. } else {
  52. echo json_encode(error('', '权限不足'));
  53. exit;
  54. }
  55. }
  56. if (!request()->isAjax()) {
  57. //获取菜单
  58. $this->menus = $this->getMenuList();
  59. $this->initBaseInfo();
  60. }
  61. }
  62. /**
  63. * 加载基础信息
  64. */
  65. private function initBaseInfo()
  66. {
  67. //获取一级权限菜单
  68. $this->getTopMenu();
  69. $menu_model = new Menu();
  70. $info = $menu_model->getMenuInfoByUrl($this->url, $this->app_module, $this->addon);
  71. if (!empty($info['data'])) {
  72. $this->getParentMenuList($info['data']['name']);
  73. $this->assign("menu_info", $info['data']);
  74. }
  75. //加载网站基础信息
  76. $website = new WebSite();
  77. $website_info = $website->getWebSite([ [ 'site_id', '=', 0 ] ], 'title,logo,desc,keywords,web_status,close_reason');
  78. $this->assign("website", $website_info['data']);
  79. //加载菜单树
  80. $init_menu = $this->initMenu($this->menus, '');
  81. // 应用下的菜单特殊处理
  82. if (!empty($this->crumbs) && $this->crumbs[0]['name'] == 'TOOL_ROOT') {
  83. //如果当前选择了【应用管理】,则只保留【应用管理】菜单
  84. if ($this->crumbs[1]['name'] == 'PROMOTION_TOOL') {
  85. foreach ($init_menu as $k => $v) {
  86. if ($v['selected']) {
  87. $init_menu[ $k ]['child_list'] = [ $v['child_list']['PROMOTION_TOOL'] ];
  88. break;
  89. }
  90. }
  91. } else {
  92. //选择了应用下的某个插件,则移除【应用管理】菜单,显示该插件下的菜单,并且标题名称改为插件名称
  93. $addon_model = new Addon();
  94. $addon_info = $addon_model->getAddonInfo([ [ 'name', '=', request()->addon() ] ], 'name,title');
  95. $addon_info = $addon_info['data'];
  96. foreach ($init_menu as $k => $v) {
  97. if ($v['selected']) {
  98. $this->crumbs[0]['title'] = $addon_info['title'];
  99. unset($init_menu[ $k ]['child_list']['PROMOTION_TOOL']);
  100. foreach ($init_menu[ $k ]['child_list'] as $ck => $cv) {
  101. if ($cv['addon'] != $addon_info['name']) {
  102. unset($init_menu[ $k ]['child_list'][ $ck ]);
  103. }
  104. }
  105. break;
  106. }
  107. }
  108. }
  109. }
  110. //加载版权信息
  111. $config_model = new ConfigModel();
  112. $copyright = $config_model->getCopyright();
  113. $this->assign('copyright', $copyright['data']['value']);
  114. $this->assign("url", $this->url);
  115. $this->assign("menu", $init_menu);
  116. $this->assign("crumbs", $this->crumbs);
  117. }
  118. /**
  119. * layui化处理菜单数据
  120. */
  121. public function initMenu($menus_list, $parent = "")
  122. {
  123. $temp_list = [];
  124. if (!empty($menus_list)) {
  125. foreach ($menus_list as $menu_k => $menu_v) {
  126. if (in_array($menu_v['name'], $this->crumbs_array)) {
  127. $selected = true;
  128. } else {
  129. $selected = false;
  130. }
  131. if ($menu_v["parent"] == $parent && $menu_v["is_show"] == 1) {
  132. $temp_item = array(
  133. 'addon' => $menu_v['addon'],
  134. 'selected' => $selected,
  135. 'url' => addon_url($menu_v['url']),
  136. 'title' => $menu_v['title'],
  137. 'icon' => $menu_v['picture'],
  138. 'icon_selected' => $menu_v['picture_select'],
  139. 'target' => ''
  140. );
  141. $child = $this->initMenu($menus_list, $menu_v["name"]);//获取下级的菜单
  142. $temp_item["child_list"] = $child;
  143. $temp_list[ $menu_v["name"] ] = $temp_item;
  144. }
  145. }
  146. }
  147. return $temp_list;
  148. }
  149. /**
  150. * 获取上级菜单列表
  151. * @param number $menu_id
  152. */
  153. private function getParentMenuList($name = '')
  154. {
  155. if (!empty($name)) {
  156. $menu_model = new Menu();
  157. $menu_info_result = $menu_model->getMenuInfo([ [ 'name', "=", $name ], [ 'app_module', '=', $this->app_module ] ]);
  158. $menu_info = $menu_info_result["data"];
  159. if (!empty($menu_info)) {
  160. $this->getParentMenuList($menu_info['parent']);
  161. $menu_info["url"] = addon_url($menu_info["url"]);
  162. $this->crumbs[] = $menu_info;
  163. $this->crumbs_array[] = $menu_info['name'];
  164. }
  165. }
  166. }
  167. /**
  168. * 获取当前用户的用户组
  169. */
  170. private function getGroupInfo()
  171. {
  172. $group_model = new Group();
  173. $group_info_result = $group_model->getGroupInfo([ [ "group_id", "=", $this->user_info["group_id"] ], [ "site_id", "=", $this->site_id ], [ "app_module", "=", $this->app_module ] ]);
  174. $this->group_info = $group_info_result["data"];
  175. }
  176. /**
  177. * 验证登录
  178. */
  179. private function checkLogin()
  180. {
  181. //验证基础登录
  182. if (!$this->uid) {
  183. $this->redirect(url('admin/login/login'));
  184. }
  185. }
  186. /**
  187. * 检测权限
  188. */
  189. private function checkAuth()
  190. {
  191. $user_model = new UserModel();
  192. $res = $user_model->checkAuth($this->url, $this->app_module, $this->group_info);
  193. return $res;
  194. }
  195. /**
  196. * 获取菜单
  197. */
  198. private function getMenuList()
  199. {
  200. $menu_model = new Menu();
  201. if ($this->group_info['is_system'] == 1) {
  202. $menus = $menu_model->getMenuList([ [ 'app_module', "=", $this->app_module ], [ 'is_show', "=", 1 ] ], '*', 'sort asc');
  203. } else {
  204. $menus = $menu_model->getMenuList([ [ 'name', 'in', $this->group_info['menu_array'] ], [ 'is_show', "=", 1 ], [ 'app_module', "=", $this->app_module ] ], '*', 'sort asc');
  205. }
  206. return $menus['data'];
  207. }
  208. /**
  209. * 获取顶级菜单
  210. */
  211. protected function getTopMenu()
  212. {
  213. $list = array_filter($this->menus, function ($v) {
  214. return $v['parent'] == '0';
  215. });
  216. return $list;
  217. }
  218. /**
  219. * 四级菜单
  220. * @param unknown $params
  221. */
  222. protected function forthMenu($params = [])
  223. {
  224. $url = strtolower($this->url);
  225. $menu_model = new Menu();
  226. $menu_info = $menu_model->getMenuInfo([ [ 'url', "=", $url ], [ 'level', '=', 4 ] ], 'parent');
  227. if (!empty($menu_info['data'])) {
  228. $menus = $menu_model->getMenuList([ [ 'app_module', "=", $this->app_module ], [ 'is_show', "=", 1 ], [ 'parent', '=', $menu_info['data']['parent'] ] ], '*', 'sort asc');
  229. foreach ($menus['data'] as $k => $v) {
  230. $menus['data'][ $k ]['parse_url'] = addon_url($menus['data'][ $k ]['url'], $params);
  231. if ($menus['data'][ $k ]['url'] == $url) {
  232. $menus['data'][ $k ]['selected'] = 1;
  233. } else {
  234. $menus['data'][ $k ]['selected'] = 0;
  235. }
  236. }
  237. $this->assign('forth_menu', $menus['data']);
  238. }
  239. }
  240. /**
  241. * 添加日志
  242. * @param string $action_name
  243. * @param array $data
  244. */
  245. protected function addLog($action_name, $data = [])
  246. {
  247. $user = new User();
  248. $user->addUserLog($this->uid, $this->user_info['username'], $this->site_id, $action_name, $data);
  249. }
  250. }