如何重写获取首页的方法

介绍

用户登录成功后或者访问网页不带任何路由参数的时候前端会请求全局的首页的视图动作viewAction配置,然后跳转到该视图动作viewAction
如何重写获取首页的方法

方案

我们可以通过在该方法的后置hook自定义获取首页的逻辑,下面以按角色跳转不同首页的需求示例

package pro.shushi.pamirs.demo.core.hook;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;
import pro.shushi.pamirs.auth.api.model.AuthRole;
import pro.shushi.pamirs.boot.base.enmu.BaseExpEnumerate;
import pro.shushi.pamirs.boot.base.model.ViewAction;
import pro.shushi.pamirs.boot.web.loader.PageLoadAction;
import pro.shushi.pamirs.demo.api.model.DemoItemCategory;
import pro.shushi.pamirs.demo.api.model.DemoItemLabel;
import pro.shushi.pamirs.meta.annotation.Hook;
import pro.shushi.pamirs.meta.api.CommonApiFactory;
import pro.shushi.pamirs.meta.api.core.faas.HookAfter;
import pro.shushi.pamirs.meta.api.dto.fun.Function;
import pro.shushi.pamirs.meta.api.session.PamirsSession;
import pro.shushi.pamirs.meta.common.exception.PamirsException;
import pro.shushi.pamirs.user.api.model.PamirsUser;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class DemoHomepageHook implements HookAfter {

    private static final String TEST_ROLE_CODE_01 = "ROLE_1211";
    private static final String TEST_ROLE_CODE_02 = "ROLE_1211_1";

    @Override
    @Hook(module = {"base"}, model = {ViewAction.MODEL_MODEL}, fun = {"homepage"})
    public Object run(Function function, Object ret) {
        if (ret == null) {
            return null;
        }
        ViewAction viewAction = getViewActionByCurrentRole();
        if (viewAction != null) {
            ViewAction retNew = CommonApiFactory.getApi(PageLoadAction.class).load(viewAction);
            ViewAction viewActionRet = (ViewAction) ((Object[]) ret)[0];
            viewActionRet.set_d(retNew.get_d());
        }
        return ret;
    }

    protected ViewAction getViewActionByCurrentRole() {
        try {
            PamirsUser user = new PamirsUser();
            user.setId(PamirsSession.getUserId());
            user.fieldQuery(PamirsUser::getRoles);
            List<AuthRole> roles = user.getRoles();
            if (CollectionUtils.isNotEmpty(roles)) {
                List<String> roleCodes = roles.stream().map(AuthRole::getCode).collect(Collectors.toList());
                if (roleCodes.contains(TEST_ROLE_CODE_01)) {
                    return new ViewAction().setModel(DemoItemCategory.MODEL_MODEL).setName("DemoMenus_ItemPMenu_DemoItemAndCateMenu_DemoItemCategoryMenu").queryOne();
                } else if (roleCodes.contains(TEST_ROLE_CODE_02)) {
                    return new ViewAction().setModel(DemoItemLabel.MODEL_MODEL).setName("DemoMenus_ItemPMenu_DemoItemAndCateMenu_DemoItemLabelMenu").queryOne();
                }
            }
        } catch (PamirsException exception) {
            if (PamirsSession.getUserId() == null) {
                throw PamirsException.construct(BaseExpEnumerate.BASE_USER_NOT_LOGIN_ERROR, exception.getCause()).errThrow();
            } else {
                throw exception;
            }
        }
        return null;
    }
}

Oinone社区 作者:nation原创文章,如若转载,请注明出处:https://doc.oinone.top/backend/14500.html

访问Oinone官网:https://www.oinone.top获取数式Oinone低代码应用平台体验

(0)
nation的头像nation数式员工
上一篇 2024年7月5日 下午10:49
下一篇 2024年7月9日 下午8:59

相关推荐

  • 替换用户中心菜单

    使用扩展点实现用户中心菜单替换 1. 工程中引起pamirs-user-api <dependency> <groupId>pro.shushi.pamirs.core</groupId> <artifactId>pamirs-user-api</artifactId> </dependenc…

    2024年7月4日
    43600
  • Oinone登录扩展:对接SSO(适应于4.7.8及之后的版本)

    适配版本 4.7.8及其之后的版本 概述 在企业内部,对于已有一套完整的登录系统(SSO)的情况下,通常会要求把所有的系统都对接到SSO中;本文主要讲解用Oinone开发的项目对接SSO的具体实现。 对接步骤 1、项目自定义实现UserCookieLogin,可参考示例说明:pro.shushi.pamirs.user.api.login.UserCooki…

    2024年4月2日
    54700
  • 引入搜索(增强模型Channel)常见问题解决办法

    总体描述 引入Oinone的搜索(即Channel模块)后,因错误的配置、缺少配置或者少引入一些Jar包,会出现一些报错。 问题1:启动报类JCTree找不到 具体现象 启动过程可能会出现报错:java.lang.NoClassDefFoundError: com/sun/tools/javac/tree/JCTree$JCExpression 产生原因 引…

    2024年5月17日
    11500
  • 全局首页及应用首页配置方法(homepage)

    1 Oinone平台首页介绍 1.1 首页包括全局首页和应用首页两类 全局首页:指用户在登录时未指定重定向地址的情况下使用的应用首页 应用首页:指用户在切换应用时使用的首页 PS:全局首页本质上也是应用首页,是在用户没有指定应用时使用的首页。如登录后。 1.2 全局首页查找规则 找到当前用户有权限访问的全部应用。 若使用AppConfig配置首页,则优先使用…

    2024年3月24日
    37100
  • 如何自定义覆盖内置模块的页面

    1.首先通过sql查询找到我们需要的页面,从其中的template字段复制出原视图的配置 通过模型编码model在base_view查找需要修改的视图 select * from base_view where model='workflow.WorkflowUserTask' and is_deleted = 0; 2.将base_vi…

    2024年7月2日
    34500

发表回复

登录后才能评论