ActionBar.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace App\Models;
  3. use foo\bar;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Log;
  7. const LINK_TYPE_URL = "url";
  8. class ActionBar extends Model
  9. {
  10. protected $table = "action_bars";
  11. public $timestamps = false;
  12. protected $fillable = ["parent", "icon", "link_type", "link", "name", "description"];
  13. /**
  14. * @param array $params
  15. * parent ActionBar 的上一级id
  16. * icon ActionBar的图标
  17. * link_type ActionBar的时间类型,当期只有url跳转
  18. * link 根据link_type需要的数据,当前为url,也可以是页面上的某个id
  19. * name ActionBar的名字
  20. * description ActionBar的的描述
  21. *
  22. * @param array $params
  23. * @return array
  24. */
  25. public function AddActionBar(array $params)
  26. {
  27. if ($params["name"] == "") {
  28. return ["code" => EMPTY_ACTION_BAR_NAME];
  29. }
  30. if ($this->parent < 0) {
  31. return ["code" => INVALID_ACTION_PARENT];
  32. }
  33. if ($this->link_type == "") {
  34. $this->link_type = LINK_TYPE_URL;
  35. }
  36. $name = $params["name"];
  37. $result = $this->firstOrCreate(["name" => $name], $params);
  38. unset($result["is_del"]);
  39. unset($result["created_user_id"]);
  40. unset($result["updated_user_id"]);
  41. return ["code" => SUCCESS, "data" => $result];
  42. }
  43. /**
  44. * 根据菜单的id更新一个菜单内容
  45. *
  46. * @param array $params
  47. * @return string
  48. */
  49. public function ModifyActionBar(array $params)
  50. {
  51. $update = [];
  52. $id = $params["id"];
  53. if ($id == "") {
  54. return EMPTY_ACTION_BAR_ID;
  55. }
  56. if ($params["parent"] > 0) {
  57. $update["parent"] = $params["parent"];
  58. }
  59. if (strlen($params["icon"]) > 0) {
  60. $update["icon"] = $params["icon"];
  61. }
  62. if (strlen($params["link_type"]) > 0) {
  63. $update["link_type"] = $params["link_type"];
  64. }
  65. if (strlen($params["link"]) > 0) {
  66. $update["link"] = $params["link"];
  67. }
  68. if (strlen($params["name"]) > 0) {
  69. Log::debug("name is " . $params["name"]);
  70. $update["name"] = $params["name"];
  71. }
  72. if (strlen($params["description"]) > 0) {
  73. $update["description"] = $params["description"];
  74. }
  75. if (count($update) == 0) {
  76. return NOTHING_UPDATE;
  77. }
  78. // check the data if exist in system.
  79. $actionBar = $this->where("id", $id)->where("is_del", false)->first();
  80. if (!$actionBar) {
  81. return INVALID_ACTION_BAR_ID;
  82. }
  83. $this->where("id", $id)->where("is_del", false)->update($update);
  84. return SUCCESS;
  85. }
  86. /**
  87. * 删除一个菜单项目,这里存在一个问题,就是如果删除上层的菜单,下层的菜单也应该都被删掉。但是可以通过parent来控制
  88. * 下层菜单不被加载出来
  89. *
  90. * @param array $params
  91. * @return int
  92. */
  93. public function DeleteActionBar(array $params)
  94. {
  95. $id = $params["id"];
  96. if ($id < 1) {
  97. return EMPTY_ACTION_BAR_ID;
  98. }
  99. $row = $this->where("id", $id)->where("is_del", false)->update(["is_del" => true]);
  100. if ($row == 0) {
  101. return INVALID_ACTION_BAR_ID;
  102. }
  103. return SUCCESS;
  104. }
  105. /**
  106. * 查出当前用户可以访问的所有action_bar的信息
  107. *
  108. * @param array $params
  109. * @return array
  110. */
  111. public function ListActionBar(array $params)
  112. {
  113. $uid = $params["uid"];
  114. // 收集当前用户有权限的bar_id
  115. $userActionBar = new UserActionBar();
  116. $bars = $userActionBar->ListActionBarIds([""], [$uid]);
  117. if (count($bars) == 0) {
  118. return ["code" => SUCCESS, "data" => []];
  119. }
  120. $barIds=[];
  121. foreach ($bars as $bar){
  122. array_push($barIds, $bar["bar_id"]);
  123. }
  124. // 查出bars的信息
  125. $result = $this->select("id", "parent", "name", "description", "icon", "link_type", "link")
  126. ->whereIn("id", $bars)
  127. ->where("status", "normal")->where("is_del", false)->get();
  128. return ["code" => SUCCESS, "data" => $result];
  129. }
  130. /**
  131. * 获取菜单栏的数据信息
  132. *
  133. * @param $ids
  134. * @return mixed
  135. */
  136. public function LoadActionBarByIds_Format($ids)
  137. {
  138. $bars = $this->select("id","parent", "name","description", "icon", "link_type", "link", "status")
  139. ->whereIn("id", $ids)->where("is_del", false)->get();
  140. return $bars;
  141. }
  142. }