'default'], [['pid', 'level', 'is_addon', 'cate_id', 'sort', 'status', 'created_at', 'updated_at'], 'integer'], [['name'], 'string', 'max' => 64], [['title', 'addons_name'], 'string', 'max' => 200], [['app_id'], 'string', 'max' => 20], [['tree'], 'string', 'max' => 500], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'name' => '别名', 'title' => '标题', 'app_id' => '应用', 'addons_name' => '插件名称', 'pid' => '父级id', 'level' => '级别', 'is_addon' => '是否插件', 'sort' => '排序', 'tree' => '树', 'status' => '状态', 'created_at' => 'Created At', 'updated_at' => 'Updated At', 'cate_id' => 'Cate Id', ]; } /** * 场景 * * @return array */ public function scenarios() { $scenarios = parent::scenarios(); $scenarios['addonsBatchCreate'] = array_keys($this->attributeLabels()); return $scenarios; } /** * @param $attribute */ public function uniquName($attribute) { $model = self::find() ->where(['name' => $this->name, 'app_id' => $this->app_id, 'cate_id' => $this->cate_id]) ->andFilterWhere(['addons_name' => $this->addons_name]) ->one(); if ($model && $model->id != $this->id) { // $this->addError($attribute, '别名已存在'); } } /** * @param AuthItem $parent */ public function setParent(AuthItem $parent) { $this->parent = $parent; } /** * @param bool $insert * @param array $changedAttributes */ public function afterSave($insert, $changedAttributes) { if (!$this->isNewRecord) { AuthItemChild::updateAll(['name' => $this->name], ['item_id' => $this->id]); } parent::afterSave($insert, $changedAttributes); } /** * 关联模型 * @return \yii\db\ActiveQuery */ public function getChild() { return $this->hasMany(self::class, ['pid' => 'id'])->where(['<>', 'status', StatusEnum::DELETE])->orderBy('sort asc,id asc'); } /** * @param array $cond * @param array $with * @param int $is_array * @return array|AuthRole[]|ActiveRecord[]\ */ public static function getItems(array $cond = [], array $with = [], bool $is_array = true) { $query = self::find(); self::paramPack(['where' => $cond, 'with' => $with], $query); return $query->asArray($is_array)->orderBy('sort asc,id asc')->limit(1000000)->all(); } public static function getRbacItemsByAppId($app_id, $cate_id = []) { $query = AuthItem::find()->orderBy('sort asc, id asc'); $query->andWhere(['status' => StatusEnum::ENABLED]); if (!empty($app_id)) $query->Where(['app_id' => $app_id]); if (!empty($cate_id)) $query->Where(['cate_id' => $cate_id]); return $query->asArray()->all(); } public static function getRabcItemsList($params) { $query = AuthItem::find()->where(['app_id' => $params['app_id'], ['or', ['is_addon' => 0, 'cate_id' => AppEnum::MALL], ['is_addon' => 1] ]]); $item_list = $query->asArray()->all(); $item_list = AuthItem::getTree($item_list, 0, $params['route']); $data['item_list'] = $item_list; } /** * 获取树状菜单 * @Author bing * @DateTime 2021-08-10 18:22:16 * @param array $menu 菜单 * @param integer $parentId * @param string $app_id APP_ID * @return array * @copyright: Copyright (c) 2020 广东七件事集团 */ public static function getTree(array $menu, int $pid = 0, string $route = '') { $tree = []; foreach ($menu as $k => $v) { $v['value'] = $v['id']; $v['label'] = $v['title']; if ($v['pid'] == $pid) { $tmp = $v; // 当前路由的菜单被选中 if ($route == $v['name']) $tmp['is_active'] = true; $children = self::getTree($menu, $v['id'], $route); $children && $tmp['children'] = $children; //判断父级菜单是否被选中 if (isset($tmp['children']) && !empty($tmp['children'])) { foreach ($tmp['children'] as $child) { if (isset($child['is_active']) && $child['is_active'] == true) { $tmp['is_active'] = true; } } } $tree[] = $tmp; } } return $tree; } /** * 获取编辑菜单数据 * @param $id * @param $type * @param $app_id * @return array */ public static function getEditData($id, $type, $app_id) { $where = ['and', ['id' => $id], ['<>', 'status', StatusEnum::DELETE] ]; $items = AuthItem::find()->where($where)->asArray()->one(); if (!empty($id) && $type == 'add') { $parent_title = $items['title']; } else { $parent_title = '顶级权限'; if (!empty($items['pid'])) { $parent_item = AuthItem::find()->where(['id' => $items['pid']])->andWhere(['<>', 'status', StatusEnum::DELETE])->asArray()->one(); $parent_title = $parent_item['title']; } } $list = \Yii::$app->services->rbacAuthItem->getDropDownForEdit($app_id, $id); return [ 'items' => !empty($items) ? $items : [], 'parent_title' => $parent_title, 'list' => $list]; } /** * 保存数据 * @param array $params * @return bool|AuthItem */ public static function setData(array $params) { try { if (!empty($params['id'])) { $model = self::findOne(['id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]); if (empty($model)) throw new Exception('服务不存在或已删除'); } else { $model = new self(); $model->loadDefaultValues(); } if (!$model->load($params, '')) throw new Exception($model->getErrorMessage()); if(empty($params['id'])){ if (in_array($params['cate_id'], [AppEnum::PLATFORM, AppEnum::MALL])) $model->is_addon = WhetherEnum::DISABLED; if (in_array($params['cate_id'], [AppEnum::ADDON])) $model->is_addon = WhetherEnum::ENABLED; } $model->level = 1; $model->tree = 'tr_0'; if (!empty($model->pid)) { $rbac_item = AuthItem::findOne($model->pid); $model->level = $rbac_item->level + 1; $model->tree = $rbac_item->tree . ' tr_' . $rbac_item['id']; } $res = $model->save(); if ($res === false) throw new Exception($model->getErrorMessage()); return $model; } catch (Exception $e) { self::$static_error = $e->getMessage(); return false; } } public static function getItemListTree($where,$route){ $item_list = self::getItems($where,[],1); return self::getTree($item_list, 0, $route); } /** * 获取商城插件的权限id * @Author: lun * @DateTime: 2022/9/6 0006 17:55 * @Copyright: copyright (c) 2021 广东七件事集团 * @param $addons_names * @return array */ public static function getAddonsItemIds($addons_names) { return self::find()->select('id')->where(['addons_name' => $addons_names ,'is_addon' => 1, 'pid' => 0, 'status' => StatusEnum::ENABLED])->column(); } /** * 迁移创建权限 * @Author: lun * @DateTime: 2023/7/13 0013 16:31 * @Copyright: copyright (c) 2021 广东七件事集团 * @param array $params * @return bool|AuthItem */ public static function createAuthItem(array $params) { try { $model = self::findOne(['name' => $params['name'], 'pid' => $params['pid'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]); if (empty($model)) { $model = new self(); $model->loadDefaultValues(); } if (!$model->load($params, '')) throw new Exception($model->getErrorMessage()); $model->level = 1; $model->tree = 'tr_0'; if (!empty($model->pid)) { $rbac_item = AuthItem::findOne($model->pid); $model->level = $rbac_item->level + 1; $model->tree = $rbac_item->tree . ' tr_' . $rbac_item['id']; } $res = $model->save(); if ($res === false) throw new Exception($model->getErrorMessage()); return $model; } catch (Exception $e) { self::$static_error = $e->getMessage(); return false; } } }