ArticleCategoryServices.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. return compact('list');
  48. }
  49. /**
  50. * 生成创建修改表单
  51. * @param int $id
  52. * @return array
  53. * @throws \FormBuilder\Exception\FormBuilderException
  54. */
  55. public function createForm(int $id)
  56. {
  57. $method = 'POST';
  58. $url = '/cms/category';
  59. if ($id) {
  60. $info = $this->dao->get($id);
  61. $method = 'PUT';
  62. $url = $url . '/' . $id;
  63. $pid = $info['pid'];
  64. } else {
  65. $pid = '';
  66. }
  67. $f = array();
  68. $f[] = Form::hidden('id', $info['id'] ?? 0);
  69. $f[] = Form::select('pid', '上级分类', (int)($info['pid'] ?? ''))->setOptions($this->menus($pid))->filterable(1);
  70. $f[] = Form::input('title', '分类名称', $info['title'] ?? '')->maxlength(20)->required();
  71. $f[] = Form::input('intr', '分类简介', $info['intr'] ?? '')->type('textarea')->required();
  72. $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]);
  73. $f[] = Form::number('sort', '排序', (int)($info['sort'] ?? 0))->precision(0);
  74. $f[] = Form::radio('status', '状态', $info['status'] ?? 1)->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  75. return create_form('添加分类', $f, Url::buildUrl($url), $method);
  76. }
  77. /**
  78. * 保存
  79. * @param array $data
  80. * @return mixed
  81. */
  82. public function save(array $data)
  83. {
  84. return $this->dao->save($data);
  85. }
  86. /**
  87. * 修改
  88. * @param array $data
  89. * @return mixed
  90. */
  91. public function update(array $data)
  92. {
  93. return $this->dao->update($data['id'], $data);
  94. }
  95. /**
  96. * 删除
  97. * @param int $id
  98. * @return mixed
  99. */
  100. public function del(int $id)
  101. {
  102. /** @var ArticleServices $articleService */
  103. $articleService = app()->make(ArticleServices::class);
  104. $pidCount = $this->dao->count(['pid' => $id]);
  105. if ($pidCount > 0) throw new AdminException(400454);
  106. $count = $articleService->count(['cid' => $id]);
  107. if ($count > 0) {
  108. throw new AdminException(400455);
  109. } else {
  110. return $this->dao->delete($id);
  111. }
  112. }
  113. /**
  114. * 修改状态
  115. * @param int $id
  116. * @param int $status
  117. * @return mixed
  118. */
  119. public function setStatus(int $id, int $status)
  120. {
  121. return $this->dao->update($id, ['status' => $status]);
  122. }
  123. /**
  124. * 获取一级分类组合数据
  125. * @param string $pid
  126. * @return array[]
  127. */
  128. public function menus($pid = '')
  129. {
  130. $list = $this->dao->getMenus(['pid' => 0]);
  131. $menus = [['value' => 0, 'label' => '顶级分类']];
  132. if ($pid === 0) return $menus;
  133. if ($pid != '') $menus = [];
  134. foreach ($list as $menu) {
  135. $menus[] = ['value' => $menu['id'], 'label' => $menu['title']];
  136. }
  137. return $menus;
  138. }
  139. /**
  140. * 树形列表
  141. * @return array
  142. * @throws \ReflectionException
  143. * @throws \think\db\exception\DataNotFoundException
  144. * @throws \think\db\exception\DbException
  145. * @throws \think\db\exception\ModelNotFoundException
  146. * @author: 吴汐
  147. * @email: 442384644@qq.com
  148. * @date: 2023/9/7
  149. */
  150. public function getTreeList()
  151. {
  152. 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');
  153. // return sort_list_tier($this->dao->getMenus([]));
  154. }
  155. }