ArticleCategory.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller\article;
  12. use app\admin\controller\AuthController;
  13. use service\FormBuilder as Form;
  14. use service\JsonService as Json;
  15. use service\UploadService as Upload;
  16. use think\Request;
  17. use think\Url;
  18. use app\admin\model\article\ArticleCategory as ArticleCategoryModel;
  19. use app\admin\model\article\Article as ArticleModel;
  20. /**
  21. * 文章分类管理 控制器
  22. * */
  23. class ArticleCategory extends AuthController
  24. {
  25. /**
  26. * 分类管理
  27. **/
  28. public function index(){
  29. $where = parent::getMore([
  30. ['status',''],
  31. ['title',''],
  32. ],$this->request);
  33. $this->assign('where',$where);
  34. $this->assign(ArticleCategoryModel::systemPage($where));
  35. return $this->fetch();
  36. }
  37. /**
  38. * 添加分类管理
  39. * */
  40. public function create(){
  41. $f = array();
  42. $f[] = Form::select('pid','父级id')->setOptions(function(){
  43. $menus[] = ['value'=>0,'label'=>'顶级分类'];
  44. return $menus;
  45. })->filterable(1);
  46. $f[] = Form::input('title','分类名称');
  47. $f[] = Form::input('intr','分类简介')->type('textarea');
  48. $f[] = Form::number('sort','排序',0);
  49. $f[] = Form::radio('status','状态',1)->options([['value'=>1,'label'=>'显示'],['value'=>0,'label'=>'隐藏']]);
  50. $form = Form::make_post_form('添加分类',$f,Url::build('save'),2);
  51. $this->assign(compact('form'));
  52. return $this->fetch('public/form-builder');
  53. }
  54. /**
  55. * s上传图片
  56. * */
  57. public function upload(){
  58. $res = Upload::image('file','article');
  59. $thumbPath = Upload::thumb($res->dir);
  60. if($res->status == 200)
  61. return Json::successful('图片上传成功!',['name'=>$res->fileInfo->getSaveName(),'url'=>Upload::pathToUrl($thumbPath)]);
  62. else
  63. return Json::fail($res->error);
  64. }
  65. /**
  66. * 保存分类管理
  67. * */
  68. public function save(Request $request){
  69. $data = parent::postMore([
  70. 'title',
  71. 'pid',
  72. 'intr',
  73. ['sort',0],
  74. 'status',],$request);
  75. if(!$data['title']) return Json::fail('请输入分类名称');
  76. if($data['sort'] < 0) return Json::fail('排序不能是负数');
  77. $data['add_time'] = time();
  78. if(ArticleCategoryModel::be(['title'=>$data['title'],'is_del'=>0])){
  79. return Json::fail('分类名称已存在!');
  80. }
  81. ArticleCategoryModel::set($data);
  82. return Json::successful('添加分类成功!');
  83. }
  84. /**
  85. * 修改分类
  86. * */
  87. public function edit($id){
  88. if(!$id) return $this->failed('参数错误');
  89. $article = ArticleCategoryModel::get($id)->getData();
  90. if(!$article) return Json::fail('数据不存在!');
  91. $f = array();
  92. $f[] = Form::select('pid','父级id',(string)$article['pid'])->setOptions(function(){
  93. $menus[] = ['value'=>0,'label'=>'顶级分类'];
  94. return $menus;
  95. })->filterable(1);
  96. $f[] = Form::input('title','分类名称',$article['title']);
  97. $f[] = Form::input('intr','分类简介',$article['intr'])->type('textarea');
  98. $f[] = Form::number('sort','排序',$article['sort']);
  99. $f[] = Form::radio('status','状态',$article['status'])->options([['value'=>1,'label'=>'显示'],['value'=>0,'label'=>'隐藏']]);
  100. $form = Form::make_post_form('编辑分类',$f,Url::build('update',array('id'=>$id)),2);
  101. $this->assign(compact('form'));
  102. return $this->fetch('public/form-builder');
  103. }
  104. /**保存修改
  105. * @param Request $request
  106. * @param $id
  107. * @throws \think\exception\DbException
  108. */
  109. public function update(Request $request, $id)
  110. {
  111. $data = parent::postMore([
  112. 'pid',
  113. 'title',
  114. 'intr',
  115. ['image',[]],
  116. ['sort',0],
  117. 'status',],$request);
  118. if(!$data['title']) return Json::fail('请输入分类名称');
  119. if($data['sort'] < 0) return Json::fail('排序不能是负数');
  120. if(!ArticleCategoryModel::get($id)) return Json::fail('编辑的记录不存在!');
  121. ArticleCategoryModel::edit($data,$id);
  122. return Json::successful('修改成功!');
  123. }
  124. /**
  125. * 删除分类
  126. * */
  127. public function delete($id)
  128. {
  129. $res = ArticleCategoryModel::delArticleCategory($id);
  130. if(!$res)
  131. return Json::fail(ArticleCategoryModel::getErrorInfo('删除失败,请稍候再试!'));
  132. else
  133. return Json::successful('删除成功!');
  134. }
  135. }