wangting
2024-05-07 46c673f0bf4a22ae108e90cfcf1bdeb4f0b6a45c
Source/ProjectWeb/src/actions/base/BaseAction.js
@@ -1,9 +1,97 @@
import {validatenull} from "@/util/validate";
import {findArray} from "@/util/util";
import Vue from 'vue';
/**
 * 按钮的基础服务
 */
/**
 * action通用入口
 */
export const doAction = (button,options,callback) => {
  options.paramVOS = paramLow(options.paramVOS)
  options.paramVOS['title']=replaceFreeMarker(options.paramVOS.title,options.dataStore,options.sourceData);
  let isShow = true;
  if (options.paramVOS['initvalue'] && typeof(options.paramVOS.initvalue)=='string') {
    let values = options.paramVOS['initvalue'].split(';');
    let initValues = {}
    values.forEach((item,i) => {
      if(isShow){
        item = item.replace(':', '=');
        debugger;
        if (item.indexOf('${') > -1) {
          if (item.split('=')[1].indexOf('.') > -1) {
            //initvaluenull=true允许初始值为空
            if ((options.sourceData.length < 1 || !options.sourceData.oid) && options.paramVOS['initvaluenull']!=true && options.paramVOS['initvaluenull']!="true") {
              isShow = false;
              Vue.prototype.$message.error("请先选择一条来源数据")
              return false;
            }
            let name = item.split('=')[1].split('.')[1].replace('${', '').replace('}', '');
            if(options.sourceData){
              initValues[item.split('=')[0]] = options.sourceData[name]
            }else {
              initValues[item.split('=')[0]]=""
            }
          } else {
            if (options.dataStore.length < 1 && options.paramVOS['initvaluenull'] != true && options.paramVOS['initvaluenull'] != "true") {
              isShow = false;
              Vue.prototype.$message.error("请先选择一条数据");
              return false;
            }
            let name = item.split('=')[1].replace('${', '').replace('}', '');
            if (options.dataStore[0]) {
              initValues[item.split('=')[0]] = options.dataStore[0][name];
            } else {
              initValues[item.split('=')[0]] = "";
            }
          }
        }
      }
    })
    if(isShow){
      options.paramVOS['initvalue'] = initValues
    }
  }
  if(!isShow){
    return;
  }
  if(button.url && button.url!='null'){
    //有配置action路径,使用路径对应的js
    import("../"+button.url+".js").then(module => {
      module.doAction(options,callback);
    })
  }else {
    //通用action
    const handlers = {
      //查看
      view: () => {},
      //创建
      add: () => {import("@/actions/base/AddAction").then(module => {
        module.doAction(options,callback);
      })},
      //修改
      edit: () =>  {import("@/actions/base/EditAction").then(module => {
        module.doAction(options,callback);
      })},
      //删除
      delete: () =>  {import("@/actions/base/DeleteAction").then(module => {
        module.doAction(options,callback);
      })},
    };
    if(validatenull(options.paramVOS.context)) {
      options.paramVOS.context = options.paramVOS.content;
    }
    if(handlers[button.actionVO.id.toLowerCase()]){
      handlers[button.actionVO.id.toLowerCase()]()
    }else{
      Vue.prototype.$message.error('未找到对应action,请重新配置按钮!');
    }
  }
};
/**
 * 替换文本中的${xxx}
@@ -28,7 +116,7 @@
      let temp = text.substring(0, text.indexOf(reg));
      let field = text.substring(text.indexOf(reg) + reg.length, text.indexOf("}"));
      let end = text.substring(text.indexOf("}") + 1);
      field = replaceData[field] || sourceData[field] || '';
      field = sourceData[field] || '';
      text = temp + field + end;
    }
    reg = "sourceData.${";
@@ -36,7 +124,7 @@
      let temp = text.substring(0, text.indexOf(reg));
      let field = text.substring(text.indexOf(reg) + reg.length, text.indexOf("}"));
      let end = text.substring(text.indexOf("}") + 1);
      field = replaceData[field] || sourceData[field] || '';
      field = sourceData[field] || '';
      text = temp + field + end;
    }
    reg = "${";
@@ -44,7 +132,7 @@
      let temp = text.substring(0, text.indexOf(reg));
      let field = text.substring(text.indexOf(reg) + reg.length, text.indexOf("}"));
      let end = text.substring(text.indexOf("}") + 1);
      field = replaceData[field] || sourceData[field] || '';
      field = replaceData[field] || '';
      text = temp + field + end;
    }
  }
@@ -70,23 +158,24 @@
/**
 * 执行前置事件
 * @param options 按钮的配置信息,前置事件里配置的参数会替换这个里的参数的信息
 * @param buttonTarget 按钮js所在的对象
 * @param fnTarget 执行方法
 * @param callback 回调,如果存在前置事件,会在执行完成后执行回调,否则直接回调
 * @param preEventName 前置事件名称,默认beforeevent
 */
