作者 liyuhang

gx

... ... @@ -6,6 +6,7 @@ use App\Enums\Common\Code;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Logic\Bside\Blog\BlogCategoryLogic;
use App\Http\Requests\Bside\Blog\BlogCategoryRequest;
use App\Models\Blog\Blog as BlogModel;
use App\Models\Blog\BlogCategory as BlogCategoryModel;
class BlogCategoryController extends BaseController
... ... @@ -21,6 +22,13 @@ class BlogCategoryController extends BaseController
$this->map['project_id'] = $this->user['project_id'];
$lists = $blogCategoryModel->lists($this->map,$this->page,$this->row,$this->order,
['id','pid','name','num','status','sort','remark','created_at','updated_at']);
if(!empty($lists['list'])){
$blogModel = new BlogModel();
foreach ($lists['list'] as $k => $v){
$v['num'] = $blogModel->formatQuery(['category_id'=>['like','%,' . $v['id'] . ',%']])->count();
$lists['list'][$k] = $v;
}
}
$this->response('success',Code::SUCCESS,$lists);
}
... ...
... ... @@ -6,8 +6,8 @@ use App\Enums\Common\Code;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Logic\Bside\News\NewsCategoryLogic;
use App\Http\Requests\Bside\News\NewsCategoryRequest;
use App\Models\News\News as NewsModel;
use App\Models\News\NewsCategory as NewsCategoryModel;
use Illuminate\Http\Request;
class NewsCategoryController extends BaseController
{
... ... @@ -22,6 +22,13 @@ class NewsCategoryController extends BaseController
$this->map['project_id'] = $this->user['project_id'];
$lists = $newsCategory->lists($this->map,$this->page,$this->row,$this->order,
['id','pid','name','num','status','sort','remark','created_at','updated_at']);
if(!empty($lists['list'])){
$newsModel = new NewsModel();
foreach ($lists['list'] as $k => $v){
$v['num'] = $newsModel->formatQuery(['category_id'=>['like','%,' . $v['id'] . ',%']])->count();
$lists['list'][$k] = $v;
}
}
$this->response('success',Code::SUCCESS,$lists);
}
... ...
... ... @@ -14,13 +14,6 @@ use Intervention\Image\Facades\Image;
class ImageController
{
public $upload_img = [
//验证
'rule' => [
'size' => 3 * 1024 * 1024, //大小限制
'ext' => 'jpeg,jpg,png,gif', //文件类型限制
],
//生成文件规则
'savename' => 'uniqid',
//设置静态缓存参数(304)
'header' => [
'Cache-Control' => 'max-age=2592000',
... ... @@ -30,16 +23,17 @@ class ImageController
'Last-Modified' => "%Last-Modified%",
'Content-Description' => 'File Transfer',
],
//图片保存根路径
'path' => './uploads/images/',
//图片上传变量名
'name' => 'image',
];
public $path = '';
public $thr_path = '';
public $request = '';
public function __construct(Request $request)
{
$this->request = $request;
$this->path = config('filesystems.disks')['upload']['root'].config('upload');
}
public function index($hash = '', $w = 0 ,$h = 0){
... ... @@ -53,7 +47,7 @@ class ImageController
$this->response('指定图片不存在!', Code::USER_ERROR);
}
//查看缩略图是否存在
$filename = './../uploads/images/cache_'. $info['hash'] . $w . '_' . $h;
$filename = $this->path . $info['hash'] . $w . '_' . $h;
if(is_file($filename)){
$last_modified_time = gmdate(time() + ((30 * 60 * 60 * 24))) . " GMT";
$header = str_replace(['%Expires%', "%etag%", '%Last-Modified%'],
... ... @@ -111,7 +105,7 @@ class ImageController
*/
public function single($files){
$hash = hash_file('md5', $files->getPathname());
$url = './../uploads/images/';
$url = $this->path;
$filename = date('ymdHis').rand(10000,99999);
$res = $this->request->file('image')->move($url,$filename);
if ($res === false) {
... ... @@ -140,7 +134,7 @@ class ImageController
*/
private function cacheImage($info, $w, $h) {
$path = $info['path'];
$filename = './../uploads/images/cache_'. $info['hash'] . $w . '_' . $h;
$filename = $this->path . $info['hash'] . $w . '_' . $h;
Image::make($path)->resize($w, $h)->save($filename);
return $filename;
}
... ... @@ -158,7 +152,7 @@ class ImageController
$data = [];
foreach ($files as $file) {
$hash = hash_file('md5', $file->getPathname());
$url = './../uploads/images/';
$url = $this->path;
$filename = date('ymdHis').rand(10000,99999);
$res = $file->move($url,$filename);
if ($res === false) {
... ...
... ... @@ -67,9 +67,9 @@ class BlogCategoryLogic extends BaseLogic
if ($cate_info === false) {
//查看当前上一级分类下是否有新闻
$blogModel = new BlogModel();
$blog_info = $blogModel->read(['category_id' => ['like', ',' . $this->param['pid'] . ',']]);
if ($blog_info !== false) {
$replacement = ',' . $cate_id . ',';
$blog_count = $blogModel->where('category_id','like', '%,' . $this->param['pid'] . ',%')->count();
if ($blog_count > 0) {
$replacement = ','. $this->param['pid'] . ',' . $cate_id . ',';
$old = ',' . $this->param['pid'] . ',';
//更新所有商品到当前分类
DB::table('gl_Blog')->where('category_id', 'like', '%' . $old . '%')
... ...
... ... @@ -75,9 +75,6 @@ class BlogLogic extends BaseLogic
$this->param['image'] = $data;
}
$rs = $this->model->insertGetId($this->param);
//分类计数
$category_data = explode(',',trim($this->param['category_id'],','));
$this->set_num(BlogCategoryModel::class,$category_data);
RouteMap::setRoute($this->param['url'], RouteMap::SOURCE_BLOG, $rs, $this->user['project_id']);
DB::commit();
}catch (\Exception $e){
... ... @@ -106,11 +103,6 @@ class BlogLogic extends BaseLogic
$this->param['operator_id'] = $this->user['id'];
DB::beginTransaction();
try {
$info = $this->model->read(['id'=>$this->param['id']],['id','image','category_id']);
//查看分类是否有更新
if($info['category_id'] !== $this->param['category_id']){
$this->update_category_num($info['category_id'],$this->param['category_id']);
}
//是否有图片更新
if(isset($this->param['image']) && is_file($this->param['image'])){
//查看当前用户是否已有头像
... ... @@ -183,7 +175,7 @@ class BlogLogic extends BaseLogic
$this->param['operator_id'] = $this->user['id'];
$rs = $this->model->edit($this->param,['id'=>$this->param['id']]);
if($rs === false){
$this->fail('error',Code::USER_ERROR);
$this->fail('error');
}
return $this->success();
}
... ... @@ -196,51 +188,15 @@ class BlogLogic extends BaseLogic
*/
public function blog_del(){
$ids = $this->param['id'];
//获取所有博客的分类id
$str = $this->get_category_id($ids);
DB::beginTransaction();
try {
$category_data = explode(',',$str);
//分类计数减1
$this->set_num(BlogCategoryModel::class,$category_data,'del');
$this->param['id'] = ['in',$this->param['id']];
$this->del($this->param,$ids);
DB::commit();
}catch (Exception $e){
DB::rollBack();
$this->fail('当前数据不存在');
$this->fail('error');
}
return $this->success();
}
/**
* @param $ids
* @name :删除数据时获取所有数据的分类id
* @return void
* @author :liyuhang
* @method
*/
public function get_category_id($ids = []){
$str = '';
$list = $this->model->list(['id'=>['in',$ids]],'id',['id','category_id']);
foreach ($list as $v){
$str .= trim($v['category_id'],',').',';
}
return trim($str,',');
}
/**
* @name :编辑分类时更新分类计数
* @return void
* @author :liyuhang
* @method
*/
public function update_category_num($category_del = '',$category_add = ''){
//分类计数(减)
$category_del_data = explode(',',trim($category_del,','));
$this->set_num(BlogCategoryModel::class,$category_del_data);
//分类计数(加)
$category_add_data = explode(',',trim($category_add,','));
$this->set_num(BlogCategoryModel::class,$category_add_data,'del');
return true;
}
}
... ...
... ... @@ -77,15 +77,13 @@ class NewsCategoryLogic extends BaseLogic
if ($cate_info === false) {
//查看当前上一级分类下是否有新闻
$newsModel = new NewsModel();
$news_count = $newsModel->formatQuery(['category_id' => ['like', ',' . $this->param['pid'] . ',']])->count();
$news_count = $newsModel->where('category_id','like', '%,' . $this->param['pid'] . ',%')->count();
if ($news_count > 0) {
$replacement = ','. $this->param['pid'] .','. $cate_id . ',';
$old = ',' . $this->param['pid'] . ',';
//更新所有商品到当前分类
DB::table('gl_news')->where('category_id', 'like', '%' . $old . '%')
->update(['category_id' => DB::raw("REPLACE(category_id, '$old', '$replacement')")]);
//更新计数
$this->set_num($this->model,$cate_id,'add',$news_count);
}
}
}
... ... @@ -106,10 +104,10 @@ class NewsCategoryLogic extends BaseLogic
public function edit_news_category(){
$condition = [
'id'=>['!=',$this->param['id']],
'name'=>$this->param['name']
'name'=>$this->param['name'],
];
//查看当前分类名称是否存在
$info = $this->model->read($condition);
$info = $this->model->read($condition,['id']);
if($info !== false){
$this->fail('当前分类名称已存在');
}
... ...
... ... @@ -67,9 +67,6 @@ class NewsLogic extends BaseLogic
$this->param['image'] = $data;
}
$rs = $this->model->insertGetId($this->param);
//产品计数
$category_data = explode(',',trim($this->param['category_id'],','));
$this->set_num(NewsCategoryModel::class,$category_data);
RouteMap::setRoute($this->param['url'], RouteMap::SOURCE_NEWS, $rs, $this->user['project_id']);
DB::commit();
}catch (\Exception $e){
... ... @@ -95,15 +92,8 @@ class NewsLogic extends BaseLogic
$this->fail('当前名称已存在');
}
$this->param['operator_id'] = $this->user['id'];
//多个分类按逗号隔开
$this->param['category_id'] = $this->param['category_id'];
DB::beginTransaction();
try {
$info = $this->model->read(['id'=>$this->param['id']],['id','image','category_id']);
//查看分类是否有跟新
if($info['category_id'] !== $this->param['category_id']){
$this->update_category_num($info['category_id'],$this->param['category_id']);
}
//上传图片
if(isset($this->param['image']) && is_file($this->param['image'])){
//查看当前用户是否已有头像
... ... @@ -188,13 +178,8 @@ class NewsLogic extends BaseLogic
*/
public function news_del(){
$ids = $this->param['id'];
//获取当前新闻下的所有新闻分类id
$str = $this->get_category_id($ids);
DB::beginTransaction();
try {
$category_data = explode(',',$str);
//减计数
$this->set_num(NewsCategoryModel::class,$category_data,'del');
$this->param['id'] = ['in',$this->param['id']];
$this->del($this->param,$ids);
DB::commit();
... ... @@ -205,35 +190,4 @@ class NewsLogic extends BaseLogic
return $this->success();
}
/**
* @param $ids
* @name :删除数据时获取所有数据的分类id
* @return void
* @author :liyuhang
* @method
*/
public function get_category_id($ids){
$str = '';
$list = $this->model->list(['id'=>['in',$ids]],'id',['id','category_id']);
foreach ($list as $v){
$str .= trim($v['category_id'],',').',';
}
return trim($str,',');
}
/**
* @name :编辑分类时更新分类计数
* @return void
* @author :liyuhang
* @method
*/
public function update_category_num($category_del = '',$category_add = ''){
//分类计数(减)
$category_del_data = explode(',',trim($category_del,','));
$this->set_num(NewsCategoryModel::class,$category_del_data);
//分类计数(加)
$category_add_data = explode(',',trim($category_add,','));
$this->set_num(NewsCategoryModel::class,$category_add_data,'del');
return true;
}
}
... ...
... ... @@ -369,10 +369,10 @@ class Logic
* @author :liyuhang
* @method
*/
public function info($map){
public function info($map,$file = ['*']){
$info = CommonHelper::get_user_cache($this->model->getTable(),$map['id']);
if(empty($info)){
$info = $this->model->read($map);
$info = $this->model->read($map,$file);
if($info === false){
$this->fail('当前数据不存在');
}
... ...
... ... @@ -26,7 +26,7 @@ class BlogCategoryRequest extends FormRequest
return [
'name'=>'required|max:100',
'remark'=>'required|max:255',
'alias'=>'required|max:10',
'alias'=>'required|max:10|unique:gl_blog_category,alias',
];
}
... ...
... ... @@ -26,7 +26,7 @@ class NewsCategoryRequest extends FormRequest
return [
'name'=>'required|max:100',
'remark'=>'required|max:255',
'alias'=>'required|max:10',
'alias'=>'required|max:10|unique:gl_news_category,alias',
];
}
... ...
... ... @@ -14,4 +14,25 @@ return [
],
'path' => '/product'
],
//用户头像
'user' =>[
'size' => [
'max' => 1024*1024*2, // 2M
],
'path' => '/user'
],
//博客图
'blog' =>[
'size' => [
'max' => 1024*1024*2, // 2M
],
'path' => '/blog'
],
//新闻图
'news' =>[
'size' => [
'max' => 1024*1024*2, // 2M
],
'path' => '/news'
],
];
... ...