123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- <?php
- namespace App\Services\Base;
- use App\Repositories\Criteria\Base\RoleCriteria;
- use App\Repositories\Eloquent\Base\RoleRepositoryEloquent;
- use App\Repositories\Models\Base\Department;
- use App\Repositories\Models\Base\Role;
- use App\Repositories\Presenters\Base\RolePresenter;
- use Illuminate\Http\Request;
- class RoleService
- {
- /**
- * @var RoleRepositoryEloquent
- */
- private $roleRepository;
- /**
- * RoleService constructor.
- *
- * @param RoleRepositoryEloquent $roleRepositoryEloquent
- */
- public function __construct(RoleRepositoryEloquent $roleRepositoryEloquent)
- {
- $this->roleRepository = $roleRepositoryEloquent;
- }
- /**
- * @param Request $request
- *
- * @return mixed
- * @throws \Prettus\Repository\Exceptions\RepositoryException
- */
- public function handleList(Request $request)
- {
- $this->roleRepository->pushCriteria(new RoleCriteria($request));
- $this->roleRepository->setPresenter(RolePresenter::class);
- return $this->roleRepository->searchRolesByPage();
- }
- /**
- * @param $id
- *
- * @return \Illuminate\Database\Eloquent\Model
- */
- public function handleProfile($id)
- {
- $this->roleRepository->setPresenter(RolePresenter::class);
- return $this->roleRepository->searchRoleBy($id);
- }
- /**
- * @param array $data
- *
- * @return mixed
- * @throws \Prettus\Validator\Exceptions\ValidatorException
- */
- public function handleStore($data)
- {
- // $data['company_id'] = 0;
- // $admin = login_admin();
- // if ($admin && $admin['company_id']) $data['company_id'] = $admin['company_id'];
- $role = $this->roleRepository->create($data);
- return $role;
- }
- /**
- * @param array $data
- *
- * @return mixed
- * @throws \Prettus\Validator\Exceptions\ValidatorException
- */
- public function handleUpdate($data)
- {
- $role = $this->roleRepository->update($data, $data['id']);
- return $role;
- }
- /**
- * @param Request $request
- *
- * @return mixed
- * @throws \Prettus\Validator\Exceptions\ValidatorException
- */
- public function handleDelete($id)
- {
- return $this->roleRepository->delete($id);
- }
- /**
- * 保存菜单权限
- * @param Request $request
- * @return bool|void
- */
- public function handleSaveMenus(Request $request)
- {
- $data = $request->all();
- $role = $this->roleRepository->find($request->get('id'));
- $role->menus()->sync($data['menus']);
- return true;
- }
- /**
- * 保存Apis
- * @param Request $request
- * @return bool
- */
- public function handleSaveApis(Request $request)
- {
- $data = $request->all();
- $role = $this->roleRepository->find($request->get('id'));
- $role->givePermissionTo($data['apis']);
- return true;
- }
- /**
- * 保存数据权限
- * @param Request $request
- * @return bool
- * @throws \Prettus\Validator\Exceptions\ValidatorException
- */
- public function handleSaveDataPermissions(Request $request)
- {
- $data = $request->all();
- $role = $this->roleRepository->update([
- 'data_permission_type' => $data['data_permission_type']
- ], $request->get('id'));
- if (array_key_exists('departments', $data) && is_array($data['departments'])) $role->departments()->sync($data['departments']);
- // if (array_key_exists('shops', $data) && is_array($data['shops'])) $role->shops()->sync($data['shops']);
- // if (array_key_exists('factory', $data) && is_array($data['factory'])) $role->factory()->sync($data['factory']);
- return true;
- }
- /**
- * 获取角色菜单id
- * @param Request $request
- * @return array
- */
- public function handleMenusLists(Request $request)
- {
- $role = $this->roleRepository->find($request->get('id'));
- if ($role) return $role->menus()->pluck('id');
- return [];
- }
- /**
- * 获取角色数据id
- * @param Request $request
- * @return array
- */
- public function handleDataLists(Request $request)
- {
- $role = $this->roleRepository->find($request->get('id'));
- if ($role) {
- switch ($role->data_permission_type) {
- // case Role::DATA_TYPE_DEPARTMENT:
- case Role::DATA_TYPE_CUSTOM:
- return [
- 'type' => $role->data_permission_type,
- 'departments' => $role->departments()->pluck('id'),
- // 'shops' => $role->shops()->pluck('id'),
- // 'factory' => $role->factory()->pluck('id')
- ];
- break;
- case Role::DATA_TYPE_ALL:
- case Role::DATA_TYPE_SELF:
- return [
- 'type' => $role->data_permission_type,
- 'data' => []
- ];
- break;
- }
- }
- return [];
- }
- /**
- * 获取角色APIid
- * @param Request $request
- * @return array
- */
- public function handleApisLists(Request $request)
- {
- $role = $this->roleRepository->find($request->get('id'));
- if ($role) return $role->permissions()->pluck('id');
- return [];
- }
- /**
- * 选项数据
- * @param Request $request
- * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection|mixed
- * @throws \Prettus\Repository\Exceptions\RepositoryException
- */
- public function handleSelectOptions(Request $request)
- {
- $this->roleRepository->pushCriteria(new RoleCriteria($request));
- return $this->roleRepository->all(['id', 'name', 'nickname', 'guard_name', 'sort']);
- }
- }
|