123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <?php
- namespace App\Models;
- 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;
- /**
- * @param array $params
- * owner_id 拥有者用户的id,0表示数据模板
- * parent ActionBar 的上一级id
- * level 表示当前是第几级,冗余字段
- * icon ActionBar的图标
- * link_type ActionBar的时间类型,当期只有url跳转
- * link 根据link_type需要的数据,当前为url,也可以是页面上的某个id
- * @return string
- */
- public function AddActionBar(array $params)
- {
- $this->parent = $params["parent"]; // default 0
- $this->level = $params["level"];
- $this->icon = $params["icon"];
- $link_type = $params["link_type"];
- $this->link = $params["link"];
- $this->name = $params["name"];
- $this->description = $params["description"];
- $this->status = "normal";
- if ($this->parent < 0) {
- return "invalid parent: " . $this->parent;
- }
- if ($this->level < 0) {
- return "invalid level: " . $this->level;
- }
- $this->link_type = trim($link_type);
- if ($this->link_type == "") {
- $this->link_type = LINK_TYPE_URL;
- }
- $result = $this->save();
- if ($result) {
- Log::debug("AddActionBar success with params: " . json_encode($params));
- return "success";
- } else {
- Log::error("AddActionBar failed with params: " . json_encode($params));
- return "AddActionBar but fail";
- }
- }
- /**
- * 根据菜单的id更新一个菜单内容
- *
- * @param array $params
- * @return string
- */
- public function ModifyActionBar(array $params)
- {
- $update = [];
- $id = $params["id"];
- if ($id == "") {
- return "invalid id";
- }
- if ($params["parent"] > 0) {
- $update["parent"] = $params["parent"];
- }
- if ($params["level"] > 0) {
- $update["level"] = $params["level"];
- }
- 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 to update";
- }
- $num = $this->where("id", $id)
- ->where("status", "normal")
- ->where("is_del", false)
- ->update($update);
- return "success";
- }
- // todo.
- /**
- * 删除一个菜单项目,这里存在一个问题,就是如果删除上层的菜单,下层的菜单也应该都被删掉。先留空。
- *
- * @param array $params
- * @return string
- */
- public function DeleteActionBar(array $params)
- {
- $id = $params["id"];
- if ($id == "") {
- return "invalid id";
- }
- $num = $this->where("id", $id)
- ->where("status", "normal")
- ->where("is_del", false)
- ->update(["is_del" => true]);
- return "success";
- }
- /**
- * 查出当前用户可以访问的所有action_bar的信息
- *
- * @param array $params
- * @return array
- */
- public function ListActionBar(array $params)
- {
- $uid = $params["uid"];
- // 收集当前用户有权限的bar_id
- $userActionBar = new UserActionBarController();
- $bar_ids = $userActionBar->ListActionBarIds($uid);
- if (count($bar_ids) == 0) {
- return [];
- }
- // 查出bars的信息
- $result = $this->select("id", "parent", "level", "name", "description", "icon", "link_type", "link")
- ->where("id", $bar_ids)
- ->where("status", "normal")
- ->where("is_del", false)
- ->all();
- return $result;
- }
- }
|