Ajax.php 6.7 KB

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