Ajax.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace app\api\controller;
  3. use Throwable;
  4. use think\Response;
  5. use app\common\library\Upload;
  6. use app\common\controller\Frontend;
  7. class Ajax extends Frontend
  8. {
  9. protected array $noNeedLogin = ['area', 'buildSuffixSvg'];
  10. public function initialize(): void
  11. {
  12. parent::initialize();
  13. }
  14. public function upload(): void
  15. {
  16. $file = $this->request->file('file');
  17. try {
  18. $upload = new Upload($file);
  19. $attachment = $upload->upload(null, 0, $this->auth->id);
  20. unset($attachment['create_time'], $attachment['quote']);
  21. } catch (Throwable $e) {
  22. $this->error($e->getMessage());
  23. }
  24. $this->success(__('File uploaded successfully'), [
  25. 'file' => $attachment ?? []
  26. ]);
  27. }
  28. /**
  29. * 省份地区数据
  30. * @throws Throwable
  31. */
  32. public function area(): void
  33. {
  34. $this->success('', get_area());
  35. }
  36. public function buildSuffixSvg(): Response
  37. {
  38. $suffix = $this->request->param('suffix', 'file');
  39. $background = $this->request->param('background');
  40. $content = build_suffix_svg((string)$suffix, (string)$background);
  41. return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/svg+xml');
  42. }
  43. }