作者 lyh

gx

... ... @@ -5,6 +5,9 @@ namespace App\Http\Controllers\Aside;
use App\Enums\Common\Code;
use App\Enums\Common\Common;
use App\Http\Controllers\Controller;
use App\Models\File\File;
use App\Models\File\Image;
use App\Services\CosService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Exceptions\HttpResponseException;
... ... @@ -126,7 +129,7 @@ class BaseController extends Controller
$result = [
'msg' => $msg == ' ' ? $code->description : $msg,
'code' => $code->value,
'data' => $this->_extents($data),
'data' => $data,
];
$this->header['Content-Type'] = $type;
$this->header['token'] = $this->token;
... ... @@ -136,36 +139,42 @@ class BaseController extends Controller
/**
* @param $data
* @remark :统一返回参数处理
* @name :_extents
* @remark :获取图片链接
* @name :getImageUrl
* @author :lyh
* @method :post
* @time :2023/6/17 16:34
* @time :2023/7/20 16:46
*/
protected function _extents($data) {
if (empty($data) || !is_array($data)) {
return empty($data) ? is_array($data) ? [] : '' : $data;
}
foreach ($data as $k => $v) {
if (is_array($v)) {
$data[$k] = $this->_extents($v);
} else {
if (is_null($v)) {
$data[$k] = '';
continue;
}
switch ((string) $k) {
case 'image':
$data['image_link'] = url('/a/image/' . $v);
break;
}
public function getImageUrl($hash){
$imageModel = new Image();
$info = $imageModel->read(['hash'=>$hash]);
$url = '';
if($info['is_cos'] == 1){
$cos = new CosService();
$url = $cos->getImageUrl($info['path']);
}else{
$url = url('b/image/'.$info['hash']);
}
return $url;
}
/**
* @remark :获取文件交接链接
* @name :getImageUrl
* @author :lyh
* @method :post
* @time :2023/7/20 16:46
*/
public function getFileUrl($hash){
$fileModel = new File();
$info = $fileModel->read(['hash'=>$hash]);
if($info['is_cos'] == 1){
$cos = new CosService();
$url = $cos->getImageUrl($info['path']);
}else{
$url = url('b/file_hash/'.$info['hash']);
}
return $data;
return $url;
}
}
... ...
... ... @@ -7,6 +7,7 @@ use App\Helper\Common;
use App\Http\Controllers\Controller;
use App\Http\Logic\Aside\Project\ProjectLogic;
use App\Http\Requests\Scene;
use App\Models\File\File;
use App\Models\File\Image;
use App\Models\User\User as UserModel;
use App\Services\CosService;
... ... @@ -249,4 +250,23 @@ class BaseController extends Controller
}
return $url;
}
/**
* @remark :获取文件交接链接
* @name :getImageUrl
* @author :lyh
* @method :post
* @time :2023/7/20 16:46
*/
public function getFileUrl($hash){
$fileModel = new File();
$info = $fileModel->read(['hash'=>$hash]);
if($info['is_cos'] == 1){
$cos = new CosService();
$url = $cos->getImageUrl($info['path']);
}else{
$url = url('b/file_hash/'.$info['hash']);
}
return $url;
}
}
... ...
... ... @@ -37,7 +37,7 @@ class FileController
public $cache = '';//缓存数据
public $upload_location = 1;
public $image_type = [
public $file_type = [
1 => 'file_other',
0 => 'file'
];
... ... @@ -188,7 +188,7 @@ class FileController
}
}
$this->saveMysql($fileModel,$size,$file_type,$fileName,$hash,$is_cos);
return $this->response('资源',Code::SUCCESS,['file'=>$hash]);
return $this->response('资源',Code::SUCCESS,['file'=>$hash,'file_link'=>$this->getFileUrl($hash)]);
}
/**
... ... @@ -230,7 +230,7 @@ class FileController
$hash = hash_file('md5', $file->getPathname());
$file_hash = $fileModel->read(['hash'=>$hash]);
if($file_hash !== false){
$data[] = $hash;
$data[] = ['file'=>$hash,'file_link'=>$this->getFileUrl($hash)];
continue;
}
$url = $this->config['root'].'/'.$this->path;
... ... @@ -250,9 +250,9 @@ class FileController
$size = $file->getSize();
$file_type = $file->getClientOriginalExtension();
$this->saveMysql($fileModel,$size,$file_type,$fileName,$hash,$is_cos);
$data[] = $hash;
$data[] = ['file'=>$hash,'file_link'=>$this->getFileUrl($hash)];
}
return $this->response('资源',Code::SUCCESS,['file'=>$data]);
return $this->response('资源',Code::SUCCESS,$data);
}
... ... @@ -274,7 +274,7 @@ class FileController
$result = [
'msg' => $msg == ' ' ? $code->description : $msg,
'code' => $code->value,
'data' => $this->_extents($data),
'data' => $data,
];
$this->header['Content-Type'] = $type;
$response = response($result,$result_code,$this->header);
... ... @@ -282,37 +282,6 @@ class FileController
}
/**
* @param $data
* @remark :参数处理
* @name :_extents
* @author :lyh
* @method :post
* @time :2023/6/17 16:32
*/
protected function _extents($data) {
if (empty($data) || !is_array($data)) {
return empty($data) ? is_array($data) ? [] : '' : $data;
}
foreach ($data as $k => $v) {
if (is_array($v)) {
$data[$k] = $this->_extents($v);
} else {
if (is_null($v)) {
$data[$k] = '';
continue;
}
switch ((string) $k) {
case 'file':
$data['file_link'] = url('/b/file_hash/'.$v);
break;
}
}
}
return $data;
}
/**
* @remark :文件下载
* @name :downLoad
* @author :lyh
... ... @@ -346,16 +315,40 @@ class FileController
* @time :2023/7/18 15:36
*/
public function setUrl(){
if(!isset($this->param['refer'])){
$this->param['refer'] = 1;
}
//A端上传
if(isset($this->param['refer_type']) && $this->param['refer_type'] == 1){
$this->path = $this->uploads['path_a'].'/'.$this->image_type[$this->param['refer']].'/'.date('Y-m');
$this->path = $this->uploads['path_a'].'/'.$this->file_type[$this->param['refer']].'/'.date('Y-m');
}else{
//B端上传
if(!isset($this->param['upload_method']) && $this->param['upload_method'] == 1){
//强制上传本地配置
$this->upload_location = 0;
}else{
//根据项目上传标识区分上传到cos/本地
$projectModel = new Project();
$project_info = $projectModel->read(['id'=>$this->cache['project_id']],['upload_location']);
$this->upload_location = $project_info['upload_location'];
$this->path = $this->uploads['path_b'].'/'.$this->cache['project_id'].'/'.$this->image_type[$this->param['refer']].'/'.date('Y-m');
}
$this->path = $this->uploads['path_b'].'/'.$this->cache['project_id'].'/'.$this->file_type[$this->param['refer']].'/'.date('Y-m');
}
}
/**
* @remark :获取文件交接链接
* @name :getImageUrl
* @author :lyh
* @method :post
* @time :2023/7/20 16:46
*/
public function getFileUrl($hash){
$fileModel = new File();
$info = $fileModel->read(['hash'=>$hash]);
if($info['is_cos'] == 1){
$cos = new CosService();
$url = $cos->getImageUrl($info['path']);
}else{
$url = url('b/file_hash/'.$info['hash']);
}
return $url;
}
}
... ...
... ... @@ -165,7 +165,7 @@ class ImageController extends Controller
}
}
$this->saveMysql($imageModel,$size,$image_type,$fileName,$hash,$this->upload_location);
return $this->response('图片资源',Code::SUCCESS,['image'=>$hash]);
return $this->response('图片资源',Code::SUCCESS,['image'=>$hash,'image_link'=>$this->getImageUrl($hash)]);
}
/**
... ... @@ -226,7 +226,7 @@ class ImageController extends Controller
$hash = hash_file('md5', $file->getPathname());
$image_hash = $imageModel->read(['hash'=>$hash]);
if($image_hash !== false){
$data[] = ['image'=>$hash];
$data[] = ['image'=>$hash,'image_link'=>$this->getImageUrl($hash)];
continue;
}
$url = $this->config['root'].$this->path;
... ... @@ -243,7 +243,7 @@ class ImageController extends Controller
}
//批量存储
$this->saveMysql($imageModel,$size,$image_type,$fileName,$hash,$this->upload_location);
$data[] = ['image'=>$hash];
$data[] = ['image'=>$hash,'image_link'=>$this->getImageUrl($hash)];
}
return $this->response('图片资源',Code::SUCCESS,$data);
}
... ... @@ -279,48 +279,13 @@ class ImageController extends Controller
$result = [
'msg' => $msg == ' ' ? $code->description : $msg,
'code' => $code->value,
'data' => $this->_extents($data),
'data' => $data,
];
$this->header['Content-Type'] = $type;
$response = response($result,$result_code,$this->header);
throw new HttpResponseException($response);
}
/**
* @param $data
* @name :返回参数处理
* @return array|string
* @author :liyuhang
* @method
*/
protected function _extents($data) {
if (empty($data) || !is_array($data)) {
return empty($data) ? is_array($data) ? [] : '' : $data;
}
foreach ($data as $k => $v) {
if (is_array($v)) {
$data[$k] = $this->_extents($v);
} else {
if (is_null($v)) {
$data[$k] = '';
continue;
}
//获取操作人
switch ((string) $k) {
case 'image':
$data['image_link'] = url('/b/image/' . $v);
break;
case 'hash':
$data['image_link'] = url('/b/image/' . $v);
break;
case 'path':
$data['path_link'] = url('/upload/images/' . basename($v));
break;
}
}
}
return $data;
}
/**
* @remark :获取所有图片
... ... @@ -332,6 +297,9 @@ class ImageController extends Controller
public function getImageList(){
$imageModel = new ImageModel();
$lists = $imageModel->list([],$order = 'id',['id','hash','type','path','created_at']);
foreach ($lists as $k => $v){
$v['image_link'] = $this->getImageUrl($v['image']);
}
$this->response('success',Code::SUCCESS,$lists);
}
... ... @@ -361,4 +329,22 @@ class ImageController extends Controller
}
}
/**
* @remark :获取图片链接
* @name :getImageUrl
* @author :lyh
* @method :post
* @time :2023/7/20 16:46
*/
public function getImageUrl($hash){
$imageModel = new ImageModel();
$info = $imageModel->read(['hash'=>$hash]);
if($info['is_cos'] == 1){
$cos = new CosService();
$url = $cos->getImageUrl($info['path']);
}else{
$url = url('b/image/'.$info['hash']);
}
return $url;
}
}
... ...