<?php

namespace Sdk\Controller;

use Think\ShardUpload;

use Admin\Model\FileModel;


/**
 * 文件控制器
 * 主要用于下载模型的文件上传和下载
 * author 鹿文学
 */
class FileController extends CommonController {

    /* 文件上传 */
    private function upload(){
        
        /* 调用文件上传组件上传文件 */
        $file = new FileModel();
        $file_driver = C('DOWNLOAD_UPLOAD_DRIVER');
        $info = $file->upload(
            $_FILES,
            C('DOWNLOAD_UPLOAD'),
            C('DOWNLOAD_UPLOAD_DRIVER'),
            C("UPLOAD_{$file_driver}_CONFIG")
        );

        /* 记录附件信息 */
        if($info){
						$return = array(
							'status'=>1,
							'file'=>$info['fileimg'],
						);
        } else {
						$return = array(
							'status'=>0,
							'info'=>$file->getError(),
						);
        }
				ob_clean();
				
        return $return;
        
    }
		
		
		/*
		 * 头像修改
		 * @author  鹿文学
		 */
		public function set_head_portrait() {

				$result = $this->upload();
				
				if($result['status'] == 1){
					$file = $result['file'];
						
					
					if(!empty($file['path'])){
						$head = 'http://'.$_SERVER['HTTP_HOST'].$file['path'];
					}else{
						$map['md5'] = $file['md5'];
						$picture = M('Picture')->where($map)->find();
						$head = 'http://'.$_SERVER['HTTP_HOST'].$picture['path'];
					}
					
					//上传成功,写入用户数据
					M("user", "tab_")->where(["id"=>UID])->setField(['head_img'=>$head]);					M('nearby','tab_')->where(['user_id'=>UID])->setField(['head_img'=>$head]);
					
					$data = array(
						'url' => $head,
					);
					$msg = array(
			            "code" => 200,
			            "msg"  => '上传成功',
			            "data" => $data
			        );
					echo base64_encode(json_encode($msg,true));
					exit;
				}else{
					$msg = array(
			            "code" => 1065,
			            "msg"  => '上传失败: ' . $result['info'],
			            "data" => []
			        );
					echo base64_encode(json_encode($msg,true));
					exit;
					
				}
			
			
		}

    
		
}