RoleRepository.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020-03-24
  7. *
  8. *
  9. */
  10. namespace app\common\repositories\system\auth;
  11. //附件
  12. use app\common\dao\system\menu\RoleDao;
  13. use app\common\repositories\BaseRepository;
  14. use FormBuilder\Exception\FormBuilderException;
  15. use FormBuilder\Factory\Elm;
  16. use FormBuilder\Form;
  17. use think\db\exception\DataNotFoundException;
  18. use think\db\exception\DbException;
  19. use think\db\exception\ModelNotFoundException;
  20. use think\facade\Route;
  21. /**
  22. * Class BaseRepository
  23. * @package common\repositories
  24. * @mixin RoleDao
  25. */
  26. class RoleRepository extends BaseRepository
  27. {
  28. public function __construct(RoleDao $dao)
  29. {
  30. /**
  31. * @var RoleDao
  32. */
  33. $this->dao = $dao;
  34. }
  35. /**
  36. * @param int $merId
  37. * @param array $where
  38. * @param $page
  39. * @param $limit
  40. * @return array
  41. * @throws DataNotFoundException
  42. * @throws DbException
  43. * @throws ModelNotFoundException
  44. * @author xaboy
  45. * @day 2020-04-18
  46. */
  47. public function search(int $merId, array $where, $page, $limit)
  48. {
  49. $query = $this->dao->search($merId, $where);
  50. $count = $query->count();
  51. $list = $query->page($page, $limit)->hidden(['update_time'])->select();
  52. foreach ($list as $k => $role) {
  53. $list[$k]['rule_name'] = $role->ruleNames();
  54. $list[$k]['status'] = (int)$role['status'];
  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(isset($formData['status'])){
  84. $formData['status']= (int)$formData['status'];
  85. }
  86. if ($is_mer)
  87. $form = Elm::createForm(is_null($id) ? Route::buildUrl('merchantRoleCreate')->build() : Route::buildUrl('merchantRoleUpdate', ['id' => $id])->build());
  88. else
  89. $form = Elm::createForm(is_null($id) ? Route::buildUrl('systemRoleCreate')->build() : Route::buildUrl('systemRoleUpdate', ['id' => $id])->build());
  90. $options = app()->make(MenuRepository::class)->getTree($is_mer);
  91. $form->setRule([
  92. Elm::input('role_name', '身份名称')->required(),
  93. Elm::input('phone', '线上用户手机号'),
  94. Elm::tree('rules', '权限')->data($options)->defaultExpandAll(true)->showCheckbox(true),
  95. Elm::switches('status', '是否开启', 1)->inactiveValue(0)->activeValue(1)->inactiveText('关闭')->activeText('开启'),
  96. ]);
  97. return $form->setTitle(is_null($id) ? '添加身份' : '编辑身份')->formData($formData);
  98. }
  99. /**
  100. * @param bool $is_mer
  101. * @param int $id
  102. * @return Form
  103. * @throws DataNotFoundException
  104. * @throws DbException
  105. * @throws FormBuilderException
  106. * @throws ModelNotFoundException
  107. * @author xaboy
  108. * @day 2020-04-18
  109. */
  110. public function updateForm(bool $is_mer, int $id)
  111. {
  112. return $this->form($is_mer, $id, $this->dao->get($id)->toArray());
  113. }
  114. }