ActionBar.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. namespace App\Models;
  3. use foo\bar;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Log;
  7. const LINK_TYPE_URL = "url";
  8. class ActionBar extends Model
  9. {
  10. protected $table = "action_bars";
  11. public $timestamps = false;
  12. protected $fillable = ["parent", "icon", "link_type", "link", "name", "description"];
  13. /**
  14. * @param array $params
  15. * parent ActionBar 的上一级id
  16. * icon ActionBar的图标
  17. * link_type ActionBar的时间类型,当期只有url跳转
  18. * link 根据link_type需要的数据,当前为url,也可以是页面上的某个id
  19. * name ActionBar的名字
  20. * description ActionBar的的描述
  21. *
  22. * @param array $params
  23. * @return array
  24. */
  25. public function AddActionBar(array $params)
  26. {
  27. if ($params["name"] == "") {
  28. return ["code" => EMPTY_ACTION_BAR_NAME];
  29. }
  30. if ($this->parent < 0) {
  31. return ["code" => INVALID_ACTION_PARENT];
  32. }
  33. if ($this->link_type == "") {
  34. $this->link_type = LINK_TYPE_URL;
  35. }
  36. $name = $params["name"];
  37. $result = $this->firstOrCreate(["name" => $name], $params);
  38. unset($result["is_del"]);
  39. unset($result["created_user_id"]);
  40. unset($result["updated_user_id"]);
  41. return ["code" => SUCCESS, "data" => $result];
  42. }
  43. /**
  44. * 根据菜单的id更新一个菜单内容
  45. *
  46. * @param array $params
  47. * @return string
  48. */
  49. public function ModifyActionBar(array $params)
  50. {
  51. $update = [];
  52. $id = $params["id"];
  53. if ($id == "") {
  54. return EMPTY_ACTION_BAR_ID;
  55. }
  56. if ($params["parent"] > 0) {
  57. $update["parent"] = $params["parent"];
  58. }
  59. if (strlen($params["icon"]) > 0) {
  60. $update["icon"] = $params["icon"];
  61. }
  62. if (strlen($params["link_type"]) > 0) {
  63. $update["link_type"] = $params["link_type"];
  64. }
  65. if (strlen($params["link"]) > 0) {
  66. $update["link"] = $params["link"];
  67. }
  68. if (strlen($params["name"]) > 0) {
  69. Log::debug("name is " . $params["name"]);
  70. $update["name"] = $params["name"];
  71. }
  72. if (strlen($params["description"]) > 0) {
  73. $update["description"] = $params["description"];
  74. }
  75. if (count($update) == 0) {
  76. return NOTHING_UPDATE;
  77. }
  78. // check the data if exist in system.
  79. $actionBar = $this->where("id", $id)->where("is_del", false)->first();
  80. if (!$actionBar) {
  81. return INVALID_ACTION_BAR_ID;
  82. }
  83. $this->where("id", $id)->where("is_del", false)->update($update);
  84. return SUCCESS;
  85. }
  86. /**
  87. * 删除一个菜单项目,这里存在一个问题,就是如果删除上层的菜单,下层的菜单也应该都被删掉。但是可以通过parent来控制
  88. * 下层菜单不被加载出来
  89. *
  90. * @param array $params
  91. * @return int
  92. */
  93. public function DeleteActionBar(array $params)
  94. {
  95. $id = $params["id"];
  96. if ($id < 1) {
  97. return EMPTY_ACTION_BAR_ID;
  98. }
  99. $row = $this->where("id", $id)->where("is_del", false)->update(["is_del" => true]);
  100. if ($row == 0) {
  101. return INVALID_ACTION_BAR_ID;
  102. }
  103. return SUCCESS;
  104. }
  105. /**
  106. * 列出所有的菜单数据
  107. *
  108. * @param array $ids
  109. * @param bool $isAll
  110. * @param array $selector
  111. * @return array
  112. */
  113. public function ListActionBar(array $ids, bool $isAll, array $selector)
  114. {
  115. if (count($ids) < 1 && !$isAll) {
  116. return [];
  117. }
  118. $opera = $this->select($selector);
  119. if (!$isAll) {
  120. $opera = $opera->whereIn("id", $ids);
  121. }
  122. $data = $opera->where("is_del", false)->orderBy("id", "asc")->get();
  123. return $data;
  124. }
  125. public function ListActionBar_KV(array $ids, bool $isAll, array $selector)
  126. {
  127. $bars = $this->ListActionBar($ids, $isAll, $selector);
  128. if (count($bars) == 0) {
  129. return [];
  130. }
  131. $result = [];
  132. foreach ($bars as $bar) {
  133. $result[$bar["id"]] = $bar;
  134. }
  135. return $result;
  136. }
  137. /**
  138. * 将菜单数据重新组合成有层级的数据,将不同等级的菜单放进多维数组里。
  139. * parent=0的放在第一级。
  140. *
  141. * @param array $ids
  142. * @param bool $isAll
  143. * @return array
  144. */
  145. public function LoadActionBarByIds_Format(array $ids, bool $isAll)
  146. {
  147. if (count($ids) < 1 && !$isAll) {
  148. return [];
  149. }
  150. $opera = $this->select("id", "parent", "name", "description", "icon", "link_type", "link", "status");
  151. if (!$isAll) {
  152. $opera = $opera->whereIn("id", $ids);
  153. }
  154. $bars = $opera->orderBy("parent", "asc")->orderBy("id", "asc")->where("is_del", false)->get();
  155. $result = $this->AssembleBars(json_decode(json_encode($bars), true));
  156. return $result;
  157. }
  158. /**
  159. * 把actionBar根据parent组成成有上下级层次的结构,起始 parent 为 0。
  160. * 这里存在的问题是,返回出去的数据,如果只有一个key,并且key为0时,
  161. * 数据会是一个array,所以外层调用的地方有必要时可以强制转为object,如:
  162. * 当渴望的数据是:
  163. {
  164. "1":{
  165. "0":[
  166. {
  167. "id":1,
  168. "parent":0,
  169. "name":"abdfdf",
  170. "description":null,
  171. "icon":"hhhe",
  172. "link_type":null,
  173. "link":null
  174. }
  175. ]
  176. }
  177. }
  178. * 得到的却是:
  179. {
  180. "1":[
  181. [
  182. {
  183. "id":1,
  184. "parent":0,
  185. "name":"abdfdf",
  186. "description":null,
  187. "icon":"hhhe",
  188. "link_type":null,
  189. "link":null
  190. }
  191. ]
  192. ]
  193. }
  194. *
  195. * @param array $bars
  196. * @return array
  197. */
  198. public function AssembleBars(array $bars)
  199. {
  200. // 开始将菜单数据重新组合成有层级的数据
  201. $tmpRes = [];
  202. foreach ($bars as $bar) {
  203. $parent = $bar["parent"];
  204. if (!key_exists($parent, $tmpRes)) {
  205. $tmpRes[$parent] = [];
  206. }
  207. $arr = $tmpRes[$parent];
  208. array_push($arr, $bar);
  209. $tmpRes[$parent] = $arr;
  210. }
  211. // 消除没有上层的菜单,或者消除上层菜单无效的菜单数据
  212. $result = [];
  213. $this->ClearInvalidActionBars(["0"], $tmpRes, $result);
  214. return $result;
  215. }
  216. /**
  217. * 消除没有上层的菜单,或者消除上层菜单无效的菜单数据
  218. *
  219. * @param array $ids
  220. * @param array $data
  221. * @param $newData
  222. */
  223. private function ClearInvalidActionBars(array $ids, array $data, &$newData)
  224. {
  225. if (count($ids) == 0) {
  226. return;
  227. }
  228. $newIds = [];
  229. foreach ($ids as $id) {
  230. if (!key_exists($id, $data)) {
  231. continue;
  232. }
  233. $newData[$id] = $data[$id];
  234. $bars = $data[$id];
  235. foreach ($bars as $bar) {
  236. array_push($newIds, $bar["id"]);
  237. }
  238. }
  239. $this->ClearInvalidActionBars($newIds, $data, $newData);
  240. return;
  241. }
  242. }