AdminRoleClass.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 static 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::getList('*', ['merchantId' => $merchantId], 'addTime DESC');
  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. //删除角色 shish 2019.12.10
  57. public static function deleteRole($role)
  58. {
  59. if (isset($role['num']) && !empty($role['num'])) {
  60. util::fail('还有成员,不能删除');
  61. }
  62. $id = $role['id'];
  63. return self::deleteById($id);
  64. }
  65. //创建管理员角色 shish 2020.2.8
  66. public static function addAdminRole($data)
  67. {
  68. return self::add($data);
  69. }
  70. }