ArticleCategory.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace app\admin\controller\article;
  3. use think\facade\Route as Url;
  4. use app\admin\controller\AuthController;
  5. use app\admin\model\system\SystemAttachment;
  6. use app\admin\model\article\{ArticleCategory as ArticleCategoryModel,Article as ArticleModel};
  7. use crmeb\services\{FormBuilder as Form,UtilService as Util,JsonService as Json};
  8. /**
  9. * 文章分类管理 控制器
  10. * */
  11. class ArticleCategory extends AuthController
  12. {
  13. /**
  14. * 分类管理
  15. * */
  16. public function index()
  17. {
  18. $where = Util::getMore([
  19. ['status', ''],
  20. ['title', ''],
  21. ], $this->request);
  22. $this->assign('where', $where);
  23. $this->assign(ArticleCategoryModel::systemPage($where));
  24. return $this->fetch();
  25. }
  26. /**
  27. * 添加分类管理
  28. * */
  29. public function create()
  30. {
  31. $f = array();
  32. $f[] = Form::select('pid', '父级id')->setOptions(function () {
  33. $list = ArticleCategoryModel::getTierList();
  34. $menus[] = ['value' => 0, 'label' => '顶级分类'];
  35. foreach ($list as $menu) {
  36. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['title']];
  37. }
  38. return $menus;
  39. })->filterable(1);
  40. $f[] = Form::input('title', '分类名称');
  41. $f[] = Form::input('intr', '分类简介')->type('textarea');
  42. // $f[] = Form::select('new_id','图文列表')->setOptions(function(){
  43. // $list = ArticleModel::getNews();
  44. // $options = [];
  45. // foreach ($list as $id=>$roleName){
  46. // $options[] = ['label'=>$roleName,'value'=>$id];
  47. // }
  48. // return $options;
  49. // })->multiple(1)->filterable(1);
  50. $f[] = Form::frameImageOne('image', '分类图片', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')))->icon('image')->width('100%')->height('500px');
  51. $f[] = Form::number('sort', '排序', 0);
  52. $f[] = Form::radio('status', '状态', 1)->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  53. $form = Form::make_post_form('添加分类', $f, Url::buildUrl('save'));
  54. $this->assign(compact('form'));
  55. return $this->fetch('public/form-builder');
  56. }
  57. /**
  58. * s上传图片
  59. * */
  60. public function upload()
  61. {
  62. $res = Upload::instance()->setUploadPath('article')->image('file');
  63. if (!is_array($res)) return Json::fail($res);
  64. SystemAttachment::attachmentAdd($res['name'], $res['size'], $res['type'], $res['dir'], $res['thumb_path'], 5, $res['image_type'], $res['time']);
  65. return Json::successful('图片上传成功!', ['name' => $res['name'], 'url' => path_to_url($res['thumb_path'])]);
  66. }
  67. /**
  68. * 保存分类管理
  69. * */
  70. public function save()
  71. {
  72. $data = Util::postMore([
  73. 'title',
  74. 'pid',
  75. 'intr',
  76. ['new_id', []],
  77. ['image', []],
  78. ['sort', 0],
  79. 'status',]);
  80. if (!$data['title']) return Json::fail('请输入分类名称');
  81. if (count($data['image']) != 1) return Json::fail('请选择分类图片,并且只能上传一张');
  82. if ($data['sort'] < 0) return Json::fail('排序不能是负数');
  83. $data['add_time'] = time();
  84. $data['image'] = $data['image'][0];
  85. $new_id = $data['new_id'];
  86. unset($data['new_id']);
  87. $res = ArticleCategoryModel::create($data);
  88. if (!ArticleModel::saveBatchCid($res['id'], implode(',', $new_id))) return Json::fail('文章列表添加失败');
  89. return Json::successful('添加分类成功!');
  90. }
  91. /**
  92. * 修改分类
  93. * */
  94. public function edit($id)
  95. {
  96. if (!$id) return $this->failed('参数错误');
  97. $article = ArticleCategoryModel::get($id)->getData();
  98. if (!$article) return Json::fail('数据不存在!');
  99. $f = array();
  100. $f[] = Form::select('pid', '父级id', (string)$article['pid'])->setOptions(function () {
  101. $list = ArticleCategoryModel::getTierList();
  102. $menus[] = ['value' => 0, 'label' => '顶级分类'];
  103. foreach ($list as $menu) {
  104. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['title']];
  105. }
  106. return $menus;
  107. })->filterable(1);
  108. $f[] = Form::input('title', '分类名称', $article['title']);
  109. $f[] = Form::input('intr', '分类简介', $article['intr'])->type('textarea');
  110. // $f[] = Form::select('new_id','图文列表',explode(',',$article->getData('new_id')))->setOptions(function(){
  111. // $list = ArticleModel::getNews();
  112. // $options = [];
  113. // foreach ($list as $id=>$roleName){
  114. // $options[] = ['label'=>$roleName,'value'=>$id];
  115. // }
  116. // return $options;
  117. // })->multiple(1)->filterable(1);
  118. $f[] = Form::frameImageOne('image', '分类图片', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')), $article['image'])->icon('image')->width('100%')->height('500px');
  119. $f[] = Form::number('sort', '排序', 0);
  120. $f[] = Form::radio('status', '状态', $article['status'])->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  121. $form = Form::make_post_form('编辑分类', $f, Url::buildUrl('update', array('id' => $id)));
  122. $this->assign(compact('form'));
  123. return $this->fetch('public/form-builder');
  124. }
  125. public function update($id)
  126. {
  127. $data = Util::postMore([
  128. 'pid',
  129. 'title',
  130. 'intr',
  131. // ['new_id',[]],
  132. ['image', []],
  133. ['sort', 0],
  134. 'status',]);
  135. if (!$data['title']) return Json::fail('请输入分类名称');
  136. if (count($data['image']) != 1) return Json::fail('请选择分类图片,并且只能上传一张');
  137. if ($data['sort'] < 0) return Json::fail('排序不能是负数');
  138. $data['image'] = $data['image'][0];
  139. if (!ArticleCategoryModel::get($id)) return Json::fail('编辑的记录不存在!');
  140. // if(!ArticleModel::saveBatchCid($id,implode(',',$data['new_id']))) return Json::fail('文章列表添加失败');
  141. // unset($data['new_id']);
  142. ArticleCategoryModel::edit($data, $id);
  143. return Json::successful('修改成功!');
  144. }
  145. /**
  146. * 删除分类
  147. * */
  148. public function delete($id)
  149. {
  150. $res = ArticleCategoryModel::delArticleCategory($id);
  151. if (!$res)
  152. return Json::fail(ArticleCategoryModel::getErrorInfo('删除失败,请稍候再试!'));
  153. else
  154. return Json::successful('删除成功!');
  155. }
  156. }