AdminRoleClass.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace biz\admin\classes;
  3. use common\components\util;
  4. use Yii;
  5. use biz\base\classes\BaseClass;
  6. class AdminRoleClass extends BaseClass
  7. {
  8. public static $baseFile = '\biz\admin\models\AdminRole';
  9. //验证权限 shish 2019.12.9
  10. public static function valid($role, $merchantId)
  11. {
  12. if (empty($role)) {
  13. util::fail('角色无效');
  14. }
  15. if ($role['merchantId'] != $merchantId) {
  16. util::fail('没有权限操作');
  17. }
  18. }
  19. //添加角色 shish 2019.12.10
  20. public function addRole($data)
  21. {
  22. $roleName = $data['roleName'];
  23. $merchantId = $data['merchantId'];
  24. $nameList = self::getNameList($merchantId);
  25. if (in_array($roleName, $nameList)) {
  26. util::fail('角色已经存在');
  27. }
  28. $data['addTime'] = time();
  29. return self::add($data);
  30. }
  31. //获取商家角色列表 shish 2019.12.10
  32. public static function getRoleList($merchantId)
  33. {
  34. return self::getAllByCondition(['merchantId' => $merchantId], null, '*');
  35. }
  36. //获取商家所有的角色名称 shish 2019.12.10
  37. public static function getNameList($merchantId)
  38. {
  39. $list = self::getRoleList($merchantId);
  40. return !empty($list) ? array_column($list, 'roleName') : [];
  41. }
  42. //更新角色 shish 2019.12.10
  43. public static function updateRole($role, $data)
  44. {
  45. $merchantId = $role['merchantId'];
  46. if ($role['roleName'] != $data['roleName']) {
  47. $name = $data['roleName'];
  48. $nameList = self::getNameList($merchantId);
  49. if (in_array($name, $nameList)) {
  50. util::fail('名称已经存在');
  51. }
  52. }
  53. $id = $role['id'];
  54. return self::updateById($id, $data);
  55. }
  56. }