示例#1
0
 /**
  * 获取用户组id数组
  *
  * @return array
  */
 public function getGroupIdArr()
 {
     $return = [];
     if ($this->group_ids) {
         $return = array_filter(StringHelper::explode($this->group_ids, ',', true, true), function ($groupId) {
             return is_numeric($groupId) && DpAdminGroup::find()->findByGroupId($groupId)->exists();
         });
     }
     return $return;
 }
 /**
  * 更新状态
  *
  * @return array
  */
 public function actionUpdateStatus()
 {
     $ids = \Yii::$app->request->post('ids');
     $status = intval(\Yii::$app->request->post('status'));
     if ($status != 0) {
         $status = 1;
     }
     foreach (StringHelper::explode($ids, ',', true, true) as $id) {
         $obj = DpAdminGroup::find()->findByGroupId($id)->one();
         if ($obj) {
             $obj->status = $status;
             $obj->save();
         }
     }
     return $this->renderSuccess('状态更新成功');
 }
 /**
  * 保存数据
  *
  * @return array
  */
 public function actionSave()
 {
     $menu_id = Yii::$app->request->post('menu_id');
     $parent_id = intval(Yii::$app->request->post('parent_id'));
     $text = Yii::$app->request->post('origin_text');
     $title = Yii::$app->request->post('title');
     $url = Yii::$app->request->post('url');
     $view_package = Yii::$app->request->post('view_package');
     $expanded = intval(Yii::$app->request->post('is_expand'));
     $closable = intval(Yii::$app->request->post('closable'));
     $is_folder = intval(Yii::$app->request->post('is_folder'));
     $is_open_url = intval(Yii::$app->request->post('is_open_url'));
     $is_open_target = intval(Yii::$app->request->post('is_open_target'));
     $is_every_open = intval(Yii::$app->request->post('is_every_open'));
     $is_hide = intval(Yii::$app->request->post('is_hide'));
     $display_order = Yii::$app->request->post('display_order');
     $params = Yii::$app->request->post('params');
     $note = Yii::$app->request->post('note');
     $status = intval(Yii::$app->request->post('status'));
     if ($parent_id) {
         if ($parent_id == $menu_id) {
             return $this->renderError('不能把自己当作父级');
         } else {
             $menu = DpAdminMenu::find()->findByMenuId($parent_id)->asArray()->one();
             if (!$menu) {
                 return $this->renderError('父级不存在');
             }
         }
     }
     $saveData = ['parent_id' => $parent_id, 'text' => $text, 'title' => $title, 'url' => $url, 'view_package' => $view_package, 'expanded' => $expanded, 'closable' => $closable, 'is_folder' => $is_folder, 'is_open_url' => $is_open_url, 'is_open_target' => $is_open_target, 'is_every_open' => $is_every_open, 'is_hide' => $is_hide, 'display_order' => $display_order, 'params' => $params, 'note' => $note, 'status' => $status];
     if ($menu_id) {
         $obj = DpAdminMenu::find()->findByMenuId($menu_id)->one();
         if (!$obj) {
             return $this->renderError('保存失败,记录不存在!');
         }
     } else {
         $obj = new DpAdminMenu();
     }
     $obj->setAttributes($saveData);
     if (!$obj->save()) {
         foreach ($obj->getErrors() as $error) {
             foreach ($error as $message) {
                 return ['success' => false, 'msg' => $message];
             }
         }
     }
     // 自动给系统用户添加菜单权限
     if (!$menu_id) {
         $users = DpAdminUser::find()->findByIsSystem(1)->asArray()->all();
         foreach ($users as $user) {
             $linkObj = new DpAdminUserMenuRelation();
             $linkObj->setAttributes(['user_id' => $user['user_id'], 'menu_id' => $obj->menu_id]);
             $linkObj->save();
         }
     }
     // 自动给系统用户组添加菜单权限
     if (!$menu_id) {
         $groups = DpAdminGroup::find()->findByIsSystem(1)->asArray()->all();
         foreach ($groups as $group) {
             $linkObj = new DpAdminGroupMenuRelation();
             $linkObj->setAttributes(['group_id' => $group['group_id'], 'menu_id' => $obj->menu_id]);
             $linkObj->save();
         }
     }
     return $this->renderSuccess('保存成功');
 }