1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import {validatenull} from "@/util/validate";
import Vue from 'vue';
import {handlerAction} from './handlers';
 
/**
 * 按钮的基础服务
 */
 
/**
 * 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(':', '=');
        if (item.indexOf('${') > -1) {
          if (item.split('=')[1].indexOf('.') > -1) {
            //initvaluenull=true允许初始值为空
            if ((options.sourceData.length < 1 || !options.sourceData.oid) && options.paramVOS['needselect'] != false && options.paramVOS['needselect'] != "false") {
              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['needselect'] != false && options.paramVOS['needselect'] != "false") {
              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 (validatenull(options.paramVOS.context)) {
    options.paramVOS.context = options.paramVOS.content;
  }
  if (button.url && button.url != 'null') {
    //有配置action路径,使用路径对应的js
    import(`./${button.url}`).then(module => {
      module.doAction(options, callback);
    })
  } else {
    //执行通用action
    handlerAction(button.actionVO.id.toLowerCase(),options, callback)
  }
};
 
 
/**
 * 替换文本中的${xxx}
 * @param text 文本
 * @param dataStore 选择的数据
 * @param sourceData 来源数据
 * @returns 替换后的值,字符串
 */
export const replaceFreeMarker = (text,dataStore,sourceData) => {
  //替换表达式
  if (!sourceData) {
    sourceData = {};
  }
  if (!dataStore) {
    dataStore = [];
  }
  let replaceData = dataStore.length > 0 ? dataStore[0] : {};
  if (text && text.indexOf("${") > -1 && text.indexOf("}")) {
    //js只能使用${xxx}这种的方式
    let reg = "root.${";
    while (text.indexOf(reg) > -1) {
      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 = sourceData[field] || '';
      text = temp + field + end;
    }
    reg = "sourceData.${";
    while (text.indexOf(reg) > -1) {
      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 = sourceData[field] || '';
      text = temp + field + end;
    }
    reg = "${";
    while (text.indexOf(reg) > -1) {
      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] || '';
      text = temp + field + end;
    }
  }
  return text;
};
 
/**
 * 参数转换为小写
 * @param paramsVOs 参数
 * @returns 小写后的参数对象
 */
export const paramLow = (paramsVOs) => {
  //
  if (!paramsVOs) {
    paramsVOs = {};
  }
  var params = {};
  for (var key in paramsVOs) {
    params[key.toLowerCase()] = paramsVOs[key];
  }
  return params;
};
/**
 * 执行前置事件
 * @param options 按钮的配置信息,前置事件里配置的参数会替换这个里的参数的信息
 * @param fnTarget 执行方法
 * @param callback 回调,如果存在前置事件,会在执行完成后执行回调,否则直接回调
 * @param preEventName 前置事件名称,默认beforeevent
 */
export const callPreEvent = (options,fnTarget,callback,preEventName) => {
  let beforeEvent = options.paramVOS[preEventName || 'preevent'];
  if(beforeEvent) {
    let buttonParse = parseEventByUrl(beforeEvent,options,true);
    if(buttonParse.params){
      Object.assign(options.paramVOS,buttonParse.params);
    }
    if(validatenull(buttonParse.jsPath)){
      fnTarget(options,callback);
    }else{
      try {
        import(`./${buttonParse.jsPath}.js`).then(module => {
          module[buttonParse.methodName](options,callback);
        })
      } catch (error) {
        Vue.prototype.$message.error('未找到前置事件执行js');
      }
    }
  }else{
    if(callback){
      callback(options);
    }
  }
};
/**
 * 执行后置事件
 * @param options 按钮的配置信息,后置事件里配置的参数会替换这个里的参数的信息
 * @param fnTarget 执行方法
 * @param callback 回调,如果存在后置事件,会在执行完成后执行回调,否则直接回调
 * @param preEventName 后置事件名称,默认 afterevent
 */
export const callPostEvent = (options,fnTarget,callback,actionType,postEventName)=>{
  let afterEvent = options.paramVOS[postEventName || 'afterevent'];
  if(afterEvent) {
    let buttonParse = parseEventByUrl(afterEvent,options,false);
    if(buttonParse.params){
      Object.assign(options.paramVOS,buttonParse.params);
    }
    if(validatenull(buttonParse.jsPath)){
      fnTarget(options,callback,actionType);
    }else{
      try {
        import(`./${buttonParse.jsPath}.js`).then(module => {
          module[buttonParse.methodName](options,callback,actionType);
        })
      } catch (error) {
        Vue.prototype.$message.error('未找到后置事件执行js');
      }
    }
  }else{
    if(callback){
      callback(actionType);
    }
  }
};
/**
 * 使用url获取事件的信息
 * @param url 路径,格式如jsPath#methodName?param=aaa&param1=bbb
 * @param options 按钮的配置信息,会自动覆盖相同属性的参数
 * @param isBefore 是否为前置事件,否则为后置
 * @returns {{jsPath: js的路径, options: 按钮的配置信息, methodName: (string)方法的名字}}
 */
export const parseEventByUrl = (url,options,isBefore,defalutmethodName) => {
  //根据配置格式化事件
  let jsPath = url;
  let methodName = defalutmethodName || (isBefore?"doBefore":"doAfter");
  let params = {};
  if (url.indexOf("?")>-1) {
    let temp = url.substring(0, url.indexOf("?"));
    if (temp.indexOf("#") > -1) {
      let array = temp.split("#");
      if(array.length == 1){
        jsPath = array[0];
      }else{
        jsPath = array[0];
        methodName = array[1];
      }
    }else{
      jsPath = temp;
    }
    let paramArray = url.substring(url.indexOf("?") + 1).split("&");
    paramArray.forEach(_item=>{
      if(_item){
        if (_item.indexOf("=") < 0) {
          Vue.prototype.$message.error(isBefore?"前置事件":"后置事件" + "的参数配置错误,需要要xxx=yyy&zzz=a的方式");
          return true;
        }
        params[_item.split("=")[0]] = _item.split("=")[1];
      }
    })
  }else{
    if (url.indexOf("#") > -1) {
      let array = url.split("#");
      if(array.length == 1){
        jsPath = array[0];
      }else{
        jsPath = array[0];
        methodName = array[1];
      }
    }else{
      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,
    params:params
  };
};