Index.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <?php
  2. namespace app\admin\controller\famysql;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. * 索引管理
  7. */
  8. class Index extends Backend
  9. {
  10. protected $noNeedRight = ['selectpage'];
  11. /**
  12. * 读取索引类型规则
  13. * @return array
  14. */
  15. protected $typeList = ['INDEX' => 'INDEX(普通)', 'UNIQUE' => 'UNIQUE(唯一)', 'FULLTEXT' => 'FULLTEXT(全文)'];
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. if (!config("app_debug")) {
  20. $this->error("数据库管理插件只允许在开发环境下使用");
  21. }
  22. if (!$this->auth->isSuperAdmin()) {
  23. $this->error(__('Access is allowed only to the super management group'));
  24. }
  25. $this->view->assign("indexList", $this->typeList);
  26. }
  27. /**
  28. * 索引首页
  29. */
  30. public function indexs()
  31. {
  32. $name = $this->request->get('name');
  33. $is_admin = (int) $this->request->get('is_admin');
  34. $offset = $this->request->get("offset");
  35. $limit = $this->request->get("limit");
  36. if ($name == NULL) {
  37. $this->error(__('Parameter %s can not be empty', 'name'));
  38. }
  39. if ($this->request->isAjax()) {
  40. $indexs = Db::query("SHOW INDEX FROM {$name}");
  41. $lists = [];
  42. $Key_names = [];
  43. foreach ($indexs as $index) {
  44. array_push($Key_names, $index['Key_name']);
  45. $Key_names = array_unique($Key_names);
  46. }
  47. foreach ($Key_names as $key => $Key_name) {
  48. $lists[$key] = $this->get_indexs($name, $Key_name, $is_admin);
  49. }
  50. $result = array("total" => count($lists), "rows" => array_slice($lists, $offset, $limit));
  51. return json($result);
  52. }
  53. $this->view->assign("name", $name);
  54. $this->view->assign("is_admin", $is_admin);
  55. return $this->view->fetch();
  56. }
  57. /**
  58. * 添加
  59. */
  60. public function index_add()
  61. {
  62. $table = $this->request->get('table');
  63. if ($table == NULL) {
  64. $this->error(__('Parameter %s can not be empty', 'table'));
  65. }
  66. if ($this->request->isPost()) {
  67. $params = $this->request->post("row/a");
  68. if ($params) {
  69. $result = false;
  70. $sql = "CREATE";
  71. Db::startTrans();
  72. try {
  73. if ($params['non_unique'] !== 'INDEX') {
  74. $sql .= " {$params['non_unique']}";
  75. }
  76. $sql .= " INDEX `{$params['name']}` ON `{$table}`";
  77. $column_names = explode(',', $params['column_name']);
  78. $sql .= " (";
  79. foreach ($column_names as $column_name) {
  80. $sql .= "`{$column_name}`,";
  81. }
  82. $sql = rtrim($sql, ',');
  83. $sql .= ")";
  84. $result = Db::execute($sql);
  85. if (Db::getPdo()->inTransaction() == true) {
  86. Db::commit();
  87. }
  88. } catch (\think\exception\PDOException $e) {
  89. Db::rollback();
  90. $this->error($e->getMessage());
  91. } catch (\think\Exception $e) {
  92. Db::rollback();
  93. $this->error($e->getMessage());
  94. }
  95. if ($result !== false) {
  96. $this->success();
  97. } else {
  98. $this->error(__('No rows were inserted'));
  99. }
  100. }
  101. $this->error(__('Parameter %s can not be empty', ''));
  102. }
  103. $this->view->assign("table", $table);
  104. return $this->view->fetch();
  105. }
  106. /**
  107. * 编辑
  108. */
  109. public function index_edit()
  110. {
  111. $table = $this->request->get('table');
  112. if ($table == NULL) {
  113. $this->error(__('Parameter %s can not be empty', 'table'));
  114. }
  115. $name = $this->request->get('name');
  116. if ($name == NULL) {
  117. $this->error(__('Parameter %s can not be empty', 'name'));
  118. }
  119. $row = $this->get_indexs($table, $name, 0);
  120. if ($this->request->isPost()) {
  121. $params = $this->request->post("row/a");
  122. if ($params) {
  123. $result = false;
  124. $sql = "ALTER TABLE `{$table}` DROP INDEX `{$row['name']}`, ADD";
  125. Db::startTrans();
  126. try {
  127. if ($params['non_unique'] !== 'INDEX') {
  128. $sql .= " {$params['non_unique']}";
  129. }
  130. $sql .= " INDEX `{$params['name']}`";
  131. $column_names = explode(',', $params['column_name']);
  132. $sql .= "(";
  133. foreach ($column_names as $column_name) {
  134. $sql .= "`{$column_name}`,";
  135. }
  136. $sql = rtrim($sql, ',');
  137. $sql .= ")";
  138. $result = Db::execute($sql);
  139. if (Db::getPdo()->inTransaction() == true) {
  140. Db::commit();
  141. }
  142. } catch (\think\exception\PDOException $e) {
  143. Db::rollback();
  144. $this->error($e->getMessage());
  145. } catch (\think\Exception $e) {
  146. Db::rollback();
  147. $this->error($e->getMessage());
  148. }
  149. if ($result !== false) {
  150. $this->success();
  151. } else {
  152. $this->error(__('No rows were inserted'));
  153. }
  154. }
  155. $this->error(__('Parameter %s can not be empty', ''));
  156. }
  157. $this->view->assign("row", $row);
  158. $this->view->assign("table", $table);
  159. return $this->view->fetch();
  160. }
  161. /**
  162. * 删除
  163. */
  164. public function index_del()
  165. {
  166. $table = $this->request->param("table");
  167. if ($table == NULL) {
  168. $this->error(__('Parameter %s can not be empty', 'table'));
  169. }
  170. $name = $this->request->param("name");
  171. if ($name == NULL) {
  172. $this->error(__('Parameter %s can not be empty', 'name'));
  173. }
  174. $result = false;
  175. try {
  176. $sql = "ALTER TABLE `{$table}` DROP INDEX `{$name}`;";
  177. $result = Db::execute($sql);
  178. if (Db::getPdo()->inTransaction() == true) {
  179. Db::commit();
  180. }
  181. } catch (\think\exception\PDOException $e) {
  182. Db::rollback();
  183. $this->error($e->getMessage());
  184. } catch (\think\Exception $e) {
  185. Db::rollback();
  186. $this->error($e->getMessage());
  187. }
  188. if ($result !== false) {
  189. $this->success();
  190. } else {
  191. $this->error(__('No rows were deleted'));
  192. }
  193. }
  194. /**
  195. * 查看
  196. * @internal
  197. */
  198. public function index()
  199. {
  200. $this->error('禁止访问');
  201. }
  202. /**
  203. * 添加
  204. * @internal
  205. */
  206. public function add()
  207. {
  208. $this->error('禁止访问');
  209. }
  210. /**
  211. * 编辑
  212. * @param string $ids
  213. * @internal
  214. */
  215. public function edit($ids = null)
  216. {
  217. $this->error('禁止访问');
  218. }
  219. /**
  220. * 删除
  221. * @param string $ids
  222. * @internal
  223. */
  224. public function del($ids = null)
  225. {
  226. $this->error('禁止访问');
  227. }
  228. /**
  229. * 批量更新
  230. * @internal
  231. * @param string $ids
  232. * @return void
  233. */
  234. public function multi($ids = null)
  235. {
  236. $this->error('禁止访问');
  237. }
  238. /**
  239. * 字段列表
  240. * @internal
  241. */
  242. public function selectpage($type = '')
  243. {
  244. //当前页
  245. $page = $this->request->request("pageNumber");
  246. //分页大小
  247. $pagesize = $this->request->request("pageSize");
  248. $q_word = (array) $this->request->request("q_word/a");
  249. $word = $q_word ? $q_word[0] : '';
  250. $custom = (array) $this->request->request("custom/a");
  251. $keyValue = $this->request->request('keyValue');
  252. if (!$keyValue) {
  253. if ($custom && is_array($custom)) {
  254. $table = $custom['table'];
  255. }
  256. $fields = Db::getFields($table);
  257. $lists = [];
  258. foreach ($fields as $field => $fieldInfo) {
  259. if (!in_array($field, ['id'])) {
  260. $lists[] = $field;
  261. }
  262. }
  263. foreach ($lists as $k => $v) {
  264. $lists[$k] = ["column_name" => $v];
  265. }
  266. if (!empty($word)) {
  267. $res_arr = [];
  268. foreach ($lists as $list) {
  269. $res_arr[] = $list['column_name'];
  270. }
  271. $res_arr = array_filter($res_arr, function ($v) use ($word) {
  272. return stripos($v, $word) !== false;
  273. });
  274. $res_arrs = array_values($res_arr);
  275. $lists_arr = [];
  276. foreach ($res_arrs as $res) {
  277. $lists_arr[] = [
  278. 'column_name' => $res,
  279. ];
  280. }
  281. $lists = $lists_arr;
  282. }
  283. } else {
  284. $values = explode(',', $keyValue);
  285. foreach ($values as $key => $value) {
  286. $lists[$key] = ['column_name' => $value];
  287. }
  288. }
  289. $result = array("total" => count($lists), "list" => array_slice($lists, ($page - 1) * $pagesize, $pagesize));
  290. return json($result);
  291. }
  292. private function get_indexs($tableName, $keyName, $is_admin)
  293. {
  294. $indexs = Db::query("SHOW INDEX FROM {$tableName} WHERE Key_name = '{$keyName}'");
  295. $lists = [];
  296. foreach ($indexs as $key => $index) {
  297. if ($index['Key_name'] == 'PRIMARY') {
  298. $unique = 'PRIMARY';
  299. } elseif (!$index['Non_unique']) {
  300. $unique = 'UNIQUE';
  301. } elseif ($index['Index_type'] == 'FULLTEXT') {
  302. $unique = 'FULLTEXT';
  303. } else {
  304. $unique = 'INDEX';
  305. }
  306. $lists[$key]['name'] = $index['Key_name'];
  307. $lists[$key]['column_name'] = $index['Column_name'];
  308. $lists[$key]['non_unique'] = $unique;
  309. }
  310. $result['column_name'] = '';
  311. foreach ($lists as $i => $list) {
  312. $result['name'] = $index['Key_name'];
  313. if (($i + 1) == count($lists)) {
  314. $result['column_name'] .= $list['column_name'];
  315. } else {
  316. $result['column_name'] .= $list['column_name'] . ',';
  317. }
  318. $result['non_unique'] = $unique;
  319. $result['is_admin'] = $is_admin;
  320. }
  321. return $result;
  322. }
  323. }