作者 liyuhang

gx

@@ -7,6 +7,7 @@ use App\Models\Image as ImageModel; @@ -7,6 +7,7 @@ use App\Models\Image as ImageModel;
7 use Illuminate\Http\Exceptions\HttpResponseException; 7 use Illuminate\Http\Exceptions\HttpResponseException;
8 use Illuminate\Http\JsonResponse; 8 use Illuminate\Http\JsonResponse;
9 use Illuminate\Http\Request; 9 use Illuminate\Http\Request;
  10 +use Illuminate\Support\Facades\Hash;
10 use Intervention\Image\Facades\Image; 11 use Intervention\Image\Facades\Image;
11 12
12 class ImageController 13 class ImageController
@@ -48,7 +49,7 @@ class ImageController @@ -48,7 +49,7 @@ class ImageController
48 $imageModel = new ImageModel(); 49 $imageModel = new ImageModel();
49 $info = $imageModel->read(['hash'=>$hash]); 50 $info = $imageModel->read(['hash'=>$hash]);
50 if ($info === false) { 51 if ($info === false) {
51 - $this->response('指定图片不存在!', 404); 52 + $this->response('指定图片不存在!', Code::USER_ERROR);
52 } 53 }
53 //查看缩略图是否存在 54 //查看缩略图是否存在
54 $filename = './../uploads/images/cache_'. $info['hash'] . $w . '_' . $h; 55 $filename = './../uploads/images/cache_'. $info['hash'] . $w . '_' . $h;
@@ -60,9 +61,9 @@ class ImageController @@ -60,9 +61,9 @@ class ImageController
60 $header['Content-Length'] = $info['size']; 61 $header['Content-Length'] = $info['size'];
61 return response($content, 200, $header); 62 return response($content, 200, $header);
62 } 63 }
63 - $path = './../'.$info['path']; 64 + $path = $info['path'];
64 if (!is_file($path)) { 65 if (!is_file($path)) {
65 - $this->response('指定图片已被系统删除!', 404); 66 + $this->response('指定图片已被系统删除!', Code::USER_ERROR);
66 } 67 }
67 $content = ''; 68 $content = '';
68 $last_modified_time = gmdate(time() + ((30 * 60 * 60 * 24))) . " GMT"; 69 $last_modified_time = gmdate(time() + ((30 * 60 * 60 * 24))) . " GMT";
@@ -117,12 +118,14 @@ class ImageController @@ -117,12 +118,14 @@ class ImageController
117 if ($res === false) { 118 if ($res === false) {
118 return $this->fail($files->getError(), 400); 119 return $this->fail($files->getError(), 400);
119 } 120 }
  121 +
