Ajax.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace app\api\controller;
  3. use think\Exception;
  4. use think\exception\FileException;
  5. use app\common\library\Upload;
  6. use app\common\controller\Frontend;
  7. class Ajax extends Frontend
  8. {
  9. protected $noNeedLogin = ['area', 'buildSuffixSvg'];
  10. public function initialize()
  11. {
  12. parent::initialize();
  13. }
  14. public function upload()
  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['createtime'], $attachment['quote']);
  21. } catch (Exception|FileException $e) {
  22. $this->error($e->getMessage());
  23. }
  24. $this->success(__('File uploaded successfully'), [
  25. 'file' => $attachment ?? []
  26. ]);
  27. }
  28. public function area()
  29. {
  30. $this->success('', get_area());
  31. }
  32. public function buildSuffixSvg()
  33. {
  34. $suffix = $this->request->param('suffix', 'file');
  35. $background = $this->request->param('background');
  36. $content = build_suffix_svg((string)$suffix, (string)$background);
  37. return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/svg+xml');
  38. }
  39. }