Ajax.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 = false;
  73. $result = Db::name('sign')->where('uid',$id)->find();
  74. if (!$result) {
  75. $this->error(__('Parameter %s can not be empty', ['']));
  76. }
  77. if (isset($result['sign']) && $result['sign']) {
  78. $this->success('签名获取成功!', [
  79. 'file' => $result ?? []
  80. ]);
  81. }else{
  82. $this->error('更新失败!');
  83. }
  84. }
  85. /**
  86. * 获取省市区数据
  87. * @throws Throwable
  88. */
  89. public function area(): void
  90. {
  91. $this->success('', get_area());
  92. }
  93. public function buildSuffixSvg(): Response
  94. {
  95. $suffix = $this->request->param('suffix', 'file');
  96. $background = $this->request->param('background');
  97. $content = build_suffix_svg((string)$suffix, (string)$background);
  98. return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/svg+xml');
  99. }
  100. /**
  101. * 获取表主键字段
  102. * @param ?string $table
  103. * @throws Throwable
  104. */
  105. public function getTablePk(?string $table = null): void
  106. {
  107. if (!$table) {
  108. $this->error(__('Parameter error'));
  109. }
  110. $tablePk = Db::query("SHOW TABLE STATUS LIKE '$table'", [], true);
  111. if (!$tablePk) {
  112. $table = config('database.connections.mysql.prefix') . $table;
  113. $tablePk = Db::query("SHOW TABLE STATUS LIKE '$table'", [], true);
  114. if (!$tablePk) {
  115. $this->error(__('Data table does not exist'));
  116. }
  117. }
  118. $tablePk = Db::table($table)->getPk();
  119. $this->success('', ['pk' => $tablePk]);
  120. }
  121. public function getTableFieldList(): void
  122. {
  123. $table = $this->request->param('table');
  124. $clean = $this->request->param('clean', true);
  125. if (!$table) {
  126. $this->error(__('Parameter error'));
  127. }
  128. $tablePk = Db::name($table)->getPk();
  129. $this->success('', [
  130. 'pk' => $tablePk,
  131. 'fieldList' => get_table_fields($table, $clean),
  132. ]);
  133. }
  134. public function changeTerminalConfig(): void
  135. {
  136. AdminLog::setTitle(__('changeTerminalConfig'));
  137. if (Terminal::changeTerminalConfig()) {
  138. $this->success();
  139. } else {
  140. $this->error(__('Failed to modify the terminal configuration. Please modify the configuration file manually:%s', ['/config/buildadmin.php']));
  141. }
  142. }
  143. public function clearCache(): void
  144. {
  145. $type = $this->request->post('type');
  146. if ($type == 'tp' || $type == 'all') {
  147. Cache::clear();
  148. } else {
  149. $this->error(__('Parameter error'));
  150. }
  151. Event::trigger('cacheClearAfter', $this->app);
  152. $this->success(__('Cache cleaned~'));
  153. }
  154. /**
  155. * 终端
  156. * @throws Throwable
  157. */
  158. public function terminal(): void
  159. {
  160. Terminal::instance()->exec();
  161. }
  162. }