123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?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;
- protected $fillable = ["name"];
- /**
- * @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 "invalid 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 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 UserActionBar();
- $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;
- }
- }
|