120 $data = [ 122 $data = [
121 'path' => $url.$filename, 123 'path' => $url.$filename,
122 'created_at' => date('Y-m-d H:i:s',time()), 124 'created_at' => date('Y-m-d H:i:s',time()),
123 'size' => $res->getSize(), 125 'size' => $res->getSize(),
124 - 'hash' => $files->hashName(),  
125 - 'mime'=>$files->extension() 126 + 'hash' => hash_file('md5', $res->getPathname()),
  127 + 'type'=>$files->getClientOriginalExtension(),
  128 +// 'mime'=>$files->getMimeType()
126 ]; 129 ];
127 $imageModel = new ImageModel(); 130 $imageModel = new ImageModel();
128 $imageModel->add($data); 131 $imageModel->add($data);
@@ -136,7 +139,7 @@ class ImageController @@ -136,7 +139,7 @@ class ImageController
136 * @return string 139 * @return string
137 */ 140 */
138 private function cacheImage($info, $w, $h) { 141 private function cacheImage($info, $w, $h) {
139 - $path = './../'.$info['path']; 142 + $path = $info['path'];
140 $filename = './../uploads/images/cache_'. $info['hash'] . $w . '_' . $h; 143 $filename = './../uploads/images/cache_'. $info['hash'] . $w . '_' . $h;
141 Image::make($path)->resize($w, $h)->save($filename); 144 Image::make($path)->resize($w, $h)->save($filename);
142 return $filename; 145 return $filename;
@@ -154,10 +157,9 @@ class ImageController @@ -154,10 +157,9 @@ class ImageController
154 $result = [ 157 $result = [
155 'msg' => $msg == ' ' ? $code->description : $msg, 158 'msg' => $msg == ' ' ? $code->description : $msg,
156 'code' => $code->value, 159 'code' => $code->value,
157 - 'data' => $this->_extents($data), 160 + 'data' => $data,
158 ]; 161 ];
159 $this->header['Content-Type'] = $type; 162 $this->header['Content-Type'] = $type;
160 - $this->header['token'] = $this->token;  
161 $response = response($result,$result_code,$this->header);; 163 $response = response($result,$result_code,$this->header);;
162 throw new HttpResponseException($response); 164 throw new HttpResponseException($response);
163 } 165 }
@@ -108,8 +108,9 @@ class BaseLogic extends Logic @@ -108,8 +108,9 @@ class BaseLogic extends Logic
108 'path' => $url.$filename, 108 'path' => $url.$filename,
109 'created_at' => date('Y-m-d H:i:s',time()), 109 'created_at' => date('Y-m-d H:i:s',time()),
110 'size' => $res->getSize(), 110 'size' => $res->getSize(),
111 - 'hash' => $image->hashName(),  
112 - 'mime'=>$image->extension() 111 + 'hash' => hash_file('md5', $res->getPathname()),
  112 + 'type'=>$image->getClientOriginalExtension(),
  113 +// 'mime'=>$image->getMimeType()
