ActionBar.php 4.2 KB

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