UploadImage.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace app\expand\controller;
  3. use app\common\service\HelperService;
  4. /**
  5. * 上传图片接口
  6. * Class UploadImage
  7. * @package app\expand\controller
  8. */
  9. class UploadImage extends BaseAuth
  10. {
  11. private $_Account = null;
  12. public function __construct(){
  13. parent::__construct();
  14. $this->_Account = $this->getKey($this->_apiCode);
  15. //验证是否具有访问这个接口的权限
  16. if(!isset($this->_Account['UploadImage'])){
  17. HelperService::returnJson(['code'=>400,'msg'=>'UploadImage interface unauthorized access','data'=>[]]);
  18. }
  19. $this->_addHeader();
  20. }
  21. /**
  22. * 添加跨域安全域名
  23. */
  24. private function _addHeader(){
  25. if(strpos($this->_Account['AllowOrigin'], 'https://') !== false){
  26. header("Access-Control-Allow-Credentials: true");
  27. }
  28. if(isset($this->_Account['AllowOrigin'])){
  29. header('Access-Control-Allow-Origin:*');
  30. header("Access-Control-Allow-Methods", "*");
  31. header("Access-Control-Allow-Headers", "Content-Type,XFILENAME,XFILECATEGORY,XFILESIZE");
  32. }
  33. }
  34. /**
  35. * 上传
  36. */
  37. public function fileUpload(){
  38. $fileStream = $this->request->file('file');
  39. if($fileStream){
  40. $info = $fileStream->move(ROOT_PATH . 'public' . DS . 'Oss'.DS.$this->_apiCode);
  41. if($info){
  42. $url = HelperService::getHttpHeader().$_SERVER['SERVER_NAME'].DS . 'Oss'.DS.$this->_apiCode.DS.$info->getSaveName();
  43. HelperService::returnJson(['code'=>200,'msg'=>"success",'data'=>['url'=>$url]]);
  44. }else{
  45. // 上传失败获取错误信息
  46. HelperService::returnJson(['code'=>400,'msg'=>$fileStream->getError(),'data'=>[]]);
  47. }
  48. }
  49. HelperService::returnJson(['code'=>400,'msg'=>'没有上传图片','data'=>[]]);
  50. }
  51. }