ActionBar.php 4.1 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 = ["parent", "icon", "link_type", "link", "name", "description"];
  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 EMPTY_ACTION_BAR_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_UPDATE;
  76. }
  77. // check the data if exist in system.
  78. $actionBar = $this->where("id", $id)->where("is_del", false)->first();
  79. if (!$actionBar) {
  80. return INVALID_ACTION_BAR_ID;
  81. }
  82. $this->where("id", $id)->where("is_del", false)->update($update);
  83. return SUCCESS;
  84. }
  85. /**
  86. * 删除一个菜单项目,这里存在一个问题,就是如果删除上层的菜单,下层的菜单也应该都被删掉。但是可以通过parent来控制
  87. * 下层菜单不被加载出来
  88. *
  89. * @param array $params
  90. * @return int
  91. */
  92. public function DeleteActionBar(array $params)
  93. {
  94. $id = $params["id"];
  95. if ($id < 1) {
  96. return EMPTY_ACTION_BAR_ID;
  97. }
  98. $row = $this->where("id", $id)->where("is_del", false)->update(["is_del" => true]);
  99. if ($row == 0) {
  100. return INVALID_ACTION_BAR_ID;
  101. }
  102. return SUCCESS;
  103. }
  104. /**
  105. * 查出当前用户可以访问的所有action_bar的信息
  106. *
  107. * @param array $params
  108. * @return array
  109. */
  110. public function ListActionBar(array $params)
  111. {
  112. $uid = $params["uid"];
  113. // 收集当前用户有权限的bar_id
  114. $userActionBar = new UserActionBar();
  115. $bar_ids = $userActionBar->ListActionBarIds("", $uid);
  116. if (count($bar_ids) == 0) {
  117. return ["code" => SUCCESS, "data" => []];
  118. }
  119. // 查出bars的信息
  120. $result = $this->select("id", "parent", "name", "description", "icon", "link_type", "link")
  121. ->where("id", $bar_ids)
  122. ->where("status", "normal")->where("is_del", false)->get();
  123. return ["code" => SUCCESS, "data" => $result];
  124. }
  125. }