作者 李小龙

Merge branch 'develop' of http://47.244.231.31:8099/zhl/globalso-v6 into develop

... ... @@ -5,6 +5,8 @@ namespace App\Http\Controllers\File;
use App\Enums\Common\Code;
use App\Models\File\File;
use App\Models\File\Image as ImageModel;
use App\Models\Project\Project;
use App\Services\CosService;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Cache;
... ... @@ -34,7 +36,7 @@ class FileController
public $token = '';//token
public $cache = '';//缓存数据
public $upload_location = 1;
public $image_type = [
1 => 'file_other',
0 => 'file'
... ... @@ -134,7 +136,9 @@ class FileController
],[
'file.required'=>'必须填写',
]);
$files = $this->param['file'];
$files = $this->request->file('file');
$size = $files->getSize();
$file_type = $files->getClientOriginalExtension();
if (empty($files)) {
$this->response('没有上传的文件!', 400);
}
... ... @@ -143,7 +147,7 @@ class FileController
if ($type == 'multi') {
return $this->multi($files);
} else {
return $this->single($files);
return $this->single($files,$size,$file_type);
}
}
... ... @@ -155,7 +159,7 @@ class FileController
* @method :post
* @time :2023/6/17 16:32
*/
public function single($files){
public function single($files,$size,$file_type){
$hash = hash_file('md5', $files->getPathname());
//查看文件是否存在
$fileModel = new File();
... ... @@ -165,37 +169,52 @@ class FileController
}
$url = $this->config['root'].$this->path;
$fileName = uniqid().rand(10000,99999).'.'.$files->getClientOriginalExtension();
//同步数据到cos
if($this->upload_location == 1){
$cosService = new CosService();
$cosService->uploadFile($files,$this->path,$fileName);
}else{
$res = $files->move($url,$fileName);
if ($res === false) {
return $this->response($files->getError(), Code::USER_ERROR);
}
}
$this->saveMysql($fileModel,$size,$file_type,$fileName,$hash);
return $this->response('资源',Code::SUCCESS,['file'=>$hash]);
}
/**
* @remark :保存数据库
* @name :saveMysql
* @author :lyh
* @method :post
* @time :2023/7/19 16:38
*/
public function saveMysql(&$fileModel,$size,$image_type,$fileName,$hash){
$data = [
'path' => $this->path.'/'.$fileName,
'created_at' => date('Y-m-d H:i:s',time()),
'size' => $res->getSize(),
'size' => $size,
'hash' => $hash,
'type'=>$files->getClientOriginalExtension(),
'refer'=>$this->param['refer'] ?? 0
'type'=>$image_type,
'refer'=>$this->param['refer'] ?? 1,
];
$rs = $fileModel->add($data);
if ($rs === false) {
return $this->response('添加失败', Code::USER_ERROR);
}
return $this->response('资源',Code::SUCCESS,['file'=>$hash]);
return true;
}
/**
* @param $files
* @remark :多文件上传
* @remark :多文件上传(暂时未用)
* @name :multi
* @author :lyh
* @method :post
* @time :2023/6/17 16:32
*/
private function multi($files) {
if (!is_array($files)) {
$files = [$files];
}
$save_data = [];
$data = [];
foreach ($files as $file) {
... ... @@ -322,6 +341,9 @@ class FileController
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');
}else{
$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');
}
}
... ...
... ... @@ -6,6 +6,8 @@ use App\Enums\Common\Code;
use App\Http\Controllers\Controller;
use App\Http\Controllers\type;
use App\Models\File\Image as ImageModel;
use App\Models\Project\Project;
use App\Services\CosService;
use App\Services\TencentCosService;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\JsonResponse;
... ... @@ -26,8 +28,6 @@ class ImageController extends Controller
'Content-Description' => 'File Transfer',
],
];
const TYPE = 1;
public $path = '';//路径
public $config = '';//存储默认配置
... ... @@ -40,11 +40,12 @@ class ImageController extends Controller
public $cache = '';//缓存数据
public $upload_location = 0;//是否同步到cos
public $image_type = [
1 => 'image_product',
2 => 'image_news',
3 => 'image_blog',
4 => 'image_other',
0 => 'image_other',
];
public function __construct()
... ... @@ -85,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);
}
... ... @@ -119,6 +120,8 @@ class ImageController extends Controller
'image.required'=>'图片必须填写',
]);
$files = $this->request->file('image');
$size = $files->getSize();
$image_type = $files->getClientOriginalExtension();
if (empty($files)) {
$this->response('没有上传的文件!', 400);
}
... ... @@ -126,8 +129,8 @@ class ImageController extends Controller
$this->setUrl();
if ($type == 'multi') {
return $this->multi($files);
} else {
return $this->single($files);
}else{
return $this->single($files,$size,$image_type);
}
}
... ... @@ -139,7 +142,7 @@ class ImageController extends Controller
* @method :post
* @time :2023/6/17 16:30
*/
public function single($files){
public function single(&$files,$size,$image_type){
$hash = hash_file('md5', $files->getPathname());
//查看文件是否存在
$imageModel = new ImageModel();
... ... @@ -149,25 +152,42 @@ class ImageController extends Controller
}
$url = $this->config['root'].$this->path;
$fileName = uniqid().rand(10000,99999).'.'.$files->getClientOriginalExtension();
//同步数据到cos
if($this->upload_location == 1){
$cosService = new CosService();
$cosService->uploadFile($files,$this->path,$fileName);
}else{
$res = $files->move($url,$fileName);
if ($res === false) {
return $this->response($files->getError(), Code::USER_ERROR);
}
}
$this->saveMysql($imageModel,$size,$image_type,$fileName,$hash);
return $this->response('图片资源',Code::SUCCESS,['image'=>$hash]);
}
/**
* @remark :保存数据库
* @name :saveMysql
* @author :lyh
* @method :post
* @time :2023/7/19 16:38
*/
public function saveMysql(&$imageModel,$size,$image_type,$fileName,$hash){
$data = [
'path' => $this->path.'/'.$fileName,
'created_at' => date('Y-m-d H:i:s',time()),
'size' => $res->getSize(),
'size' => $size,
'hash' => $hash,
'type'=>$files->getClientOriginalExtension(),
'refer'=>$this->param['refer'] ?? 0,
'type'=>$image_type,
'refer'=>$this->param['refer'] ?? 1,
];
$rs = $imageModel->add($data);
if ($rs === false) {
return $this->response('添加失败', Code::USER_ERROR);
}
return $this->response('图片资源',Code::SUCCESS,['image'=>$hash]);
return true;
}
/**
* @param $info
* @param $w
... ... @@ -193,13 +213,12 @@ class ImageController extends Controller
* @method :post
* @time :2023/6/17 16:31
*/
private function multi($files) {
if (!is_array($files)) {
$files = [$files];
}
private function multi(&$files) {
$save_data = [];
$data = [];
foreach ($files as $file) {
$size = $file->getSize();
$image_type = $file->getClientOriginalExtension();
$imageModel = new ImageModel();
$hash = hash_file('md5', $file->getPathname());
$image_hash = $imageModel->read(['hash'=>$hash]);
... ... @@ -209,18 +228,24 @@ class ImageController extends Controller
}
$url = $this->config['root'].$this->path;
$fileName = uniqid().rand(10000,99999).'.'.$file->getClientOriginalExtension();
//同步数据到cos
if($this->upload_location == 1){
$cosService = new CosService();
$cosService->uploadFile($file,$this->path,$fileName);
}else{
$res = $file->move($url,$fileName);
if ($res === false) {
return $this->response($file->getError(), Code::USER_ERROR);
}
}
$save_data[] = [
'path' => $this->path.'/'.$fileName,
'created_at' => date('Y-m-d H:i:s',time()),
'updated_at'=>date('Y-m-d H:i:s',time()),
'size' => $res->getSize(),
'size' => $size,
'hash' => $hash,
'type'=>$file->getClientOriginalExtension(),
'refer'=>$this->param['refer'] ?? '',
'type'=>$image_type,
'refer'=>$this->param['refer'] ?? 0,
];
$data[] = ['image'=>$hash];
}
... ... @@ -274,7 +299,8 @@ class ImageController extends Controller
//获取操作人
switch ((string) $k) {
case 'image':
$data['image_link'] = url('/b/image/' . $v);
$cos = new CosService();
$data['image_link'] = $cos->getImageUrl('/p/1/image_other/2023-07/64b7abf16721020421.png');
break;
case 'hash':
$data['image_link'] = url('/b/image/' . $v);
... ... @@ -315,6 +341,9 @@ class ImageController extends Controller
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');
}else{
$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');
}
}
... ...
... ... @@ -57,10 +57,6 @@ class BlogLogic extends BaseLogic
//拼接参数
DB::beginTransaction();
try {
if(isset($this->param['image'])){
$data = $this->upload();
$this->param['image'] = $data;
}
$this->param = $this->paramProcessing($this->param);
$rs = $this->model->insertGetId($this->param);
RouteMap::setRoute($this->param['url'] ?: $this->param['name'], RouteMap::SOURCE_BLOG, $rs, $this->user['project_id']);
... ... @@ -84,10 +80,6 @@ class BlogLogic extends BaseLogic
$this->param = $this->paramProcessing($this->param);
DB::beginTransaction();
try {
//是否有图片更新
if(isset($this->param['image']) && is_file($this->param['image'])){
$this->param['image'] = $this->upload();
}
RouteMap::setRoute($this->param['url'] ?: $this->param['name'], RouteMap::SOURCE_BLOG, $this->param['id'], $this->user['project_id']);
$this->edit($this->param,['id'=>$this->param['id']]);
DB::commit();
... ...
... ... @@ -57,10 +57,6 @@ class NewsLogic extends BaseLogic
//拼接参数
DB::beginTransaction();
try {
if(isset($this->param['image'])){
$data = $this->upload();
$this->param['image'] = $data;
}
$this->param = $this->paramProcessing($this->param);
$rs = $this->model->insertGetId($this->param);
RouteMap::setRoute($this->param['url'] ?: $this->param['name'], RouteMap::SOURCE_NEWS, $rs, $this->user['project_id']);
... ... @@ -83,10 +79,6 @@ class NewsLogic extends BaseLogic
$this->param = $this->paramProcessing($this->param);
DB::beginTransaction();
try {
//上传图片
if(isset($this->param['image']) && is_file($this->param['image'])){
$this->param['image'] = $this->upload();
}
//设置路由
RouteMap::setRoute($this->param['url'] ?: $this->param['name'], RouteMap::SOURCE_NEWS, $this->param['id'], $this->user['project_id']);
$this->edit($this->param,['id'=>$this->param['id']]);
... ...
<?php
namespace App\Services;
use Qcloud\Cos\Client;
/**
* @remark :
* @class :CosService.php
* @author :lyh
* @time :2023/7/19 15:09
*/
class CosService
{
/**
* @param $file
* @remark :上传图片
* @name :uploadFile
* @author :lyh
* @method :post
* @time :2023/7/19 15:28
*/
public function uploadFile(&$files,$path,$filename)
{
$cos = config('filesystems.disks.cos');
$cosClient = new Client([
'region' => $cos['region'],
'credentials' => [
'secretId' => $cos['credentials']['secretId'],
'secretKey' => $cos['credentials']['secretKey'],
],
]);
$key = $path.'/'.$filename;
$cosClient->putObject([
'Bucket' => $cos['bucket'],
'Key' => $key,
'Body' => fopen($files->getRealPath(), 'r'),
]);
return $key;
}
/**
* @param $image_name
* @remark :获取图片访问链接
* @name :getImageUrl
* @author :lyh
* @method :post
* @time :2023/7/19 16:08
*/
public function getImageUrl($image_name)
{
$cos = config('filesystems.disks.cos');
$cosClient = new Client([
'region' => $cos['region'],
'credentials' => [
'secretId' => $cos['credentials']['secretId'],
'secretKey' => $cos['credentials']['secretKey'],
],
]);
$imageUrl = $cosClient->getObjectUrl(config('app.cos_bucket'), basename($image_name));
return response()->json([
'image_url' => $imageUrl,
]);
}
}
... ...
... ... @@ -86,7 +86,12 @@ Route::middleware(['aloginauth'])->group(function () {
Route::any('/delete', [Aside\Manage\MenuController::class, 'delete'])->name('admin.menu_delete');
Route::any('/routes', [Aside\Manage\MenuController::class, 'routes'])->name('admin.menu_routes');
});
//人事
Route::prefix('hr')->group(function () {
Route::any('/', [Aside\Manage\HrController::class, 'list'])->name('admin.hr');
Route::any('/info', [Aside\Manage\HrController::class, 'info'])->name('admin.hr_info');
Route::any('/save', [Aside\Manage\HrController::class, 'save'])->name('admin.hr_save');
});
//权限组
Route::prefix('group')->group(function () {
Route::get('/', [Aside\Manage\GroupController::class, 'list'])->name('admin.group');
... ...