ArticleCategory.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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::frameImageOne('image', '分类图片', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')))->icon('image')->width('100%')->height('500px');
  43. $f[] = Form::number('sort', '排序', 0);
  44. $f[] = Form::radio('status', '状态', 1)->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  45. $form = Form::make_post_form('添加分类', $f, Url::buildUrl('save'));
  46. $this->assign(compact('form'));
  47. return $this->fetch('public/form-builder');
  48. }
  49. /**
  50. * s上传图片
  51. * */
  52. public function upload()
  53. {
  54. $res = Upload::instance()->setUploadPath('article')->image('file');
  55. if (!is_array($res)) return Json::fail($res);
  56. SystemAttachment::attachmentAdd($res['name'], $res['size'], $res['type'], $res['dir'], $res['thumb_path'], 5, $res['image_type'], $res['time']);
  57. return Json::successful('图片上传成功!', ['name' => $res['name'], 'url' => path_to_url($res['thumb_path'])]);
  58. }
  59. /**
  60. * 保存分类管理
  61. * */
  62. public function save()
  63. {
  64. $data = Util::postMore([
  65. 'title',
  66. 'pid',
  67. 'intr',
  68. ['new_id', []],
  69. ['image', []],
  70. ['sort', 0],
  71. 'status',]);
  72. if (!$data['title']) return Json::fail('请输入分类名称');
  73. if (count($data['image']) != 1) return Json::fail('请选择分类图片,并且只能上传一张');
  74. if ($data['sort'] < 0) return Json::fail('排序不能是负数');
  75. $data['add_time'] = time();
  76. $data['image'] = $data['image'][0];
  77. $new_id = $data['new_id'];
  78. unset($data['new_id']);
  79. $res = ArticleCategoryModel::create($data);
  80. if (!ArticleModel::saveBatchCid($res['id'], implode(',', $new_id))) return Json::fail('文章列表添加失败');
  81. return Json::successful('添加分类成功!');
  82. }
  83. /**
  84. * 修改分类
  85. * */
  86. public function edit($id)
  87. {
  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. $list = ArticleCategoryModel::getTierList();
  94. $menus[] = ['value' => 0, 'label' => '顶级分类'];
  95. foreach ($list as $menu) {
  96. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['title']];
  97. }
  98. return $menus;
  99. })->filterable(1);
  100. $f[] = Form::input('title', '分类名称', $article['title']);
  101. $f[] = Form::input('intr', '分类简介', $article['intr'])->type('textarea');
  102. $f[] = Form::frameImageOne('image', '分类图片', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')), $article['image'])->icon('image')->width('100%')->height('500px');
  103. $f[] = Form::number('sort', '排序', $article['sort']);
  104. $f[] = Form::radio('status', '状态', $article['status'])->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  105. $form = Form::make_post_form('编辑分类', $f, Url::buildUrl('update', array('id' => $id)));
  106. $this->assign(compact('form'));
  107. return $this->fetch('public/form-builder');
  108. }
  109. public function update($id)
  110. {
  111. $data = Util::postMore([
  112. 'pid',
  113. 'title',
  114. 'intr',
  115. // ['new_id',[]],
  116. ['image', []],
  117. ['sort', 0],
  118. 'status',]);
  119. if (!$data['title']) return Json::fail('请输入分类名称');
  120. if (count($data['image']) != 1) return Json::fail('请选择分类图片,并且只能上传一张');
  121. if ($data['sort'] < 0) return Json::fail('排序不能是负数');
  122. $data['image'] = $data['image'][0];
  123. if (!ArticleCategoryModel::get($id)) return Json::fail('编辑的记录不存在!');
  124. // if(!ArticleModel::saveBatchCid($id,implode(',',$data['new_id']))) return Json::fail('文章列表添加失败');
  125. // unset($data['new_id']);
  126. ArticleCategoryModel::edit($data, $id);
  127. return Json::successful('修改成功!');
  128. }
  129. /**
  130. * 删除分类
  131. * */
  132. public function delete($id)
  133. {
  134. $res = ArticleCategoryModel::delArticleCategory($id);
  135. if (!$res)
  136. return Json::fail(ArticleCategoryModel::getErrorInfo('删除失败,请稍候再试!'));
  137. else
  138. return Json::successful('删除成功!');
  139. }
  140. }