Ajax.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace app\admin\controller;
  3. use Throwable;
  4. use ba\Terminal;
  5. use think\Response;
  6. use think\facade\Db;
  7. use think\facade\Cache;
  8. use think\facade\Event;
  9. use app\admin\model\AdminLog;
  10. use app\common\library\Upload;
  11. use app\common\controller\Backend;
  12. class Ajax extends Backend
  13. {
  14. protected array $noNeedPermission = ['*'];
  15. /**
  16. * 无需登录的方法
  17. * terminal 内部自带验权
  18. */
  19. protected array $noNeedLogin = ['terminal'];
  20. public function initialize(): void
  21. {
  22. parent::initialize();
  23. }
  24. public function upload(): void
  25. {
  26. AdminLog::setTitle(__('upload'));
  27. $file = $this->request->file('file');
  28. try {
  29. $upload = new Upload($file);
  30. $attachment = $upload->upload(null, $this->auth->id);
  31. unset($attachment['create_time'], $attachment['quote']);
  32. } catch (Throwable $e) {
  33. $this->error($e->getMessage());
  34. }
  35. $this->success(__('File uploaded successfully'), [
  36. 'file' => $attachment ?? []
  37. ]);
  38. }
  39. //保存电子签名
  40. public function saveSign(): void
  41. {
  42. if ($this->request->isPost()) {
  43. $data = $this->request->post();
  44. if (!$data) {
  45. $this->error(__('Parameter %s can not be empty', ['']));
  46. }
  47. $id = $this->auth->id;
  48. $result = false;
  49. if (isset($data['sign']) && $data['sign']) {
  50. $result = Db::name('sign')->where('uid',$id)->find();
  51. if($result){
  52. $result['sign']= $data['sign'];
  53. if (Db::name('sign')->update($result)) {
  54. $this->success(__('Avatar modified successfully!'));
  55. }
  56. }else{
  57. $result = Db::name('sign')->insert(['uid'=> $id , 'sign'=> $data['sign']]);
  58. if ($result) {
  59. $this->success(__('Avatar modified successfully!'));
  60. }
  61. }
  62. }
  63. if ($result == false){
  64. $this->error('更新失败!');
  65. }
  66. }
  67. }
  68. //保存电子签名
  69. public function getSign(): void
  70. {
  71. $id = $this->auth->id;
  72. $result = Db::query('SELECT * FROM yx_borrow WHERE user_id = ? ORDER BY id DESC LIMIT 1', [$id]);
  73. $this->success('签名获取成功!', [
  74. 'sign' => $result ?? []
  75. ]);
  76. }
  77. /**
  78. * 获取省市区数据
  79. * @throws Throwable
  80. */
  81. public function area(): void
  82. {
  83. $this->success('', get_area());
  84. }
  85. public function buildSuffixSvg(): Response
  86. {
  87. $suffix = $this->request->param('suffix', 'file');
  88. $background = $this->request->param('background');
  89. $content = build_suffix_svg((string)$suffix, (string)$background);
  90. return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/svg+xml');
  91. }
  92. /**
  93. * 获取表主键字段
  94. * @param ?string $table
  95. * @throws Throwable
  96. */
  97. public function getTablePk(?string $table = null): void
  98. {
  99. if (!$table) {
  100. $this->error(__('Parameter error'));
  101. }
  102. $tablePk = Db::query("SHOW TABLE STATUS LIKE '$table'", [], true);
  103. if (!$tablePk) {
  104. $table = config('database.connections.mysql.prefix') . $table;
  105. $tablePk = Db::query("SHOW TABLE STATUS LIKE '$table'", [], true);
  106. if (!$tablePk) {
  107. $this->error(__('Data table does not exist'));
  108. }
  109. }
  110. $tablePk = Db::table($table)->getPk();
  111. $this->success('', ['pk' => $tablePk]);
  112. }
  113. public function getTableFieldList(): void
  114. {
  115. $table = $this->request->param('table');
  116. $clean = $this->request->param('clean', true);
  117. if (!$table) {
  118. $this->error(__('Parameter error'));
  119. }
  120. $tablePk = Db::name($table)->getPk();
  121. $this->success('', [
  122. 'pk' => $tablePk,
  123. 'fieldList' => get_table_fields($table, $clean),
  124. ]);
  125. }
  126. public function changeTerminalConfig(): void
  127. {
  128. AdminLog::setTitle(__('changeTerminalConfig'));
  129. if (Terminal::changeTerminalConfig()) {
  130. $this->success();
  131. } else {
  132. $this->error(__('Failed to modify the terminal configuration. Please modify the configuration file manually:%s', ['/config/buildadmin.php']));
  133. }
  134. }
  135. public function clearCache(): void
  136. {
  137. $type = $this->request->post('type');
  138. if ($type == 'tp' || $type == 'all') {
  139. Cache::clear();
  140. } else {
  141. $this->error(__('Parameter error'));
  142. }
  143. Event::trigger('cacheClearAfter', $this->app);
  144. $this->success(__('Cache cleaned~'));
  145. }
  146. /**
  147. * 终端
  148. * @throws Throwable
  149. */
  150. public function terminal(): void
  151. {
  152. Terminal::instance()->exec();
  153. }
  154. }