Ajax.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace app\admin\controller;
  3. use ba\Terminal;
  4. use think\Exception;
  5. use think\facade\Db;
  6. use think\facade\Cache;
  7. use think\facade\Event;
  8. use app\admin\model\AdminLog;
  9. use app\common\library\Upload;
  10. use think\exception\FileException;
  11. use app\common\controller\Backend;
  12. class Ajax extends Backend
  13. {
  14. protected $noNeedPermission = ['*'];
  15. public function initialize()
  16. {
  17. parent::initialize();
  18. }
  19. public function upload()
  20. {
  21. AdminLog::setTitle(__('upload'));
  22. $file = $this->request->file('file');
  23. try {
  24. $upload = new Upload($file);
  25. $attachment = $upload->upload(null, $this->auth->id);
  26. unset($attachment['createtime'], $attachment['quote']);
  27. } catch (Exception|FileException $e) {
  28. $this->error($e->getMessage());
  29. }
  30. $this->success(__('File uploaded successfully'), [
  31. 'file' => $attachment ?? []
  32. ]);
  33. }
  34. public function area()
  35. {
  36. $this->success('', get_area());
  37. }
  38. public function buildSuffixSvg()
  39. {
  40. $suffix = $this->request->param('suffix', 'file');
  41. $background = $this->request->param('background');
  42. $content = build_suffix_svg((string)$suffix, (string)$background);
  43. return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/svg+xml');
  44. }
  45. public function getTablePk($table = null)
  46. {
  47. if (!$table) {
  48. $this->error(__('Parameter error'));
  49. }
  50. $tablePk = Db::query("SHOW TABLE STATUS LIKE '{$table}'", [], true);
  51. if (!$tablePk) {
  52. $table = config('database.connections.mysql.prefix') . $table;
  53. $tablePk = Db::query("SHOW TABLE STATUS LIKE '{$table}'", [], true);
  54. if (!$tablePk) {
  55. $this->error(__('Data table does not exist'));
  56. }
  57. }
  58. $tablePk = Db::table($table)->getPk();
  59. $this->success('', ['pk' => $tablePk]);
  60. }
  61. public function getTableFieldList()
  62. {
  63. $table = $this->request->param('table');
  64. $clean = $this->request->param('clean', true);
  65. if (!$table) {
  66. $this->error(__('Parameter error'));
  67. }
  68. $tablePk = Db::name($table)->getPk();
  69. $this->success('', [
  70. 'pk' => $tablePk,
  71. 'fieldList' => get_table_fields($table, $clean),
  72. ]);
  73. }
  74. public function changeTerminalConfig()
  75. {
  76. AdminLog::setTitle(__('changeTerminalConfig'));
  77. if (Terminal::changeTerminalConfig()) {
  78. $this->success();
  79. } else {
  80. $this->error(__('Failed to modify the terminal configuration. Please modify the configuration file manually:%s', ['/config/buildadmin.php']));
  81. }
  82. }
  83. public function clearCache()
  84. {
  85. $type = $this->request->post('type');
  86. if ($type == 'tp' || $type == 'all') {
  87. Cache::clear();
  88. } else {
  89. $this->error(__('Parameter error'));
  90. }
  91. Event::trigger('cacheClearAfter', $this->app);
  92. $this->success(__('Cache cleaned~'));
  93. }
  94. }