<?php
class uploadAction extends baseAction
{
	/**
	  * @method 图片上传
	  * @author soul
	  * @copyright 2017/5/9
	  */
	function uploadImg()
	{
		$Params = mvc::$URL_PARAMS;
		$site = isset($_REQUEST['site']) ? strtolower(trim($_REQUEST['site'])): 'b2c';
		$OthFileCenter = new OthFileCenter();
		$dataArr = ['cate'=>$_REQUEST['cate'], 'file'=>$_FILES['file']];
		if($site == 'b2c')
		{
			$shopId = (int) $_REQUEST['shopId'];
			if($shopId > 0)
			{
				$B2cBase = new B2cBase();
				$res = $B2cBase->get('sh_shop', ['shop_id'], sprintf("and shop_id=%d", $shopId), true);
				if(empty($res['data'])) LibFc::ajaxJsonEncode(['status'=>false, 'data'=>'shopId不存在']);
			}
			switch($dataArr['cate'])
			{
				case 'product':
					$dataArr['path'] = 'shop'.$shopId.'/product/';
					$dataArr['spi_style']=0;
					break;
				case 'adorn':
					$dataArr['path'] = 'shop'.$shopId.'/adorn/';
					$dataArr['spi_style']=1;
					break;
			}
		}

		//跑街令上传
		if($site == 'pjl' && !empty($_REQUEST['shopId']))
		{
			$shopId = (int) $_REQUEST['shopId'];
			$PjlBase = new PjlBase();
			$res = $PjlBase->get('pl_shop', ['shop_id'], sprintf("and shop_id=%d", $shopId), true);
			if(empty($res['data']))
				$res = $PjlBase->get('pl_repair', ['shopr_id'], sprintf("and shopr_id=%d", $shopId), true);
			if(empty($res['data'])) LibFc::ajaxJsonEncode(['status'=>false, 'data'=>'shopId不存在']);
		}

		$fileRes = $OthFileCenter->upload($dataArr, $site);
		if($site == 'b2c')
		{
			switch($dataArr['cate'])
			{
				case 'adorn':
				case 'product':
					//入库
					$fileRes['data']['oriname'] = empty($fileRes['data']['oriname'])? $fileRes['data']['name']: $fileRes['data']['oriname'];
					$temArr = [
						'spi_cate'=>$dataArr['cate'],
						'spi_shop_id'=>$shopId,
						'spi_path'=>$fileRes['data']['path'],
						'spi_name'=>$fileRes['data']['name'],
						'spi_oriname'=>$fileRes['data']['oriname'],
						'spi_ext'=>$fileRes['data']['ext'],
						'spi_crc32'=>$fileRes['data']['crc32'],
						'spi_size'=>$fileRes['data']['size'],
						'spi_width'=>$fileRes['data']['width'],
						'spi_height'=>$fileRes['data']['heigth'],
						'spi_atime'=>time(),
						'spi_style'=>$dataArr['spi_style']
					];
					$res = $B2cBase->add('sh_pic', $temArr);
					$fileRes['data']['spi_id'] = $res['data'];
					break;
				case 'shopFile':
					$fjid = (int) $_REQUEST['fjid'];
					if ($fjid < 1)
					{
						$temArr = [
							'shf_shop_id' => $_REQUEST['shopId'],
							'shf_name' => $fileRes['data']['oriname'],
							'shf_file' => $fileRes['data']['sfile'],
							'shf_atime' => time()
						];
						$res = $B2cBase->add('sh_shop_file', $temArr);
						$fileRes['data']['shf_id'] = $res['data'];
					}else
					{
						$temArr = [
							'shf_name' => $fileRes['data']['oriname'],
							'shf_file' => $fileRes['data']['sfile'],
						];
						$fileRes['data']['shf_id'] = $fjid;
						$B2cBase->set('sh_shop_file', $temArr,sprintf('and shf_id=%d and shf_shop_id=%d',$fjid,$_REQUEST['shopId']));
					}
					break;
			}
		}
		$fileRes['code'] = 0;
		LibFc::ajaxJsonEncode($fileRes);
	}
}
