From 8b2e8fe33cfbb8487cd4765512a723085932f25f Mon Sep 17 00:00:00 2001 From: sugar1569 Date: Sat, 27 Oct 2018 19:25:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8E=E5=8F=B0=E6=96=87=E7=AB=A0=E7=AE=A1?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/controller/article/Article.php | 194 ++++++++++++++++++ .../controller/article/ArticleCategory.php | 1 + 2 files changed, 195 insertions(+) create mode 100644 application/admin/controller/article/Article.php create mode 100644 application/admin/controller/article/ArticleCategory.php diff --git a/application/admin/controller/article/Article.php b/application/admin/controller/article/Article.php new file mode 100644 index 00000000..c14c6898 --- /dev/null +++ b/application/admin/controller/article/Article.php @@ -0,0 +1,194 @@ +request); + if($cid) + $where['cid'] = $cid; + else + $where['cid'] = ''; + $this->assign('where',$where); + $where['merchant'] = 0;//区分是管理员添加的图文显示 0 还是 商户添加的图文显示 1 + $this->assign('cid',$cid); + $this->assign('cate',ArticleCategoryModel::getTierList()); + $this->assign(ArticleModel::getAll($where)); + return $this->fetch(); + } + + /** + * 展示页面 添加和删除 + * @return mixed + */ + public function create(){ + $id = input('id'); + $cid = input('cid'); + $news = array(); + $news['id'] = ''; + $news['image_input'] = ''; + $news['title'] = ''; + $news['author'] = ''; + $news['is_banner'] = ''; + $news['is_hot'] = ''; + $news['content'] = ''; + $news['synopsis'] = ''; + $news['url'] = ''; + $news['cid'] = array(); + if($id){ + $news = ArticleModel::where('n.id',$id)->alias('n')->field('n.*,c.content')->join('ArticleContent c','c.nid=n.id')->find(); + if(!$news) return $this->failedNotice('数据不存在!'); + $news['cid'] = explode(',',$news['cid']); + } + $all = array(); + $select = 0; + if(!$cid) + $cid = ''; + else { + if($id){ + $all = ArticleCategoryModel::where('id',$cid)->where('hidden','neq',0)->column('id,title'); + $select = 1; + }else{ + $all = ArticleCategoryModel::where('id',$cid)->column('id,title'); + $select = 1; + } + + } + if(empty($all)){ + $select = 0; + $list = ArticleCategoryModel::getTierList(); + $all = []; + foreach ($list as $menu){ + $all[$menu['id']] = $menu['html'].$menu['title']; + } + } + $this->assign('all',$all); + $this->assign('news',$news); + $this->assign('cid',$cid); + $this->assign('select',$select); + return $this->fetch(); + } + + /** + * 上传图文图片 + * @return \think\response\Json + */ + public function upload_image(){ + $res = Upload::Image($_POST['file'],'wechat/image/'.date('Ymd')); + //产品图片上传记录 + $fileInfo = $res->fileInfo->getinfo(); + SystemAttachment::attachmentAdd($res->fileInfo->getSaveName(),$fileInfo['size'],$fileInfo['type'],$res->dir,'',5); + if(!$res->status) return Json::fail($res->error); + return Json::successful('上传成功!',['url'=>$res->filePath]); + } + + /** + * 添加和修改图文 + * @param Request $request + * @return \think\response\Json + */ + public function add_new(Request $request){ + $post = $request->post(); + $data = Util::postMore([ + ['id',0], + ['cid',[]], + 'title', + 'author', + 'image_input', + 'content', + 'synopsis', + 'share_title', + 'share_synopsis', + 'is_banner', + 'is_hot', + ['visit',0], + ['sort',0], + 'url', + ['status',1],],$request); + $data['cid'] = implode(',',$data['cid']); + $content = $data['content']; + unset($data['content']); + if($data['id']){ + $id = $data['id']; + unset($data['id']); + ArticleModel::beginTrans(); + $res1 = ArticleModel::edit($data,$id,'id'); + $res2 = ArticleModel::setContent($id,$content); + if($res1 && $res2) + $res = true; + else + $res =false; +// dump($res); +// exit(); + ArticleModel::checkTrans($res); + if($res) + return Json::successful('修改图文成功!',$id); + else + return Json::fail('修改图文失败!',$id); + }else{ + $data['add_time'] = time(); + $data['admin_id'] = $this->adminId; + ArticleModel::beginTrans(); + $res1 = ArticleModel::set($data); + $res2 = false; + if($res1) + $res2 = ArticleModel::setContent($res1->id,$content); + if($res1 && $res2) + $res = true; + else + $res =false; + ArticleModel::checkTrans($res); + if($res) + return Json::successful('添加图文成功!',$res1->id); + else + return Json::successful('添加图文失败!',$res1->id); + } + } + + /** + * 删除图文 + * @param $id + * @return \think\response\Json + */ + public function delete($id) + { + $res = ArticleModel::del($id); + if(!$res) + return Json::fail('删除失败,请稍候再试!'); + else + return Json::successful('删除成功!'); + } + + public function merchantIndex(){ + $where = Util::getMore([ + ['title',''] + ],$this->request); + $this->assign('where',$where); + $where['cid'] = input('cid'); + $where['merchant'] = 1;//区分是管理员添加的图文显示 0 还是 商户添加的图文显示 1 + $this->assign(ArticleModel::getAll($where)); + return $this->fetch(); + } +} \ No newline at end of file diff --git a/application/admin/controller/article/ArticleCategory.php b/application/admin/controller/article/ArticleCategory.php new file mode 100644 index 00000000..a446e233 --- /dev/null +++ b/application/admin/controller/article/ArticleCategory.php @@ -0,0 +1 @@ +request); $this->assign('where',$where); $this->assign(ArticleCategoryModel::systemPage($where)); return $this->fetch(); } /** * 添加分类管理 * */ public function create(){ $f = array(); $f[] = Form::select('pid','父级id')->setOptions(function(){ $list = ArticleCategoryModel::getTierList(); $menus[] = ['value'=>0,'label'=>'顶级分类']; foreach ($list as $menu){ $menus[] = ['value'=>$menu['id'],'label'=>$menu['html'].$menu['title']]; } return $menus; })->filterable(1); $f[] = Form::input('title','分类名称'); $f[] = Form::input('intr','分类简介')->type('textarea'); // $f[] = Form::select('new_id','图文列表')->setOptions(function(){ // $list = ArticleModel::getNews(); // $options = []; // foreach ($list as $id=>$roleName){ // $options[] = ['label'=>$roleName,'value'=>$id]; // } // return $options; // })->multiple(1)->filterable(1); $f[] = Form::frameImageOne('image','分类图片',Url::build('admin/widget.images/index',array('fodder'=>'image')))->icon('image'); $f[] = Form::number('sort','排序',0); $f[] = Form::radio('status','状态',1)->options([['value'=>1,'label'=>'显示'],['value'=>0,'label'=>'隐藏']]); $form = Form::make_post_form('添加分类',$f,Url::build('save')); $this->assign(compact('form')); return $this->fetch('public/form-builder'); } /** * s上传图片 * */ public function upload(){ $res = Upload::image('file','article'); $thumbPath = Upload::thumb($res->dir); if($res->status == 200) return Json::successful('图片上传成功!',['name'=>$res->fileInfo->getSaveName(),'url'=>Upload::pathToUrl($thumbPath)]); else return Json::fail($res->error); } /** * 保存分类管理 * */ public function save(Request $request){ $data = Util::postMore([ 'title', 'pid', 'intr', ['new_id',[]], ['image',[]], ['sort',0], 'status',],$request); if(!$data['title']) return Json::fail('请输入分类名称'); if(count($data['image']) != 1) return Json::fail('请选择分类图片,并且只能上传一张'); if($data['sort'] < 0) return Json::fail('排序不能是负数'); $data['add_time'] = time(); $data['image'] = $data['image'][0]; $new_id = $data['new_id']; unset($data['new_id']); $res = ArticleCategoryModel::set($data); if(!ArticleModel::saveBatchCid($res['id'],implode(',',$new_id))) return Json::fail('文章列表添加失败'); return Json::successful('添加分类成功!'); } /** * 修改分类 * */ public function edit($id){ if(!$id) return $this->failed('参数错误'); $article = ArticleCategoryModel::get($id)->getData(); if(!$article) return Json::fail('数据不存在!'); $f = array(); $f[] = Form::select('pid','父级id',(string)$article['pid'])->setOptions(function(){ $list = ArticleCategoryModel::getTierList(); $menus[] = ['value'=>0,'label'=>'顶级分类']; foreach ($list as $menu){ $menus[] = ['value'=>$menu['id'],'label'=>$menu['html'].$menu['title']]; } return $menus; })->filterable(1); $f[] = Form::input('title','分类名称',$article['title']); $f[] = Form::input('intr','分类简介',$article['intr'])->type('textarea'); // $f[] = Form::select('new_id','图文列表',explode(',',$article->getData('new_id')))->setOptions(function(){ // $list = ArticleModel::getNews(); // $options = []; // foreach ($list as $id=>$roleName){ // $options[] = ['label'=>$roleName,'value'=>$id]; // } // return $options; // })->multiple(1)->filterable(1); $f[] = Form::frameImageOne('image','分类图片',Url::build('admin/widget.images/index',array('fodder'=>'image')),$article['image'])->icon('image'); $f[] = Form::number('sort','排序',0); $f[] = Form::radio('status','状态',$article['status'])->options([['value'=>1,'label'=>'显示'],['value'=>0,'label'=>'隐藏']]); $form = Form::make_post_form('编辑分类',$f,Url::build('update',array('id'=>$id))); $this->assign(compact('form')); return $this->fetch('public/form-builder'); } public function update(Request $request, $id) { $data = Util::postMore([ 'pid', 'title', 'intr', // ['new_id',[]], ['image',[]], ['sort',0], 'status',],$request); if(!$data['title']) return Json::fail('请输入分类名称'); if(count($data['image']) != 1) return Json::fail('请选择分类图片,并且只能上传一张'); if($data['sort'] < 0) return Json::fail('排序不能是负数'); $data['image'] = $data['image'][0]; if(!ArticleCategoryModel::get($id)) return Json::fail('编辑的记录不存在!'); // if(!ArticleModel::saveBatchCid($id,implode(',',$data['new_id']))) return Json::fail('文章列表添加失败'); // unset($data['new_id']); ArticleCategoryModel::edit($data,$id); return Json::successful('修改成功!'); } /** * 删除分类 * */ public function delete($id) { $res = ArticleCategoryModel::delArticleCategory($id); if(!$res) return Json::fail(ArticleCategoryModel::getErrorInfo('删除失败,请稍候再试!')); else return Json::successful('删除成功!'); } } \ No newline at end of file