ArticleCategoryRepository.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace app\common\repositories\article;
  3. use app\common\dao\article\ArticleCategoryDao;
  4. use app\common\repositories\BaseRepository;
  5. use FormBuilder\Exception\FormBuilderException;
  6. use FormBuilder\Factory\Elm;
  7. use FormBuilder\Form;
  8. use think\db\exception\DataNotFoundException;
  9. use think\db\exception\DbException;
  10. use think\db\exception\ModelNotFoundException;
  11. use think\facade\Route;
  12. /**
  13. * Class ArticleCategoryRepository
  14. * @package app\common\repositories\article
  15. * @author zfy
  16. * @day 2020-04-20
  17. * @mixin ArticleCategoryDao
  18. */
  19. class ArticleCategoryRepository extends BaseRepository
  20. {
  21. /**
  22. * ArticleCategoryRepository constructor.
  23. * @param ArticleCategoryDao $dao
  24. */
  25. public function __construct(ArticleCategoryDao $dao)
  26. {
  27. $this->dao = $dao;
  28. }
  29. /**
  30. * @param int $merId
  31. * @return array
  32. * @throws DataNotFoundException
  33. * @throws DbException
  34. * @throws ModelNotFoundException
  35. * @author zfy
  36. * @day 2020-04-20
  37. */
  38. public function getFormatList($merId = 0,$status = null)
  39. {
  40. return formatCategory($this->dao->getAll($merId,$status)->toArray(), 'article_category_id');
  41. }
  42. /**
  43. * @return \think\Collection
  44. * @throws DataNotFoundException
  45. * @throws DbException
  46. * @throws ModelNotFoundException
  47. * @author zfy
  48. * @day 2020/9/18
  49. */
  50. public function apiGetArticleCategory()
  51. {
  52. return $this->dao->search(['status' => 1, 'pid' => 0])->select();
  53. }
  54. /**
  55. * @param int $merId
  56. * @param int|null $id
  57. * @param array $formData
  58. * @return Form
  59. * @throws FormBuilderException
  60. * @author zfy
  61. * @day 2020-04-20
  62. */
  63. public function form(int $merId, ?int $id = null, array $formData = [])
  64. {
  65. $form = Elm::createForm(is_null($id) ? Route::buildUrl('systemArticleCategoryCreate')->build() : Route::buildUrl('systemArticleCategoryUpdate', ['id' => $id])->build());
  66. $form->setRule([
  67. // Elm::cascader('pid', '上级分类')->options(function () use ($id, $merId) {
  68. // $menus = $this->dao->getAllOptions($merId);
  69. // if ($id && isset($menus[$id])) unset($menus[$id]);
  70. // $menus = formatCascaderData($menus, 'title');
  71. // array_unshift($menus, ['label' => '顶级分类', 'value' => 0]);
  72. // return $menus;
  73. // })->props(['props' => ['checkStrictly' => true, 'emitPath' => false]]),
  74. Elm::input('title', '分类名称')->required(),
  75. Elm::input('info', '分类简介'),
  76. Elm::frameImage('image', '分类图片', '/' . config('admin.admin_prefix') . '/setting/uploadPicture?field=image&type=1')->width('896px')->height('480px')->props(['footer' => false])->modal(['modal' => false, 'custom-class' => 'suibian-modal']),
  77. Elm::switches('status', '状态', 1)->activeValue(1)->inactiveValue(0)->inactiveText('关闭')->activeText('开启'),
  78. Elm::number('sort', '排序', 0),
  79. ]);
  80. return $form->setTitle(is_null($id) ? '添加文章配置' : '编辑文章分类')->formData($formData);
  81. }
  82. /**
  83. * @param int $merId
  84. * @param $id
  85. * @return Form
  86. * @throws DataNotFoundException
  87. * @throws DbException
  88. * @throws ModelNotFoundException
  89. * @throws FormBuilderException
  90. * @author zfy
  91. * @day 2020-04-20
  92. */
  93. public function updateForm(int $merId, $id)
  94. {
  95. return $this->form($merId, $id, $this->dao->get($id, $merId)->toArray());
  96. }
  97. }