ActionBar.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Log;
  6. const LINK_TYPE_URL = "url";
  7. class ActionBar extends Model
  8. {
  9. protected $table = "action_bars";
  10. public $timestamps = false;
  11. /**
  12. * @param array $params
  13. * owner_id 拥有者用户的id,0表示数据模板
  14. * parent ActionBar 的上一级id
  15. * level 表示当前是第几级,冗余字段
  16. * icon ActionBar的图标
  17. * link_type ActionBar的时间类型,当期只有url跳转
  18. * link 根据link_type需要的数据,当前为url,也可以是页面上的某个id
  19. * @return string
  20. */
  21. public function AddActionBar(array $params)
  22. {
  23. $this->parent = $params["parent"]; // default 0
  24. $this->level = $params["level"];
  25. $this->icon = $params["icon"];
  26. $link_type = $params["link_type"];
  27. $this->link = $params["link"];
  28. $this->name = $params["name"];
  29. $this->description = $params["description"];
  30. $this->status = "normal";
  31. if ($this->parent < 0) {
  32. return "invalid parent: " . $this->parent;
  33. }
  34. if ($this->level < 0) {
  35. return "invalid level: " . $this->level;
  36. }
  37. $this->link_type = trim($link_type);
  38. if ($this->link_type == "") {
  39. $this->link_type = LINK_TYPE_URL;
  40. }
  41. $result = $this->save();
  42. if ($result) {
  43. Log::debug("AddActionBar success with params: " . json_encode($params));
  44. return "success";
  45. } else {
  46. Log::error("AddActionBar failed with params: " . json_encode($params));
  47. return "AddActionBar but fail";
  48. }
  49. }
  50. /**
  51. * 根据菜单的id更新一个菜单内容
  52. *
  53. * @param array $params
  54. * @return string
  55. */
  56. public function ModifyActionBar(array $params)
  57. {
  58. $update = [];
  59. $id = $params["id"];
  60. if ($id == "") {
  61. return "invalid id";
  62. }
  63. if ($params["parent"] > 0) {
  64. $update["parent"] = $params["parent"];
  65. }
  66. if ($params["level"] > 0) {
  67. $update["level"] = $params["level"];
  68. }
  69. if (strlen($params["icon"]) > 0) {
  70. $update["icon"] = $params["icon"];
  71. }
  72. if (strlen($params["link_type"]) > 0) {
  73. $update["link_type"] = $params["link_type"];
  74. }
  75. if (strlen($params["link"]) > 0) {
  76. $update["link"] = $params["link"];
  77. }
  78. if (strlen($params["name"]) > 0) {
  79. Log::debug("name is " . $params["name"]);
  80. $update["name"] = $params["name"];
  81. }
  82. if (strlen($params["description"]) > 0) {
  83. $update["description"] = $params["description"];
  84. }
  85. if (count($update) == 0) {
  86. return "nothing to update";
  87. }
  88. $num = $this->where("id", $id)
  89. ->where("status", "normal")
  90. ->where("is_del", false)
  91. ->update($update);
  92. return "success";
  93. }
  94. // todo.
  95. /**
  96. * 删除一个菜单项目,这里存在一个问题,就是如果删除上层的菜单,下层的菜单也应该都被删掉。先留空。
  97. *
  98. * @param array $params
  99. * @return string
  100. */
  101. public function DeleteActionBar(array $params)
  102. {
  103. $id = $params["id"];
  104. if ($id == "") {
  105. return "invalid id";
  106. }
  107. $num = $this->where("id", $id)
  108. ->where("status", "normal")
  109. ->where("is_del", false)
  110. ->update(["is_del" => true]);
  111. return "success";
  112. }
  113. /**
  114. * 查出当前用户可以访问的所有action_bar的信息
  115. *
  116. * @param array $params
  117. * @return array
  118. */
  119. public function ListActionBar(array $params)
  120. {
  121. $uid = $params["uid"];
  122. // 收集当前用户有权限的bar_id
  123. $userActionBar = new UserActionBarController();
  124. $bar_ids = $userActionBar->ListActionBarIds($uid);
  125. if (count($bar_ids) == 0) {
  126. return [];
  127. }
  128. // 查出bars的信息
  129. $result = $this->select("id", "parent", "level", "name", "description", "icon", "link_type", "link")
  130. ->where("id", $bar_ids)
  131. ->where("status", "normal")
  132. ->where("is_del", false)
  133. ->all();
  134. return $result;
  135. }
  136. }