Ajax.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. protected array $noNeedPermission = ['upload'];
  11. public function initialize(): void
  12. {
  13. parent::initialize();
  14. }
  15. public function upload(): void
  16. {
  17. $file = $this->request->file('file');
  18. $driver = $this->request->param('driver', 'local');
  19. $topic = $this->request->param('topic', 'default');
  20. try {
  21. $upload = new Upload();
  22. $attachment = $upload
  23. ->setFile($file)
  24. ->setDriver($driver)
  25. ->setTopic($topic)
  26. ->upload(null, 0, $this->auth->id);
  27. unset($attachment['create_time'], $attachment['quote']);
  28. } catch (Throwable $e) {
  29. $this->error($e->getMessage());
  30. }
  31. $this->success(__('File uploaded successfully'), [
  32. 'file' => $attachment ?? []
  33. ]);
  34. }
  35. /**
  36. * 省份地区数据
  37. * @throws Throwable
  38. */
  39. public function area(): void
  40. {
  41. $this->success('', get_area());
  42. }
  43. public function buildSuffixSvg(): Response
  44. {
  45. $suffix = $this->request->param('suffix', 'file');
  46. $background = $this->request->param('background');
  47. $content = build_suffix_svg((string)$suffix, (string)$background);
  48. return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/svg+xml');
  49. }
  50. }