|
@@ -117,45 +117,148 @@ class ActionBar extends Model
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 查出当前用户可以访问的所有action_bar的信息
|
|
|
+ * 列出所有的菜单数据
|
|
|
*
|
|
|
- * @param array $params
|
|
|
+ * @param array $ids
|
|
|
+ * @param bool $isAll
|
|
|
+ * @param array $selector
|
|
|
* @return array
|
|
|
*/
|
|
|
- public function ListActionBar(array $params)
|
|
|
+ public function ListActionBar(array $ids, bool $isAll, array $selector)
|
|
|
{
|
|
|
- $uid = $params["uid"];
|
|
|
+ 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;
|
|
|
+ }
|
|
|
|
|
|
- // 收集当前用户有权限的bar_id
|
|
|
- $userActionBar = new UserActionBar();
|
|
|
- $bars = $userActionBar->ListActionBarIds([""], [$uid]);
|
|
|
+ public function ListActionBar_KV(array $ids, bool $isAll, array $selector)
|
|
|
+ {
|
|
|
+ $bars = $this->ListActionBar($ids, $isAll, $selector);
|
|
|
if (count($bars) == 0) {
|
|
|
- return ["code" => SUCCESS, "data" => []];
|
|
|
+ return [];
|
|
|
+ }
|
|
|
+ $result = [];
|
|
|
+ foreach ($bars as $bar) {
|
|
|
+ $result[$bar["id"]] = $bar;
|
|
|
}
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
- $barIds=[];
|
|
|
- foreach ($bars as $bar){
|
|
|
- array_push($barIds, $bar["bar_id"]);
|
|
|
+ /**
|
|
|
+ * 将菜单数据重新组合成有层级的数据,将不同等级的菜单放进多维数组里。
|
|
|
+ * 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();
|
|
|
|
|
|
- // 查出bars的信息
|
|
|
- $result = $this->select("id", "parent", "name", "description", "icon", "link_type", "link")
|
|
|
- ->whereIn("id", $bars)
|
|
|
- ->where("status", "normal")->where("is_del", false)->get();
|
|
|
- return ["code" => SUCCESS, "data" => $result];
|
|
|
+ $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 $ids
|
|
|
- * @return mixed
|
|
|
+ * @param array $bars
|
|
|
+ * @return array
|
|
|
*/
|
|
|
- public function LoadActionBarByIds_Format($ids)
|
|
|
+ public function AssembleBars(array $bars)
|
|
|
{
|
|
|
- $bars = $this->select("id","parent", "name","description", "icon", "link_type", "link", "status")
|
|
|
- ->whereIn("id", $ids)->where("is_del", false)->get();
|
|
|
- return $bars;
|
|
|
+ // 开始将菜单数据重新组合成有层级的数据
|
|
|
+ $tmpRes = [];
|
|
|
+ foreach ($bars as $bar) {
|
|
|
+ $parent = $bar["parent"];
|
|
|
+ Log::debug($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;
|
|
|
}
|
|
|
}
|
|
|
|