Common.php 3.9 KB
<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Area;
use app\common\model\Version;
use fast\Random;
use think\Config;
use Qiniu\Storage\UploadManager;
use Qiniu\Auth;

/**
 * 公共接口
 */
class Common extends Api
{
    protected $noNeedLogin = ['init'];
    protected $noNeedRight = '*';

    /**
     * 加载初始化
     *
     * @param string $version 版本号
     * @param string $lng     经度
     * @param string $lat     纬度
     */
    public function init()
    {
        if ($version = $this->request->request('version')) {
            $lng = $this->request->request('lng');
            $lat = $this->request->request('lat');
            $content = [
                'citydata'    => Area::getCityFromLngLat($lng, $lat),
                'versiondata' => Version::check($version),
                'uploaddata'  => Config::get('upload'),
                'coverdata'   => Config::get("cover"),
            ];
            $this->success('', $content);
        } else {
            $this->error(__('Invalid parameters'));
        }
    }

    /**
     * @ApiTitle    (上传文件)
     * @ApiSummary  (上传文件)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/common/uploadFile)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="image[]", type="file", required=false, description="图片")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1553839125",
    "data": {

    }
    })
     */
    public function uploadFile(){
        $files = request()->file('image');
        if (empty($files)) {
            $this->error('未检出文件上传');
        }
        $countFile = count($files);
        if($countFile > 5) {
            $this->error('最多上传5张图片');
        }
        $url2 = '';
        $host = "http://q3fjhcx63.bkt.clouddn.com";  //七牛云地址
        foreach ($files as $file){
            //移动到框架应用根目录/public/uploads/ 目录下
            $moveUrl = ROOT_PATH . 'public' . DS . 'uploads';
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                //上传七牛云逻辑
                $url = str_replace('//', '/', str_replace('\\', '/', $info->getSaveName())); //20190602/1214564654.jpg目录

                $filePath = $moveUrl.DS.$url;//本地磁盘路径
                //上传至七牛云文件路径
                $qiniu_file = 'uploads/'.$url;
                $upManager = new UploadManager();
                $config = get_addon_config('qiniu');
                // 构建鉴权对象
                $auth = new Auth($config['app_key'], $config['secret_key']);
                // 生成上传 Token
                $token = $auth->uploadToken($config['bucket']);
                // 调用 UploadManager 的 putFile 方法进行文件的上传。
                $qi_res = $upManager->putFile($token,$qiniu_file , $filePath);
                if($qi_res){
                    $a = $host.'/'.$qiniu_file;
                    $sys = $this->getOperateSys();
                    //删除本地服务器图片逻辑
                    if($sys == 'Linux'){
                        unlink($filePath);//适用于linux
                    }
                    $url2 .= $a.',';
//                    $fan .= '/'.$qiniu_file.',';
                }else{
                    $this->error('上传七牛云出错!');
                }
            }else{
                $this->error($files->getError());
            }
        }
        $this->success('SUCCESS',rtrim($url2,','));
    }

    //判断当前操作系统
    public function getOperateSys(){
        $os_name = php_uname('s');
        //判断
        if(strpos($os_name,"Linux")!==false){
            $os_str="Linux";
        }else if(strpos($os_name,"Windows")!==false){
            $os_str="Windows";
        }else{
            $os_str='';
        }
        return $os_str;
    }
}