| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- <?php
- namespace common\models\rbac;
- use common\enums\AppEnum;
- use common\enums\StatusEnum;
- use common\enums\WhetherEnum;
- use common\traits\Tree;
- use PHPUnit\Util\Exception;
- use yii\db\ActiveRecord;
- /**
- * This is the model class for table "{{%rbac_auth_item}}".
- *
- *
- * @property int $id
- * @property string $name 别名
- * @property string $title 标题
- * @property string $app_id 应用
- * @property string $addons_name 插件名称
- * @property int $pid 父级id
- * @property int $level 级别
- * @property int $is_addon 是否插件
- * @property int $sort 排序
- * @property string $tree 树
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- * @property int $cate_id
- */
- class AuthItem extends \common\models\base\BaseActiveRecord
- {
- use Tree;
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%rbac_auth_item}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['title', 'name'], 'trim'],
- [['title', 'name'], 'required'],
- [['name'], 'uniquName', 'on' => '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;
- }
- }
- }
|