作者 lyh

gx

@@ -5,6 +5,9 @@ namespace App\Http\Controllers\Aside; @@ -5,6 +5,9 @@ namespace App\Http\Controllers\Aside;
5 use App\Enums\Common\Code; 5 use App\Enums\Common\Code;
6 use App\Enums\Common\Common; 6 use App\Enums\Common\Common;
7 use App\Http\Controllers\Controller; 7 use App\Http\Controllers\Controller;
  8 +use App\Models\File\File;
  9 +use App\Models\File\Image;
  10 +use App\Services\CosService;
8 use Illuminate\Http\JsonResponse; 11 use Illuminate\Http\JsonResponse;
9 use Illuminate\Http\Request; 12 use Illuminate\Http\Request;
10 use Illuminate\Http\Exceptions\HttpResponseException; 13 use Illuminate\Http\Exceptions\HttpResponseException;
@@ -126,7 +129,7 @@ class BaseController extends Controller @@ -126,7 +129,7 @@ class BaseController extends Controller
126 $result = [ 129 $result = [
127 'msg' => $msg == ' ' ? $code->description : $msg, 130 'msg' => $msg == ' ' ? $code->description : $msg,
128 'code' => $code->value, 131 'code' => $code->value,
129 - 'data' => $this->_extents($data), 132 + 'data' => $data,
130 ]; 133 ];
131 $this->header['Content-Type'] = $type; 134 $this->header['Content-Type'] = $type;
132 $this->header['token'] = $this->token; 135 $this->header['token'] = $this->token;
@@ -136,36 +139,42 @@ class BaseController extends Controller @@ -136,36 +139,42 @@ class BaseController extends Controller
136 139
137 140
138 /** 141 /**
139 - * @param $data  
140 - * @remark :统一返回参数处理  
141 - * @name :_extents 142 + * @remark :获取图片链接
  143 + * @name :getImageUrl
142 * @author :lyh 144 * @author :lyh
143 * @method :post 145 * @method :post
144 - * @time :2023/6/17 16:34 146 + * @time :2023/7/20 16:46
145 */ 147 */
146 - protected function _extents($data) {  
147 - if (empty($data) || !is_array($data)) {  
148 - return empty($data) ? is_array($data) ? [] : '' : $data; 148 + public function getImageUrl($hash){
  149 + $imageModel = new Image();
  150 + $info = $imageModel->read(['hash'=>$hash]);
  151 + $url = '';
  152 + if($info['is_cos'] == 1){
  153 + $cos = new CosService();
  154 + $url = $cos->getImageUrl($info['path']);
  155 + }else{
  156 + $url = url('b/image/'.$info['hash']);
149 } 157 }
150 - foreach ($data as $k => $v) {  
151 - if (is_array($v)) {  
152 - $data[$k] = $this->_extents($v);  
153 - } else {  
154 - if (is_null($v)) {  
155 - $data[$k] = '';  
156 - continue;  
157 - }  
158 - switch ((string) $k) {  
159 - case 'image':  
160 - $data['image_link'] = url('/a/image/' . $v);  
161 - break;  
162 - }  
163 - }  
164 - }  
165 - return $data; 158 + return $url;
166 } 159 }
167 160
168 -  
169 - 161 + /**
  162 + * @remark :获取文件交接链接
  163 + * @name :getImageUrl
  164 + * @author :lyh
  165 + * @method :post
  166 + * @time :2023/7/20 16:46
  167 + */
  168 + public function getFileUrl($hash){
  169 + $fileModel = new File();
  170 + $info = $fileModel->read(['hash'=>$hash]);
  171 + if($info['is_cos'] == 1){
  172 + $cos = new CosService();
  173 + $url = $cos->getImageUrl($info['path']);
  174 + }else{
  175 + $url = url('b/file_hash/'.$info['hash']);
  176 + }
  177 + return $url;
  178 + }
170 179
171 } 180 }
@@ -7,6 +7,7 @@ use App\Helper\Common; @@ -7,6 +7,7 @@ use App\Helper\Common;
7 use App\Http\Controllers\Controller; 7 use App\Http\Controllers\Controller;
8 use App\Http\Logic\Aside\Project\ProjectLogic; 8 use App\Http\Logic\Aside\Project\ProjectLogic;
9 use App\Http\Requests\Scene; 9 use App\Http\Requests\Scene;
  10 +use App\Models\File\File;
