RoleRepository.php 3.8 KB

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