Pub.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * @Created by PhpStorm
  4. * @author: Kirin
  5. * @day: 2024/11/19
  6. * @time: 12:48
  7. */
  8. namespace app\controller\api;
  9. use app\common\ApiBaseController;
  10. use app\services\system\CityAreaServices;
  11. use app\services\system\attachment\SystemAttachmentServices;
  12. use Psr\SimpleCache\InvalidArgumentException;
  13. use qiniu\services\CacheService;
  14. use qiniu\services\UploadService;
  15. use think\db\exception\DataNotFoundException;
  16. use think\db\exception\DbException;
  17. use think\db\exception\ModelNotFoundException;
  18. class Pub extends ApiBaseController
  19. {
  20. /**
  21. * 获取版本信息
  22. * @return mixed
  23. */
  24. public function version()
  25. {
  26. $version = sys_config('app_version', '');
  27. $apk = sys_config('app_apk', '');
  28. return $this->success(compact('version', 'apk'));
  29. }
  30. /**
  31. * 图片上传
  32. * @param SystemAttachmentServices $services
  33. * @return mixed
  34. * @throws InvalidArgumentException
  35. */
  36. public function upload_image(SystemAttachmentServices $services)
  37. {
  38. $request = $this->request;
  39. $data = $request->postMore([
  40. ['filename', 'file'],
  41. ]);
  42. if (!$data['filename']) return $this->error('参数有误');
  43. if (CacheService::has('start_uploads_' . $request->uid()) && CacheService::get('start_uploads_' . $request->uid()) >= 100) return $this->error('非法操作');
  44. $upload = UploadService::init();
  45. $info = $upload->to('store/comment')->validate()->move($data['filename']);
  46. if ($info === false) {
  47. return $this->error($upload->getError());
  48. }
  49. $res = $upload->getUploadInfo();
  50. $services->attachmentAdd($res['name'], $res['size'], $res['type'], $res['dir'], $res['thumb_path'], 1, (int)sys_config('upload_type', 1), $res['time'], 3);
  51. if (CacheService::has('start_uploads_' . $request->uid()))
  52. $start_uploads = (int)CacheService::get('start_uploads_' . $request->uid());
  53. else
  54. $start_uploads = 0;
  55. $start_uploads++;
  56. CacheService::set('start_uploads_' . $request->uid(), $start_uploads, 86400);
  57. $res['dir'] = path_to_url($res['dir']);
  58. if (strpos($res['dir'], 'http') === false) $res['dir'] = sys_config('site_url') . $res['dir'];
  59. return $this->success('图片上传成功!', ['name' => $res['name'], 'url' => $res['dir']]);
  60. }
  61. /**
  62. * 获取城市
  63. * @param CityAreaServices $services
  64. * @return mixed
  65. * @throws DataNotFoundException
  66. * @throws DbException
  67. * @throws ModelNotFoundException
  68. */
  69. public function city(CityAreaServices $services)
  70. {
  71. $pid = $this->request->get('pid', 0);
  72. $type = $this->request->get('type', 0);
  73. return $this->success($services->getCityTreeList((int)$pid, (int)$type));
  74. }
  75. /**
  76. * 解析(获取导入微信地址)
  77. * @param CityAreaServices $services
  78. * @return mixed
  79. * @throws DataNotFoundException
  80. * @throws DbException
  81. * @throws ModelNotFoundException
  82. */
  83. public function cityList(CityAreaServices $services)
  84. {
  85. $address = $this->request->param('address', '');
  86. if (!$address)
  87. return app('json')->fail('地址不存在');
  88. $city = $services->searchCity(compact('address'));
  89. if (!$city) return app('json')->fail('地址暂未录入,请联系管理员');
  90. $where = [['id', 'in', array_merge([$city['id']], explode('/', trim($city->path, '/')))]];
  91. return app('json')->success($services->getCityList($where, 'id as value,id,name as label,parent_id as pid', ['children']));
  92. }
  93. }