1821349743@qq.com
2023-04-06 83abc7eff92a6f448758a3aad615a8606348592a
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title th:text="${file_title}"></title>
    <style type="text/css">
        html, body {
            margin: 0;
            padding: 0;
            height: 100vh;
        }
 
        .openOffice {
            height: calc(100% - 20px);
            width: 100%;
            margin: 0;
            padding: 0;
        }
    </style>
    <script type="text/javascript" src="/lx/static/js/jquery.min.js"></script>
</head>
<body>
 
 
<div style="height: 100%" id="openOffice-area">
    <script type="text/javascript">
        if (!!window.ActiveXObject || "ActiveXObject" in window) {
            document.write('<object class="openOffice" classid="clsid:FF09E4FA-BFAA-486E-ACB4-86EB0AE875D5" codebase="http://www.officectrl.com/weboffice/WebOffice.ocx#Version=2018,1,6,2" id="WebOffice" width="100%" height="900" >');
            document.write('</object>');
        } else {
            document.write('<object class="openOffice"  id="WebOffice" CLSID="{FF09E4FA-BFAA-486E-ACB4-86EB0AE875D5}" TYPE="application/x-itst-activex"  width=100% height=900></object>');
        }
    </script>
</div>
</body>
 
<script type="text/javascript" src="/lx/static/js/crypto-js.min.js"></script>
<script type="text/javascript" src="/lx/static/js/qs.min.js"></script>
<script type="text/javascript" th:inline="javascript">
    var requestPrefix = window.location.protocol + "//" + window.location.host + "/lx/api"
    var saveFileApi = requestPrefix + "/office/save-office/v1"
    var getFileApi = requestPrefix + "/office/get-file/v1"
    var nestFileApi = requestPrefix + "/office/get-nest-red-file/v1"
    let WebOffice;
    var readOnly = 1;
    var formData;
    var signature;
    var fileType;
    var nestRedDocument = [[${nestDocument}]];
 
    /**
     * 正在编辑状态发送
     */
    function beingEditedStatusSend() {
        //每15秒标识一次正在编辑 配合后端 redis 过期时间设置定时发送时间
        // setTimeout(function () {
        //     $.ajaxSettings.async = true;
        //     $.post("/lx/api/office/reset-document-edit-time/v1", {
        //         'signature': signature,
        //         'formData': formData
        //     }, function (success, data) {
        //         if (!data || data.code != 200) {
        //             if (data) {
        //                 if (confirm(data.msg)) {
        //                     readOnly = 1
        //                     WebMenubar(false);
        //                     getOffice().ProtectDoc(1, 1, new Date().toString() + Math.random() * 10);
        //                 } else {
        //                     //退出
        //                     customCloseWindow();
        //                 }
        //             } else {
        //                 readOnly = 1
        //                 alert('标识文档编辑状态错误!');
        //                 customCloseWindow();
        //             }
        //         }
        //     }, 'json')
        //     $.ajaxSettings.async = true;
        //     beingEditedStatusSend()
        // }, 15000)
 
    }
 
    /**
     * 打开文档
     */
    function openOffice() {
        try {
            getOffice().UserName = "成都龙欣软件有限公司";
            getOffice().Authorizer = "www.officectrl.com";
            getOffice().Open(
                getFileApi + '?signature=' + signature + '&formData=' + encodeURI(formData), true, fileType
            )
            //不允许新建
            getOffice().SetSecurity(254);
            //不允许打开
            getOffice().SetSecurity(253);
            if (readOnly == 1) {
                // WebToolbar(false)
                WebMenubar(false);
                getOffice().ProtectDoc(1, 1, new Date().toString() + Math.random() * 10);
            } else {
                beingEditedStatusSend();
                autoSave()
            }
        } catch (e) {
            if (e.message && e.message.indexOf('消息筛选器显示应用程序正在使用中') != -1) {
                alert('WPS 或 Microsoft Office 应用程序正在被占用')
            }
            // alert(e.message)
            console.error(e);
        }
    }
 
    /**
     * 计划开始编辑文档
     * @param signature
     * @param formData
     */
    function planEditDocument() {
        if (readOnly != 1) {
 
            $.post("/lx/api/office/document-edit-plan/v1", {
                'signature': signature,
                'formData': encodeURI(formData)
            }, function (data, status) {
                if (data && data.code == 200) {
                    setTimeout(openOffice, 500)
                } else {
                    if (data.code == '90043') {
                        WebMenubar(false);
                        if (confirm(data.msg)) {
                            readOnly = 1
                            planEditDocument()
                        } else {
                            customCloseWindow();
                        }
                        return;
                    }
                    //计划编辑文档错误
                    console.error(data ? data.msg : '请求错误')
                    alert('打开文档错误,请联系系统管理员!')
                }
 
            }, 'json');
 
        } else {
            //只读
            setTimeout(openOffice, 500)
        }
 
 
    }
 
    window.onload = function () {
 
        $.ajaxSettings.async = false;
        WebTitlebar(false)
        WebMenubar(true);
        signature = [[${signature}]];
        let data = JSON.parse([[${requestParaTemp}]]);
        if (!data.fileType) {
            alert('文件类型获取失败')
            return;
        }
        formData = JSON.stringify(data);
        readOnly = (String(data.readOnly) != undefined && String(data.readOnly) != '' && String(data.readOnly) != null) ? String(data.readOnly) : 1
        fileType = data.fileType;
        setMenu();
        planEditDocument()
 
    }
    /**
     * 自动保存标识
     * @type {null}
     */
    var autoSaveStatus = null;
 
    /**
     * 自动保存
     */
    function autoSave() {
        if (readOnly == 1 || autoSaveStatus === false) {
            return;
        }
 
        autoSaveStatus = true;
        //每30秒自动保存一次
        setTimeout(function () {
            customSave();
            autoSave();
        }, 30000)
    }
 
    window.close = function () {
        window.opener = null;
        window.open("", "_self");
        window.close();
    }
 
    function cutArray(array, subLength) {
        let count = 1;
        let newArr = [];
        let resultArr = [];
        if (array.length < subLength) {
            return [array];
        }
        array.forEach(function (item) {
            newArr.push(item);
            if (count % subLength == 0) {
                resultArr.push(newArr);
                newArr = [];
            }
            if (count == array.length - 1) {
                resultArr.push(newArr);
            }
            count++;
        });
        return resultArr;
    }
 
    function setMenu() {
        getOffice()
        var menuCount = Array.isArray(nestRedDocument) && nestRedDocument.length > 0 ? (Math.ceil(nestRedDocument.length / 10)) + 2 : 2
        WebOffice.SetMenuName(0, '保存');//修改文件菜单名称为:新建文件
        WebOffice.CustomMenuCount = menuCount;//设置新增的菜单个数
        WebOffice.SetMenuName(1, '保存并关闭');
        WebOffice.SetMenuName(2, '关闭');
        // WebOffice.SetMenuName(3, '关闭自动保存');
        if (menuCount > 2) {
            for (let i = 0; i < Math.ceil(nestRedDocument.length / 10); i++) {
                WebOffice.SetMenuName(i + 3, '套红' + (i > 0 ? (i + 1) : ''));
            }
        }
 
        setTimeout(function () {
            if (Array.isArray(nestRedDocument) && nestRedDocument.length > 0) {
                let array = cutArray(nestRedDocument, 10);
                for (let i = 0; i < array.length; i++) {
                    var group = array[i];
                    for (let j = 0; j < group.length; j++) {
                        var e = group[j];
                        WebOffice.WebAddMenuItem(3 + i, e.title, "5" + (3 + i) + "0" + (j == 9 ? '10' : '0' + (j + 1)))
                    }
                }
            }
            WebOffice.WebClearMenuItem(0);//清除控件默认文件菜单里的所有子项
        }, 1500)
    }
 
    function customSave() {
        var strSaveUrl = saveFileApi;
        try {
            WebOffice.HttpInit();
            var signature = [[${signature}]];
            let data = JSON.parse([[${requestParaTemp}]]);
            var formData = JSON.stringify(data);
            WebOffice.HttpAddPostString('signature', signature);
            WebOffice.HttpAddPostString('~table~', 'product_sys_attachments');
            WebOffice.HttpAddPostString('formData', formData);
            WebOffice.HttpAddPostCurrFile("docfile", "");//此句为固定语句,不管是保存word还是excel,ppt等都这样写
 
            //strResults用于获得网页提交后的返回数据,可用以调试服务器端代码。
            var strResults = WebOffice.HttpPost(strSaveUrl);
            console.log(strResults)
            if (strResults) {
                var res = JSON.parse(strResults);
                if (res && res.code != 200) {
                    if (res.code == "90021" || res.code == '90044') {
                        autoSaveStatus = false;
                        alert(res.msg);
                        customCloseWindow();
                        return;
                    }
                    alert(res.msg);
                }
                // alert(strResults)
                // JSON.parse()
            }
            // alert(strResults);//用alert语句可以显示服务器端网页返回的数据
            // strResults;//如果保存成功,编程时让服务器接收代码如upload.jsp,upload.aspx,upload.php等返回空值或OK字串。
        } catch (e) {
            console.error(e);
            alert('发生错误!请使用查阅返回值!');
        }
    }
 
    function customSaveClose() {
        customSave()
        customCloseWindow()
    }
 
    function customCloseWindow() {
        window.opener = null;
        window.open("", "_self");
        window.close();
    }
 
    /**
     * 设置当前用户名称  (wps 测试好像无效)
     */
    function setCurrentUserName() {
        getOffice().SetCurrUserName([[${user_name}]])
    }
 
 
    function getOffice() {
        if (!WebOffice) {
            WebOffice = document.getElementById('WebOffice');
        }
        return WebOffice;
    }
 
    /**
     * 标题栏
     * @param e
     * @constructor
     */
    function WebTitlebar(e) {
        getOffice().Titlebar = e
    }
 
    /**
     * 菜单栏
     * @param e
     * @constructor
     */
    function WebMenubar(e) {
        getOffice().MenuBars = e;
    }
 
    /**
     * 工具栏
     * @param e
     * @constructor
     */
    function WebToolbar(e) {
        getOffice().Toolbars = e
    }
 
    /**
     * 套红文档
     */
    function nestRedDocumentInsert(menuId) {
        if (!menuId) {
            return;
        }
 
        try {
            var data;
            try {
                var groupId = parseInt(menuId.substring(1, 2)) - 3;
                var menuItemCount = parseInt(menuId.substring(menuId.length - 2));
                var index = (groupId * 10) + menuItemCount - 1
                data = nestRedDocument[index];
            } catch (e) {
                return;
            }
            if (!data) {
                return;
            }
            if (confirm("套红后文档将自动保存,是否继续?")) {
                WebOffice.HttpInit();
                WebOffice.HttpAddPostString('signature', data.signature);
                // WebOffice.HttpAddPostString('~table~', 'product_sys_attachments');
                WebOffice.HttpAddPostString('formData', data.formJson);
                WebOffice.HttpAddPostCurrFile("docfile", "");//此句为固定语句,不管是保存word还是excel,ppt等都这样写
 
                //strResults用于获得网页提交后的返回数据,可用以调试服务器端代码。
                var strResults = WebOffice.HttpPost(nestFileApi);
                openOffice();
            }
            // console.log(data.signature, data.formJson)
            // WebOffice.InSertFile(nestFileApi + "?location=1&formData=" + (encodeURI(data.formJson)) + "&signature=" + data.signature, 1)
            // if (data.document_template_tail) {
            //     WebOffice.InSertFile(nestFileApi + "?location=2&formData=" + (encodeURI(data.formJson)) + "&signature=" + data.signature, 2)
            // }
        } catch (e) {
            console.error(e);
            alert('套红失败,未知错误')
        }
 
    }
 
 
</script>
<script language="javascript" for="WebOffice" event="OnMenuCommand(param1,param2)">
 
    switch (param1) {
        case 50100:
            //控件菜单的第一个标题是固定值:50100,第二个是:50101,第三个50102,依次类推,一共支持最大20个值,即最后一个值为:50120
            // alert('您点击了第一个菜单');
            customSave()
            break;
        case 50101:
            customSaveClose()
            break;
        case 50102:
            customCloseWindow()
            break;
        // case 50103:
        //     if (typeof autoSaveStatus == 'boolean') {
        //         autoSaveStatus = !autoSaveStatus
        //         getOffice().SetMenuName(3, autoSaveStatus ? '关闭' : '开启' + '自动保存');
        //     }
        //     break;
        default:
            var p = String(param1)
            if (p.indexOf("50") != 0) {
                //套红
                nestRedDocumentInsert(p)
            }
 
    }
</script>
</html>