ArticleCategoryServices.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\article;
  12. use app\dao\article\ArticleCategoryDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\AdminException;
  15. use crmeb\services\FormBuilder as Form;
  16. use crmeb\utils\Arr;
  17. use think\facade\Route as Url;
  18. /**
  19. * Class ArticleCategoryServices
  20. * @package app\services\article
  21. * @method getArticleCategory()
  22. * @method getArticleTwoCategory()
  23. */
  24. class ArticleCategoryServices extends BaseServices
  25. {
  26. /**
  27. * ArticleCategoryServices constructor.
  28. * @param ArticleCategoryDao $dao
  29. */
  30. public function __construct(ArticleCategoryDao $dao)
  31. {
  32. $this->dao = $dao;
  33. }
  34. /**
  35. * 获取文章分类列表
  36. * @param array $where
  37. * @return array
  38. * @throws \ReflectionException
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\DbException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. */
  43. public function getList(array $where)
  44. {
  45. $list = $this->dao->getList($where);
  46. $list = get_tree_children($list);
  47. $count = $this->dao->count($where);
  48. return compact('list', 'count');
  49. }
  50. /**
  51. * 生成创建修改表单
  52. * @param int $id
  53. * @return array
  54. * @throws \FormBuilder\Exception\FormBuilderException
  55. */
  56. public function createForm(int $id)
  57. {
  58. $method = 'POST';
  59. $url = '/cms/category';
  60. if ($id) {
  61. $info = $this->dao->get($id);
  62. $method = 'PUT';
  63. $url = $url . '/' . $id;
  64. $pid = $info['pid'];
  65. } else {
  66. $pid = '';
  67. }
  68. $f = array();
  69. $f[] = Form::hidden('id', $info['id'] ?? 0);
  70. $f[] = Form::select('pid', '上级分类', (int)($info['pid'] ?? ''))->setOptions($this->menus($pid))->filterable(1);
  71. $f[] = Form::input('title', '分类名称', $info['title'] ?? '')->maxlength(20)->required();
  72. $f[] = Form::input('intr', '分类简介', $info['intr'] ?? '')->type('textarea')->required();
  73. $f[] = Form::frameImage('image', '分类图片', Url::buildUrl(config('app.admin_prefix', 'admin') . '/widget.images/index', array('fodder' => 'image')), $info['image'] ?? '')->icon('el-icon-picture-outline')->width('950px')->height('560px')->props(['footer' => false]);
  74. $f[] = Form::number('sort', '排序', (int)($info['sort'] ?? 0))->precision(0);
  75. $f[] = Form::radio('status', '状态', $info['status'] ?? 1)->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  76. return create_form('添加分类', $f, Url::buildUrl($url), $method);
  77. }
  78. /**
  79. * 保存
  80. * @param array $data
  81. * @return mixed
  82. */
  83. public function save(array $data)
  84. {
  85. return $this->dao->save($data);
  86. }
  87. /**
  88. * 修改
  89. * @param array $data
  90. * @return mixed
  91. */
  92. public function update(array $data)
  93. {
  94. return $this->dao->update($data['id'], $data);
  95. }
  96. /**
  97. * 删除
  98. * @param int $id
  99. * @return mixed
  100. */
  101. public function del(int $id)
  102. {
  103. /** @var ArticleServices $articleService */
  104. $articleService = app()->make(ArticleServices::class);
  105. $pidCount = $this->dao->count(['pid' => $id]);
  106. if ($pidCount > 0) throw new AdminException(400454);
  107. $count = $articleService->count(['cid' => $id]);
  108. if ($count > 0) {
  109. throw new AdminException(400455);
  110. } else {
  111. return $this->dao->delete($id);
  112. }
  113. }
  114. /**
  115. * 修改状态
  116. * @param int $id
  117. * @param int $status
  118. * @return mixed
  119. */
  120. public function setStatus(int $id, int $status)
  121. {
  122. return $this->dao->update($id, ['status' => $status]);
  123. }
  124. /**
  125. * 获取一级分类组合数据
  126. * @param string $pid
  127. * @return array[]
  128. */
  129. public function menus($pid = '')
  130. {
  131. $list = $this->dao->getMenus(['pid' => 0]);
  132. $menus = [['value' => 0, 'label' => '顶级分类']];
  133. if ($pid === 0) return $menus;
  134. if ($pid != '') $menus = [];
  135. foreach ($list as $menu) {
  136. $menus[] = ['value' => $menu['id'], 'label' => $menu['title']];
  137. }
  138. return $menus;
  139. }
  140. /**
  141. * 树形列表
  142. * @return array
  143. * @throws \ReflectionException
  144. * @throws \think\db\exception\DataNotFoundException
  145. * @throws \think\db\exception\DbException
  146. * @throws \think\db\exception\ModelNotFoundException
  147. * @author: 吴汐
  148. * @email: 442384644@qq.com
  149. * @date: 2023/9/7
  150. */
  151. public function getTreeList()
  152. {
  153. return get_tree_children($this->dao->getTreeList(['is_del' => 0, 'status' => 1, 'hidden' => 0], ['id', 'id as value', 'title as label', 'title', 'pid']), 'children', 'id');
  154. // return sort_list_tier($this->dao->getMenus([]));
  155. }
  156. }