<?php
// +----------------------------------------------------------------------
// | 奇乐自媒体内容管理系统 免费版 2019
// +----------------------------------------------------------------------
// | 官方网址：http://www.qilecms.com
// +----------------------------------------------------------------------
// | 官方论坛：http://bbs.qilecms.com
// +----------------------------------------------------------------------
// | Author:奇乐网络
// +----------------------------------------------------------------------
// | 版权说明：本产品为免费版，使用请保留底部版权
// +----------------------------------------------------------------------
namespace app\user\controller;
class Upload extends Base
{
   public function initialize()
    {
       parent::initialize();
       $this->isLogin();
    }
   public function images(){
    if(!$this->request->isPost()){
          return error_json('非法请求');
    }
     //数据安全过滤
    $param = $this->request->param();

    if(!$this->request->file('images')){ return error_json('图片上传接口：请求参数错误');}

    $settings = $this->settings;
    // 获取表单上传文件 例如上传了001.jpg
    $files    = $this->request->file('images');
    // 移动到框架应用根目录/uploads/ 目录下

     $images_path = !empty($settings['attachments']['images_path'])?$settings['attachments']['images_path']:config('app.upload_images.path');  

    $images_ext  = !empty($settings['attachments']['images_ext'])?$settings['attachments']['images_ext']:config('app.upload_images.ext');
    $images_size = !empty($settings['attachments']['images_size'])?$settings['attachments']['images_size']:config('app.upload_images.size');


    foreach($files as $file){
 
          $info = $file->validate(['size'=> $images_size,'ext'=>$images_ext])->move(QL_ROOT.$images_path);
        if($info){
           $images =  $file->getInfo('images');

            // 成功上传后 获取上传信息
            // 输出 jpg
            // echo $info->getExtension();
            // // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            // echo $info->getSaveName();
            // // 输出 42a79759f284b767dfcb2a0197904287.jpg
            // echo $info->getFilename(); 
  

        // $image = \think\Image::open('./image.png');
        // // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.png
        // $image->thumb(150, 150)->save('./thumb.png');

          session('__token__',null);  //清空session token
           return success_json('上传成功','/'.trim($images_path,'/').'/'.str_replace('\\','/',$info->getSaveName()));
        }else{
            // 上传失败获取错误信息
               return error_json($file->getError());
        }

    }


 }

 //上传头像
public function avatar(){
    if(!$this->request->isPost()){
          return error_json('非法请求');
    }
     //数据安全过滤
    $param = $this->request->param();
    // if(empty($param['__token__'])){
    //     return error_json('请求 Token 参数错误');  
    // }
    // if($param['__token__'] != session('__token__')){
    //     return error_json('请勿重复提交，请刷新后操作');
    // }  


    if(!$this->request->file('image')){ return error_json('图片上传接口：请求参数错误');}
    $settings = $this->settings;

 
    // 获取表单上传文件 例如上传了001.jpg
    $file    = $this->request->file('image');
    // 移动到框架应用根目录/upload/ 目录下
     // $images_path = !empty($settings['attachments']['images_path'])?$settings['attachments']['images_path']:config('app.upload_images.path');  

    $images_ext  = !empty($settings['attachments']['images_ext'])?$settings['attachments']['images_ext']:config('app.upload_images.ext');
    $images_size = !empty($settings['attachments']['images_size'])?$settings['attachments']['images_size']:config('app.upload_images.size');
        $name = session('uid').'.png';
        $images_path ='/upload/avatar/';
        $info = $file
        ->rule('uniqid')
        ->validate(['size'=> $images_size,'ext'=>$images_ext])
        ->move(QL_ROOT.$images_path,$name);
        if($info){
            $images =  $file->getInfo('image');

        
                    session('__token__',null);  //清空session token
                   return success_json('上传成功','/'.trim($images_path,'/').'/'.str_replace('\\','/',$info->getSaveName()));
        }else{
             // 上传失败获取错误信息
                       return error_json($file->getError()); 
        }


  



 }

