Menu.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace plugin\curd\library;
  3. use laytp\library\Str;
  4. use laytp\traits\Error;
  5. use plugin\curd\model\curd\Field;
  6. use plugin\curd\model\curd\Table;
  7. use think\facade\Env;
  8. class Menu
  9. {
  10. use Error;
  11. protected
  12. $tableId,//表ID
  13. $tableName,//表名
  14. $parentMenuId//上级菜单ID
  15. ;
  16. public function compatibleHtmlPath($path)
  17. {
  18. if(Env::get("domain.admin")){
  19. if(substr($path, 0,7) === '/admin/'){
  20. $path = '/' . substr($path, 7);
  21. }
  22. }
  23. return $path;
  24. }
  25. public function compatibleApiRoute($route)
  26. {
  27. if(Env::get("domain.adminapi")){
  28. if(substr($route, 0,7) === '/admin.'){
  29. $route = '/' . substr($route, 7);
  30. }
  31. }
  32. return $route;
  33. }
  34. /**
  35. * 创建菜单
  36. * @param $tableId integer 要生成菜单的lt_plugin_devtool_curd_table表ID
  37. * @param $parentMenuId integer 所属菜单ID
  38. * @return bool
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\DbException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. */
  43. public function create($tableId, $parentMenuId)
  44. {
  45. $table = Table::find($tableId);
  46. $menu = new \app\model\admin\Menu();
  47. $midName = strtolower($this->getMidName($table->table));
  48. $hrefMidName = str_replace('.', '/', $midName);
  49. $menuId = $menu->insertGetId([
  50. 'name' => $table->comment,
  51. 'href' => $this->compatibleHtmlPath('/admin/' . $hrefMidName . '/index.html'),
  52. 'rule' => $this->compatibleApiRoute('/admin.' . $midName . '/index'),
  53. 'is_menu' => 1,
  54. 'pid' => $parentMenuId,
  55. 'is_show' => 1,
  56. 'icon' => 'layui-icon layui-icon-share',
  57. 'des' => '',
  58. ]);
  59. $arrMenu = [
  60. [
  61. 'name' => '查看和搜索列表',
  62. 'rule' => $this->compatibleApiRoute('/admin.' . $midName . '/index'),
  63. 'is_menu' => 2,
  64. 'pid' => $menuId,
  65. 'is_show' => 1,
  66. 'icon' => 'layui-icon layui-icon-share',
  67. 'des' => '',
  68. ],
  69. [
  70. 'name' => '查看单条数据详情',
  71. 'rule' => $this->compatibleApiRoute('/admin.' . $midName . '/info'),
  72. 'is_menu' => 2,
  73. 'pid' => $menuId,
  74. 'is_show' => 1,
  75. 'icon' => 'layui-icon layui-icon-share',
  76. 'des' => '',
  77. ],
  78. [
  79. 'name' => '添加',
  80. 'rule' => $this->compatibleApiRoute('/admin.' . $midName . '/add'),
  81. 'is_menu' => 2,
  82. 'pid' => $menuId,
  83. 'is_show' => 1,
  84. 'icon' => 'layui-icon layui-icon-share',
  85. 'des' => '',
  86. ],
  87. [
  88. 'name' => '编辑',
  89. 'rule' => $this->compatibleApiRoute('/admin.' . $midName . '/edit'),
  90. 'is_menu' => 2,
  91. 'pid' => $menuId,
  92. 'is_show' => 1,
  93. 'icon' => 'layui-icon layui-icon-share',
  94. 'des' => '',
  95. ],
  96. [
  97. 'name' => '删除',
  98. 'rule' => $this->compatibleApiRoute('/admin.' . $midName . '/del'),
  99. 'is_menu' => 2,
  100. 'pid' => $menuId,
  101. 'is_show' => 1,
  102. 'icon' => 'layui-icon layui-icon-share',
  103. 'des' => '',
  104. ],
  105. ];
  106. $fields = Field::where('table_id', '=', $tableId)->order(['show_sort' => 'desc', 'id' => 'asc'])->select()->toArray();
  107. foreach($fields as $k=>$v){
  108. if (($v['form_type'] == 'input' && isset($v['addition']['open_table_edit']) && $v['addition']['open_table_edit'] == 1) || $v['form_type'] == 'switch') {
  109. $tempMenu = [
  110. 'name' => '设置' . $v['comment'],
  111. 'rule' => $this->compatibleApiRoute('/admin.' . $midName . '/set' . ucfirst(Str::underlineToCamel($v['field']))),
  112. 'is_menu' => 2,
  113. 'pid' => $menuId,
  114. 'is_show' => 1,
  115. 'icon' => 'layui-icon layui-icon-share',
  116. 'des' => '',
  117. ];
  118. $arrMenu[] = $tempMenu;
  119. }
  120. }
  121. $hasSoftDelete = Field::where('table_id', '=', $tableId)->where('field', '=', 'delete_time')->find();
  122. if ($hasSoftDelete) {
  123. $arrMenu[] =
  124. [
  125. 'name' => '回收站',
  126. 'rule' => $this->compatibleApiRoute('/admin.' . $midName . '/recycle'),
  127. 'is_menu' => 2,
  128. 'pid' => $menuId,
  129. 'is_show' => 1,
  130. 'icon' => 'layui-icon layui-icon-share',
  131. 'des' => '',
  132. ];
  133. $arrMenu[] =
  134. [
  135. 'name' => '还原',
  136. 'rule' => $this->compatibleApiRoute('/admin.' . $midName . '/restore'),
  137. 'is_menu' => 2,
  138. 'pid' => $menuId,
  139. 'is_show' => 1,
  140. 'icon' => 'layui-icon layui-icon-share',
  141. 'des' => '',
  142. ];
  143. $arrMenu[] =
  144. [
  145. 'name' => '真实删除',
  146. 'rule' => $this->compatibleApiRoute('/admin.' . $midName . '/trueDel'),
  147. 'is_menu' => 2,
  148. 'pid' => $menuId,
  149. 'is_show' => 1,
  150. 'icon' => 'layui-icon layui-icon-share',
  151. 'des' => '',
  152. ];
  153. }
  154. $menu->saveAll($arrMenu);
  155. return true;
  156. }
  157. /**
  158. * 根据表名获取中间名
  159. * @param $tableName
  160. * @return string
  161. */
  162. protected function getMidName($tableName)
  163. {
  164. $arrTable = explode('_', $tableName);
  165. $basename = ucfirst($arrTable[count($arrTable) - 1]);
  166. array_shift($arrTable);
  167. array_pop($arrTable);
  168. if (count($arrTable)) {
  169. $strTable = implode('.', $arrTable) . '.' . $basename;
  170. } else {
  171. $strTable = $basename;
  172. }
  173. return (substr($strTable, 0, 6) == 'admin.') ? substr($strTable, 6) : $strTable;
  174. }
  175. }