Ajax.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace app\admin\controller;
  3. use Throwable;
  4. use ba\Terminal;
  5. use think\Response;
  6. use ba\TableManager;
  7. use think\facade\Db;
  8. use think\facade\Cache;
  9. use think\facade\Event;
  10. use app\admin\model\AdminLog;
  11. use app\common\library\Upload;
  12. use app\common\controller\Backend;
  13. class Ajax extends Backend
  14. {
  15. protected array $noNeedPermission = ['*'];
  16. /**
  17. * 无需登录的方法
  18. * terminal 内部自带验权
  19. */
  20. protected array $noNeedLogin = ['terminal'];
  21. public function initialize(): void
  22. {
  23. parent::initialize();
  24. }
  25. public function upload(): void
  26. {
  27. AdminLog::instance()->setTitle(__('upload'));
  28. $file = $this->request->file('file');
  29. try {
  30. $upload = new Upload($file);
  31. $attachment = $upload->upload(null, $this->auth->id);
  32. unset($attachment['create_time'], $attachment['quote']);
  33. } catch (Throwable $e) {
  34. $this->error($e->getMessage());
  35. }
  36. $this->success(__('File uploaded successfully'), [
  37. 'file' => $attachment ?? []
  38. ]);
  39. }
  40. /**
  41. * 获取省市区数据
  42. * @throws Throwable
  43. */
  44. public function area(): void
  45. {
  46. $this->success('', get_area());
  47. }
  48. public function buildSuffixSvg(): Response
  49. {
  50. $suffix = $this->request->param('suffix', 'file');
  51. $background = $this->request->param('background');
  52. $content = build_suffix_svg((string)$suffix, (string)$background);
  53. return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/svg+xml');
  54. }
  55. /**
  56. * 获取已脱敏的数据库连接配置列表
  57. * @throws Throwable
  58. */
  59. public function getDatabaseConnectionList(): void
  60. {
  61. $quickSearch = $this->request->get("quickSearch/s", '');
  62. $connections = config('database.connections');
  63. $desensitization = [];
  64. foreach ($connections as $key => $connection) {
  65. $connection = TableManager::getConnectionConfig($key);
  66. $desensitization[] = [
  67. 'type' => $connection['type'],
  68. 'database' => substr_replace($connection['database'], '****', 1, strlen($connection['database']) > 4 ? 2 : 1),
  69. 'key' => $key,
  70. ];
  71. }
  72. if ($quickSearch) {
  73. $desensitization = array_filter($desensitization, function ($item) use ($quickSearch) {
  74. return preg_match("/$quickSearch/i", $item['key']);
  75. });
  76. $desensitization = array_values($desensitization);
  77. }
  78. $this->success('', [
  79. 'list' => $desensitization,
  80. ]);
  81. }
  82. /**
  83. * 获取表主键字段
  84. * @param ?string $table
  85. * @param ?string $connection
  86. * @throws Throwable
  87. */
  88. public function getTablePk(?string $table = null, ?string $connection = null): void
  89. {
  90. if (!$table) {
  91. $this->error(__('Parameter error'));
  92. }
  93. $table = TableManager::tableName($table, true, $connection);
  94. if (!TableManager::phinxAdapter(false, $connection)->hasTable($table)) {
  95. $this->error(__('Data table does not exist'));
  96. }
  97. $tablePk = Db::connect(TableManager::getConnection($connection))
  98. ->table($table)
  99. ->getPk();
  100. $this->success('', ['pk' => $tablePk]);
  101. }
  102. /**
  103. * 获取数据表列表
  104. * @throws Throwable
  105. */
  106. public function getTableList(): void
  107. {
  108. $quickSearch = $this->request->get("quickSearch/s", '');
  109. $connection = $this->request->request('connection');// 数据库连接配置标识
  110. $samePrefix = $this->request->request('samePrefix/b', true);// 是否仅返回项目数据表(前缀同项目一致的)
  111. $excludeTable = $this->request->request('excludeTable/a', []);// 要排除的数据表数组(表名无需带前缀)
  112. $outTables = [];
  113. $dbConfig = TableManager::getConnectionConfig($connection);
  114. $tables = TableManager::getTableList($connection);
  115. if ($quickSearch) {
  116. $tables = array_filter($tables, function ($comment) use ($quickSearch) {
  117. return preg_match("/$quickSearch/i", $comment);
  118. });
  119. }
  120. $pattern = '/^' . $dbConfig['prefix'] . '/i';
  121. foreach ($tables as $table => $comment) {
  122. if ($samePrefix && !preg_match($pattern, $table)) continue;
  123. $table = preg_replace($pattern, '', $table);
  124. if (!in_array($table, $excludeTable)) {
  125. $outTables[] = [
  126. 'table' => $table,
  127. 'comment' => $comment,
  128. 'connection' => $connection,
  129. 'prefix' => $dbConfig['prefix'],
  130. ];
  131. }
  132. }
  133. $this->success('', [
  134. 'list' => $outTables,
  135. ]);
  136. }
  137. /**
  138. * 获取数据表字段列表
  139. * @throws Throwable
  140. */
  141. public function getTableFieldList(): void
  142. {
  143. $table = $this->request->param('table');
  144. $clean = $this->request->param('clean', true);
  145. $connection = $this->request->request('connection');
  146. if (!$table) {
  147. $this->error(__('Parameter error'));
  148. }
  149. $connection = TableManager::getConnection($connection);
  150. $tablePk = Db::connect($connection)->name($table)->getPk();
  151. $this->success('', [
  152. 'pk' => $tablePk,
  153. 'fieldList' => TableManager::getTableColumns($table, $clean, $connection),
  154. ]);
  155. }
  156. public function changeTerminalConfig(): void
  157. {
  158. AdminLog::instance()->setTitle(__('Change terminal config'));
  159. if (Terminal::changeTerminalConfig()) {
  160. $this->success();
  161. } else {
  162. $this->error(__('Failed to modify the terminal configuration. Please modify the configuration file manually:%s', ['/config/buildadmin.php']));
  163. }
  164. }
  165. public function clearCache(): void
  166. {
  167. AdminLog::instance()->setTitle(__('Clear cache'));
  168. $type = $this->request->post('type');
  169. if ($type == 'tp' || $type == 'all') {
  170. Cache::clear();
  171. } else {
  172. $this->error(__('Parameter error'));
  173. }
  174. Event::trigger('cacheClearAfter', $this->app);
  175. $this->success(__('Cache cleaned~'));
  176. }
  177. /**
  178. * 终端
  179. * @throws Throwable
  180. */
  181. public function terminal(): void
  182. {
  183. (new Terminal())->exec();
  184. }
  185. }