export const callPreEvent = (options,buttonTarget,callback,preEventName) => {
  const params = paramLow(options.paramVOS);
  options.paramVOS = params;
  let beforeEvent = params[preEventName || 'beforeevent'];
export const callPreEvent = (options,fnTarget,callback,preEventName) => {
  let beforeEvent = options.paramVOS[preEventName || 'prepvent'];
  if(beforeEvent) {
    let buttonParse = parseEventByUrl(beforeEvent,options,true);
    buttonParse.options.callback = callback;
    if(validatenull(buttonParse)){
      buttonTarget[buttonParse.methodName](buttonParse);
    if(validatenull(buttonParse.jsPath)){
      fnTarget(buttonParse,callback);
    }else{
      layui.use(buttonParse.jsPath,function () {
        layui[buttonParse.jsPath][buttonParse.methodName](options);
      });
      try {
        import("../"+buttonParse.jsPath+".js").then(module => {
          module[buttonParse.methodName](options,callback);
        })
      } catch (error) {
        Vue.prototype.$message.error('未找到前置事件执行js');
      }
    }
  }else{
    if(callback){
@@ -95,26 +184,30 @@
  }
};
/**
 * 执行后置时间
 * 执行后置事件
 * @param options 按钮的配置信息,后置事件里配置的参数会替换这个里的参数的信息
 * @param buttonTarget 按钮Js所在的对象
 * @param fnTarget 执行方法
 * @param callback 回调,如果存在后置事件,会在执行完成后执行回调,否则直接回调
 * @param preEventName 后置事件名称,默认 afterevent
 */
export const callPostEvent = (options,buttonTarget,callback,postEventName)=>{
  const params = paramLow(options.paramVOS);
  options.paramVOS = params;
  var afterEvent = params[postEventName || 'afterevent'];
export const callPostEvent = (options,fnTarget,callback,actionType,postEventName)=>{
  let afterEvent = options.paramVOS[postEventName || 'afterevent'];
  if(afterEvent) {
    var buttonParse = parseEventByUrl(afterEvent,options,false);
    if(validatenull(buttonParse)){
      buttonTarget[buttonParse.methodName](buttonParse);
    let buttonParse = parseEventByUrl(afterEvent,options,false);
    if(validatenull(buttonParse.jsPath)){
      fnTarget(buttonParse,callback,actionType);
    }else{
      layui.use(buttonParse.jsPath,function () {
        layui[buttonParse.jsPath][buttonParse.methodName](options);
      });
      try {
        import("../"+buttonParse.jsPath+".js").then(module => {
          module[buttonParse.methodName](options,callback,actionType);
        })
      } catch (error) {
        Vue.prototype.$message.error('未找到后置事件执行js');
      }
    }
  }else{
    if(callback){
      callback(options);
      callback(actionType);
    }
  }
};
@@ -125,15 +218,15 @@
 * @param isBefore 是否为前置事件,否则为后置
 * @returns {{jsPath: js的路径, options: 按钮的配置信息, methodName: (string)方法的名字}}
 */
export const parseEventByUrl = (url,options,isBefore) => {
export const parseEventByUrl = (url,options,isBefore,defalutmethodName) => {
  //根据配置格式化事件
  var jsPath = url;
  var methodName = isBefore?"doBefore":"doAfter";
  var params = {};
  let jsPath = url;
  let methodName = defalutmethodName || (isBefore?"doBefore":"doAfter");
  let params = {};
  if (url.indexOf("?")) {
    var temp = url.substring(0, url.indexOf("?"));
    let temp = url.substring(0, url.indexOf("?"));
    if (temp.indexOf("#") > -1) {
      var array = temp.split("#");
      let array = temp.split("#");
      if(array.length == 1){
        jsPath = array[0];
      }else{
@@ -143,17 +236,17 @@
    }else{
      jsPath = temp;
    }
    var paramArray = url.substring(url.indexOf("?") + 1).split("&");
    layui.each(paramArray, function (_index, _item) {
    let paramArray = url.substring(url.indexOf("?") + 1).split("&");
    paramArray.forEach(_item=>{
      if (_item.indexOf("=") < 0) {
        this.$message.error(isBefore?"前置事件":"后置事件" + "的参数配置错误,需要要xxx=yyy&zzz=a的方式");
        Vue.prototype.$message.error(isBefore?"前置事件":"后置事件" + "的参数配置错误,需要要xxx=yyy&zzz=a的方式");
        return true;
      }
      params[_item.split("=")[0]] = _item.split("=")[1];
    });
    })
  }else{
    if (url.indexOf("#") > -1) {
      var array = url.split("#");
      let array = url.split("#");
      if(array.length == 1){
        jsPath = array[0];
      }else{
@@ -164,12 +257,21 @@
      jsPath = url;
    }
  }
  if(!options){
    options={
      paramVOS:{}
    }
  }else if(!options.paramVOS){
    options.paramVOS={}
  }
  for (var key in params) {
    options.paramVOS[key.toLowerCase()] = params[key];
  }
  return {
    jsPath:jsPath,
    methodName:methodName,
    options:options
    options:options,
    params:params
  };
};