EMPTY_ACTION_BAR_NAME]; } if ($this->parent < 0) { return ["code" => INVALID_ACTION_PARENT]; } if ($this->link_type == "") { $this->link_type = LINK_TYPE_URL; } $name = $params["name"]; $result = $this->firstOrCreate(["name" => $name], $params); unset($result["is_del"]); unset($result["created_user_id"]); unset($result["updated_user_id"]); return ["code" => SUCCESS, "data" => $result]; } /** * 根据菜单的id更新一个菜单内容 * * @param array $params * @return string */ public function ModifyActionBar(array $params) { $update = []; $id = $params["id"]; if ($id == "") { return EMPTY_ACTION_BAR_ID; } if ($params["parent"] > 0) { $update["parent"] = $params["parent"]; } if (strlen($params["icon"]) > 0) { $update["icon"] = $params["icon"]; } if (strlen($params["link_type"]) > 0) { $update["link_type"] = $params["link_type"]; } if (strlen($params["link"]) > 0) { $update["link"] = $params["link"]; } if (strlen($params["name"]) > 0) { Log::debug("name is " . $params["name"]); $update["name"] = $params["name"]; } if (strlen($params["description"]) > 0) { $update["description"] = $params["description"]; } if (count($update) == 0) { return NOTHING_UPDATE; } // check the data if exist in system. $actionBar = $this->where("id", $id)->where("is_del", false)->first(); if (!$actionBar) { return INVALID_ACTION_BAR_ID; } $this->where("id", $id)->where("is_del", false)->update($update); return SUCCESS; } /** * 删除一个菜单项目,这里存在一个问题,就是如果删除上层的菜单,下层的菜单也应该都被删掉。但是可以通过parent来控制 * 下层菜单不被加载出来 * * @param array $params * @return int */ public function DeleteActionBar(array $params) { $id = $params["id"]; if ($id < 1) { return EMPTY_ACTION_BAR_ID; } $row = $this->where("id", $id)->where("is_del", false)->update(["is_del" => true]); if ($row == 0) { return INVALID_ACTION_BAR_ID; } return SUCCESS; } /** * 列出所有的菜单数据 * * @param array $ids * @param bool $isAll * @param array $selector * @return array */ public function ListActionBar(array $ids, bool $isAll, array $selector) { if (count($ids) < 1 && !$isAll) { return []; } $opera = $this->select($selector); if (!$isAll) { $opera = $opera->whereIn("id", $ids); } $data = $opera->where("is_del", false)->orderBy("id", "asc")->get(); return $data; } public function ListActionBar_KV(array $ids, bool $isAll, array $selector) { $bars = $this->ListActionBar($ids, $isAll, $selector); if (count($bars) == 0) { return []; } $result = []; foreach ($bars as $bar) { $result[$bar["id"]] = $bar; } return $result; } /** * 将菜单数据重新组合成有层级的数据,将不同等级的菜单放进多维数组里。 * parent=0的放在第一级。 * * @param array $ids * @param bool $isAll * @return array */ public function LoadActionBarByIds_Format(array $ids, bool $isAll) { if (count($ids) < 1 && !$isAll) { return []; } $opera = $this->select("id", "parent", "name", "description", "icon", "link_type", "link", "status"); if (!$isAll) { $opera = $opera->whereIn("id", $ids); } $bars = $opera->orderBy("parent", "asc")->orderBy("id", "asc")->where("is_del", false)->get(); $result = $this->AssembleBars(json_decode(json_encode($bars), true)); return $result; } /** * 把actionBar根据parent组成成有上下级层次的结构,起始 parent 为 0。 * 这里存在的问题是,返回出去的数据,如果只有一个key,并且key为0时, * 数据会是一个array,所以外层调用的地方有必要时可以强制转为object,如: * 当渴望的数据是: { "1":{ "0":[ { "id":1, "parent":0, "name":"abdfdf", "description":null, "icon":"hhhe", "link_type":null, "link":null } ] } } * 得到的却是: { "1":[ [ { "id":1, "parent":0, "name":"abdfdf", "description":null, "icon":"hhhe", "link_type":null, "link":null } ] ] } * * @param array $bars * @return array */ public function AssembleBars(array $bars) { // 开始将菜单数据重新组合成有层级的数据 $tmpRes = []; foreach ($bars as $bar) { $parent = $bar["parent"]; if (!key_exists($parent, $tmpRes)) { $tmpRes[$parent] = []; } $arr = $tmpRes[$parent]; array_push($arr, $bar); $tmpRes[$parent] = $arr; } // 消除没有上层的菜单,或者消除上层菜单无效的菜单数据 $result = []; $this->ClearInvalidActionBars(["0"], $tmpRes, $result); return $result; } /** * 消除没有上层的菜单,或者消除上层菜单无效的菜单数据 * * @param array $ids * @param array $data * @param $newData */ private function ClearInvalidActionBars(array $ids, array $data, &$newData) { if (count($ids) == 0) { return; } $newIds = []; foreach ($ids as $id) { if (!key_exists($id, $data)) { continue; } $newData[$id] = $data[$id]; $bars = $data[$id]; foreach ($bars as $bar) { array_push($newIds, $bar["id"]); } } $this->ClearInvalidActionBars($newIds, $data, $newData); return; } }