<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------
namespace app\controller\system;

use app\controller\Base;
use app\validate\File as Files;
use app\services\system\DictionaryService;

/**
 * @title 文件
 */
class File extends Base{

	/**
	 * @title 上传
	 *
	 * @return void
	 */
	public function upload(){
		$file = request()->file('file');
		$type = request()->param('type', 'images');

		try {
			$this->data['data'] = $this->$type($file, $type);
		} catch (\think\Exception $e) {
			$this->data['code'] = 0;
			$this->data['message'] = $e->getMessage();
		}
		
		return $this->data;
	}

	/**
	 * @title 用户头像上传
	 *
	 * @return void
	 */
	protected function avatar($file, $type){
		try {
			validate(Files::class)->check(['avatar' => $file]);
			// 上传到本地服务器
			$savename = \think\facade\Filesystem::putFile( $type, $file);
			return ['src' => request()->static() . $savename, 'fileName' => ''];
		} catch (\think\exception\ValidateException $e) {
			throw new \think\Exception($e->getMessage(), 1);
		}
	}

	/**
	 * @title 图片上传
	 *
	 * @return void
	 */
	protected function images($file, $type){
		try {
			validate(Files::class)->check(['image' => $file]);
			// 上传到本地服务器
			$savename = \think\facade\Filesystem::putFile( $type, $file);
			return ['src' => request()->static() . $savename, 'fileName' => ''];
		} catch (\think\exception\ValidateException $e) {
			throw new \think\Exception($e->getMessage(), 1);
		}
	}
}