ActionBar.php 3.8 KB

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