RoleRepository.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace app\common\repositories\system\auth;
  3. //附件
  4. use app\common\dao\system\menu\RoleDao;
  5. use app\common\repositories\BaseRepository;
  6. use FormBuilder\Exception\FormBuilderException;
  7. use FormBuilder\Factory\Elm;
  8. use FormBuilder\Form;
  9. use think\db\exception\DataNotFoundException;
  10. use think\db\exception\DbException;
  11. use think\db\exception\ModelNotFoundException;
  12. use think\facade\Route;
  13. /**
  14. * Class BaseRepository
  15. * @package common\repositories
  16. * @mixin RoleDao
  17. */
  18. class RoleRepository extends BaseRepository
  19. {
  20. public function __construct(RoleDao $dao)
  21. {
  22. /**
  23. * @var RoleDao
  24. */
  25. $this->dao = $dao;
  26. }
  27. /**
  28. * @param int $merId
  29. * @param array $where
  30. * @param $page
  31. * @param $limit
  32. * @return array
  33. * @throws DataNotFoundException
  34. * @throws DbException
  35. * @throws ModelNotFoundException
  36. * @author zfy
  37. * @day 2020-04-18
  38. */
  39. public function search(int $merId, array $where, $page, $limit)
  40. {
  41. $query = $this->dao->search($merId, $where);
  42. $count = $query->count();
  43. $list = $query->page($page, $limit)->hidden(['update_time'])->select();
  44. foreach ($list as $k => $role) {
  45. $list[$k]['rule_name'] = $role->ruleNames();
  46. }
  47. return compact('count', 'list');
  48. }
  49. /**
  50. * @param int $id
  51. * @param array $data
  52. * @return int
  53. * @throws DbException
  54. * @author zfy
  55. * @day 2020-04-09
  56. */
  57. public function update(int $id, array $data)
  58. {
  59. if (isset($data['rules']))
  60. $data['rules'] = implode(',', $data['rules']);
  61. return $this->dao->update($id, $data);
  62. }
  63. /**
  64. * @param bool $is_mer
  65. * @param int|null $id
  66. * @param array $formData
  67. * @return Form
  68. * @throws FormBuilderException
  69. * @author zfy
  70. * @day 2020-04-18
  71. */
  72. public function form(bool $is_mer = false, ?int $id = null, array $formData = []): Form
  73. {
  74. if ($is_mer)
  75. $form = Elm::createForm(is_null($id) ? Route::buildUrl('merchantRoleCreate')->build() : Route::buildUrl('merchantRoleUpdate', ['id' => $id])->build());
  76. else
  77. $form = Elm::createForm(is_null($id) ? Route::buildUrl('systemRoleCreate')->build() : Route::buildUrl('systemRoleUpdate', ['id' => $id])->build());
  78. $options = app()->make(MenuRepository::class)->getTree($is_mer ? 1 : 0);
  79. $form->setRule([
  80. Elm::input('role_name', '身份名称')->required(),
  81. Elm::tree('rules', '权限')->data($options)->defaultExpandAll(true)->showCheckbox(true),
  82. Elm::switches('status', '是否开启', 1)->inactiveValue(0)->activeValue(1)->inactiveText('关闭')->activeText('开启'),
  83. ]);
  84. return $form->setTitle(is_null($id) ? '添加身份' : '编辑身份')->formData($formData);
  85. }
  86. /**
  87. * @param bool $is_mer
  88. * @param int $id
  89. * @return Form
  90. * @throws DataNotFoundException
  91. * @throws DbException
  92. * @throws FormBuilderException
  93. * @throws ModelNotFoundException
  94. * @author zfy
  95. * @day 2020-04-18
  96. */
  97. public function updateForm(bool $is_mer, int $id)
  98. {
  99. return $this->form($is_mer, $id, $this->dao->get($id)->toArray());
  100. }
  101. }