PageCategoryServices.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. declare (strict_types=1);
  12. namespace app\services\diy;
  13. use app\services\BaseServices;
  14. use app\dao\diy\PageCategoryDao;
  15. use crmeb\exceptions\AdminException;
  16. use crmeb\services\CacheService;
  17. use crmeb\services\FormBuilder as Form;
  18. use think\facade\Route as Url;
  19. /**
  20. * Class PageCategoryServices
  21. * @package app\services\diy
  22. */
  23. class PageCategoryServices extends BaseServices
  24. {
  25. protected $tree_page_category_key = 'tree_page_categroy';
  26. /**
  27. * PageCategoryServices constructor.
  28. * @param PageCategoryDao $dao
  29. */
  30. public function __construct(PageCategoryDao $dao)
  31. {
  32. $this->dao = $dao;
  33. }
  34. /**
  35. * 获取分类列表
  36. * @return bool|mixed|null
  37. */
  38. public function getCategroyList()
  39. {
  40. // return CacheService::remember($this->tree_page_category_key, function () {
  41. return $this->getSonCategoryList();
  42. // }, 86400);
  43. }
  44. /**
  45. * tree分类列表
  46. * @param int $pid
  47. * @param string $parent_name
  48. * @return array
  49. * @throws \think\db\exception\DataNotFoundException
  50. * @throws \think\db\exception\DbException
  51. * @throws \think\db\exception\ModelNotFoundException
  52. */
  53. public function getSonCategoryList($pid = 0)
  54. {
  55. $list = $this->dao->getList(['pid' => $pid], 'id,pid,type,name');
  56. $arr = [];
  57. if ($list) {
  58. foreach ($list as $item) {
  59. $item['title'] = $item['name'];
  60. $item['expand'] = true;
  61. $item['children'] = $this->getSonCategoryList($item['id']);
  62. $arr[] = $item;
  63. }
  64. }
  65. return $arr;
  66. }
  67. public function getLinkCategoryForm($cate_id = 0, $pid = 1)
  68. {
  69. $info = $this->dao->get($cate_id);
  70. $list = $this->dao->getList(['pid' => 1]);
  71. $data = [['value' => 1, 'label' => '顶级分类']];
  72. foreach ($list as $menu) {
  73. $data[] = ['value' => $menu['id'], 'label' => $menu['name']];
  74. }
  75. $pid = isset($info['pid']) ? $info['pid'] : $pid;
  76. $f[] = Form::hidden('id', $cate_id);
  77. $f[] = Form::select('pid', '上级分类', (int)$pid)->setOptions($data)->filterable(true);
  78. $f[] = Form::input('name', '分类名称', $info['name'] ?? '')->required();
  79. $f[] = Form::input('type', '分类类型', $info['type'] ?? '')->required();
  80. $f[] = Form::number('sort', '排序', (int)($info['sort'] ?? 0))->min(0)->precision(0);
  81. $f[] = Form::radio('status', '状态', $info['status'] ?? 1)->options([['label' => '显示', 'value' => 1], ['label' => '隐藏', 'value' => 0]]);
  82. return create_form($cate_id ? '修改分类' : '添加分类', $f, Url::buildUrl('/diy/link/category/save/' . $cate_id), 'POST');
  83. }
  84. public function getLinkCategorySave($cate_id, $data)
  85. {
  86. if ($cate_id) {
  87. $res = $this->dao->update($cate_id, $data);
  88. } else {
  89. $data['add_time'] = time();
  90. $res = $this->dao->save($data);
  91. }
  92. if (!$res) {
  93. throw new AdminException('保存失败');
  94. } else {
  95. return true;
  96. }
  97. }
  98. public function getLinkCategoryDel($cate_id)
  99. {
  100. $res = $this->dao->delete($cate_id);
  101. if (!$res) {
  102. throw new AdminException('删除失败');
  103. } else {
  104. return true;
  105. }
  106. }
  107. }