如何重写获取首页的方法

介绍

用户登录成功后或者访问网页不带任何路由参数的时候前端会请求全局的首页的视图动作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

相关推荐

  • 【DM】后端部署使用Dameng数据库(达梦)

    达梦数据库配置 驱动配置 达梦数据库的服务端版本和驱动版本需要匹配,建议使用服务端安装时提供的jdbc驱动,不要使用官方maven仓库中的驱动。 报错 表 xx 中不能同时包含聚集 KEY 和大字段,建表的时候就指定非聚集主键。SELECT * FROM V$DM_INI WHERE PARA_NAME = ‘PK_WITH_CLUSTER’;SP_SET_…

    后端 2023年11月1日
    1.3K00
  • 自定义审批方式、自定义审批节点名称

    @Model.model(审批模型.MODEL_MODEL) @Component public class 审批模型Action { @Function @Function.Advanced(category = FunctionCategoryEnum.CUSTOM_DESIGNER, displayName = "测试自定义审批类型&quot…

    2023年12月5日
    46800
  • Dubbo配置详解

    概述 Dubbo是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。 Oinone平台默认使用dubbo-v2.7.22版本,本文以该版本为例进行描述。 基本概念 Dubbo在注册provider/consumer时使用Netty作为RPC调用的核心服务,其具备客户端/服务…

    2024年8月10日
    61900
  • 工作流-流程代办等页面自定义

    1. 审批/填写节点的视图页面 在界面设计器中创建对应模型的表单视图,可根据业务场景需要自定义所需流程待办的审批页面 2. 在审批/填写节点中选择刚创建的视图 在工作流待办数据权限可在节点数据权限中可对字段设置查看、编辑、隐藏

    2024年5月14日
    68100
  • 如何发送邮箱、手机短信以及设置

    1.邮件发送 1.1 邮件服务设置 1.1.1 方法一:通过yaml文件配置 pamirs: email: smtp: smtpHost: smtp.exmail.qq.com smtpUser: xxx@xxx.com smtpPassword: xxxxxx smtpPort: 465 smtpSecurity: SSL #邮件模块可后续后台自行添加 t…

    后端 2023年11月6日
    47400

发表回复

登录后才能评论