作者 liyuhang

gx

... ... @@ -158,7 +158,7 @@ class BaseController extends Controller
}
switch ((string) $k) {
case 'image':
$v['image_link'] = file_get_contents($v);
$v['image_link'] = url('/image/' . $v);
break;
}
}
... ...
... ... @@ -87,10 +87,8 @@ class ImageController
public function upload() {
$this->request->validate([
'image'=>['required'],
'type'=>['required'],
],[
'image.required'=>'图片必须填写',
'type.required'=>'类型必须填写',
]);
$files = $this->request->file('image');
if (empty($files)) {
... ... @@ -123,11 +121,12 @@ class ImageController
'path' => $url.$filename,
'created_at' => date('Y-m-d H:i:s',time()),
'size' => $res->getSize(),
'hash' => sha1($url.$filename.date('Y-m-d H:i:s',time())),
'hash' => $files->hashName(),
'mime'=>$files->extension()
];
$imageModel = new ImageModel();
$imageModel->add($data);
return 'image/'.$data['hash'];
return $data['hash'];
}
/**
* 生成缩略图缓存
... ...
... ... @@ -2,6 +2,7 @@
namespace App\Http\Logic\Bside;
use App\Enums\Common\Code;
use App\Exceptions\BsideGlobalException;
use App\Http\Logic\Logic;
use App\Models\Image as ImageModel;
... ... @@ -101,15 +102,19 @@ class BaseLogic extends Logic
$filename = date('ymdHis').rand(10000,99999);
$res = $request->file('image')->move($url,$filename);
if ($res === false) {
return $this->fail($image->getError(), 400);
return $this->fail($image->getError(), Code::USER_ERROR);
}
$data = [
'path' => $url.$filename,
'created_at' => date('Y-m-d H:i:s',time()),
'size' => $res->getSize(),
'hash' => $res->hash(),
'hash' => $image->hashName(),
'mime'=>$image->extension()
];
$imageModel->add($data);
return $data;
$rs = $imageModel->add($data);
if ($rs === false) {
return $this->fail('添加失败', Code::USER_ERROR);
}
return $data['hash'];
}
}
... ...
... ... @@ -4,6 +4,7 @@ namespace App\Http\Logic\Bside;
use App\Enums\Common\Code;
use App\Models\User;
use Illuminate\Support\Facades\DB;
class UserLogic extends BaseLogic
{
... ... @@ -28,6 +29,10 @@ class UserLogic extends BaseLogic
if($info !== false){
$this->fail('error',Code::USER_ERROR);
}
//上传图片
if(isset($this->param['image'])){
$this->param['image'] = $this->upload();
}
//密码加密
$this->param['password'] = base64_encode(md5($this->param['password']));
$rs = $this->model->add($this->param);
... ... @@ -51,6 +56,15 @@ 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();
}
$this->param['image'] = $this->upload();
}
$rs = $this->model->edits($this->param);
if($rs === false){
$this->fail('参数错误或其他服务器原因,编辑失败',Code::USER_ERROR,[]);
... ...
... ... @@ -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}/', [\App\Http\Controllers\ImageController::class,'index'])->name('image_show');
Route::get('/image/{hash}/{w}/{h}', [\App\Http\Controllers\ImageController::class,'index'])->name('image_show');
});
... ...