作者 赵彬吉

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

... ... @@ -158,7 +158,7 @@ class BaseController extends Controller
}
switch ((string) $k) {
case 'image':
$v['image_link'] = url('/image/' . $v);
$data['image_link'] = url('/b/image/' . $v);
break;
}
}
... ...
... ... @@ -26,11 +26,9 @@ class ComController extends BaseController
'mobile'=>['required'],
'password'=>['required'],
],[
'mobile.required'=>'标题必须填写',
'mobile.string'=>'标题中含有非法文字',
'mobile.required'=>'电话号码必须填写',
'password.required'=>'内容必须填写',
'password.string'=>'内容中含有非法文字',
'mobile.max' => 'account不大于12字符.',
'mobile.max' => 'mobile不大于12字符.',
]);
$userModel = new UserModel();
$res = $userModel->login($this->param);
... ...
... ... @@ -7,6 +7,7 @@ use App\Models\Image as ImageModel;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Intervention\Image\Facades\Image;
class ImageController
... ... @@ -48,7 +49,7 @@ class ImageController
$imageModel = new ImageModel();
$info = $imageModel->read(['hash'=>$hash]);
if ($info === false) {
$this->response('指定图片不存在!', 404);
$this->response('指定图片不存在!', Code::USER_ERROR);
}
//查看缩略图是否存在
$filename = './../uploads/images/cache_'. $info['hash'] . $w . '_' . $h;
... ... @@ -58,23 +59,23 @@ class ImageController
[$last_modified_time, $hash . ':' . $w . '_' . $h . '_' . 2, $last_modified_time], $this->upload_img['header']);
$content = file_get_contents($filename);
$header['Content-Length'] = $info['size'];
return response($content, 200, $header);
}
$path = './../'.$info['path'];
if (!is_file($path)) {
$this->response('指定图片已被系统删除!', 404);
}
$content = '';
$last_modified_time = gmdate(time() + ((30 * 60 * 60 * 24))) . " GMT";
$header = str_replace(['%Expires%', "%etag%", '%Last-Modified%'],
[$last_modified_time, $hash . ':' . $w . '_' . $h . '_' . 2, $last_modified_time], $this->upload_img['header']);
if ($w > 0 && $h > 0) {
$path = $this->cacheImage($info, $w, $h);
$content = file_get_contents($path);
$header['Content-Length'] = strlen($content);
} else {
$content = file_get_contents($path);
$header['Content-Length'] = $info['size'];
}else{
$path = $info['path'];
if (!is_file($path)) {
$this->response('指定图片已被系统删除!', Code::USER_ERROR);
}
$content = '';
$last_modified_time = gmdate(time() + ((30 * 60 * 60 * 24))) . " GMT";
$header = str_replace(['%Expires%', "%etag%", '%Last-Modified%'],
[$last_modified_time, $hash . ':' . $w . '_' . $h . '_' . 2, $last_modified_time], $this->upload_img['header']);
if ($w > 0 && $h > 0) {
$path = $this->cacheImage($info, $w, $h);
$content = file_get_contents($path);
$header['Content-Length'] = strlen($content);
} else {
$content = file_get_contents($path);
$header['Content-Length'] = $info['size'];
}
}
$img_type = $info['type'];
$content = base64_encode($content);
... ... @@ -117,12 +118,14 @@ class ImageController
if ($res === false) {
return $this->fail($files->getError(), 400);
}
$data = [
'path' => $url.$filename,
'created_at' => date('Y-m-d H:i:s',time()),
'size' => $res->getSize(),
'hash' => $files->hashName(),
'mime'=>$files->extension()
'hash' => hash_file('md5', $res->getPathname()),
'type'=>$files->getClientOriginalExtension(),
// 'mime'=>$files->getMimeType()
];
$imageModel = new ImageModel();
$imageModel->add($data);
... ... @@ -136,7 +139,7 @@ class ImageController
* @return string
*/
private function cacheImage($info, $w, $h) {
$path = './../'.$info['path'];
$path = $info['path'];
$filename = './../uploads/images/cache_'. $info['hash'] . $w . '_' . $h;
Image::make($path)->resize($w, $h)->save($filename);
return $filename;
... ... @@ -154,10 +157,9 @@ class ImageController
$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;
$response = response($result,$result_code,$this->header);;
throw new HttpResponseException($response);
}
... ...
... ... @@ -19,12 +19,14 @@ class BaseLogic extends Logic
protected $param;
protected $request;
protected $user;
public function __construct()
{
$this->request = request();
$this->requestAll = request()->all();
$this->user = Cache::get(request()->header('token'));
}
... ... @@ -93,14 +95,14 @@ class BaseLogic extends Logic
* @author :liyuhang
* @method
*/
public function upload(Request $request,ImageModel $imageModel){
$image = $request->file('image');
public function upload(){
$image = $this->request->file('image');
if(empty($image)){
return $this->fail('没有上传图片',Code::USER_ERROR);
}
$url = './../uploads/images/';
$filename = date('ymdHis').rand(10000,99999);
$res = $request->file('image')->move($url,$filename);
$res = $this->request->file('image')->move($url,$filename);
if ($res === false) {
return $this->fail($image->getError(), Code::USER_ERROR);
}
... ... @@ -108,9 +110,11 @@ class BaseLogic extends Logic
'path' => $url.$filename,
'created_at' => date('Y-m-d H:i:s',time()),
'size' => $res->getSize(),
'hash' => $image->hashName(),
'mime'=>$image->extension()
'hash' => hash_file('md5', $res->getPathname()),
'type'=>$image->getClientOriginalExtension(),
// 'mime'=>$image->getMimeType()
];
$imageModel = new ImageModel();
$rs = $imageModel->add($data);
if ($rs === false) {
return $this->fail('添加失败', Code::USER_ERROR);
... ...
... ... @@ -6,6 +6,7 @@ use App\Enums\Common\Code;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\Blog\Blog;
use App\Models\Blog\BlogCategory as BlogCategoryModel;
use App\Models\Image;
use App\Models\RouteMap;
use Illuminate\Support\Facades\DB;
... ... @@ -50,12 +51,19 @@ class BlogLogic extends BaseLogic
* @method
*/
public function blog_add(){
$this->param['create_id'] = $this->uid;
$this->param['operator_id'] = $this->uid;
$this->param['create_id'] = $this->user['id'];
$this->param['operator_id'] = $this->user['id'];
$this->param['project_id'] = $this->user['project_id'];
$this->param['created_at'] = date('Y-m-d H:i:s',time());
$this->param['updated_at'] = date('Y-m-d H:i:s',time());
$this->param['category_id'] = ','.$this->param['category_id'].',';
DB::beginTransaction();
try {
$rs = $this->model->insertGetId($this->param);
if(isset($this->param['image'])){
$data = $this->upload();
$this->param['image'] = $data;
}
$this->model->insertGetId($this->param);
RouteMap::setRoute($this->param['url'], RouteMap::SOURCE_BLOG, $rs, $this->user['project_id']);
DB::commit();
}catch (\Exception $e){
... ... @@ -77,8 +85,19 @@ class BlogLogic extends BaseLogic
$this->param['operator_id'] = $this->uid;
DB::beginTransaction();
try {
$this->model->edit($this->param,['id'=>$this->param['id']]);
if(isset($this->param['image']) && is_file($this->param['image'])){
//查看当前用户是否已有头像
$info = $this->model->read(['id'=>$this->param['id']],['id','hash']);
if($info !== false && !empty($info['hash'])){
//TODO::删除资源
$imageModel = new Image();
$image_info = $imageModel->read(['hash'=>$info['hash']],['id','path']);
shell_exec('rm -rf '.$image_info['path'] .'./../uploads/images/cache_'. $info['hash'] . '*');
}
$this->param['image'] = $this->upload();
}
RouteMap::setRoute($this->param['url'], RouteMap::SOURCE_BLOG, $this->param['id'], $this->user['project_id']);
$this->model->edit($this->param,['id'=>$this->param['id']]);
DB::commit();
}catch (\Exception $e){
DB::rollBack();
... ...
... ... @@ -4,6 +4,7 @@ namespace App\Http\Logic\Bside\News;
use App\Enums\Common\Code;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\Image;
use App\Models\News\News;
use App\Models\News\NewsCategory as NewsCategoryModel;
use App\Models\RouteMap;
... ... @@ -52,22 +53,26 @@ class NewsLogic extends BaseLogic
* @method
*/
public function news_add(){
$this->param['create_id'] = $this->uid;
$this->param['operator_id'] = $this->uid;
$this->param['create_id'] = $this->user['id'];
$this->param['operator_id'] = $this->user['id'];
$this->param['project_id'] = $this->user['project_id'];
$this->param['created_at'] = date('Y-m-d H:i:s',time());
$this->param['updated_at'] = date('Y-m-d H:i:s',time());
$this->param['category_id'] = ','.$this->param['category_id'].',';
DB::beginTransaction();
try {
if(isset($this->param['image'])){
$data = $this->upload();
$this->param['image'] = $data['path'];
$this->param['image'] = $data;
}
$rs = $this->model->insertGetId($this->param);
RouteMap::setRoute($this->param['url'], RouteMap::SOURCE_NEWS, $rs, $this->user['project_id']);
DB::commit();
}catch (\Exception $e){
DB::rollBack();
$this->fail('error',Code::USER_ERROR);
$this->fail('添加失败',Code::USER_ERROR);
}
//TODO::写入日志
$this->success();
}
... ... @@ -83,9 +88,26 @@ class NewsLogic extends BaseLogic
$this->param['operator_id'] = $this->user['id'];
//多个分类按逗号隔开
$this->param['category_id'] = ','.$this->param['category_id'].',';
$rs = $this->model->edit($this->param,['id'=>$this->param['id']]);
if($rs === false){
$this->fail('error',Code::USER_ERROR);
DB::beginTransaction();
try {
//上传图片
if(isset($this->param['image']) && is_file($this->param['image'])){
//查看当前用户是否已有头像
$info = $this->model->read(['id'=>$this->param['id']],['id','hash']);
if($info !== false && !empty($info['hash'])){
//TODO::删除资源
$imageModel = new Image();
$image_info = $imageModel->read(['hash'=>$info['hash']],['id','path']);
shell_exec('rm -rf '.$image_info['path'] .'./../uploads/images/cache_'. $info['hash'] . '*');
}
$this->param['image'] = $this->upload();
}
RouteMap::setRoute($this->param['url'], RouteMap::SOURCE_NEWS, $this->param['id'], $this->user['project_id']);
$this->model->edit($this->param,['id'=>$this->param['id']]);
DB::commit();
}catch (\exception $e){
DB::rollBack();
$this->fail('参数错误或其他服务器原因,编辑失败',Code::USER_ERROR,[]);
}
//TODO::写入日志
$this->success();
... ...
... ... @@ -3,6 +3,7 @@
namespace App\Http\Logic\Bside;
use App\Enums\Common\Code;
use App\Models\Image;
use App\Models\User;
use Illuminate\Support\Facades\DB;
... ... @@ -56,17 +57,21 @@ class UserLogic extends BaseLogic
$this->fail('当前编辑的手机号码已存在',Code::USER_PARAMS_ERROE);
}
$this->param['operator_id'] = $this->user['id'];
//上传图片
if(isset($this->param['image'])){
//查看当前用户是否已有头像
$info = $this->model->read(['id'=>$this->param['id']],'hash');
if($info !== false){
DB::table('gl_image')->where(['hash'=>$info['hash']])->first();
try {
//上传图片
if(isset($this->param['image'])){
//查看当前用户是否已有头像
$info = $this->model->read(['id'=>$this->param['id']],['id','hash']);
if($info !== false && !empty($info['hash'])){
//TODO::删除资源
$imageModel = new Image();
$image_info = $imageModel->read(['hash'=>$info['hash']],['id','path']);
shell_exec('rm -rf '.$image_info['path'] .'./../uploads/images/cache_'. $info['hash'] . '*');
}
$this->param['image'] = $this->upload();
$this->model->edits($this->param);
}
$this->param['image'] = $this->upload();
}
$rs = $this->model->edits($this->param);
if($rs === false){
}catch (\exception $e){
$this->fail('参数错误或其他服务器原因,编辑失败',Code::USER_ERROR,[]);
}
return $this->success();
... ...
... ... @@ -164,5 +164,5 @@ Route::middleware(['bloginauth'])->group(function () {
Route::group([], function () {
Route::any('/login', [\App\Http\Controllers\Bside\ComController::class, 'login'])->name('login');
Route::get('/file/download', [\App\Http\Controllers\Bside\FileController::class, 'download'])->name('file_download');
Route::get('/image/{hash}/{w}/{h}', [\App\Http\Controllers\ImageController::class,'index'])->name('image_show');
Route::any('/image/{hash}/{w?}/{h?}', [\App\Http\Controllers\ImageController::class,'index'])->name('image_show');
});
... ...