ArticleCategory.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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. {
  30. $where = parent::getMore([
  31. ['status', ''],
  32. ['title', ''],
  33. ], $this->request);
  34. $this->assign('where', $where);
  35. $this->assign(ArticleCategoryModel::systemPage($where));
  36. return $this->fetch();
  37. }
  38. /**
  39. * 添加分类管理
  40. * */
  41. public function create()
  42. {
  43. $f = array();
  44. $f[] = Form::input('title', '分类名称');
  45. $f[] = Form::input('intr', '分类简介')->type('textarea');
  46. $f[] = Form::number('sort', '排序', 0)->max(99999);
  47. $f[] = Form::radio('status', '状态', 1)->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  48. $form = Form::make_post_form('添加分类', $f, Url::build('save'), 2);
  49. $this->assign(compact('form'));
  50. return $this->fetch('public/form-builder');
  51. }
  52. /**
  53. * s上传图片
  54. * */
  55. public function upload()
  56. {
  57. $res = Upload::image('file', 'article');
  58. $thumbPath = Upload::thumb($res->dir);
  59. if ($res->status == 200)
  60. return Json::successful('图片上传成功!', ['name' => $res->fileInfo->getSaveName(), 'url' => Upload::pathToUrl($thumbPath)]);
  61. else
  62. return Json::fail($res->error);
  63. }
  64. /**
  65. * 保存分类管理
  66. * */
  67. public function save(Request $request)
  68. {
  69. $data = parent::postMore([
  70. ['title', ''],
  71. ['intr', ''],
  72. ['sort', 0],
  73. ['status', 1]
  74. ], $request);
  75. $data['pid'] = 0;
  76. $data['title'] = preg_replace("#(^( |\s)+|( |\s)+$)#", "", $data['title']);
  77. if (!$data['title']) return Json::fail('请输入分类名称');
  78. if ($data['sort'] < 0) return Json::fail('排序不能是负数');
  79. $data['add_time'] = time();
  80. if (ArticleCategoryModel::be(['title' => $data['title'], 'is_del' => 0])) {
  81. return Json::fail('分类名称已存在!');
  82. }
  83. ArticleCategoryModel::set($data);
  84. return Json::successful('添加分类成功!');
  85. }
  86. /**
  87. * 修改分类
  88. * */
  89. public function edit($id)
  90. {
  91. if (!$id) return $this->failed('参数错误');
  92. $article = ArticleCategoryModel::get($id)->getData();
  93. if (!$article) return Json::fail('数据不存在!');
  94. $f = array();
  95. $f[] = Form::input('title', '分类名称', $article['title']);
  96. $f[] = Form::input('intr', '分类简介', $article['intr'])->type('textarea');
  97. $f[] = Form::number('sort', '排序', $article['sort']);
  98. $f[] = Form::radio('status', '状态', $article['status'])->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  99. $form = Form::make_post_form('编辑分类', $f, Url::build('update', array('id' => $id)), 2);
  100. $this->assign(compact('form'));
  101. return $this->fetch('public/form-builder');
  102. }
  103. /**保存修改
  104. * @param Request $request
  105. * @param $id
  106. * @throws \think\exception\DbException
  107. */
  108. public function update(Request $request, $id)
  109. {
  110. $data = parent::postMore([
  111. 'pid',
  112. 'title',
  113. 'intr',
  114. ['image', []],
  115. ['sort', 0],
  116. 'status',], $request);
  117. if (!$data['title']) return Json::fail('请输入分类名称');
  118. if ($data['sort'] < 0) return Json::fail('排序不能是负数');
  119. if (!ArticleCategoryModel::get($id)) return Json::fail('编辑的记录不存在!');
  120. if (ArticleCategoryModel::where(['title' => $data['title'], 'is_del' => 0])->where('id', '<>', $id)->count() >= 1) 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. }