iWebOffice.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <template>
  2. <div style="width:100%;height:600px; bottom:20px;">
  3. <el-button type="success" value="保存文档到服务器" @click="SaveDocument()">保存文档到服务器</el-button>
  4. <el-button type="primary" value="打开本地文档(有窗口)" @click="OnUnLoad(),openlocal()">打开本地文档(有窗口)</el-button>
  5. <el-button type="info" value="锁定文档" @click="WebSetProtect(true, '123456')">锁定文档</el-button>
  6. <el-button type="warning" value="解锁文档" @click="WebSetProtect(false, '123456')">解锁文档</el-button>
  7. <el-button type="danger" value="添加区域保护" @click="WebAreaProtect()">添加区域保护</el-button>
  8. <el-button type="info" style="background:#6E30B4;" value="解除区域保护" @click="WebAreaUnProtect()">解除区域保护</el-button>
  9. <el-button type="info" style="background:#303133" value="禁止拷贝" @click="WebEnableCopy()">禁止拷贝</el-button>
  10. <el-button type="info" style="background:#19868E"value="允许拷贝" @click="WebEnableCopy()">允许拷贝</el-button>
  11. <div id="office"></div>
  12. </div>
  13. </template>
  14. <script>
  15. import Vue from 'vue/dist/vue.js'
  16. import webOfficeTpl from '../../../../../static/iweboffice/iWebOffice2015.js';
  17. import {WebOffice2015} from '../../../../../static/iweboffice/WebOffice.js'
  18. import {getToken} from "@/utils/auth";
  19. export default {
  20. props: {
  21. fileData: {}
  22. },
  23. data() {
  24. return {
  25. webOffice: null,
  26. webOfficeObj: null,
  27. headers: "Bearer " + getToken(),
  28. saveResult: '',
  29. }
  30. },
  31. beforeCreate() {
  32. },
  33. mounted() {
  34. this.$nextTick(() => {
  35. this.initWebOffice();
  36. this.initWebOfficeObject();
  37. })
  38. },
  39. created() {
  40. },
  41. beforeDestroy() {
  42. },
  43. methods: {
  44. openlocal() {
  45. this.webOfficeObj.WebOpenLocal()
  46. },
  47. OnUnLoad() {
  48. this.webOfficeObj.WebClose();
  49. },
  50. initWebOffice() {
  51. this.webOffice = new Vue({
  52. template: webOfficeTpl
  53. }).$mount('#office');
  54. },
  55. initWebOfficeObject() {
  56. this.webOfficeObj = new WebOffice2015();
  57. this.webOfficeObj.setObj(document.getElementById('WebOffice'));
  58. try {
  59. this.webOfficeObj.ServerUrl = "http://localhost/dev-api";
  60. // this.webOfficeObj.RecordID = "web"; //RecordID:本文档记录编号
  61. this.webOfficeObj.UserName = this.fileData.user;
  62. this.webOfficeObj.FileName = this.fileData.filePath;
  63. this.webOfficeObj.getResultJson = {};
  64. this.webOfficeObj.SaveServlet = "/content/upload";
  65. this.webOfficeObj.FileType = "." + this.fileData.filePath.split(".")[this.fileData.filePath.split(".").length - 1]; //FileType:文档类型 .doc .xls
  66. this.webOfficeObj.ShowWindow = false; //显示/隐藏进度条
  67. this.webOfficeObj.EditType = this.fileData.EditType; //设置加载文档类型 0 锁定文档,1无痕迹模式,2带痕迹模式
  68. this.webOfficeObj.ShowMenu = 1;
  69. this.webOfficeObj.ShowToolBar = 0;
  70. this.webOfficeObj.SetCaption(this.webOfficeObj.UserName + "正在编辑文档"); // 设置控件标题栏标题文本信息
  71. //参数顺序依次为:控件标题栏颜色、自定义菜单开始颜色、自定义工具栏按钮开始颜色、自定义工具栏按钮结束颜色、
  72. //自定义工具栏按钮边框颜色、自定义工具栏开始颜色、控件标题栏文本颜色(默认值为:0x000000)
  73. if (!this.webOfficeObj.WebSetSkin(0xdbdbdb,
  74. 0xeaeaea,
  75. 0xeaeaea,
  76. 0xdbdbdb,
  77. 0xdbdbdb,
  78. 0xdbdbdb,
  79. 0x000000)) {
  80. // alert("插件皮肤发生错误,请新打开!");
  81. this.msgError("插件皮肤发生错误,请新打开!");
  82. // this.msgSuccess(this.webOfficeObj.Status);
  83. } //设置控件皮肤
  84. if (this.webOfficeObj.WebOpen()) {
  85. this.msgSuccess(this.webOfficeObj.Status);
  86. }
  87. // if (this.webOfficeObj.WebOpen2(this.webOfficeObj.FileName)) {
  88. // alert(webOfficeObj.Status);
  89. // }
  90. this.webOfficeObj.AppendMenu("1", "打开本地文件(&L)");
  91. this.webOfficeObj.AppendMenu("2", "保存本地文件(&S)");
  92. this.webOfficeObj.AppendMenu("3", "-");
  93. this.webOfficeObj.AppendMenu("4", "打印预览(&C)");
  94. this.webOfficeObj.AppendMenu("5", "退出打印预览(&E)");
  95. this.webOfficeObj.AddCustomMenu();
  96. this.webOfficeObj.HookEnabled();
  97. // this.webOfficeObj.CreateFile() // 根据FileType设置的扩展名来创建对应的空白文档
  98. } catch (e) {
  99. console.log("catch");
  100. console.log(e.description);
  101. }
  102. },
  103. closeWindow()
  104. {
  105. top.window.opener = top; top.window.open('','_self','');
  106. top.window.close();
  107. },
  108. //设置页面中的状态值
  109. StatusMsg(mValue) {
  110. try {
  111. this.msgSuccess(mValue);
  112. } catch (e) {
  113. return false;
  114. }
  115. },
  116. //保存文档
  117. SaveDocument(d) {
  118. // this.$emit('func', d)
  119. if (this.webOfficeObj.WebSave(this.headers)) { //交互OfficeServer的OPTION="SAVEFILE"
  120. this.$emit('func', this.webOfficeObj.getResultJson.fileName)
  121. this.webOfficeObj.FileName = this.webOfficeObj.getResultJson.fileName
  122. this.webOfficeObj.WebClose();
  123. // window.close();
  124. } else {
  125. this.webOfficeObj.Alert(this.webOfficeObj.Status);
  126. this.StatusMsg(this.webOfficeObj.Status);
  127. }
  128. },
  129. //锁定/解锁文档
  130. WebSetProtect(Boolean, PassWord) {
  131. this.webOfficeObj.WebSetProtect(Boolean, PassWord);
  132. this.msgSuccess(this.webOfficeObj.Status);
  133. },
  134. //允许/禁止拷贝文档
  135. WebEnableCopy(Boolean) {
  136. this.webOfficeObj.WebEnableCopy(Boolean);
  137. this.msgSuccess(this.webOfficeObj.Status);
  138. },
  139. //添加区域保护
  140. WebAreaProtect() {
  141. //如果非ie浏览器调用HidePlugin隐藏插件避免窗体被遮挡
  142. this.webOfficeObj.HidePlugin(0);
  143. var mText = window.prompt("文档里需要先设置保护的书签,示例文档默认添加了KingGrid的书签,请输入书签名称", "KingGrid", "");
  144. if (mText != null) {
  145. this.webOfficeObj.WebAreaProtect(mText)
  146. }
  147. ;
  148. this.webOfficeObj.HidePlugin(1);
  149. },
  150. //取消区域保护
  151. WebAreaUnProtect() {
  152. //如果非ie浏览器调用HidePlugin隐藏插件避免窗体被遮挡
  153. this.webOfficeObj.HidePlugin(0);
  154. var mText = window.prompt("需要和保护的书签对应,示例文档默认添加了KingGrid的书签,请输入书签名称", "KingGrid", "");
  155. //显示HidePlugin隐藏的插件
  156. if (mText != null) {
  157. this.webOfficeObj.WebAreaUnprotect(mText)
  158. }
  159. ;
  160. this.webOfficeObj.HidePlugin(1);
  161. },
  162. OnCommand(ID, Caption, bCancel) {
  163. switch (ID) {
  164. case 1:
  165. WebOpenLocal();
  166. break;//打开本地文件
  167. case 2:
  168. this.webOfficeObj.WebSaveLocal();
  169. break;//另存本地文件
  170. case 4:
  171. this.webOfficeObj.PrintPreview();
  172. break;//启用
  173. case 5:
  174. this.webOfficeObj.PrintPreviewExit();
  175. this.webOfficeObj.ShowField();
  176. break;//启用
  177. case 17:
  178. this.webOfficeObj.SaveEnabled(true);
  179. this.StatusMsg("启用保存");
  180. break;//启用保存
  181. case 18:
  182. this.webOfficeObj.SaveEnabled(false);
  183. this.StatusMsg("关闭保存");
  184. break;//关闭保存
  185. case 19:
  186. this.webOfficeObj.PrintEnabled(true);
  187. this.StatusMsg("启用打印");
  188. break;//启用打印
  189. case 20:
  190. this.webOfficeObj.PrintEnabled(false);
  191. this.StatusMsg("关闭打印");
  192. break;//关闭打印
  193. case 301:
  194. this.webOfficeObj.HandWriting("255", "4");
  195. this.StatusMsg("手写签批");
  196. break;//手写签批
  197. case 302:
  198. this.webOfficeObj.StopHandWriting();
  199. this.StatusMsg("停止手写签批");
  200. break;//停止手写签批
  201. case 303:
  202. this.webOfficeObj.TextWriting();
  203. this.StatusMsg("文字签名");
  204. break;//文字签名
  205. case 304:
  206. this.webOfficeObj.ShapeWriting();
  207. this.StatusMsg("图形签批");
  208. break;//图形签批
  209. case 305:
  210. this.webOfficeObj.RemoveLastWriting();
  211. this.StatusMsg("取消上一次签批");
  212. break;//取消上一次签批
  213. case 306:
  214. this.webOfficeObj.ShowWritingUser(false, this.webOfficeObj.UserName);
  215. this.StatusMsg("显示签批用户");
  216. break;//显示签批用户
  217. default:
  218. ;
  219. return;
  220. }
  221. },
  222. //服务端Servlet方式打开文档
  223. LoadServlet() {
  224. this.webOfficeObj = new WebOffice2015();
  225. try {
  226. this.webOfficeObj.ServerUrl = "http://localhost/dev-api/"; // 用来保存文件的Server
  227. var downloadLink = "profile/doc/web.docx";
  228. this.webOfficeObj.ShowMenu = 1;
  229. this.webOfficeObj.ShowToolBar = 0;
  230. this.SetGraySkin(); //设置控件皮肤
  231. if (this.webOfficeObj.WebOpen3(downloadLink)) // 文件在服务器上的相对路径 FileName
  232. {
  233. this.StatusMsg(this.webOfficeObj.Status);
  234. }
  235. } catch (e) {
  236. this.StatusMsg(e.description);
  237. }
  238. },
  239. //URL地址打开文档
  240. LoadURL() {
  241. try {
  242. this.webOfficeObj.ServerUrl = "http://localhost/dev-api/"; //服务器地址
  243. this.webOfficeObj.ShowMenu = 1;
  244. this.webOfficeObj.ShowToolBar = 0;
  245. this.webOfficeObj.UserName = "xqx";
  246. this.webOfficeObj.FileName = "web.docx";
  247. this.webOfficeObj.FileType = ".docx"; //FileType:文档类型 .doc .xls
  248. this.webOfficeObj.ShowWindow = true; //显示/隐藏进度条
  249. this.webOfficeObj.EditType = "1"; //设置加载文档类型 0 锁定文档,1无痕迹模式,2带痕迹模式
  250. this.webOfficeObj.setObj(document.getElementById('WebOffice'));
  251. if (!this.webOfficeObj.WebSetSkin(0xdbdbdb, 0xeaeaea, 0xeaeaea, 0xdbdbdb, 0xdbdbdb, 0xdbdbdb, 0x000000)) {
  252. alert(this.webOfficeObj.Status);
  253. } //设置控件皮肤 //设置控件皮肤
  254. alert("1")
  255. if (this.webOfficeObj.WebOpen2("/profile/doc/web.docx")) // 文件在服务器上的相对路径 FileName
  256. {
  257. this.StatusMsg(this.webOfficeObj.Status);
  258. }
  259. } catch (e) {
  260. this.StatusMsg(e.description);
  261. }
  262. },
  263. //烟枪灰皮肤
  264. SetGraySkin() {
  265. //参数顺序依次为:控件标题栏颜色、自定义菜单开始颜色、自定义工具栏按钮开始颜色、自定义工具栏按钮结束颜色、
  266. //自定义工具栏按钮边框颜色、自定义工具栏开始颜色、控件标题栏文本颜色(默认值为:0x000000)
  267. if (!this.webOfficeObj.WebSetSkin(0xdbdbdb, 0xeaeaea, 0xeaeaea, 0xdbdbdb, 0xdbdbdb, 0xdbdbdb, 0x000000))
  268. this.webOfficeObj.Alert(this.webOfficeObj.Status);
  269. }
  270. }
  271. }
  272. </script>
  273. <style lang="less">
  274. </style>