田源
2024-04-07 2ac55ce0edf4870a29691b56bfad59f4830a11a2
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/**
 * 按钮的基础服务
 * @author weidy
 * @date 2021-03-22
 */
layui.define(['layer','element'],function(exports) {
    var webUtil = $webUtil;
    var Class = function () {
        this.MODELNAME = "BaseAction";
        this.moduleKey = "BaseAction";
        this.id = "BaseAction";
        /**
         * 替换文本中的${xxx}
         * @param text 文本
         * @param dataStore 选择的数据
         * @param sourceData 来源数据
         * @returns 替换后的值,字符串
         */
        this.replaceFreeMarker = function (text,dataStore,sourceData) {
            return $webUtil.replaceFreeMarker(text,dataStore,sourceData);
        };
        /**
         * 执行前置事件
         * @param options 按钮的配置信息,前置事件里配置的参数会替换这个里的参数的信息
         * @param buttonTarget 按钮js所在的对象
         * @param callback 回调,如果存在前置事件,会在执行完成后执行回调,否则直接回调
         */
        this.callPreEvent = function (options,buttonTarget,callback,preEventName) {
            var that = this;
            var params = $webUtil.paramLow(options.paramVOS);
            options.paramVOS = params;
            var beforeEvent = params[preEventName || 'beforeevent'];
            if(beforeEvent) {
                var buttonParse = that.parseEventByUrl(beforeEvent,options,true);
                buttonParse.options.callback = callback;
                if($webUtil.isNull(buttonParse)){
                    buttonTarget[buttonParse.methodName](buttonParse);
                }else{
                    layui.use(buttonParse.jsPath,function () {
                        layui[buttonParse.jsPath][buttonParse.methodName](options);
                    });
                }
            }else{
                if(callback){
                    callback(options);
                }
            }
        };
        /**
         * 执行后置时间
         * @param options 按钮的配置信息,后置事件里配置的参数会替换这个里的参数的信息
         * @param buttonTarget 按钮Js所在的对象
         */
        this.callPostEvent = function(options,buttonTarget,callback,postEventName){
            var that = this;
            var params = $webUtil.paramLow(options.paramVOS);
            options.paramVOS = params;
            var afterEvent = params[postEventName || 'afterevent'];
            if(afterEvent) {
                var buttonParse = that.parseEventByUrl(afterEvent,options,false);
                if($webUtil.isNull(buttonParse)){
                    buttonTarget[buttonParse.methodName](buttonParse);
                }else{
                    layui.use(buttonParse.jsPath,function () {
                        layui[buttonParse.jsPath][buttonParse.methodName](options);
                    });
                }
            }else{
                if(callback){
                    callback(options);
                }
            }
        };
        /**
         * 使用url获取事件的信息
         * @param url 路径
         * @param options 按钮的配置信息,会自动覆盖相同属性的参数
         * @param isBefore 是否为前置事件,否则为后置
         * @returns {{jsPath: js的路径, options: 按钮的配置信息, methodName: (string)方法的名字}}
         */
        this.parseEventByUrl = function (url,options,isBefore) {
            //根据配置格式化事件
            var jsPath = url;
            var methodName = isBefore?"doBefore":"doAfter";
            var params = {};
            if (url.indexOf("?")) {
                var temp = url.substring(0, url.indexOf("?"));
                if (temp.indexOf("#") > -1) {
                    var array = temp.split("#");
                    if(array.length == 1){
                        jsPath = array[0];
                    }else{
                        jsPath = array[0];
                        methodName = array[1];
                    }
                }else{
                    jsPath = temp;
                }
                var paramArray = url.substring(url.indexOf("?") + 1).split("&");
                layui.each(paramArray, function (_index, _item) {
                    if (_item.indexOf("=") < 0) {
                        $webUtil.showErrorMsg(isBefore?"前置事件":"后置事件" + "的参数配置错误,需要要xxx=yyy&zzz=a的方式");
                        return true;
                    }
                    params[_item.split("=")[0]] = _item.split("=")[1];
                });
            }else{
                if (url.indexOf("#") > -1) {
                    var array = url.split("#");
                    if(array.length == 1){
                        jsPath = array[0];
                    }else{
                        jsPath = array[0];
                        methodName = array[1];
                    }
                }else{
                    jsPath = url;
                }
            }
            for (var key in params) {
                options.paramVOS[key.toLowerCase()] = params[key];
            }
            return {
                jsPath:jsPath,
                methodName:methodName,
                options:options
            };
        };
 
        
        this.renderUploadFile=function (filter,paramVOS,extraParams,callback){
            var extendAttrMap = JSON.parse($(filter ).attr('extendAttrMap'));
            var data={
                fileDocClassify: 'filePathField',
                fileDocClassifyName: '文件路径字段',
                updateFileFlag: false,//是否为修改
                fileOid: ''//只在更新的时候才传递
            }
            if(extraParams){
                $.extend(data, extraParams);
            }
            layui.upload.render({
                elem: filter
                , url: paramVOS.uploadfilebackpath + paramVOS.uploadfileurl
                , accept: extendAttrMap && extendAttrMap.accept ? extendAttrMap.accept : ''
                , exts: extendAttrMap && extendAttrMap.exts ? extendAttrMap.exts : ''
                , data: data
                , before: function (res) {
                    $webUtil.showProgress('文件上传中');
                }
                , done: function (res, index, upload) {
                    $webUtil.closeProgress();
                    if (res.success) {
                        layer.msg('上传成功');
                        if(layui.$( filter + ' .uploadDemoView').length>0 && res.obj){
                            layui.$( filter + ' .uploadDemoView').removeClass('layui-hide').find('img').attr('src', paramVOS.uploadfilebackpath + 'vciFileDownloadController/downloadByFileOid?fileOid=' + res.obj.oid + '&' + TOKEN_KEY + '=' + $webUtil.getToken());
                            layui.$( filter + ' .uploadDemoView input.uploadFileOid').val(res.obj.filePath);
                        }
 
                        if(callback){
                            callback(res.obj)
                        }
                    }else{
                        $webUtil.showErrorMsg(res.msg||"上传失败!");
                    }
                },
                error: function (index, upload) {
                    $webUtil.showErrorMsg("上传失败!");
                }
            });
        }
 
        //附件上传
        this.renderUploadAttach=function (filter,paramVOS,extraParams){
            var attachHtml=[
                '<div class="layui-form-item"><div class="layui-upload" style="margin-left: 30px;border-top: 1px solid #eeeeee;padding-top: 15px;margin-bottom: 20px;">',
                '<button id="',filter,'_selectBtn" type="button" class="layui-btn layui-btn-sm layui-btn-normal">选择',paramVOS.uploadAttachmentTitle||'附件','</button>',
                '<button id="',filter,'_uploadAction" type="button" class="layui-btn layui-btn-sm" style="margin-left: 15px;">开始上传</button>',
                '<input type="hidden" name="releaseFileOids">',
                '<div class="layui-upload-list layui-table-view">',
                '<table cellspacing="0" cellpadding="0" border="0" class="layui-table" style="width: 100%;">',
                '<thead>',
                '<tr><th>文件名</th><th>大小</th><th>状态</th><th>操作</th></tr>'+
                '</thead>',
                '<tbody id="',filter,'_uploadList"></tbody>',
                '</table>',
                '</div>',
                '</div></div>'
            ]
            $('#' + filter + '').append(attachHtml.join(''))
            var uploadListView = $('#'+filter+'_uploadList');
            var data={
                fileDocClassify: 'attachment',
                fileDocClassifyName: '附件',
                updateFileFlag: false,
                fileOid: ''
            }
            if(extraParams){
                $.extend(data, extraParams);
            }
            var uploadListIns = layui.upload.render({
                elem: '#'+filter+'_selectBtn'
                ,url: paramVOS.uploadfilebackpath + paramVOS.uploadfileurl
                ,accept: 'file'
                ,multiple: true
                ,auto: false
                ,data: data
                ,bindAction: '#'+filter+'_uploadAction'
                ,choose: function(obj){
                    var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
                    var elemFiles= this.elemFiles = obj.getElemFiles()//针对ie
                    //读取本地文件
                    obj.preview(function(index, file, result){
                        var tr = $(['<tr id="upload-'+ index +'">'
                            ,'<td>'+ file.name +'</td>'
                            , '<td>' + (file.size ? (file.size / 1024).toFixed(1) + 'kb' : '') + '</td>'
                            ,'<td>等待上传</td>'
                            ,'<td>'
                            ,'<a class="layui-btn layui-btn-xs attach-reload layui-hide">重传</a>'
                            ,'<a class="layui-btn layui-btn-xs layui-btn-danger attach-delete">删除</a>'
                            ,'</td>'
                            ,'</tr>'].join(''));
 
                        //单个重传
                        tr.find('.attach-reload').on('click', function(){
                            obj.upload(index, file);
                        });
 
                        //删除
                        tr.find('.attach-delete').on('click', function(){
                            delete files[index]; //删除对应的文件
                            if(layui.device().ie  && layui.device().ie<10){
                                $('#'+elemFiles[index][0].id).remove()
                                //elemFiles[index].parent()[0].removeChild(elemFiles[index][0])
                                delete elemFiles[index];//针对ie
                            }
                            var oid=tr.attr('oid');
                            if(oid){
                                var val=$('#' + filter + ' [name="releaseFileOids"]').val();
 
                                val =val.replace(oid,'').replace(',,',',')
                                if(val.indexOf(',')==0){
                                    val=val.replace(',','')
                                }
                                $('#' + filter + ' [name="releaseFileOids"]').val(val)
                            }
                            tr.remove();
                            uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值,以免删除后出现同名文件不可选
                            $('.layui-upload-choose').remove();
                        });
 
                        uploadListView.append(tr);
                    });
                }
                ,done: function(res, index, upload){
                    if(res.success){ //上传成功
                        var tr = uploadListView.find('tr#upload-'+ index)
                            ,tds = tr.children();
                        tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
                        tr.attr('oid',res.obj.oid);
                        tds.eq(3).find('.attach-reload').addClass('layui-hide').html(''); //清空操作
                        var val=$('#' + filter + ' [name="releaseFileOids"]').val().split(',');
                        if(val[0]==''){
                            val[0]=res.obj.oid
                        }else{
                            val.push(res.obj.oid)
                        }
 
                        $('#' + filter + ' [name="releaseFileOids"]').val(val.join(','))
                        delete this.files[index]; //删除文件队列已经上传成功的文件
                        if(layui.device().ie  && layui.device().ie<10){
                            $('#'+this.elemFiles[index][0].id).remove()
                            delete this.elemFiles[index];
                        }
                        return ;
                    }
                    this.error(index, upload,res.msg);
                }
                ,error: function(index, upload,msg){
                    var tr = uploadListView.find('tr#upload-'+ index)
                        ,tds = tr.children();
                    tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
                    tds.eq(3).find('.attach-reload').removeClass('layui-hide'); //显示重传
                    $webUtil.showErrorMsg(msg);
                }
            });
        }
 
        //编辑器
        this.renderEditor=function (editId,initValue,paramVOS,callback){
            function editorCb(){
                //KindEditor.ready(function(K) {
                    window.editor = KindEditor.create('#' + editId, {
                        allowFileManager : true,
                        resizeMode : 1, //编辑器只能调整高度
                        width:800,
                        height:260,
                        afterBlur: function () { this.sync(); },
                        afterCreate: function () {
                            if(callback) callback()
                        }
                    });
 
                //});
            }
            $webUtil.createScript('jslib/kindeditor-4.1.7/kindeditor-min.js',editorCb)
        }
 
        //弹窗内容加载完后重新定位弹窗位置
        this.relocationOpen=function (classP){
            !(/^\d+%$/.test(classP.config.area[0]) || /^\d+%$/.test(classP.config.area[1])) && classP.auto(classP.index);
            classP.offset();
            classP.config.type == 4 && classP.tips();
        }
    };
    var cs = new Class();
    exports(cs.MODELNAME, cs);
});