ActionBar.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 UserActionBar();
  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. }
  137. class UserActionBar extends Model
  138. {
  139. protected $table = "user_action_bars";
  140. public $timestamps = false;
  141. public function AddUserActionBar($uid, $barId)
  142. {
  143. if ($uid == "") {
  144. return "empty user id";
  145. }
  146. if ($barId == "") {
  147. return "empty action bar id";
  148. }
  149. //INSERT INTO demo_in(a,b,c) SELECT 123, 2, 4 FROM DUAL WHERE NOT EXISTS(SELECT c FROM demo_in WHERE c = 4);
  150. $sql = sprintf("insert into user_action_bars(user_id, bar_id)
  151. select ?, ? from temp_uab
  152. where not exists(
  153. select id from user_action_bars
  154. where user_id = ? and bar_id = ? and is_del = false);");
  155. $result = DB::insert($sql, [$uid, $barId, $uid, $barId]);
  156. Log::debug("AddUserActionBar " . $result);
  157. return "success";
  158. }
  159. public function RemoveUserActionBar($uid, $barId)
  160. {
  161. if ($uid == "") {
  162. return "empty user id";
  163. }
  164. if ($barId == "") {
  165. return "empty action bar id";
  166. }
  167. $data = $this->where("user_id", $uid)
  168. ->where("bar_id", $barId)
  169. ->where("is_del", false)
  170. ->first();
  171. if (!$data) {
  172. return "nothing to remove";
  173. }
  174. $this->is_del = true;
  175. $this->where("user_id", $uid)
  176. ->where("bar_id", $barId)
  177. ->where("is_del", false)
  178. ->update(["is_del" => true]);
  179. return "success";
  180. }
  181. /**
  182. * @param $uid
  183. * @return array
  184. */
  185. public function ListActionBarIds($uid)
  186. {
  187. $bars = $this->select("bar_id")
  188. ->where("user_id", $uid)
  189. ->where("status", "normal")
  190. ->where("is_del", false)
  191. ->get();
  192. if (count($bars) == 0) {
  193. return [];
  194. }
  195. $bar_ids = [];
  196. foreach ($bar_ids as $bar_id) {
  197. array_push($bar_ids, $bar_id->bar_id);
  198. }
  199. return $bar_ids;
  200. }
  201. }