UserActionBar.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\Log;
  5. class UserActionBar extends Model
  6. {
  7. protected $table = "user_action_bars";
  8. protected $fillable = ["owner_type", "owner_id", "bar_id"];
  9. public $timestamps = false;
  10. /**
  11. * 配置角色/用户/群组有权限的action bars,当前的做法是,
  12. * 每次更新都先把ownerId和ownerType匹配的数据全部标记删除,
  13. * 然后批量插入新的配置的action bars数据行。
  14. *
  15. * @param $ownerType 拥有者用户类型
  16. * @param $ownerId 拥有者用户id
  17. * @param $barId 分配的action bars
  18. * @return array
  19. */
  20. public function AssignUserActionBar($ownerType, $ownerId, $barIds)
  21. {
  22. if ($ownerType == "") {
  23. return ["code" => EMPTY_OWNER_TYPE];
  24. }
  25. if ($ownerId < 1) {
  26. return ["code" => EMPTY_OWNER_ID];
  27. }
  28. // 删除所有ownerType 和 ownerId
  29. $this->where("owner_type", $ownerType)->where("owner_id", $ownerId)->where("is_del", false)
  30. ->update(["is_del" => true]);
  31. // 重新添加ownerType 和 ownerId的数据
  32. if (count($barIds) < 1) {
  33. return ["code" => SUCCESS, "data" => []];
  34. }
  35. $data = [];
  36. foreach ($barIds as $barId) {
  37. array_push($data, array("owner_type" => $ownerType, "owner_id" => $ownerId, "bar_id" => $barId, "is_del" => false));
  38. }
  39. $this->insert($data);
  40. // // 查出批量插入的数据,并返回给前端
  41. // $new_data = $this->select("id", "owner_type", "owner_id", "bar_id", "status")
  42. // ->where("owner_type", $ownerType)->where("owner_id", $ownerId)->where("is_del", false)->get();
  43. //
  44. // $newBarIds = [];
  45. // foreach ($new_data as $item) {
  46. // array_push($newBarIds, $item["bar_id"]);
  47. // }
  48. // $barModel = new ActionBar();
  49. // $barData = $barModel->LoadActionBarByIds_Format($barIds, false, ["id", "name"]);
  50. //// $barData = $barModel->ListActionBar_KV($barIds, false, ["id", "name"]);
  51. // return ["code" => SUCCESS, "data" => ["bars" => $new_data, "barInfo" => $barData]];
  52. return ["code" => SUCCESS];
  53. }
  54. /**
  55. * 根据多个类型,和多个id,加载菜单栏信息
  56. *
  57. * @param array $ownerTypes
  58. * @param array $ownerIds
  59. * @return array
  60. */
  61. public function ListActionBarIds(array $ownerTypes, array $ownerIds)
  62. {
  63. $opera = $this->select("owner_type", "owner_id", "bar_id");
  64. if (count($ownerTypes) > 0) {
  65. $opera = $opera->whereIn("owner_type", $ownerTypes);
  66. }
  67. if (count($ownerIds) > 0) {
  68. $opera = $opera->whereIn("owner_id", $ownerIds);
  69. }
  70. $userBars = $opera->where("is_del", false)->get();
  71. if (count($userBars) == 0) {
  72. return [];
  73. }
  74. return $userBars;
  75. }
  76. /**
  77. * 列出actionBars,并且根据parent组合成分层次的数据结构
  78. *
  79. * @param array $ownerTypes
  80. * @param array $ownerIds
  81. * @return array
  82. */
  83. public function ListActionBarIds_Format(array $ownerTypes, array $ownerIds)
  84. {
  85. // 先收集所有的actionBars,并且根据角色分类
  86. $userBars = $this->ListActionBarIds($ownerTypes, $ownerIds);
  87. if (count($userBars) < 1) {
  88. return [];
  89. }
  90. // 收集所有的bar id
  91. $barIds = [];
  92. foreach ($userBars as $bar) {
  93. array_push($barIds, $bar["bar_id"]);
  94. }
  95. // 加载actionBar 的信息
  96. $bar = new ActionBar();
  97. $barInfo = $bar->ListActionBar_KV([], true, ["id", "parent", "name", "description", "icon", "link_type", "link"]);
  98. // 根据角色分类收集各自的action bar
  99. $roleActionBars = [];
  100. foreach ($userBars as $userBar) {
  101. $key = strval($userBar["owner_id"]);
  102. $barId = $userBar["bar_id"];
  103. if (!key_exists($key, $roleActionBars)) {
  104. $roleActionBars[$key] = [];
  105. }
  106. $actionBars = $roleActionBars[$key];
  107. if (key_exists($barId, $barInfo)) {
  108. array_push($actionBars, $barInfo[$barId]);
  109. }
  110. $roleActionBars[$key] = $actionBars;
  111. }
  112. // 处理每个角色有用的actionBar,并分层
  113. $result = [];
  114. foreach ($roleActionBars as $key => $value) {
  115. $assembleValue = $bar->AssembleBars($value);
  116. $result[$key] = (object)$assembleValue;
  117. }
  118. return $result;
  119. }
  120. }