Upload.Class.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * 公用图片上传类
  4. * Created by PhpStorm.
  5. * User: phperstar
  6. * Date: 2019/10/24
  7. * Time: 2:06 PM
  8. */
  9. namespace JinDouYun\Controller\Common;
  10. use Mall\Framework\Factory;
  11. use Mall\Framework\Core\ErrorCode;
  12. use JinDouYun\Controller\BaseController;
  13. use Util\QiNiu\Kodo;
  14. class Upload extends BaseController
  15. {
  16. public function __construct($isCheckAcl = false, $isMustLogin = true, $checkToken = false)
  17. {
  18. parent::__construct($isCheckAcl, $isMustLogin, $checkToken,false,false,false);
  19. }
  20. /**
  21. * 图片base64转图片地址
  22. */
  23. public function base64ToPath()
  24. {
  25. $base64 = $this->request->param('image');
  26. $upload = Factory::baseImg();
  27. if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64, $result)){
  28. if(!empty($result)){
  29. $upload->move($base64);
  30. $this->sendOutput($upload->getSaveName());
  31. }
  32. }else{
  33. $this->sendOutput('请求图片格式错误', ErrorCode::$paramError);
  34. }
  35. }
  36. /**
  37. * 上传文件接口
  38. */
  39. public function uploadFile()
  40. {
  41. parent::getEnterpriseIdByToken();
  42. $upload = Factory::file($_FILES['file']['tmp_name']);
  43. $upload->setUploadInfo($_FILES['file']);
  44. $upload->validate(['ext'=>'pem']);
  45. if(!$upload->move('weixinCert/'.$this->token.'/'.$_FILES['file']['name'])){
  46. parent::sendOutput($upload->getError(), ErrorCode::$notAllowAccess);
  47. }
  48. parent::sendOutput($upload->getSaveName());
  49. }
  50. /**
  51. * 上传图片到七牛对象存储获取上传凭证
  52. */
  53. public function uploadToken()
  54. {
  55. $params = $this->request->getRawJson();
  56. if( empty($params) ){
  57. $this->sendOutput('参数为空', ErrorCode::$paramError );
  58. }
  59. $uploadData = [
  60. 'bucket' => $params['bucket'],
  61. 'key' => $params['key'],
  62. ];
  63. foreach ($uploadData as $key => $value) {
  64. if (empty($value) && $value !== 0) {
  65. parent::sendOutput($key . '参数错误', ErrorCode::$paramError);
  66. }
  67. }
  68. $qiniuConfigData = Factory::config()->get('qiniu');
  69. if(empty($qiniuConfigData)){
  70. $this->sendOutput('七牛配置错误',ErrorCode::$configEroor);
  71. }
  72. $objKodo = new Kodo($qiniuConfigData['accessKey'], $qiniuConfigData['secretKey']);
  73. $result = $objKodo->uploadToken($uploadData['bucket'], $uploadData['key']);
  74. if($result->isSuccess()){
  75. $this->sendOutput($result->getData());
  76. }else{
  77. $this->sendOutput($result->getData(), $result->getErrorCode());
  78. }
  79. }
  80. /**
  81. * 删除上传资源
  82. */
  83. public function delete()
  84. {
  85. $params = $this->request->getRawJson();
  86. if( empty($params) ){
  87. $this->sendOutput('参数为空', ErrorCode::$paramError );
  88. }
  89. $uploadData = [
  90. 'bucket' => $params['bucket'],
  91. 'key' => $params['key'],
  92. ];
  93. foreach ($uploadData as $key => $value) {
  94. if (empty($value) && $value !== 0) {
  95. parent::sendOutput($key . '参数错误', ErrorCode::$paramError);
  96. }
  97. }
  98. $qiniuConfigData = Factory::config()->get('qiniu');
  99. if(empty($qiniuConfigData)){
  100. $this->sendOutput('七牛配置错误',ErrorCode::$configEroor);
  101. }
  102. $objKodo = new Kodo($qiniuConfigData['accessKey'], $qiniuConfigData['secretKey']);
  103. $result = $objKodo->delete($uploadData['bucket'], $uploadData['key']);
  104. if($result->isSuccess()){
  105. $this->sendOutput($result->getData());
  106. }else{
  107. $this->sendOutput($result->getData(), $result->getErrorCode());
  108. }
  109. }
  110. }