ArticleCategoryServices.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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 app\services\wechat\WechatNewsCategoryServices;
  15. use crmeb\exceptions\AdminException;
  16. use crmeb\services\FormBuilder as Form;
  17. use think\facade\Route as Url;
  18. /**
  19. * 文章分类
  20. * Class ArticleCategoryServices
  21. * @package app\services\article
  22. * @mixin ArticleCategoryDao
  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. */
  39. public function getList(array $where)
  40. {
  41. [$page, $limit] = $this->getPageValue();
  42. $list = $this->dao->getList($where, $page, $limit);
  43. $count = $this->dao->count($where);
  44. return compact('list', 'count');
  45. }
  46. /**
  47. * 生成创建修改表单
  48. * @param int $id
  49. * @return array
  50. * @throws \FormBuilder\Exception\FormBuilderException
  51. */
  52. public function createForm(int $id)
  53. {
  54. $method = 'POST';
  55. $url = '/cms/category';
  56. if ($id) {
  57. $info = $this->dao->get($id);
  58. $method = 'PUT';
  59. $url = $url . '/' . $id;
  60. }
  61. $f = array();
  62. $f[] = Form::hidden('id', $info['id'] ?? 0);
  63. $f[] = Form::input('title', '分类名称', $info['title'] ?? '')->maxlength(20)->required();
  64. $f[] = Form::input('intr', '分类简介', $info['intr'] ?? '')->type('textarea')->required();
  65. $f[] = Form::frameImage('image', '分类图片', Url::buildUrl(config('admin.admin_prefix') . '/widget.images/index', array('fodder' => 'image')), $info['image'] ?? '')->icon('ios-add')->width('960px')->height('505px')->modal(['footer-hide' => true]);
  66. $f[] = Form::number('sort', '排序', (int)($info['sort'] ?? 0))->min(0);
  67. $f[] = Form::radio('status', '状态', $info['status'] ?? 1)->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  68. return create_form('添加分类', $f, Url::buildUrl($url), $method);
  69. }
  70. /**
  71. * 保存
  72. * @param array $data
  73. */
  74. public function save(array $data)
  75. {
  76. $this->dao->save($data);
  77. }
  78. /**
  79. * 修改
  80. * @param array $data
  81. */
  82. public function update(array $data)
  83. {
  84. $this->dao->update($data['id'], $data);
  85. }
  86. /**
  87. * 删除
  88. * @param int $id
  89. */
  90. public function del(int $id)
  91. {
  92. /** @var WechatNewsCategoryServices $services */
  93. $services = app()->make(WechatNewsCategoryServices::class);
  94. $ids = $services->getNewIds();
  95. /** @var ArticleServices $articleService */
  96. $articleService = app()->make(ArticleServices::class);
  97. $count = $articleService->count(['cid' => $id, 'ids' => $ids]);
  98. if ($count > 0) {
  99. throw new AdminException('该分类下有文章,无法删除!');
  100. } else {
  101. $this->dao->delete($id);
  102. }
  103. }
  104. /**
  105. * 修改状态
  106. * @param int $id
  107. * @param int $status
  108. */
  109. public function setStatus(int $id, int $status)
  110. {
  111. $this->dao->update($id, ['status' => $status]);
  112. }
  113. }