123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- /**
- * 公用图片上传类
- * Created by PhpStorm.
- * User: phperstar
- * Date: 2019/10/24
- * Time: 2:06 PM
- */
- namespace JinDouYun\Controller\Common;
- use Mall\Framework\Factory;
- use Mall\Framework\Core\ErrorCode;
- use JinDouYun\Controller\BaseController;
- use Util\QiNiu\Kodo;
- class Upload extends BaseController
- {
- public function __construct($isCheckAcl = false, $isMustLogin = true, $checkToken = false)
- {
- parent::__construct($isCheckAcl, $isMustLogin, $checkToken,false,false,false);
- }
- /**
- * 图片base64转图片地址
- */
- public function base64ToPath()
- {
- $base64 = $this->request->param('image');
- $upload = Factory::baseImg();
- if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64, $result)){
- if(!empty($result)){
- $upload->move($base64);
- $this->sendOutput($upload->getSaveName());
- }
- }else{
- $this->sendOutput('请求图片格式错误', ErrorCode::$paramError);
- }
- }
- /**
- * 上传文件接口
- */
- public function uploadFile()
- {
- parent::getEnterpriseIdByToken();
- $upload = Factory::file($_FILES['file']['tmp_name']);
- $upload->setUploadInfo($_FILES['file']);
- $upload->validate(['ext'=>'pem']);
- if(!$upload->move('weixinCert/'.$this->token.'/'.$_FILES['file']['name'])){
- parent::sendOutput($upload->getError(), ErrorCode::$notAllowAccess);
- }
- parent::sendOutput($upload->getSaveName());
- }
- /**
- * 上传图片到七牛对象存储获取上传凭证
- */
- public function uploadToken()
- {
- $params = $this->request->getRawJson();
- if( empty($params) ){
- $this->sendOutput('参数为空', ErrorCode::$paramError );
- }
- $uploadData = [
- 'bucket' => $params['bucket'],
- 'key' => $params['key'],
- ];
- foreach ($uploadData as $key => $value) {
- if (empty($value) && $value !== 0) {
- parent::sendOutput($key . '参数错误', ErrorCode::$paramError);
- }
- }
- $qiniuConfigData = Factory::config()->get('qiniu');
- if(empty($qiniuConfigData)){
- $this->sendOutput('七牛配置错误',ErrorCode::$configEroor);
- }
- $objKodo = new Kodo($qiniuConfigData['accessKey'], $qiniuConfigData['secretKey']);
- $result = $objKodo->uploadToken($uploadData['bucket'], $uploadData['key']);
- if($result->isSuccess()){
- $this->sendOutput($result->getData());
- }else{
- $this->sendOutput($result->getData(), $result->getErrorCode());
- }
- }
- /**
- * 删除上传资源
- */
- public function delete()
- {
- $params = $this->request->getRawJson();
- if( empty($params) ){
- $this->sendOutput('参数为空', ErrorCode::$paramError );
- }
- $uploadData = [
- 'bucket' => $params['bucket'],
- 'key' => $params['key'],
- ];
- foreach ($uploadData as $key => $value) {
- if (empty($value) && $value !== 0) {
- parent::sendOutput($key . '参数错误', ErrorCode::$paramError);
- }
- }
- $qiniuConfigData = Factory::config()->get('qiniu');
- if(empty($qiniuConfigData)){
- $this->sendOutput('七牛配置错误',ErrorCode::$configEroor);
- }
- $objKodo = new Kodo($qiniuConfigData['accessKey'], $qiniuConfigData['secretKey']);
- $result = $objKodo->delete($uploadData['bucket'], $uploadData['key']);
- if($result->isSuccess()){
- $this->sendOutput($result->getData());
- }else{
- $this->sendOutput($result->getData(), $result->getErrorCode());
- }
- }
- }
|