Crud.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021 勾股工作室
  4. * @license https://opensource.org/licenses/GPL-2.0
  5. * @link https://www.gougucms.com
  6. */
  7. declare (strict_types = 1);
  8. namespace app\admin\controller;
  9. use app\admin\BaseController;
  10. use think\facade\Console;
  11. use think\exception\ValidateException;
  12. use think\facade\Db;
  13. use think\facade\View;
  14. class Crud extends BaseController
  15. {
  16. public function index()
  17. {
  18. $prefix = config('database.connections.mysql.prefix');
  19. //查询所有表信息
  20. $table_info = Db::query('SHOW TABLE STATUS');
  21. foreach ($table_info as $k=>&$val) {
  22. $val['a'] = strpos($val['Comment'],'::crud');
  23. if(strpos($val['Comment'],'::crud') == false){
  24. unset($table_info[$k]);
  25. continue;
  26. }
  27. $val['title'] = str_replace($prefix,'',$val['Name']);
  28. $val['crud'] = $this->check($val['title']);
  29. $val['Comment'] = str_replace('::crud','',$val['Comment']);
  30. }
  31. View::assign('prefix',$prefix);
  32. View::assign('table_info',$table_info);
  33. return view();
  34. }
  35. public function check($table,$type='c',$path='admin')
  36. {
  37. !defined('DS') && define('DS', DIRECTORY_SEPARATOR);
  38. if($type == 'c'){
  39. $table = ucfirst(camelize($table));
  40. $filePath = 'app'. DS . $path . DS . 'controller' . DS . $table . '.php';
  41. $realPath = base_path() . $path . DS . 'controller' . DS . $table . '.php';
  42. }
  43. if($type == 'm'){
  44. $table = ucfirst(camelize($table));
  45. $filePath = 'app'. DS . $path . DS . 'model' . DS . $table . '.php';
  46. $realPath = base_path() . $path . DS . 'model' . DS . $table . '.php';
  47. }
  48. if($type == 'v'){
  49. $table = ucfirst(camelize($table)) . 'Validate';
  50. $filePath = 'app'. DS . $path . DS . 'validate' . DS . $table . '.php';
  51. $realPath = base_path() . $path . DS . 'validate' . DS . $table . '.php';
  52. }
  53. if($type == 'l'){
  54. $filePath = 'app'. DS . $path . DS . 'view' . DS . $table . DS . 'datalist.html';
  55. $realPath = base_path() . $path . DS . 'view' . DS . $table . DS . 'datalist.html';
  56. }
  57. if($type == 'a'){
  58. $filePath = 'app'. DS . $path . DS . 'view' . DS . $table . DS . 'add.html';
  59. $realPath = base_path() . $path . DS . 'view' . DS . $table . DS . 'add.html';
  60. }
  61. if($type == 'e'){
  62. $filePath = 'app'. DS . $path . DS . 'view' . DS . $table . DS . 'edit.html';
  63. $realPath = base_path() . $path . DS . 'view' . DS . $table . DS . 'edit.html';
  64. }
  65. if($type == 'r'){
  66. $filePath = 'app'. DS . $path . DS . 'view' . DS . $table . DS . 'read.html';
  67. $realPath = base_path() . $path . DS . 'view' . DS . $table . DS . 'read.html';
  68. }
  69. if (file_exists($realPath)) {
  70. return $filePath;
  71. }
  72. else{
  73. return 0;
  74. }
  75. }
  76. //crud
  77. public function table()
  78. {
  79. $param = get_params();
  80. $prefix = config('database.connections.mysql.prefix');
  81. //查询指定表信息
  82. $table_info = Db::query('SHOW TABLE STATUS LIKE ' . "'" . $param['name'] . "'");
  83. $detail = $table_info[0];
  84. $detail['title'] = str_replace($prefix,'',$detail['Name']);
  85. $detail['Comment'] = str_replace('::crud','',$detail['Comment']);
  86. $table_columns = Db::query("SHOW FULL COLUMNS FROM " . $param['name']);
  87. foreach($table_columns as $k => &$v)
  88. {
  89. $temp_array = explode(":", $v['Comment']);
  90. $v['name'] = $temp_array[0];
  91. }
  92. //var_dump($table_info);exit;
  93. //var_dump($table_columns);exit;
  94. $detail['c'] = $this->check($detail['title'], $type='c');
  95. $detail['m'] = $this->check($detail['title'], $type='m');
  96. $detail['v'] = $this->check($detail['title'], $type='v');
  97. $detail['l'] = $this->check($detail['title'], $type='l');
  98. $detail['a'] = $this->check($detail['title'], $type='a');
  99. $detail['e'] = $this->check($detail['title'], $type='e');
  100. $detail['r'] = $this->check($detail['title'], $type='r');
  101. View::assign('detail', $detail);
  102. View::assign('columns', $table_columns);
  103. return view();
  104. }
  105. //一键crud
  106. public function crud()
  107. {
  108. $uid = get_login_admin('id');
  109. if($uid!=1){
  110. return to_assign(1,'只有系统超级管理员才有权限使用一键crud功能!');
  111. }
  112. $param = get_params();
  113. set_cache('crud_v_'.$param['field'], $param['crud_v']);
  114. set_cache('crud_a_'.$param['field'], $param['crud_a']);
  115. set_cache('crud_e_'.$param['field'], $param['crud_e']);
  116. set_cache('crud_r_'.$param['field'], $param['crud_r']);
  117. set_cache('crud_l_'.$param['field'], $param['crud_l']);
  118. $t = '-t'.$param['field'];
  119. $c = '-c'.$param['field'];
  120. $m = '-m'.$param['name'];
  121. try {
  122. $output = Console::call('crud', [$t,$c,$m]);
  123. //return $output->fetch();
  124. } catch(\Exception $e) {
  125. clear_cache('crud_v_'.$param['field']);
  126. clear_cache('crud_a_'.$param['field']);
  127. clear_cache('crud_e_'.$param['field']);
  128. clear_cache('crud_r_'.$param['field']);
  129. clear_cache('crud_l_'.$param['field']);
  130. return to_assign(1, $e->getMessage());
  131. }
  132. }
  133. //一键生成菜单
  134. public function menu($field='',$name='')
  135. {
  136. $uid = get_login_admin('id');
  137. if($uid!=1){
  138. return to_assign(1,'只有系统超级管理员才有权限使用一键生成菜单功能!');
  139. }
  140. if(empty($field) || empty($name)){
  141. return to_assign(1,'参数错误!');
  142. }
  143. $rule = [
  144. [
  145. 'title' => $name.'管理',
  146. 'name' => $name,
  147. 'src' => '',
  148. 'module' => '',
  149. 'crud' => $field,
  150. 'menu' => '1',
  151. 'icon' => 'bi-folder',
  152. 'son' => [
  153. [
  154. 'title' => $name.'列表',
  155. 'name' => $name.'列表',
  156. 'src' => 'admin/'.$field.'/datalist',
  157. 'module' => '',
  158. 'crud' => $field,
  159. 'menu' => '1',
  160. 'icon' => '',
  161. 'son' => [
  162. [
  163. 'title' => '新建',
  164. 'name' => $name,
  165. 'src' => 'admin/'.$field.'/add',
  166. 'module' => '',
  167. 'crud' => $field,
  168. 'menu' => '2',
  169. 'icon' => '',
  170. 'son' => []
  171. ],
  172. [
  173. 'title' => '编辑',
  174. 'name' => $name,
  175. 'src' => 'admin/'.$field.'/edit',
  176. 'module' => '',
  177. 'crud' => $field,
  178. 'menu' => '2',
  179. 'icon' => '',
  180. 'son' => []
  181. ],
  182. [
  183. 'title' => '查看',
  184. 'name' => $name,
  185. 'src' => 'admin/'.$field.'/read',
  186. 'module' => '',
  187. 'crud' => $field,
  188. 'menu' => '2',
  189. 'icon' => '',
  190. 'son' => []
  191. ],
  192. [
  193. 'title' => '删除',
  194. 'name' => $name,
  195. 'src' => 'admin/'.$field.'/del',
  196. 'module' => '',
  197. 'crud' => $field,
  198. 'menu' => '2',
  199. 'icon' => '',
  200. 'son' => []
  201. ]
  202. ]
  203. ]
  204. ]
  205. ]
  206. ];
  207. //如果安装过该模块,删除原来的菜单信息
  208. Db::name('AdminRule')->where('crud',$field)->delete();
  209. $sort = Db::name('AdminRule')->where('pid',0)->max('sort');
  210. $this->add_rule($rule,0,$sort+1);
  211. //更新超级管理员的权限节点
  212. $rules = Db::name('AdminRule')->column('id');
  213. $admin_rules = implode(',',$rules);
  214. $res = Db::name('AdminGroup')->strict(false)->where('id',1)->update(['rules'=>$admin_rules,'update_time'=>time()]);
  215. if($res!==false){
  216. // 删除后台节点缓存
  217. clear_cache('adminRules');
  218. return to_assign();
  219. }
  220. else{
  221. return to_assign(1,'操作失败');
  222. }
  223. }
  224. //递归插入菜单数据
  225. protected function add_rule($data, $pid=0,$sort=0)
  226. {
  227. foreach($data as $k => $v)
  228. {
  229. $rule=[
  230. 'title' => $v['title'],
  231. 'name' => $v['name'],
  232. 'src' => $v['src'],
  233. 'module' => $v['module'],
  234. 'menu' => $v['menu'],
  235. 'icon' => $v['icon'],
  236. 'crud' => $v['crud'],
  237. 'pid' => $pid,
  238. 'sort' => $sort,
  239. 'create_time' => time()
  240. ];
  241. $new_id = Db::name('AdminRule')->strict(false)->field(true)->insertGetId($rule);
  242. if(!empty($v['son'] && $new_id)){
  243. $this->add_rule($v['son'],$new_id);
  244. }
  245. }
  246. }
  247. }