123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- <?php
- namespace App\Models;
- use foo\bar;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- const LINK_TYPE_URL = "url";
- class ActionBar extends Model
- {
- protected $table = "action_bars";
- public $timestamps = false;
- protected $fillable = ["parent", "icon", "link_type", "link", "name", "description"];
- /**
- * @param array $params
- * parent ActionBar 的上一级id
- * icon ActionBar的图标
- * link_type ActionBar的时间类型,当期只有url跳转
- * link 根据link_type需要的数据,当前为url,也可以是页面上的某个id
- * name ActionBar的名字
- * description ActionBar的的描述
- *
- * @param array $params
- * @return array
- */
- public function AddActionBar(array $params)
- {
- if ($params["name"] == "") {
- return ["code" => 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;
- }
- }
|