需求
需要在前端流程表单页面添加流程按钮
方法一:通过 cmd 代理添加按钮
可通过后端对流程按钮 cmd 类进行代理,添加按钮
代理GetRightMenu类
@CommandDynamicProxy(target = GetRightMenuCmd.class,desc = "添加右键菜单")
public class AddMeunu extends AbstractCommandProxy<Map<String ,Object>> {
@Override
public Map<String, Object> execute(Command<Map<String, Object>> command) {
return null;
}
}
按照下面逻辑添加一个按钮
import com.engine.core.cfg.annotation.CommandDynamicProxy;
import com.engine.core.interceptor.AbstractCommandProxy;
import com.engine.core.interceptor.Command;
import com.engine.workflow.cmd.requestForm.GetRightMenuCmd;
import com.engine.workflow.constant.menu.SystemMenuType;
import com.engine.workflow.constant.requestForm.RequestMenuType;
import com.engine.workflow.entity.requestForm.RightMenu;
import java.util.List;
import java.util.Map;
@CommandDynamicProxy(target = GetRightMenuCmd.class,desc = "添加流程顶部菜单")
public class GetRightMenuProxyWithCmd extends AbstractCommandProxy<Map<String, Object>>{
@Override
public Map<String, Object> execute(Command<Map<String, Object>> command) {
Map<String ,Object> result =nextExecute(command);
List<RightMenu> rightMenus = (List<RightMenu>) result.get("rightMenus");
RightMenu rightMenu = new RightMenu(
"测试2",
RequestMenuType.BTN_CUSTOMIZE,
"window.doProxyCmd(\"proxyCmdBtn\")",
"icon-coms-daiban", 101,
SystemMenuType.CUSTOMIZE);
rightMenu.setIsTop("1");
rightMenus.add(rightMenu);
return result;
}
}
RightMenu 构造函数中的 order 参数,可以控制是否在表单顶部显示,如果order的值比较小,比如0,按钮就会显示在第一个位置,如果order比较大,则会显示在下拉菜单中,如果要显示在顶部,order推荐小于等于3
方法二:通过接口拦截添加按钮
拦截 `/api/workflow/reqform/rightMenu` 接口,修改接口返回数据进行添加按钮
示例:
import com.alibaba.fastjson.JSONObject;
import com.cloudstore.dev.api.util.Util_DataCache;
import com.customization.yll.common.util.PropertiesUtil;
import com.engine.workflow.constant.menu.SystemMenuType;
import com.engine.workflow.constant.requestForm.RequestMenuType;
import com.engine.workflow.entity.requestForm.RightMenu;
import com.weaverboot.frame.ioc.anno.classAnno.WeaIocReplaceComponent;
import com.weaverboot.frame.ioc.anno.methodAnno.WeaReplaceAfter;
import com.weaverboot.frame.ioc.handler.replace.weaReplaceParam.impl.WeaAfterReplaceParam;
import org.jetbrains.annotations.NotNull;
import weaver.conn.RecordSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
/**
* @author 姚礼林
* @desc 流程添加自定义按钮
* @date 2023/11/17
*/
@WeaIocReplaceComponent("AddWorkflowCustomMenu")
public class AddWorkflowBtIntercept {
private static final String CONFIG_FILE_NAME = "FW20240521-workflow-custom-menu";
private static final String CONFIG_TABLE_NAME_PROP_NAME = "modeTableName";
private static final String WORKFLOW_FIELD_NAME_PROP_NAME = "workflowFieldName";
private static final String MENU_NAME_PROP_NAME = "menuName";
private static final String IS_TOP_PROP_NAME = "isTop";
private static final String CACHE_KEY_NAME = "customWorkflowMenuProp";
@WeaReplaceAfter(value = "/api/workflow/reqform/rightMenu",order = 1,description = "流程添加自定义按钮")
public String rightMenuAfter(WeaAfterReplaceParam weaAfterReplaceParam){
Map<String ,Object> paramMap = weaAfterReplaceParam.getParamMap();
String workflowId = (String) paramMap.get("workflowid");
Properties properties = getProperties();
if (!isTargetWorkflow(workflowId,properties)){
return weaAfterReplaceParam.getData();
}
Map<String, Object> data = JSONObject.parseObject(weaAfterReplaceParam.getData(), Map.class);
List<RightMenu> rightMenus = (List<RightMenu>) data.getOrDefault("rightMenus", new ArrayList<>());
RightMenu rightMenu = buildMenu(properties,rightMenus.size());
rightMenus.add(rightMenu);
data.put("rightMenus", rightMenus);
return JSONObject.toJSONString(data);
}
@NotNull
private RightMenu buildMenu(Properties properties,int menuSize) {
String menuName = properties.getProperty(MENU_NAME_PROP_NAME);
String isTop = properties.getProperty(IS_TOP_PROP_NAME);
int order = "1".equals(isTop) ? 2 : menuSize;
RightMenu rightMenu = new RightMenu(menuName, RequestMenuType.BTN_CUSTOMIZE, "", "icon-coms-daiban",
order, SystemMenuType.CUSTOMIZE);
rightMenu.setIsTop(isTop);
rightMenu.setMobile(true);
return rightMenu;
}
private boolean isTargetWorkflow(String workflowId,Properties properties) {
String tableName = properties.getProperty(CONFIG_TABLE_NAME_PROP_NAME);
String workflowFieldName = properties.getProperty(WORKFLOW_FIELD_NAME_PROP_NAME);
RecordSet recordSet = new RecordSet();
recordSet.executeQuery("SELECT id from "+tableName+" where "+workflowFieldName+"=?", workflowId);
return recordSet.next();
}
private Properties getProperties() {
int oneHourSeconds = 3600;
if (!Util_DataCache.containsKey(CACHE_KEY_NAME)) {
Properties properties = PropertiesUtil.fetchProperties(CONFIG_FILE_NAME);
Util_DataCache.setObjVal(CACHE_KEY_NAME,properties,oneHourSeconds);
return properties;
}
return (Properties) Util_DataCache.getObjVal(CACHE_KEY_NAME);
}
}