10 use App\Models\File\Image; 11 use App\Models\File\Image;
11 use App\Models\User\User as UserModel; 12 use App\Models\User\User as UserModel;
12 use App\Services\CosService; 13 use App\Services\CosService;
@@ -249,4 +250,23 @@ class BaseController extends Controller @@ -249,4 +250,23 @@ class BaseController extends Controller
249 } 250 }
250 return $url; 251 return $url;
251 } 252 }
  253 +
  254 + /**
  255 + * @remark :获取文件交接链接
  256 + * @name :getImageUrl
  257 + * @author :lyh
  258 + * @method :post
  259 + * @time :2023/7/20 16:46
  260 + */
  261 + public function getFileUrl($hash){
  262 + $fileModel = new File();
  263 + $info = $fileModel->read(['hash'=>$hash]);
  264 + if($info['is_cos'] == 1){
  265 + $cos = new CosService();
  266 + $url = $cos->getImageUrl($info['path']);
  267 + }else{
  268 + $url = url('b/file_hash/'.$info['hash']);
  269 + }
  270 + return $url;
  271 + }
252 } 272 }
@@ -37,7 +37,7 @@ class FileController @@ -37,7 +37,7 @@ class FileController
37 37
38 public $cache = '';//缓存数据 38 public $cache = '';//缓存数据
39 public $upload_location = 1; 39 public $upload_location = 1;
40 - public $image_type = [ 40 + public $file_type = [
41 1 => 'file_other', 41 1 => 'file_other',
42 0 => 'file' 42 0 => 'file'
43 ]; 43 ];
@@ -188,7 +188,7 @@ class FileController @@ -188,7 +188,7 @@ class FileController
188 } 188 }
189 } 189 }
190 $this->saveMysql($fileModel,$size,$file_type,$fileName,$hash,$is_cos); 190 $this->saveMysql($fileModel,$size,$file_type,$fileName,$hash,$is_cos);
191 - return $this->response('资源',Code::SUCCESS,['file'=>$hash]); 191 + return $this->response('资源',Code::SUCCESS,['file'=>$hash,'file_link'=>$this->getFileUrl($hash)]);
192 } 192 }
193 193
194 /** 194 /**
@@ -230,7 +230,7 @@ class FileController @@ -230,7 +230,7 @@ class FileController
230 $hash = hash_file('md5', $file->getPathname()); 230 $hash = hash_file('md5', $file->getPathname());
231 $file_hash = $fileModel->read(['hash'=>$hash]); 231 $file_hash = $fileModel->read(['hash'=>$hash]);
232 if($file_hash !== false){ 232 if($file_hash !== false){
233 - $data[] = $hash; 233 + $data[] = ['file'=>$hash,'file_link'=>$this->getFileUrl($hash)];
234 continue; 234 continue;
235 } 235 }
236 $url = $this->config['root'].'/'.$this->path; 236 $url = $this->config['root'].'/'.$this->path;
@@ -250,9 +250,9 @@ class FileController @@ -250,9 +250,9 @@ class FileController
250 $size = $file->getSize(); 250 $size = $file->getSize();
251 $file_type = $file->getClientOriginalExtension(); 251 $file_type = $file->getClientOriginalExtension();
252 $this->saveMysql($fileModel,$size,$file_type,$fileName,$hash,$is_cos); 252 $this->saveMysql($fileModel,$size,$file_type,$fileName,$hash,$is_cos);
253 - $data[] = $hash; 253 + $data[] = ['file'=>$hash,'file_link'=>$this->getFileUrl($hash)];
254 } 254 }
255 - return $this->response('资源',Code::SUCCESS,['file'=>$data]); 255 + return $this->response('资源',Code::SUCCESS,$data);
256 } 256 }
257 257
258 258
@@ -274,7 +274,7 @@ class FileController @@ -274,7 +274,7 @@ class FileController
274 $result = [ 274 $result = [
275 'msg' => $msg == ' ' ? $code->description : $msg, 275 'msg' => $msg == ' ' ? $code->description : $msg,
276 'code' => $code->value, 276 'code' => $code->value,
277 - 'data' => $this->_extents($data), 277 + 'data' => $data,
278 ]; 278 ];
279 $this->header['Content-Type'] = $type; 279 $this->header['Content-Type'] = $type;
280 $response = response($result,$result_code,$this->header); 280 $response = response($result,$result_code,$this->header);
@@ -282,37 +282,6 @@ class FileController @@ -282,37 +282,6 @@ class FileController
282 } 282 }
283 283
284 /** 284 /**
285 - * @param $data  
286 - * @remark :参数处理  
287 - * @name :_extents  
288 - * @author :lyh  
289 - * @method :post  
290 - * @time :2023/6/17 16:32  
291 - */  
292 - protected function _extents($data) {  
293 -  
294 - if (empty($data) || !is_array($data)) {  
295 - return empty($data) ? is_array($data) ? [] : '' : $data;  
296 - }  
297 - foreach ($data as $k => $v) {  
298 - if (is_array($v)) {  
299 - $data[$k] = $this->_extents($v);  
300 - } else {  
301 - if (is_null($v)) {  
302 - $data[$k] = '';  
303 - continue;  
304 - }  
305 - switch ((string) $k) {  
306 - case 'file':  
307 - $data['file_link'] = url('/b/file_hash/'.$v);  
308 - break;  
309 - }  
310 - }  
311 - }  
312 - return $data;  
313 - }  
314 -  
315 - /**  
316 * @remark :文件下载 285 * @remark :文件下载
317 * @name :downLoad 286 * @name :downLoad
318 * @author :lyh 287 * @author :lyh
@@ -346,16 +315,40 @@ class FileController @@ -346,16 +315,40 @@ class FileController
346 * @time :2023/7/18 15:36 315 * @time :2023/7/18 15:36
347 */ 316 */
348 public function setUrl(){ 317 public function setUrl(){
349 - if(!isset($this->param['refer'])){  
350 - $this->param['refer'] = 1;  
351 - } 318 + //A端上传
352 if(isset($this->param['refer_type']) && $this->param['refer_type'] == 1){ 319 if(isset($this->param['refer_type']) && $this->param['refer_type'] == 1){
353 - $this->path = $this->uploads['path_a'].'/'.$this->image_type[$this->param['refer']].'/'.date('Y-m'); 320 + $this->path = $this->uploads['path_a'].'/'.$this->file_type[$this->param['refer']].'/'.date('Y-m');
  321 + }else{
  322 + //B端上传
  323 + if(!isset($this->param['upload_method']) && $this->param['upload_method'] == 1){
  324 + //强制上传本地配置
  325 + $this->upload_location = 0;
  326 + }else{
  327 + //根据项目上传标识区分上传到cos/本地
  328 + $projectModel = new Project();
  329 + $project_info = $projectModel->read(['id'=>$this->cache['project_id']],['upload_location']);
  330 + $this->upload_location = $project_info['upload_location'];
  331 + }
  332 + $this->path = $this->uploads['path_b'].'/'.$this->cache['project_id'].'/'.$this->file_type[$this->param['refer']].'/'.date('Y-m');
  333 + }
  334 + }
  335 +
  336 + /**
  337 + * @remark :获取文件交接链接
  338 + * @name :getImageUrl
  339 + * @author :lyh
  340 + * @method :post
  341 + * @time :2023/7/20 16:46
  342 + */
  343 + public function getFileUrl($hash){
  344 + $fileModel = new File();
  345 + $info = $fileModel->read(['hash'=>$hash]);
  346 + if($info['is_cos'] == 1){
  347 + $cos = new CosService();
  348 + $url = $cos->getImageUrl($info['path']);
354 }else{ 349 }else{
355 - $projectModel = new Project();  
356 - $project_info = $projectModel->read(['id'=>$this->cache['project_id']],['upload_location']);  
357 - $this->upload_location = $project_info['upload_location'];  
358 - $this->path = $this->uploads['path_b'].'/'.$this->cache['project_id'].'/'.$this->image_type[$this->param['refer']].'/'.date('Y-m'); 350 + $url = url('b/file_hash/'.$info['hash']);
359 } 351 }
  352 + return $url;
360 } 353 }
361 } 354 }
@@ -165,7 +165,7 @@ class ImageController extends Controller @@ -165,7 +165,7 @@ class ImageController extends Controller
165 } 165 }
166 } 166 }
167 $this->saveMysql($imageModel,$size,$image_type,$fileName,$hash,$this->upload_location); 167 $this->saveMysql($imageModel,$size,$image_type,$fileName,$hash,$this->upload_location);
168 - return $this->response('图片资源',Code::SUCCESS,['image'=>$hash]); 168 + return $this->response('图片资源',Code::SUCCESS,['image'=>$hash,'image_link'=>$this->getImageUrl($hash)]);
169 } 169 }
170 170
171 /** 171 /**
@@ -226,7 +226,7 @@ class ImageController extends Controller @@ -226,7 +226,7 @@ class ImageController extends Controller
226 $hash = hash_file('md5', $file->getPathname()); 226 $hash = hash_file('md5', $file->getPathname());
227 $image_hash = $imageModel->read(['hash'=>$hash]); 227 $image_hash = $imageModel->read(['hash'=>$hash]);
228 if($image_hash !== false){ 228 if($image_hash !== false){
229 - $data[] = ['image'=>$hash]; 229 + $data[] = ['image'=>$hash,'image_link'=>$this->getImageUrl($hash)];
230 continue; 230 continue;
231 } 231 }
232 $url = $this->config['root'].$this->path; 232 $url = $this->config['root'].$this->path;
@@ -243,7 +243,7 @@ class ImageController extends Controller @@ -243,7 +243,7 @@ class ImageController extends Controller
243 } 243 }
244 //批量存储 244 //批量存储
245 $this->saveMysql($imageModel,$size,$image_type,$fileName,$hash,$this->upload_location); 245 $this->saveMysql($imageModel,$size,$image_type,$fileName,$hash,$this->upload_location);
246 - $data[] = ['image'=>$hash]; 246 + $data[] = ['image'=>$hash,'image_link'=>$this->getImageUrl($hash)];
247 } 247 }
248 return $this->response('图片资源',Code::SUCCESS,$data); 248 return $this->response('图片资源',Code::SUCCESS,$data);
249 } 249 }
@@ -279,48 +279,13 @@ class ImageController extends Controller @@ -279,48 +279,13 @@ class ImageController extends Controller
279 $result = [ 279 $result = [
280 'msg' => $msg == ' ' ? $code->description : $msg, 280 'msg' => $msg == ' ' ? $code->description : $msg,
281 'code' => $code->value, 281 'code' => $code->value,
282 - 'data' => $this->_extents($data), 282 + 'data' => $data,
283 ]; 283 ];
284 $this->header['Content-Type'] = $type; 284 $this->header['Content-Type'] = $type;
285 $response = response($result,$result_code,$this->header); 285 $response = response($result,$result_code,$this->header);
286 throw new HttpResponseException($response); 286 throw new HttpResponseException($response);
287 } 287 }
288 288
289 - /**  
290 - * @param $data  
291 - * @name :返回参数处理  
292 - * @return array|string  
293 - * @author :liyuhang  
294 - * @method  
295 - */  
296 - protected function _extents($data) {  
297 - if (empty($data) || !is_array($data)) {  
298 - return empty($data) ? is_array($data) ? [] : '' : $data;  
299 - }  
300 - foreach ($data as $k => $v) {  
301 - if (is_array($v)) {  
302 - $data[$k] = $this->_extents($v);  
303 - } else {  
304 - if (is_null($v)) {  
305 - $data[$k] = '';  
306 - continue;  
307 - }  
308 - //获取操作人  
309 - switch ((string) $k) {  
310 - case 'image':  
311 - $data['image_link'] = url('/b/image/' . $v);  
312 - break;  
313 - case 'hash':  
314 - $data['image_link'] = url('/b/image/' . $v);  
315 - break;  
316 - case 'path':  
317 - $data['path_link'] = url('/upload/images/' . basename($v));  
318 - break;  
319 - }  
320 - }  
321 - }  
322 - return $data;  
323 - }  
324 289
325 /** 290 /**
326 * @remark :获取所有图片 291 * @remark :获取所有图片
@@ -332,6 +297,9 @@ class ImageController extends Controller @@ -332,6 +297,9 @@ class ImageController extends Controller
332 public function getImageList(){ 297 public function getImageList(){
333 $imageModel = new ImageModel(); 298 $imageModel = new ImageModel();
334 $lists = $imageModel->list([],$order = 'id',['id','hash','type','path','created_at']); 299 $lists = $imageModel->list([],$order = 'id',['id','hash','type','path','created_at']);
  300 + foreach ($lists as $k => $v){
  301 + $v['image_link'] = $this->getImageUrl($v['image']);
  302 + }
335 $this->response('success',Code::SUCCESS,$lists); 303 $this->response('success',Code::SUCCESS,$lists);
336 } 304 }
337 305
@@ -361,4 +329,22 @@ class ImageController extends Controller @@ -361,4 +329,22 @@ class ImageController extends Controller
361 } 329 }
362 } 330 }
363 331
  332 + /**
  333 + * @remark :获取图片链接
  334 + * @name :getImageUrl
  335 + * @author :lyh
  336 + * @method :post
  337 + * @time :2023/7/20 16:46
  338 + */
  339 + public function getImageUrl($hash){
  340 + $imageModel = new ImageModel();
  341 + $info = $imageModel->read(['hash'=>$hash]);
  342 + if($info['is_cos'] == 1){
  343 + $cos = new CosService();
  344 + $url = $cos->getImageUrl($info['path']);
  345 + }else{
  346 + $url = url('b/image/'.$info['hash']);
  347 + }
  348 + return $url;
  349 + }
364 } 350 }