113 ]; 114 ];
114 $rs = $imageModel->add($data); 115 $rs = $imageModel->add($data);
115 if ($rs === false) { 116 if ($rs === false) {
@@ -6,6 +6,7 @@ use App\Enums\Common\Code; @@ -6,6 +6,7 @@ use App\Enums\Common\Code;
6 use App\Http\Logic\Bside\BaseLogic; 6 use App\Http\Logic\Bside\BaseLogic;
7 use App\Models\Blog\Blog; 7 use App\Models\Blog\Blog;
8 use App\Models\Blog\BlogCategory as BlogCategoryModel; 8 use App\Models\Blog\BlogCategory as BlogCategoryModel;
  9 +use App\Models\Image;
9 use App\Models\RouteMap; 10 use App\Models\RouteMap;
10 use Illuminate\Support\Facades\DB; 11 use Illuminate\Support\Facades\DB;
11 12
@@ -55,6 +56,10 @@ class BlogLogic extends BaseLogic @@ -55,6 +56,10 @@ class BlogLogic extends BaseLogic
55 $this->param['project_id'] = $this->user['project_id']; 56 $this->param['project_id'] = $this->user['project_id'];
56 DB::beginTransaction(); 57 DB::beginTransaction();
57 try { 58 try {
  59 + if(isset($this->param['image'])){
  60 + $data = $this->upload();
  61 + $this->param['image'] = $data;
  62 + }
58 $rs = $this->model->insertGetId($this->param); 63 $rs = $this->model->insertGetId($this->param);
59 RouteMap::setRoute($this->param['url'], RouteMap::SOURCE_BLOG, $rs, $this->user['project_id']); 64 RouteMap::setRoute($this->param['url'], RouteMap::SOURCE_BLOG, $rs, $this->user['project_id']);
60 DB::commit(); 65 DB::commit();
@@ -77,8 +82,19 @@ class BlogLogic extends BaseLogic @@ -77,8 +82,19 @@ class BlogLogic extends BaseLogic
77 $this->param['operator_id'] = $this->uid; 82 $this->param['operator_id'] = $this->uid;
78 DB::beginTransaction(); 83 DB::beginTransaction();
79 try { 84 try {
80 - $this->model->edit($this->param,['id'=>$this->param['id']]); 85 + if(isset($this->param['image'])){
  86 + //查看当前用户是否已有头像
  87 + $info = $this->model->read(['id'=>$this->param['id']],['id','hash']);
  88 + if($info !== false && !empty($info['hash'])){
  89 + //TODO::删除资源
  90 + $imageModel = new Image();
  91 + $image_info = $imageModel->read(['hash'=>$info['hash']],['id','path']);
  92 + shell_exec('rm -rf '.$image_info['path'] .'./../uploads/images/cache_'. $info['hash'] . '*');
  93 + }
  94 + $this->param['image'] = $this->upload();
  95 + }
81 RouteMap::setRoute($this->param['url'], RouteMap::SOURCE_BLOG, $this->param['id'], $this->user['project_id']); 96 RouteMap::setRoute($this->param['url'], RouteMap::SOURCE_BLOG, $this->param['id'], $this->user['project_id']);
  97 + $this->model->edit($this->param,['id'=>$this->param['id']]);
82 DB::commit(); 98 DB::commit();
83 }catch (\Exception $e){ 99 }catch (\Exception $e){
84 DB::rollBack(); 100 DB::rollBack();
@@ -4,6 +4,7 @@ namespace App\Http\Logic\Bside\News; @@ -4,6 +4,7 @@ namespace App\Http\Logic\Bside\News;
4 4
5 use App\Enums\Common\Code; 5 use App\Enums\Common\Code;
6 use App\Http\Logic\Bside\BaseLogic; 6 use App\Http\Logic\Bside\BaseLogic;
  7 +use App\Models\Image;
7 use App\Models\News\News; 8 use App\Models\News\News;
8 use App\Models\News\NewsCategory as NewsCategoryModel; 9 use App\Models\News\NewsCategory as NewsCategoryModel;
9 use App\Models\RouteMap; 10 use App\Models\RouteMap;
@@ -59,7 +60,7 @@ class NewsLogic extends BaseLogic @@ -59,7 +60,7 @@ class NewsLogic extends BaseLogic
59 try { 60 try {
60 if(isset($this->param['image'])){ 61 if(isset($this->param['image'])){
61 $data = $this->upload(); 62 $data = $this->upload();
62 - $this->param['image'] = $data['path']; 63 + $this->param['image'] = $data;
63 } 64 }
64 $rs = $this->model->insertGetId($this->param); 65 $rs = $this->model->insertGetId($this->param);
65 RouteMap::setRoute($this->param['url'], RouteMap::SOURCE_NEWS, $rs, $this->user['project_id']); 66 RouteMap::setRoute($this->param['url'], RouteMap::SOURCE_NEWS, $rs, $this->user['project_id']);
@@ -83,9 +84,26 @@ class NewsLogic extends BaseLogic @@ -83,9 +84,26 @@ class NewsLogic extends BaseLogic
83 $this->param['operator_id'] = $this->user['id']; 84 $this->param['operator_id'] = $this->user['id'];
84 //多个分类按逗号隔开 85 //多个分类按逗号隔开
85 $this->param['category_id'] = ','.$this->param['category_id'].','; 86 $this->param['category_id'] = ','.$this->param['category_id'].',';
86 - $rs = $this->model->edit($this->param,['id'=>$this->param['id']]);  
87 - if($rs === false){  
88 - $this->fail('error',Code::USER_ERROR); 87 + DB::beginTransaction();
  88 + try {
  89 + //上传图片
  90 + if(isset($this->param['image'])){
  91 + //查看当前用户是否已有头像
  92 + $info = $this->model->read(['id'=>$this->param['id']],['id','hash']);
  93 + if($info !== false && !empty($info['hash'])){
  94 + //TODO::删除资源
  95 + $imageModel = new Image();
  96 + $image_info = $imageModel->read(['hash'=>$info['hash']],['id','path']);
  97 + shell_exec('rm -rf '.$image_info['path'] .'./../uploads/images/cache_'. $info['hash'] . '*');
  98 + }
  99 + $this->param['image'] = $this->upload();
  100 + }
  101 + RouteMap::setRoute($this->param['url'], RouteMap::SOURCE_NEWS, $this->param['id'], $this->user['project_id']);
  102 + $this->model->edit($this->param,['id'=>$this->param['id']]);
  103 + DB::commit();
  104 + }catch (\exception $e){
  105 + DB::rollBack();
  106 + $this->fail('参数错误或其他服务器原因,编辑失败',Code::USER_ERROR,[]);
89 } 107 }
90 //TODO::写入日志 108 //TODO::写入日志
91 $this->success(); 109 $this->success();
@@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
3 namespace App\Http\Logic\Bside; 3 namespace App\Http\Logic\Bside;
4 4
5 use App\Enums\Common\Code; 5 use App\Enums\Common\Code;
  6 +use App\Models\Image;
6 use App\Models\User; 7 use App\Models\User;
7 use Illuminate\Support\Facades\DB; 8 use Illuminate\Support\Facades\DB;
8 9
@@ -56,17 +57,21 @@ class UserLogic extends BaseLogic @@ -56,17 +57,21 @@ class UserLogic extends BaseLogic
56 $this->fail('当前编辑的手机号码已存在',Code::USER_PARAMS_ERROE); 57 $this->fail('当前编辑的手机号码已存在',Code::USER_PARAMS_ERROE);
57 } 58 }
58 $this->param['operator_id'] = $this->user['id']; 59 $this->param['operator_id'] = $this->user['id'];
59 - //上传图片  
60 - if(isset($this->param['image'])){  
61 - //查看当前用户是否已有头像  
62 - $info = $this->model->read(['id'=>$this->param['id']],'hash');  
63 - if($info !== false){  
64 - DB::table('gl_image')->where(['hash'=>$info['hash']])->first(); 60 + try {
  61 + //上传图片
  62 + if(isset($this->param['image'])){
  63 + //查看当前用户是否已有头像
  64 + $info = $this->model->read(['id'=>$this->param['id']],['id','hash']);
  65 + if($info !== false && !empty($info['hash'])){
  66 + //TODO::删除资源
  67 + $imageModel = new Image();
  68 + $image_info = $imageModel->read(['hash'=>$info['hash']],['id','path']);
  69 + shell_exec('rm -rf '.$image_info['path'] .'./../uploads/images/cache_'. $info['hash'] . '*');
  70 + }
  71 + $this->param['image'] = $this->upload();
  72 + $this->model->edits($this->param);
65 } 73 }
66 - $this->param['image'] = $this->upload();  
67 - }  
68 - $rs = $this->model->edits($this->param);  
69 - if($rs === false){ 74 + }catch (\exception $e){
70 $this->fail('参数错误或其他服务器原因,编辑失败',Code::USER_ERROR,[]); 75 $this->fail('参数错误或其他服务器原因,编辑失败',Code::USER_ERROR,[]);
71 } 76 }
72 return $this->success(); 77 return $this->success();
@@ -164,5 +164,5 @@ Route::middleware(['bloginauth'])->group(function () { @@ -164,5 +164,5 @@ Route::middleware(['bloginauth'])->group(function () {
164 Route::group([], function () { 164 Route::group([], function () {
165 Route::any('/login', [\App\Http\Controllers\Bside\ComController::class, 'login'])->name('login'); 165 Route::any('/login', [\App\Http\Controllers\Bside\ComController::class, 'login'])->name('login');
166 Route::get('/file/download', [\App\Http\Controllers\Bside\FileController::class, 'download'])->name('file_download'); 166 Route::get('/file/download', [\App\Http\Controllers\Bside\FileController::class, 'download'])->name('file_download');
167 - Route::get('/image/{hash}/{w}/{h}', [\App\Http\Controllers\ImageController::class,'index'])->name('image_show'); 167 + Route::any('/image/{hash}/{w}/{h}', [\App\Http\Controllers\ImageController::class,'index'])->name('image_show');
168 }); 168 });