Menu.php 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. <?php
  2. namespace Manage\Src;
  3. use Dever;
  4. class Menu
  5. {
  6. /**
  7. * fast
  8. *
  9. * @var array
  10. */
  11. private $fast = array();
  12. private $group = array();
  13. /**
  14. * data
  15. *
  16. * @var array
  17. */
  18. private $data = array();
  19. private $default = array
  20. (
  21. 'value' => -1,
  22. 'name' => '不选择',
  23. );
  24. private $search_default = array
  25. (
  26. 'value' => -1,
  27. 'name' => '菜单',
  28. );
  29. /**
  30. * __construct
  31. *
  32. * @return mixed
  33. */
  34. public function __construct()
  35. {
  36. }
  37. public function getManageMenu()
  38. {
  39. # 联动总数,默认到县区
  40. $level_total = Dever::input('level_total', 3);
  41. # 当前联动级别
  42. $level_num = Dever::input('level_num');
  43. # 一般为id
  44. $level_id = Dever::input('level_id');
  45. # 是否是搜索列表页
  46. $level_search = Dever::input('level_search');
  47. if ($level_search) {
  48. $default = $this->search_default;
  49. if ($level_num == 1) {
  50. $default['name'] = '分组选择';
  51. } elseif ($level_num == 2) {
  52. $default['name'] = '主菜单选择';
  53. } elseif ($level_num == 3) {
  54. $default['name'] = '子菜单选择';
  55. }
  56. } else {
  57. $default = $this->default;
  58. }
  59. if ($level_num == 1) {
  60. $data = Dever::db('manage/menu_group')->getAll();
  61. } elseif ($level_num == 2) {
  62. $where = array('group_id' => $level_id, 'menu_id' => -1);
  63. $data = Dever::db('manage/menu')->getAll($where);
  64. if ($data) {
  65. foreach ($data as $k => $v) {
  66. $child = Dever::db('manage/menu')->total(array('menu_id' => $v['id']));
  67. if (!$child) {
  68. unset($data[$k]);
  69. }
  70. }
  71. }
  72. } elseif ($level_num == 3) {
  73. if ($level_id == -1) {
  74. Dever::alert('error');
  75. }
  76. $where = array('menu_id' => $level_id);
  77. $data = Dever::db('manage/menu')->getAll($where);
  78. }
  79. if (!$data) {
  80. Dever::alert('error');
  81. }
  82. if ($level_search || $level_num > 1) {
  83. array_unshift($data, $default);
  84. }
  85. $result['level_total'] = $level_total;
  86. $result['list'] = $data;
  87. return $result;
  88. }
  89. public function updateKey($id, $name, $data)
  90. {
  91. $key = Dever::param('key', $data);
  92. $menu = Dever::param('menu_id', $data);
  93. if (!$key || $key == 'null' || strstr($key, 'menu_')) {
  94. Dever::config('base')->hook = true;
  95. $prefix = '';
  96. if ($menu > 0) {
  97. $info = Dever::db('manage/menu')->one($menu);
  98. if ($info) {
  99. if (!$info['key']) {
  100. $info['key'] = $info_update['key'] = 'menu_' . $info['id'];
  101. $info_update['where_id'] = $info['id'];
  102. Dever::db('manage/menu')->update($info_update);
  103. }
  104. $prefix .= $info['key'] . '.';
  105. }
  106. }
  107. $update['key'] = $prefix . 'menu_' . $id;
  108. $update['where_id'] = $id;
  109. Dever::db('manage/menu')->update($update);
  110. }
  111. }
  112. public function test_api()
  113. {
  114. $data = Dever::db('manage/menu-in', array('where_id' => '2,1'));
  115. print_r(Dever::sql());die;
  116. }
  117. public function checkKey($id = 0)
  118. {
  119. $id = Dever::input('where_id');
  120. $key = Dever::input('update_key');
  121. if ($key && !$id) {
  122. $info = Dever::db('manage/menu')->key(array('where_key' => $key));
  123. if ($info) {
  124. Dever::alert('该菜单唯一key已被使用,请换一个吧');
  125. }
  126. } elseif ($id > 0) {
  127. $info = Dever::db('manage/menu')->keyId(array('where_key' => $key, 'where_id' => $id));
  128. if ($info) {
  129. Dever::alert('该菜单唯一key已被使用,请换一个吧');
  130. }
  131. }
  132. }
  133. # 菜单无限分类
  134. public function getCate()
  135. {
  136. # 联动总数
  137. $level_total = 1;
  138. # 当前联动级别
  139. $level_num = Dever::input('level_num', 1);
  140. # 一般为id
  141. $level_id = Dever::input('level_id');
  142. # 传入的参数
  143. $level_param = Dever::input('level_param');
  144. if ($level_param) {
  145. parse_str($level_param, $param);
  146. $where['id'] = $param['id'];
  147. }
  148. # 是否是搜索列表页
  149. $level_search = Dever::input('level_search');
  150. if ($level_search) {
  151. $default = $this->search_default;
  152. $default['name'] = $level_num . '级' . $default['name'];
  153. } else {
  154. $default = $this->default;
  155. }
  156. $data = array();
  157. # 三级联动
  158. if ($level_num == 1) {
  159. $where['menu_id'] = -1;
  160. } elseif($level_id > 0) {
  161. $where['menu_id'] = $level_id;
  162. }
  163. if (isset($where['menu_id'])) {
  164. $data = Dever::db('manage/menu')->getAll($where);
  165. }
  166. if ($data || $level_num == 1) {
  167. array_unshift($data, $default);
  168. }
  169. if (!$data) {
  170. Dever::alert('error');
  171. }
  172. $result['level_total'] = $level_total;
  173. $result['list'] = $data;
  174. return $result;
  175. }
  176. /**
  177. * menu 从数据库中得到所有菜单,如果为空,则从项目中读取一次
  178. *
  179. * @return array
  180. */
  181. private function get($auth = '', $state = 2)
  182. {
  183. $ms = Dever::input('ms');
  184. $param = array();
  185. if ($ms) {
  186. $param['option_key'] = $ms;
  187. }
  188. $menu = Dever::db('manage/menu')->main($param);
  189. if ($menu) {
  190. $this->fast = array();
  191. $child = Dever::db('manage/menu')->child();
  192. if ($auth && is_array($auth)) {
  193. //$auth = implode(',', $auth);
  194. }
  195. $console = false;
  196. if (layadmin()) {
  197. $console = true;
  198. }
  199. foreach ($menu as $k => $v) {
  200. if (!$console && $k == 'console') {
  201. unset($menu[$k]);
  202. continue;
  203. }
  204. if ($auth && !in_array($v['id'], $auth)) {
  205. //unset($menu[$k]);
  206. //continue;
  207. }
  208. /*
  209. if ($auth && strpos($auth . '.', $k . '.') === false) {
  210. unset($menu[$k]);
  211. continue;
  212. }
  213. */
  214. $path = Dever::load('manage/project.path', $v['key']);
  215. if ($path && strpos($path, 'http://') === 0) {
  216. unset($menu[$k]);
  217. continue;
  218. }
  219. $menu[$k] = $this->getChild($menu[$k], $child, $v, $auth);
  220. if (!isset($menu[$k]['child'])) {
  221. unset($menu[$k]);
  222. } else {
  223. $this->group[$menu[$k]['group_id']] = $menu[$k]['group_id'];
  224. }
  225. }
  226. } elseif (!$ms && $state == 2) {
  227. # 数据为空,读取一次,后续就得手动读取了
  228. $this->load();
  229. $menu = $this->get($auth, 1);
  230. }
  231. return $menu;
  232. }
  233. public function getChild($menu, $child, $v, $auth, $state = false)
  234. {
  235. if (isset($child[$v['id']])) {
  236. foreach ($child[$v['id']] as $i => $j) {
  237. //if ($state == false && $auth && $i && strpos($auth . '.', $i) === false) {
  238. if ($state == false && $auth && !in_array($j['id'], $auth)) {
  239. //unset($child[$parent][$i]);
  240. //continue;
  241. } else {
  242. if ($j['link'] && $i != 'console.index') {
  243. $j['link'] = Dever::url($j['link'] . '&menu_id=' . $j['id']);
  244. }
  245. $menu['child'][$i] = $j;
  246. $menu['child'][$i] = $this->getChild($menu['child'][$i], $child, $j, $auth, true);
  247. $menu['child'][$i]['func'] = Dever::db('manage/menu_func')->select(array('menu_id' => $menu['child'][$i]['id']));
  248. if ($j['fast'] == 1) {
  249. $this->fast[$i] = $j;
  250. }
  251. }
  252. }
  253. }
  254. return $menu;
  255. }
  256. /**
  257. * 清空所有菜单
  258. *
  259. * @return array
  260. */
  261. public function delete()
  262. {
  263. $data = $menu = Dever::db('manage/menu')->all;
  264. foreach ($data as $k => $v) {
  265. }
  266. Dever::out('yes');
  267. }
  268. /**
  269. * 更新菜单
  270. *
  271. * @return array
  272. */
  273. public function update_api()
  274. {
  275. $this->load();
  276. Dever::out('yes');
  277. }
  278. private function getKey($k, $i, $j)
  279. {
  280. if (is_string($j['menu'])) {
  281. $m = $j['menu'];
  282. } else {
  283. $m = $k;
  284. }
  285. if ($m == $k) {
  286. $key = $k . '.' . $i;
  287. } else {
  288. $key = $m . '.' . $k . '.' . $i;
  289. }
  290. return array($m, $key);
  291. }
  292. /**
  293. * 载入从项目中定义好的菜单
  294. *
  295. * @return array
  296. */
  297. public function load()
  298. {
  299. $project = array();
  300. $project['console'] = array
  301. (
  302. 'name' => '主页',
  303. 'lang' => '主页',
  304. 'path' => 'console',
  305. 'icon' => 'layui-icon-home',
  306. 'order' => 10000,
  307. 'child' => array
  308. (
  309. 'index' => Array
  310. (
  311. 'key' => 'console.index',
  312. 'name' => '控制台',
  313. 'link' => Dever::url('console'),
  314. 'order' => 10,
  315. 'fast' => 2,
  316. )
  317. )
  318. );
  319. $project += Dever::load('manage/project.get');
  320. $database = Dever::load('manage/database');
  321. $top = Dever::load('manage/top');
  322. $auth = Dever::load('manage/auth');
  323. $child = array();
  324. foreach ($project as $k => $v) {
  325. $project[$k]['group_id'] = 1;
  326. if (!isset($v['url'])) {
  327. continue;
  328. }
  329. if (!isset($v['lang'])) {
  330. continue;
  331. }
  332. if (isset($v['group_name'])) {
  333. $group = Dever::db('manage/menu_group')->find(array('name' => $v['group_name']));
  334. if (!$group) {
  335. $project[$k]['group_id'] = Dever::db('manage/menu_group')->insert(array('name' => $v['group_name'], 'reorder' => $v['group_order']));
  336. } else {
  337. $project[$k]['group_id'] = $group['id'];
  338. }
  339. }
  340. if (isset($v['base']) && isset($v['rel'])) {
  341. $v['path'] = DEVER_INCLUDE_PATH . $v['rel'];
  342. }
  343. $project[$k]['name'] = $v['lang'];
  344. $config = $database->config($k, $v['path']);
  345. if ($config) {
  346. foreach ($config as $i => $j) {
  347. //$this->sync('top', 'update', $j, $k, $i, $v, $top);
  348. $this->sync('auth', 'update_action', $j, $k, $i, $v, $auth);
  349. if (!isset($j['menu'])) {
  350. $j['menu'] = true;
  351. }
  352. if (isset($j['manage']) && $j['menu'] != false) {
  353. list($m, $key) = $this->getKey($k, $i, $j);
  354. $project[$m]['child'][$i]['key'] = $key;
  355. $project[$m]['child'][$i]['name'] = $j['lang'];
  356. $project[$m]['child'][$i]['link'] = 'project/database/list?project=' . $k . '&table=' . $i . '&menu=' . $m . '&search_option_state=1';
  357. $project[$m]['child'][$i]['order'] = isset($j['order']) ? $j['order'] : 1;
  358. $project[$m]['child'][$i]['fast'] = isset($j['fast']) ? $j['fast'] : 2;
  359. if (isset($j['parent'])) {
  360. $parent = $j['parent'];
  361. $parent['menu'] = $parent['key'];
  362. list($parent_menu, $parent_key) = $this->getKey($k, $parent['name'], $parent);
  363. $project[$m]['child'][$i]['parent'] = array();
  364. $project[$m]['child'][$i]['parent']['key'] = $parent_key;
  365. $project[$m]['child'][$i]['parent']['name'] = $parent['name'];
  366. $project[$m]['child'][$i]['parent']['link'] = '';
  367. $project[$m]['child'][$i]['parent']['order'] = isset($parent['order']) ? $parent['order'] : 1;
  368. $project[$m]['child'][$i]['parent']['fast'] = isset($parent['fast']) ? $parent['fast'] : 2;
  369. }
  370. }
  371. }
  372. } else {
  373. unset($project[$k]);
  374. }
  375. }
  376. //print_r($project);die;
  377. # 将所有数据都记录到菜单中
  378. if ($project) {
  379. $menu = Dever::config('base', 'manage')->menu;
  380. if ($menu && layadmin()) {
  381. $old = $project;
  382. $project = array();
  383. if (!isset($menu['console']) && isset($old['console'])) {
  384. $project['console'] = $old['console'];
  385. }
  386. foreach ($menu as $k => $v) {
  387. $project[$k] = $v;
  388. $project[$k]['key'] = $v['project'];
  389. $project[$k]['lang'] = $v['name'];
  390. $project[$k]['path'] = $k;
  391. $project[$k]['icon'] = $old[$v['icon']]['icon'];
  392. $child = explode(',', $v['project']);
  393. foreach ($child as $k1 => $v1) {
  394. $project[$k]['child'][$v1] = $old[$v1];
  395. $project[$k]['child'][$v1]['key'] = $v1;
  396. $project[$k]['child'][$v1]['link'] = $old[$v1]['url'];
  397. $project[$k]['child'][$v1]['fast'] = 2;
  398. }
  399. }
  400. if (!isset($project['manage']) && isset($old['manage'])) {
  401. $project['manage'] = $old['manage'];
  402. }
  403. }
  404. foreach ($project as $k => $v) {
  405. if (!isset($v['child'])) {
  406. unset($project[$k]);
  407. continue;
  408. }
  409. if ($v && isset($v['lang']) && $v['child']) {
  410. $id = $this->updateMenu($k, $v['lang'], $v['path'] . ':' . $v['name'], -1, $v['order'], 2, (isset($v['icon']) ? $v['icon'] : ''), $v['group_id']);
  411. if ($id > 0) {
  412. foreach ($v['child'] as $i => $j) {
  413. if (isset($j['child']) && $j['child']) {
  414. $parent = $this->updateMenu($j['key'], $j['name'], $j['link'], $id, $j['order'], $j['fast'], '', $v['group_id']);
  415. foreach ($j['child'] as $i1 => $j1) {
  416. $this->updateMenu($j1['key'], $j1['name'], $j1['link'], $parent, $j1['order'], $j1['fast'], '', $v['group_id']);
  417. }
  418. } elseif (isset($j['parent']) && $j['parent']) {
  419. $p = $j['parent'];
  420. $parent = $this->updateMenu($p['key'], $p['name'], $p['link'], $id, $p['order'], $p['fast'], '', $v['group_id']);
  421. if ($parent) {
  422. $this->updateMenu($j['key'], $j['name'], $j['link'], $parent, $j['order'], $j['fast'], '', $v['group_id']);
  423. }
  424. } else {
  425. $this->updateMenu($j['key'], $j['name'], $j['link'], $id, $j['order'], $j['fast'], '', $v['group_id']);
  426. }
  427. }
  428. }
  429. }
  430. }
  431. }
  432. return $project;
  433. }
  434. private function sync($key, $update, $data, $k, $i, $project, $model)
  435. {
  436. if (isset($data[$key]) && is_array($data[$key])) {
  437. # 加入头部左侧控制权限的菜单
  438. if ($key == 'top') {
  439. $data[$key]['name'] = $project['lang'] . '-' . $data[$key]['name'];
  440. }
  441. if (isset($data[$key]['type']) && $data[$key]['type'] == 2) {
  442. $id = false;
  443. } else {
  444. $id = $model->{$update}($data[$key]);
  445. }
  446. if (strpos($data[$key]['data'], '/')) {
  447. $parent = Dever::load($data[$key]['data']);
  448. } else {
  449. $parent = Dever::load($k . '/' . $i . '-' . $data[$key]['data']);
  450. }
  451. //print_r($data[$key]);die;
  452. if ($parent) {
  453. if (!$id) {
  454. foreach ($parent as $k => $v) {
  455. if (isset($v['child']) && $v['child']) {
  456. $param = array();
  457. $param['project'] = $project['name'];
  458. $param['project_name'] = $project['lang'];
  459. $param['name'] = $v[$data[$key]['col']];
  460. if (isset($data[$key]['type']) && $data[$key]['type'] && isset($v[$data[$key]['type']])) {
  461. $type = $v[$data[$key]['type']];
  462. $param['key'] = $data[$key]['key'] . '-' . $type . '_' . $v['id'];
  463. } else {
  464. $param['key'] = $data[$key]['key'] . '_' . $v['id'];
  465. }
  466. $param[$key] = -1;
  467. $param['value'] = $v['id'];
  468. $pid = $model->{$update}($param);
  469. foreach ($v['child'] as $k1 => $v1) {
  470. $param = array();
  471. $param['project'] = $project['name'];
  472. $param['project_name'] = $project['lang'];
  473. if (isset($data[$key]['type']) && $data[$key]['type'] && isset($v1[$data[$key]['type']])) {
  474. $type = $v1[$data[$key]['type']];
  475. $param['key'] = $data[$key]['key'] . '-' . $type . '_' . $v1['id'];
  476. } else {
  477. $param['key'] = $data[$key]['key'] . '_' . $v1['id'];
  478. }
  479. $param['name'] = $v1[$data[$key]['col']];
  480. $param[$key] = $pid;
  481. $param['value'] = $v1['id'];
  482. $model->{$update}($param);
  483. }
  484. }
  485. }
  486. } elseif ($id) {
  487. foreach ($parent as $v) {
  488. $param = array();
  489. if ($key == 'auth') {
  490. $param['project'] = $project['name'];
  491. $param['project_name'] = $project['lang'];
  492. }
  493. if (isset($data[$key]['type']) && $data[$key]['type'] && isset($v[$data[$key]['type']])) {
  494. $type = $v[$data[$key]['type']];
  495. $param['key'] = $data[$key]['key'] . '-' . $type . '_' . $v['id'];
  496. } else {
  497. if (!isset($v['id'])) {
  498. print_r($parent);die;
  499. }
  500. $param['key'] = $data[$key]['key'] . '_' . $v['id'];
  501. }
  502. $param['name'] = $v[$data[$key]['col']];
  503. $param[$key] = $id;
  504. $param['value'] = $v['id'];
  505. $model->{$update}($param);
  506. }
  507. }
  508. }
  509. }
  510. }
  511. /**
  512. * 更新菜单到数据库
  513. *
  514. * @return array
  515. */
  516. public function updateMenu($key, $name, $link, $menu, $order, $fast, $icon = '', $group_id = 1)
  517. {
  518. if (!$key) {
  519. return;
  520. }
  521. //$where['name'] = $name;
  522. //$where['menu_id'] = $menu;
  523. $where['key'] = $key;
  524. $info = Dever::db('manage/menu')->one($where);
  525. if (!$info) {
  526. $update['add_key'] = $key;
  527. $update['add_name'] = $name;
  528. $update['add_link'] = $link;
  529. $update['add_menu_id'] = $menu;
  530. $update['add_menu'] = $menu;
  531. $update['add_reorder'] = $order;
  532. $update['add_fast'] = $fast;
  533. $update['add_state'] = 1;
  534. $update['add_type'] = 2;
  535. if ($icon) {
  536. $update['add_icon'] = $icon;
  537. }
  538. if ($group_id) {
  539. $update['group_id'] = $group_id;
  540. }
  541. $info['id'] = Dever::db('manage/menu')->insert($update);
  542. }
  543. /*
  544. elseif($link != $info['link'])
  545. {
  546. $update['set_link'] = $link;
  547. $id = $update['where_id'] = $info['id'];
  548. Dever::load('manage/menu-update', $update);
  549. }
  550. */
  551. return $info['id'];
  552. }
  553. private function getGroup()
  554. {
  555. $company_id = Dever::load('manage/company')->get();
  556. $where = array();
  557. if ($company_id) {
  558. $company = Dever::db('manage/company')->find($company_id);
  559. if ($company && $company['is_project'] == 2 && $company['project']) {
  560. $where['ids'] = $company['project'];
  561. }
  562. }
  563. if ($this->group) {
  564. $group = Dever::db('manage/menu_group')->getIds($where);
  565. foreach ($group as $k => $v) {
  566. if (!isset($this->group[$k])) {
  567. unset($group[$k]);
  568. }
  569. }
  570. return $group;
  571. } else {
  572. $this->group = Dever::db('manage/menu_group')->getIds($where);
  573. }
  574. return $this->group;
  575. }
  576. /**
  577. * menu 生成顶部菜单
  578. *
  579. * @return array
  580. */
  581. public function getTop()
  582. {
  583. Dever::load('manage/menu.getHtml');
  584. $data = $this->getGroup();
  585. $html = '<li class="layui-nav-item layadmin-flexible" lay-unselect>
  586. <a href="javascript:;" layadmin-event="flexible" title="侧边伸缩">
  587. <i class="layui-icon layui-icon-shrink-right" id="LAY_app_flexible"></i>
  588. </a>
  589. </li><li class="layui-nav-item" lay-unselect style="display:">
  590. <a href="javascript:;" layadmin-event="back" title="上一页">
  591. <i class="layui-icon layui-icon-return"></i>
  592. </a>
  593. </li><li class="layui-nav-item" lay-unselect>
  594. <a href="javascript:;" layadmin-event="refresh" title="刷新">
  595. <i class="layui-icon layui-icon-refresh-3"></i>
  596. </a>
  597. </li></ul><ul class="layui-nav nav layui-layout-left" style="margin-left: 161px;">';
  598. $cur_group = Dever::input('group');
  599. if (layadmin()) {
  600. $i = 0;
  601. foreach ($data as $k => $v) {
  602. $v['icon'] = $v['icon'] ? "layui-icon " . $v['icon'] : "";
  603. $cur = '';
  604. if ($cur_group) {
  605. if ($v['id'] == $cur_group) {
  606. $cur = 'layui-this';
  607. }
  608. } elseif ($i == 0) {
  609. $cur = 'layui-this';
  610. }
  611. $html .= '<li class="layui-nav-item '.$cur.'">';
  612. $link = 'href="javascript:;" onclick="menuGroup('.$v['id'].', $(this))"';
  613. $html .= '<a '.$link.' lay-tipss="'.$v['name'].'" id="menu_group_'.$v['id'].'">';
  614. $html .= '<i class="'.$v['icon'].'"></i><cite style="margin-left:5px;">'.$v['name'].'</cite></a>';
  615. if ($cur && !$cur_group) {
  616. $html .= '<script>$(document).ready(function(){$("#menu_group_'.$v['id'].'").click()});</script>';
  617. }
  618. $html .= '</li>';
  619. $i++;
  620. }
  621. $html .= '';
  622. }
  623. return $html;
  624. }
  625. /**
  626. * menu 生成左侧的菜单 此处应加上权限来显示是否管理员有该菜单的权限
  627. *
  628. * @return array
  629. */
  630. public function getHtml($state = false)
  631. {
  632. $data = $this->all();
  633. $html = '';
  634. $menu = Dever::input('parent_menu');
  635. if (layadmin()) {
  636. foreach ($data as $k => $v) {
  637. $v['icon'] = $v['icon'] ? "layui-icon " . $v['icon'] : "layui-icon layui-icon-home";
  638. $style = 'style="display:none;"';
  639. $nav = '';
  640. if ($menu && $menu == $v['key']) {
  641. $style = '';
  642. $nav = 'layui-nav-itemed';
  643. }
  644. $html .= '<li data-name="'.$v['key'].'" class="layui-nav-item menu_group menu_group_'.$v['group_id'].' '.$nav.'" '.$style.'>';
  645. if (isset($v['child']) && $v['child']) {
  646. $link = 'href="javascript:;"';
  647. } else {
  648. $link = 'lay-href="'.$v['link'].'"';
  649. }
  650. $html .= '<a '.$link.' lay-tips="'.$v['name'].'" lay-direction="2">';
  651. $html .= '<i class="'.$v['icon'].'"></i><cite>'.$v['name'].'</cite></a>';
  652. $html .= $this->getHtmlChild($v);
  653. $html .= '</li>';
  654. }
  655. $html .= '';
  656. }
  657. return $html;
  658. }
  659. public function getHtmlChild($v)
  660. {
  661. $html = '';
  662. $menu = Dever::input('menu');
  663. if (isset($v['child']) && $v['child']) {
  664. $html .= '<dl class="layui-nav-child">';
  665. foreach ($v['child'] as $k1 => $v1) {
  666. $active = '';
  667. if ($menu && $menu == $v1['key']) {
  668. $active = 'layui-this';
  669. } elseif ($v1['key'] == 'console.index') {
  670. $active = 'layui-this';
  671. }
  672. $html .= '<dd data-name="'.$v1['key'].'" class="'.$active.'">';
  673. if (isset($v1['child']) && $v1['child']) {
  674. $html .= '<a href="javascript:;">'.$v1['name'].'</a><dl class="layui-nav-child">';
  675. foreach ($v1['child'] as $k2 => $v2) {
  676. $html .= '<dd data-name="'.$v2['key'].'"><a lay-href="'.$v2['link'].'">'.$v2['name'].'</a></dd>';
  677. }
  678. $html .= '</dl>';
  679. } else {
  680. $html .= '<a lay-href="'.$v1['link'].'">'.$v1['name'].'</a>';
  681. }
  682. $html .= '</dd>';
  683. }
  684. $html .= '</dl>';
  685. }
  686. return $html;
  687. }
  688. /**
  689. * menu 生成左侧的菜单 此处应加上权限来显示是否管理员有该菜单的权限
  690. *
  691. * @return array
  692. */
  693. public function all($state = false)
  694. {
  695. $data = $this->left($state);
  696. if (!$data) {
  697. Dever::location(Dever::url('home', 'manage'));
  698. }
  699. return $data;
  700. }
  701. /**
  702. * menu 生成左侧的菜单 此处应加上权限来显示是否管理员有该菜单的权限
  703. *
  704. * @return array
  705. */
  706. public function left($state = false)
  707. {
  708. if ($this->data) {
  709. return $this->data;
  710. }
  711. $auth = '';
  712. if ($state == false) {
  713. # 需要验证权限
  714. $auth = Dever::load('manage/auth.admin');
  715. }
  716. //$auth = 'manage_admin';
  717. $menu = $this->get($auth);
  718. $this->data = $menu;
  719. return $menu;
  720. /*
  721. //$auth = 'manage_admin,image_thumb';
  722. foreach($project as $k => $v)
  723. {
  724. if($auth && strpos($auth, $k . '_') === false)
  725. {
  726. unset($project[$k]);
  727. continue;
  728. }
  729. if($auth && strpos($auth, $k . '_' . $i) === false)
  730. {
  731. //unset($project[$k]['child'][$i]);
  732. continue;
  733. }
  734. if(!isset($v['child']))
  735. {
  736. unset($project[$k]);
  737. continue;
  738. }
  739. if($k == 'manage')
  740. {
  741. $project[$k]['child']['manage_project'] = array
  742. (
  743. 'name' => '管理菜单设置',
  744. 'link' => Dever::load('manage/menu.link#project'),
  745. 'order' => 1,
  746. );
  747. $project[$k]['child']['manage_log'] = array
  748. (
  749. 'name' => '管理操作日志',
  750. 'link' => Dever::load('manage/menu.link#log'),
  751. 'order' => 1,
  752. );
  753. //print_r($project[$k]);die;
  754. }
  755. //array_multisort($v['child_order'], SORT_DESC, $v['child']);
  756. $order = array();
  757. foreach($project[$k]['child'] as $i => $j)
  758. {
  759. $order[] = $j['order'];
  760. }
  761. array_multisort($order, SORT_DESC, $project[$k]['child']);
  762. }
  763. //此处加缓存吧,不过要针对用户,加到session中也可以
  764. return $project;
  765. */
  766. }
  767. /**
  768. * menu 获取快捷管理菜单
  769. *
  770. * @return array
  771. */
  772. public function fast()
  773. {
  774. $this->left();
  775. return $this->fast;
  776. }
  777. /**
  778. * menu 对一些固定的菜单,生成其链接
  779. *
  780. * @return array
  781. */
  782. public function link()
  783. {
  784. $link = Dever::input('link');
  785. return array
  786. (
  787. 'project' => Dever::url('project/list?menu=manage&table=manage_project'),
  788. 'log' => Dever::url('log/list?menu=manage&table=manage_log'),
  789. 'logout' => Dever::url('auth.quit'),
  790. 'index' => Dever::url('home'),
  791. 'console' => $link ? urldecode($link) : Dever::url('console'),
  792. );
  793. }
  794. /**
  795. * getLink
  796. *
  797. * @return array
  798. */
  799. public function getLink($param)
  800. {
  801. $extend = '';
  802. if (is_array($param)) {
  803. $key = $param[0];
  804. $type = $param[1];
  805. $extend = isset($param[2]) ? $param[2] : '';
  806. } else {
  807. $key = $param;
  808. $type = 'list';
  809. }
  810. $info = Dever::load('manage/menu-one', array('option_key' => $key));
  811. $link = '';
  812. if ($info) {
  813. $link = $info['link'] . '&menu_id=' . $info['id'] . '&' . $extend;
  814. if ($type != 'list') {
  815. $link = str_replace('project/database/list', 'project/database/' . $type, $link);
  816. }
  817. }
  818. $link = Dever::url($link);
  819. return $link;
  820. }
  821. /**
  822. * database
  823. *
  824. * @return array
  825. */
  826. public function database()
  827. {
  828. $this->config();
  829. $result = array();
  830. $method = $this->project . '/' . $this->table . '-list';
  831. $data = Dever::load($method);
  832. //print_r($data);die;
  833. //print_r(Helper::page("current"));die;
  834. if ($data) {
  835. foreach ($data as $k => $v) {
  836. $result[$k] = '<tr>';
  837. foreach ($this->config['manage']['list'] as $ki => $vi) {
  838. if (isset($v[$vi['col']]) && $v[$vi['col']]) {
  839. if (isset($vi['value'])) {
  840. $eval = '$value = ' . str_replace('{col}', $v[$vi['col']], $vi['value']) . ';';
  841. eval($eval);
  842. if ($value) {
  843. $v[$vi['col']] = $value;
  844. }
  845. } elseif (isset($vi['modal'])) {
  846. $v[$vi['col']] = '<a href="#" data-toggle="modal" data-target="#DEVER_modal" onclick="$(\'#DEVER_modal_body\').html($(this).next().html())">' . $vi['modal'] . '</a><div style="display:none;">' . $v[$vi['col']] . '</div>';
  847. } elseif (isset($vi['option'])) {
  848. $v[$vi['col']] = $vi['option'][$v[$vi['col']]];
  849. }
  850. $result[$k] .= '<td>' . $v[$vi['col']] . '</td>';
  851. } elseif ($vi['col'] == 'manage' && isset($vi['value']) && is_array($vi['value'])) {
  852. $result[$k] .= '<td>';
  853. foreach ($vi['value'] as $kj => $vj) {
  854. $result[$k] .= '<a href="' . $this->url($kj, $v['id']) . '" class="oper_' . $kj . '">' . $vj . '</a>';
  855. }
  856. $result[$k] .= '</td>';
  857. }
  858. }
  859. $result[$k] .= '</tr>';
  860. }
  861. }
  862. return $result;
  863. }
  864. }