Role.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace app\supplier\model\auth;
  3. use app\common\model\supplier\Role as RoleModel;
  4. use app\supplier\model\auth\UserRole as UserRoleModel;
  5. /**
  6. * 角色模型
  7. */
  8. class Role extends RoleModel
  9. {
  10. /**
  11. * 获取所有角色列表
  12. */
  13. public function getTreeData($shop_supplier_id)
  14. {
  15. $all = $this->getAll($shop_supplier_id);
  16. return $this->formatTreeData($all);
  17. }
  18. /**
  19. * 获取所有角色
  20. */
  21. private function getAll($shop_supplier_id)
  22. {
  23. $data = $this->order(['sort' => 'asc', 'create_time' => 'asc'])->where(['shop_supplier_id'=>$shop_supplier_id])->select();
  24. return $data ? $data->toArray() : [];
  25. }
  26. /**
  27. * 获取权限列表
  28. */
  29. private function formatTreeData(&$all, $parent_id = 0, $deep = 1)
  30. {
  31. static $tempTreeArr = [];
  32. foreach ($all as $key => $val) {
  33. // 根据角色深度处理名称前缀
  34. $val['role_name_h1'] = $this->htmlPrefix($deep) . $val['role_name'];
  35. $tempTreeArr[] = $val;
  36. }
  37. return $tempTreeArr;
  38. }
  39. /**
  40. * 角色名称 html格式前缀
  41. */
  42. private function htmlPrefix($deep)
  43. {
  44. // 根据角色深度处理名称前缀
  45. $prefix = '';
  46. if ($deep > 1) {
  47. for ($i = 1; $i <= $deep - 1; $i++) {
  48. $prefix .= ' ├ ';
  49. }
  50. $prefix .= ' ';
  51. }
  52. return $prefix;
  53. }
  54. public function add($data)
  55. {
  56. $this->startTrans();
  57. try {
  58. $arr = [
  59. 'role_name' => $data['role_name'],
  60. 'sort' => $data['sort'],
  61. 'app_id' => self::$app_id,
  62. 'shop_supplier_id'=>$data['shop_supplier_id'],
  63. ];
  64. $res = self::create($arr);
  65. $arr1 = [];
  66. foreach ($data['access_id'] as $val) {
  67. $arr1[] = [
  68. 'role_id' => $res['role_id'],
  69. 'access_id' => $val,
  70. 'app_id' => self::$app_id
  71. ];
  72. }
  73. $model = new RoleAccess();
  74. $model->saveAll($arr1);
  75. // 事务提交
  76. $this->commit();
  77. return true;
  78. } catch (\Exception $e) {
  79. $this->error = $e->getMessage();
  80. $this->rollback();
  81. return false;
  82. }
  83. }
  84. /**
  85. * 编辑
  86. * @param $data
  87. * @return bool
  88. */
  89. public function edit($data)
  90. {
  91. $this->startTrans();
  92. try {
  93. $this->save([
  94. 'role_name' => $data['role_name'],
  95. 'sort' => $data['sort'],
  96. ]);
  97. if (!isset($data['access_id'])) {
  98. $this->commit();
  99. return true;
  100. }
  101. $access_list = [];
  102. $access_model = new RoleAccess();
  103. $access_model->where(['role_id' => $data['role_id']])->delete();
  104. foreach ($data['access_id'] as $val) {
  105. $access_list[] = [
  106. 'role_id' => $data['role_id'],
  107. 'access_id' => $val,
  108. 'app_id' => self::$app_id
  109. ];
  110. }
  111. $access_model->saveAll($access_list);
  112. // 事务提交
  113. $this->commit();
  114. return true;
  115. } catch (\Exception $e) {
  116. $this->error = $e->getMessage();
  117. $this->rollback();
  118. return false;
  119. }
  120. }
  121. public function del($role_id)
  122. {
  123. //如果角色下有用户,则不能删除
  124. if(UserRoleModel::getUserRoleCount($role_id) > 0){
  125. $this->error = '当前角色下存在用户,不允许删除';
  126. return false;
  127. }
  128. RoleAccess::destroy(['role_id', '=', $role_id]);
  129. return self::destroy(['role_id', '=', $role_id]);
  130. }
  131. }