作者 lyh

gx

@@ -40,7 +40,7 @@ class ImageController extends Controller @@ -40,7 +40,7 @@ class ImageController extends Controller
40 40
41 public $cache = '';//缓存数据 41 public $cache = '';//缓存数据
42 42
43 - public $upload_location = 1; 43 + public $upload_location = 1;//是否同步到cos
44 public $image_type = [ 44 public $image_type = [
45 1 => 'image_product', 45 1 => 'image_product',
46 2 => 'image_news', 46 2 => 'image_news',
@@ -86,7 +86,7 @@ class ImageController extends Controller @@ -86,7 +86,7 @@ class ImageController extends Controller
86 $content = file_get_contents($filename); 86 $content = file_get_contents($filename);
87 $header['Content-Length'] = strlen($content); 87 $header['Content-Length'] = strlen($content);
88 }else{ 88 }else{
89 - $path = $this->config['url'].'/'.$info['path']; 89 + $path = $this->config['root'].'/'.$info['path'];
90 if (!is_file($path)) { 90 if (!is_file($path)) {
91 $this->response('指定图片已被系统删除!', Code::USER_ERROR); 91 $this->response('指定图片已被系统删除!', Code::USER_ERROR);
92 } 92 }
@@ -150,30 +150,41 @@ class ImageController extends Controller @@ -150,30 +150,41 @@ class ImageController extends Controller
150 } 150 }
151 $url = $this->config['root'].$this->path; 151 $url = $this->config['root'].$this->path;
152 $fileName = uniqid().rand(10000,99999).'.'.$files->getClientOriginalExtension(); 152 $fileName = uniqid().rand(10000,99999).'.'.$files->getClientOriginalExtension();
153 - $res = $files->move($url,$fileName);  
154 - if ($res === false) {  
155 - return $this->response($files->getError(), Code::USER_ERROR); 153 + //同步数据到cos
  154 + if($this->upload_location == 1){
  155 + $cosService = new CosService();
  156 + $cosService->uploadFile($this->path,$fileName);
  157 + }else{
  158 + $res = $files->move($url,$fileName);
  159 + if ($res === false) {
  160 + return $this->response($files->getError(), Code::USER_ERROR);
  161 + }
156 } 162 }
  163 +
  164 + return $this->response('图片资源',Code::SUCCESS,['image'=>$hash]);
  165 + }
  166 +
  167 + /**
  168 + * @remark :保存数据库
  169 + * @name :saveMysql
  170 + * @author :lyh
  171 + * @method :post
  172 + * @time :2023/7/19 16:38
  173 + */
  174 + public function saveMysql(&$imageModel,&$files,$fileName,$hash){
157 $data = [ 175 $data = [
158 'path' => $this->path.'/'.$fileName, 176 'path' => $this->path.'/'.$fileName,
159 'created_at' => date('Y-m-d H:i:s',time()), 177 'created_at' => date('Y-m-d H:i:s',time()),
160 - 'size' => $res->getSize(), 178 + 'size' => $files->getSize(),
161 'hash' => $hash, 179 'hash' => $hash,
162 'type'=>$files->getClientOriginalExtension(), 180 'type'=>$files->getClientOriginalExtension(),
163 - 'refer'=>$this->param['refer'] ?? 0, 181 + 'refer'=>$this->param['refer'] ?? 1,
164 ]; 182 ];
165 $rs = $imageModel->add($data); 183 $rs = $imageModel->add($data);
166 if ($rs === false) { 184 if ($rs === false) {
167 return $this->response('添加失败', Code::USER_ERROR); 185 return $this->response('添加失败', Code::USER_ERROR);
168 } 186 }
169 - //同步数据到cos  
170 - if($this->upload_location == 1){  
171 - $cosService = new CosService();  
172 - $cosService->uploadFile($this->path,$fileName);  
173 - }  
174 - return $this->response('图片资源',Code::SUCCESS,['image'=>$hash]);  
175 } 187 }
176 -  
177 /** 188 /**
178 * @param $info 189 * @param $info
179 * @param $w 190 * @param $w