 public function videos(){
    if(!$this->request->isPost()){
          return error_json('非法请求');
    }
     //数据安全过滤
    $param = $this->request->param();
    if(empty($param['__token__'])){
        return error_json('请求 Token 参数错误');  
    }
    if($param['__token__'] != session('__token__')){
        return error_json('请勿重复提交，请刷新后操作');
    }  


   if(!$this->request->file('videos')){ return error_json('视频上传接口：请求参数错误');}
    $settings = $this->settings;
    // 获取表单上传文件 例如上传了001.jpg
    $files = $this->request->file('videos');
    // 移动到框架应用根目录/uploads/ 目录下
    // print_r($files);
    $videos_path = !empty($settings['attachments']['videos_path'])?$settings['attachments']['videos_path']:config('app.upload_videos.path');
    $videos_ext = !empty($settings['attachments']['videos_ext'])?$settings['attachments']['videos_ext']:config('app.upload_videos.ext');
    $videos_size = !empty($settings['attachments']['videos_size'])?$settings['attachments']['videos_size']:config('app.upload_videos.size');

    foreach($files as $file){
          $info = $file->validate(['size'=> $videos_size,'ext'=>$videos_ext])->move(QL_ROOT.$videos_path);
        if($info){
           $videos =  $file->getInfo('videos');
            // 成功上传后 获取上传信息
            // 输出 jpg
            // echo $info->getExtension();
            // // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            // echo $info->getSaveName();
            // // 输出 42a79759f284b767dfcb2a0197904287.jpg
            // echo $info->getFilename(); 
           //添加文件记录
            // $data = ['name'=>$videos['name'],
            // 'save_name'=>$info->getFilename(),
            // 'save_path'=>$info->getSaveName(),
            // 'ext'=>$info->getExtension(),
            // 'type'=>$videos['type'],
            // 'size'=>$videos['size'],
            // 'md5'=>$info->md5(),
            // 'sha1'=>$info->sha1(),
            // 'create_time'=>time()
            // ];
            //  model('file')->addData($data);
            session('__token__',null);  //清空session token
             return success_json('上传成功','/'.trim($videos_path,'/').'/'.str_replace('\\','/',$info->getSaveName()));
        }else{
            // 上传失败获取错误信息
             return error_json($file->getError());
        }

    }


 }

//上传附件
 public function files(){
    if(!$this->request->isPost()){
        return $this->error('非法访问');
    }
     //数据安全过滤
    $header = $this->request->header();
    $param = $this->request->param();
    if(empty($header['token'])){
        return error_json('请求 Token 参数不存在');  
    }

    if($header['token'] != session('__token__')){
        return error_json('请勿重复提交，请刷新后操作');
    }  

    if(!$this->request->file('files')){
        return error_json('文件上传接口：请求参数错误');
    }



    $settings = $this->settings;
    // 获取表单上传文件 例如上传了001.jpg
    $files = $this->request->file('files');
    // 移动到框架应用根目录/uploads/ 目录下

    $files_path = !empty($settings['attachments']['files_path'])?$settings['attachments']['files_path']:config('app.upload_files.path');
    $files_ext = !empty($settings['attachments']['files_ext'])?$settings['attachments']['files_ext']:config('app.upload_files.ext');
    $files_size = !empty($settings['attachments']['files_size'])?$settings['attachments']['files_size']:config('app.upload_videos.size');

    foreach($files as $file){
          $info = $file->validate(['size'=> $files_size,'ext'=>$files_ext])->move(QL_ROOT.$files_path);
        if($info){
           $files =  $file->getInfo('files');
            // 成功上传后 获取上传信息
            // 输出 jpg
            // echo $info->getExtension();
            // // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            // echo $info->getSaveName();
            // // 输出 42a79759f284b767dfcb2a0197904287.jpg
            // echo $info->getFilename(); 
        
              $id  = !empty($param['id'])?intval($param['id']):0;
          
             $path = str_replace("\\",'/',$files_path.$info->getSaveName());
           //添加文件记录
            $data = [
            'module'   => $this->request->module(),
            'oid'      => $id,
            'name'     => $files['name'],
            'save_name'=>$info->getFilename(),
            'save_path'=>$path,
            'ext'   =>$info->getExtension(),
            'type'  =>$files['type'],
            'size'  =>$files['size'],
            'md5'   =>$info->md5(),
            'sha1'  =>$info->sha1(),
            'create_time'=>time()
            ];
           
           
             session('__token__',null);  //清空session token
             return success_json('上传成功',$path);
        }else{
            // 上传失败获取错误信息
             return error_json($file->getError());
        }

    }


 }
}