<?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());
        }
    }
}