<?php
/**
 *  Guojiz网址导航系统
 *	www.guojiz.com
 *  作者：梦雨
 *  @ QQ50361804
 */
namespace app\admin\model;
use Think\Loader;
use think\Model;
use think\Db;
use think\File;
use think\Request;
use org\Http;
use org\Upload;
use think\Cache;
class Up extends Model
{
	
	function initialize()
	{
        parent::initialize();
	}
    public function Pic()
    {
        if (!session('userid') || !session('username')) {
            $this->error('亲！请登录',url('index/login/index'));
        } else {
        	if (config('web.qiniuopen') == 1) {
        		//开启七牛
            $driverConfig = array('secrectKey' => config('web.secrectKey'), 'accessKey' => config('web.accessKey'), 'domain' => config('web.domain'), 'bucket' => config('web.bucket'));
            $setting['exts'] = explode(',','jpg,png,gif');
            $setting['maxSize'] = 50 * 1024 * 1024;
            $Upload = new uploads($setting, 'Qiniu', $driverConfig);
            $info = $Upload->uploadOne($_FILES['FileName']);
            if ($info) {
              echo $info['url'];
            }
        	} else {
        		//关闭七牛
        	$file = request()->file('FileName');
			$info = $file->validate(['size'=>50000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . DS . 'uploads');
			if ($info) {
				$path = WEB_URL . DS . 'uploads' . DS . $info->getSaveName();
				echo str_replace("\\", "/", $path);
              }
              
        	}
		}
    }
	public function upfile($type,$filename = 'file',$is_water = false){
      
      if (config('web.qiniuopen') == 1) {
            $driverConfig = array('secrectKey' => config('web.secrectKey'), 'accessKey' => config('web.accessKey'), 'domain' => config('web.domain'), 'bucket' => config('web.bucket'));
            $setting = array('rootPath' => './', 'saveName' => array('uniqid', ''), 'hash' => true);
            $setting['exts'] = explode(',', config('web.WEB_RXT'));
            $setting['maxSize'] = 50 * 1024 * 1024;
            $File = $_FILES['file'];
            $Upload = new Upload($setting, 'Qiniu', $driverConfig);
            $info = $Upload->uploadOne($File);
            if ($info) {
                $data['sha1'] = $info['sha1'];
                $data['md5'] = $info['md5'];
                $data['create_time'] = time();
                $data['uid'] = session('userid');
                $data['download'] = 0;
                $data['size'] = $info['size'];
                $data['name'] = $info['name'];
                $data['ext'] = $info['ext'];
                $data['savepath'] = $info['url'];
                $data['savename'] = $info['savename'];
                $data['mime'] = $info['type'];
                $map['md5'] = $info['md5'];
                $mmn = Db::name('file')->where($map)->find();
                if (empty($mmn)) {
                    Db::name('file')->insert($data);
                    $res = Db::name('file')->getLastInsID();
                    if ($res > 0) {
                        return array('code' => 200, 'msg' => '上传成功', 'hasscore' => 0, 'ext' => $data['ext'], 'size' => $data['size'],'id' => $res, 'headpath' => $data['savepath'],'path' => $data['savepath'], 'md5' => $data['md5'], 'savename' => $data['savename'], 'filename' => $data['name'], 'info' => $info);
                    } else {
                        return array('code' => 0, 'msg' => '上传失败');
                    }
                } else {
                    return array('code' => 200, 'msg' => '上传成功', 'hasscore' => 1, 'ext' => $mmn['ext'], 'size' => $data['size'],'id' => $mmn['id'], 'headpath' => $data['savepath'],'path' => $mmn['savepath'], 'md5' => $mmn['md5'], 'savename' => $mmn['savename'], 'filename' => $mmn['name'], 'info' => $mmn);
                }
            } else {
                return array('code' => 0, 'msg' => $Upload->getError());
            }
     } else {
		$file = request()->file($filename);
        $md5 = $file->hash('md5');
            $n = Db::name('file')->where('md5', $md5)->find();
            if (empty($n)) {
                $info = $file->validate(['size' => 50 * 1024 * 1024, 'ext' => config('web.WEB_RXT')])->move(ROOT_PATH . DS . 'uploads');
                if ($info) {
                    $path = DS . 'uploads' . DS . $info->getSaveName();
                    $path = str_replace("\\", "/", $path);
                    $realpath = WEB_URL . $path;
                    $data['sha1'] = $info->sha1();
                    $data['md5'] = $info->md5();
                    $data['create_time'] = time();
                    $data['uid'] = session('userid');
                    $data['download'] = 0;
                    $data['size'] = $info->getSize();
                    $fileinfo = $info->getInfo();
                    $data['name'] = $fileinfo['name'];
                    $data['ext'] = $info->getExtension();
                    $data['savepath'] = $path;
                    $data['savename'] = $info->getFilename();
                    $data['mime'] = $fileinfo['type'];
                    Db::name('file')->insert($data);
                    $res = Db::name('file')->getLastInsID();
                  
                    if ($res > 0) {
                        return array('code' => 200, 'msg' => '上传成功', 'hasscore' => 0, 'ext' => $data['ext'], 'size' => $data['size'],'id' => $res, 'path' => $path, 'headpath' => $realpath, 'md5' => $data['md5'], 'savename' => $data['savename'], 'filename' => $data['name'], 'info' => $info->getInfo());
                    } else {
                        return array('code' => 0, 'msg' => '上传失败');
                    }
                } else {
                    return array('code' => 0, 'msg' => $file->getError());
                }
            } else {
                $path = $n['savepath'];
                $realpath = WEB_URL . $path;
                return array('code' => 200, 'msg' => '上传成功', 'hasscore' => 1, 'ext' => $n['ext'], 'size' => $data['size'],'id' => $n['id'], 'path' => $path, 'headpath' => $realpath, 'md5' => $n['md5'], 'savename' => $n['savename'], 'filename' => $n['name'], 'info' => $n);
              
            }
        }
   }               
 
}