123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Facades\Log;
- class UserActionBar extends Model
- {
- protected $table = "user_action_bars";
- protected $fillable = ["owner_type", "owner_id", "bar_id"];
- public $timestamps = false;
- /**
- * 配置角色/用户/群组有权限的action bars,当前的做法是,
- * 每次更新都先把ownerId和ownerType匹配的数据全部标记删除,
- * 然后批量插入新的配置的action bars数据行。
- *
- * @param $ownerType 拥有者用户类型
- * @param $ownerId 拥有者用户id
- * @param $barId 分配的action bars
- * @return array
- */
- public function AssignUserActionBar($ownerType, $ownerId, $barIds)
- {
- if ($ownerType == "") {
- return ["code" => EMPTY_OWNER_TYPE];
- }
- if ($ownerId < 1) {
- return ["code" => EMPTY_OWNER_ID];
- }
- // 删除所有ownerType 和 ownerId
- $this->where("owner_type", $ownerType)->where("owner_id", $ownerId)->where("is_del", false)
- ->update(["is_del" => true]);
- // 重新添加ownerType 和 ownerId的数据
- if (count($barIds) < 1) {
- return ["code" => SUCCESS, "data" => []];
- }
- $data = [];
- foreach ($barIds as $barId) {
- array_push($data, array("owner_type" => $ownerType, "owner_id" => $ownerId, "bar_id" => $barId, "is_del" => false));
- }
- $this->insert($data);
- // // 查出批量插入的数据,并返回给前端
- // $new_data = $this->select("id", "owner_type", "owner_id", "bar_id", "status")
- // ->where("owner_type", $ownerType)->where("owner_id", $ownerId)->where("is_del", false)->get();
- //
- // $newBarIds = [];
- // foreach ($new_data as $item) {
- // array_push($newBarIds, $item["bar_id"]);
- // }
- // $barModel = new ActionBar();
- // $barData = $barModel->LoadActionBarByIds_Format($barIds, false, ["id", "name"]);
- //// $barData = $barModel->ListActionBar_KV($barIds, false, ["id", "name"]);
- // return ["code" => SUCCESS, "data" => ["bars" => $new_data, "barInfo" => $barData]];
- return ["code" => SUCCESS];
- }
- /**
- * 根据多个类型,和多个id,加载菜单栏信息
- *
- * @param array $ownerTypes
- * @param array $ownerIds
- * @return array
- */
- public function ListActionBarIds(array $ownerTypes, array $ownerIds)
- {
- $opera = $this->select("owner_type", "owner_id", "bar_id");
- if (count($ownerTypes) > 0) {
- $opera = $opera->whereIn("owner_type", $ownerTypes);
- }
- if (count($ownerIds) > 0) {
- $opera = $opera->whereIn("owner_id", $ownerIds);
- }
- $userBars = $opera->where("is_del", false)->get();
- if (count($userBars) == 0) {
- return [];
- }
- return $userBars;
- }
- /**
- * 列出actionBars,并且根据parent组合成分层次的数据结构
- *
- * @param array $ownerTypes
- * @param array $ownerIds
- * @return array
- */
- public function ListActionBarIds_Format(array $ownerTypes, array $ownerIds)
- {
- // 先收集所有的actionBars,并且根据角色分类
- $userBars = $this->ListActionBarIds($ownerTypes, $ownerIds);
- if (count($userBars) < 1) {
- return [];
- }
- // 收集所有的bar id
- $barIds = [];
- foreach ($userBars as $bar) {
- array_push($barIds, $bar["bar_id"]);
- }
- // 加载actionBar 的信息
- $bar = new ActionBar();
- $barInfo = $bar->ListActionBar_KV([], true, ["id", "parent", "name", "description", "icon", "link_type", "link"]);
- // 根据角色分类收集各自的action bar
- $roleActionBars = [];
- foreach ($userBars as $userBar) {
- $key = strval($userBar["owner_id"]);
- $barId = $userBar["bar_id"];
- if (!key_exists($key, $roleActionBars)) {
- $roleActionBars[$key] = [];
- }
- $actionBars = $roleActionBars[$key];
- if (key_exists($barId, $barInfo)) {
- array_push($actionBars, $barInfo[$barId]);
- }
- $roleActionBars[$key] = $actionBars;
- }
- // 处理每个角色有用的actionBar,并分层
- $result = [];
- foreach ($roleActionBars as $key => $value) {
- $assembleValue = $bar->AssembleBars($value);
- $result[$key] = (object)$assembleValue;
- }
- return $result;
- }
- }
|