作者 lyh

gx

... ... @@ -40,7 +40,7 @@ class ImageController extends Controller
public $cache = '';//缓存数据
public $upload_location = 1;
public $upload_location = 1;//是否同步到cos
public $image_type = [
1 => 'image_product',
2 => 'image_news',
... ... @@ -86,7 +86,7 @@ class ImageController extends Controller
$content = file_get_contents($filename);
$header['Content-Length'] = strlen($content);
}else{
$path = $this->config['url'].'/'.$info['path'];
$path = $this->config['root'].'/'.$info['path'];
if (!is_file($path)) {
$this->response('指定图片已被系统删除!', Code::USER_ERROR);
}
... ... @@ -150,30 +150,41 @@ class ImageController extends Controller
}
$url = $this->config['root'].$this->path;
$fileName = uniqid().rand(10000,99999).'.'.$files->getClientOriginalExtension();
$res = $files->move($url,$fileName);
if ($res === false) {
return $this->response($files->getError(), Code::USER_ERROR);
//同步数据到cos
if($this->upload_location == 1){
$cosService = new CosService();
$cosService->uploadFile($this->path,$fileName);
}else{
$res = $files->move($url,$fileName);
if ($res === false) {
return $this->response($files->getError(), Code::USER_ERROR);
}
}
return $this->response('图片资源',Code::SUCCESS,['image'=>$hash]);
}
/**
* @remark :保存数据库
* @name :saveMysql
* @author :lyh
* @method :post
* @time :2023/7/19 16:38
*/
public function saveMysql(&$imageModel,&$files,$fileName,$hash){
$data = [
'path' => $this->path.'/'.$fileName,
'created_at' => date('Y-m-d H:i:s',time()),
'size' => $res->getSize(),
'size' => $files->getSize(),
'hash' => $hash,
'type'=>$files->getClientOriginalExtension(),
'refer'=>$this->param['refer'] ?? 0,
'refer'=>$this->param['refer'] ?? 1,
];
$rs = $imageModel->add($data);
if ($rs === false) {
return $this->response('添加失败', Code::USER_ERROR);
}
//同步数据到cos
if($this->upload_location == 1){
$cosService = new CosService();
$cosService->uploadFile($this->path,$fileName);
}
return $this->response('图片资源',Code::SUCCESS,['image'=>$hash]);
}
/**
* @param $info
* @param $w
... ...