index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <!--onlyoffice 编辑器-->
  2. <template>
  3. <div style="height:100%;width:100%" v-loading="loading">
  4. <div id='vabOnlyOffice' v-if="show"></div>
  5. </div>
  6. </template>
  7. <script>
  8. import mixins from './index.js'
  9. export default {
  10. name: 'VabOnlyOffice',
  11. components:{
  12. },
  13. mixins:[mixins],
  14. props: {
  15. option: {
  16. type: Object,
  17. default: () => {
  18. return {}
  19. },
  20. },
  21. },
  22. data() {
  23. return {
  24. doctype: '',
  25. docEditor: null,
  26. loading:false,
  27. show:true,
  28. currentVersion:{},
  29. historyData:[]
  30. }
  31. },
  32. beforeDestroy() {
  33. if (this.docEditor !== null) {
  34. if(this.option.model != 'view'){
  35. var params = {
  36. c:'forcesave',
  37. key:this.option.key,
  38. }
  39. this.$api.onlyOffice_forceSave(params).then(response=>{
  40. if(response.code == 200){
  41. }
  42. })
  43. }
  44. this.docEditor.destroyEditor();
  45. this.docEditor = null;
  46. }
  47. },
  48. watch: {
  49. // option: {
  50. // handler: function(n) {
  51. // this.setEditor(n)
  52. // this.doctype = this.getFileType(n.fileType)
  53. // },
  54. // deep: true,
  55. // },
  56. },
  57. mounted() {
  58. if (this.option.url) {
  59. this.setEditor(this.option)
  60. }
  61. },
  62. methods: {
  63. async setEditor(option) {
  64. if (this.docEditor !== null) {
  65. this.docEditor.destroyEditor();
  66. this.docEditor = null;
  67. }
  68. var params = {
  69. guid:this.option.id,
  70. }
  71. await this.$api.getCurrentVersionData(params).then(response=>{
  72. if(response.code == 200){
  73. if(response.data && response.data.fileType){
  74. var obj = response.data
  75. this.option.fileType=obj.fileType,
  76. this.option.key=obj.key,
  77. this.option.version = obj.version
  78. this.option.url=this.$c.url+ (this.$c.env=='production'?'/api':'')+'/fileManager/downloadFile?fileId=' +obj.url
  79. }
  80. }
  81. })
  82. this.doctype = this.getFileType(option.fileType)
  83. var user={
  84. id:option.user.id,
  85. name:option.user.name
  86. }
  87. let callbackUrl = this.$c.url + (this.$c.env=='production'?'/api/fms':'') +`/onlyOffice/callback?guid=${option.id}`
  88. if(option.title){
  89. callbackUrl += `&fileName=${option.title}`
  90. }
  91. if(option.created){
  92. callbackUrl += `&created=${option.created}`
  93. }
  94. if(user.id){
  95. callbackUrl += `&user=${user.id}`
  96. }
  97. if(option.version){
  98. callbackUrl += `&previousVersion=${option.version}`
  99. }
  100. let config = {
  101. document: {
  102. //后缀
  103. fileType: option.fileType,
  104. key: option.key ||'',
  105. title: option.title,
  106. permissions: {
  107. edit: option.isEdit||true,//是否可以编辑: 只能查看,传false
  108. print: option.isPrint ||true,
  109. download: true,
  110. fillForms: true,//是否可以填写表格,如果将mode参数设置为edit,则填写表单仅对文档编辑器可用。 默认值与edit或review参数的值一致。
  111. review: true, //跟踪变化
  112. comment:true,//是否可以注释文档
  113. copy:true,
  114. chat: true,
  115. },
  116. url: option.url,
  117. },
  118. documentType: this.doctype,
  119. editorConfig: {
  120. // callbackUrl: 'http://192.168.2.24:8803/Test/TestQuery',//"编辑word后保存时回调的地址,这个api需要自己写了,将编辑后的文件通过这个api保存到自己想要的位置
  121. callbackUrl: callbackUrl,//"编辑word后保存时回调的地址,这个api需要自己写了,将编辑后的文件通过这个api保存到自己想要的位置
  122. lang: option.lang,//语言设置
  123. //定制
  124. customization: {
  125. spellcheck: false,
  126. comments:true,
  127. about:false,
  128. autosave: true,//是否自动保存
  129. forcesave:true,
  130. review: {
  131. hideReviewDisplay: false,
  132. showReviewChanges: false,
  133. reviewDisplay: "markup",
  134. trackChanges: true,
  135. hoverMode: true,
  136. },
  137. help: false,
  138. // "hideRightMenu": false,//定义在第一次加载时是显示还是隐藏右侧菜单。 默认值为false
  139. //是否显示插件
  140. plugins: false,
  141. // "feedback": false
  142. // customer:{
  143. // logo:'https://www.xsip.cn/api/fileManager/downloadFile?fileId=9ba11d7c72d24387a8cb2f484c23e0a5',
  144. // address:'',
  145. // info:'',
  146. // logoDark:'https://www.xsip.cn/api/fileManager/downloadFile?fileId=9ba11d7c72d24387a8cb2f484c23e0a5',
  147. // mail:'',
  148. // name:'',
  149. // www:'',
  150. // },
  151. logo: {
  152. // image: "https://www.xsip.cn/api/fileManager/downloadFile?fileId=9ba11d7c72d24387a8cb2f484c23e0a5",
  153. // imageDark: "https://www.xsip.cn/api/fileManager/downloadFile?fileId=9ba11d7c72d24387a8cb2f484c23e0a5",
  154. // url: "",
  155. visible: false,
  156. },
  157. },
  158. user:user,
  159. mode:option.model?option.model:'edit',
  160. // "canCoAuthoring": true
  161. },
  162. events:{
  163. onRequestHistory:this.onRequestHistory,//获取历史记录
  164. onRequestHistoryData:this.onRequestHistoryData,//获取历史文件
  165. onRequestRestore:this.onRequestRestore,//恢复
  166. onRequestHistoryClose:this.onRequestHistoryClose,//关闭历史
  167. },
  168. width: '100%',
  169. height: '100%',
  170. // token:option.token||'',
  171. // frameEditorId: "vabOnlyOffice",
  172. // parentOrigin: window.location.origin,
  173. // type: "desktop"
  174. }
  175. if(!config.token){
  176. //获取token并赋值
  177. var data = {
  178. jsons:JSON.stringify(config)
  179. }
  180. this.loading = true
  181. await this.$api.GetJWT(data).then(response=>{
  182. if(response.code == 200){
  183. config.token = response.data
  184. this.option.token = response.data
  185. this.loading = false
  186. }
  187. }).catch(error=>{
  188. this.loading = false
  189. })
  190. // config.token = this.getJWT(config)
  191. }
  192. // eslint-disable-next-line no-undef,no-unused-vars
  193. this.docEditor = new DocsAPI.DocEditor('vabOnlyOffice', config)
  194. },
  195. async onRequestHistory(event){
  196. var params = {
  197. guid:this.option.id
  198. }
  199. var that = this
  200. await this.$api.queryFileVersion(params).then(response=>{
  201. if(response.code == 200){
  202. that.currentVersion = response.data.currentVersion
  203. that.historyData = response.data.history
  204. }
  205. })
  206. if((this.historyData && this.historyData.length == 0) || !this.historyData || !this.currentVersion){
  207. that.docEditor.refreshHistory({
  208. currentVersion:'',
  209. history:[]
  210. })
  211. return
  212. }
  213. var currentVersion = this.currentVersion
  214. this.option.version = currentVersion
  215. var history = []
  216. for(var i = 0;i<that.historyData.length;i++){
  217. var item = that.historyData[i]
  218. history.push(
  219. {
  220. serverVersion:item.serverVersion,
  221. // changes:item.changes,
  222. created:item.created,
  223. user:item.user,
  224. version:item.version,
  225. key:item.key,
  226. }
  227. )
  228. }
  229. history.reverse()
  230. this.docEditor.refreshHistory(
  231. {
  232. currentVersion:currentVersion,
  233. history:history
  234. }
  235. )
  236. return
  237. },
  238. async onRequestHistoryData(event){
  239. const version = event.data
  240. var obj = this.historyData.find(item=>{
  241. return item.version == version
  242. })
  243. if(obj){
  244. var obj1 = null
  245. if(obj.previousVersion){
  246. obj1 = this.historyData.find(item=>{
  247. return item.version == obj.previousVersion
  248. })
  249. }
  250. var obj2 = {
  251. fileType:obj.fileType,
  252. key:obj.key,
  253. url:this.$c.url+ (this.$c.env=='production'?'/api':'')+'/fileManager/downloadFile?fileId=' +obj.url,
  254. version:obj.version,
  255. // previous:obj1?{
  256. // fileType:obj1.fileType,
  257. // key:obj1.key,
  258. // url:this.$c.url+ (this.$c.env=='production'?'/api':'')+'/fileManager/downloadFile?fileId=' +obj1.url,
  259. // }:null,
  260. // changesUrl:obj.changesUrl?(this.$c.url+ (this.$c.env=='production'?'/api':'')+'/fileManager/downloadFile?fileId=' +obj.changesUrl):""
  261. }
  262. var params = {
  263. jsons:JSON.stringify(obj2)
  264. }
  265. this.loading = true
  266. await this.$api.GetJWT(params).then(response=>{
  267. if(response.code == 200){
  268. obj2.token = response.data
  269. this.option.token = response.data
  270. this.loading = false
  271. }
  272. }).catch(error=>{
  273. this.loading = false
  274. })
  275. this.docEditor.setHistoryData(obj2)
  276. }
  277. },
  278. //恢复
  279. onRequestRestore(event){
  280. const version = event.data.version
  281. var params = {
  282. userId:this.option.user.id,
  283. userName:this.option.user.name,
  284. restoreVersion:version,
  285. previousVersion:this.option.version,
  286. guid:this.option.id
  287. }
  288. this.$api.restoreVersion(params).then(response=>{
  289. if(response.code == 200){
  290. this.setEditor(this.option)
  291. }
  292. })
  293. },
  294. onRequestHistoryClose(event){
  295. this.setEditor(this.option)
  296. },
  297. getFileType(fileType) {
  298. let docType = ''
  299. let fileTypesDoc = [
  300. 'doc', 'docm', 'docx', 'dot', 'dotm', 'dotx', 'epub', 'fodt', 'htm', 'html', 'mht', 'odt', 'ott', 'pdf', 'rtf', 'txt', 'djvu', 'xps',
  301. ]
  302. let fileTypesCsv = [
  303. 'csv', 'fods', 'ods', 'ots', 'xls', 'xlsm', 'xlsx', 'xlt', 'xltm', 'xltx',
  304. ]
  305. let fileTypesPPt = [
  306. 'fodp', 'odp', 'otp', 'pot', 'potm', 'potx', 'pps', 'ppsm', 'ppsx', 'ppt', 'pptm', 'pptx',
  307. ]
  308. if (fileTypesDoc.includes(fileType)) {
  309. docType = 'text'
  310. }
  311. if (fileTypesCsv.includes(fileType)) {
  312. docType = 'spreadsheet'
  313. }
  314. if (fileTypesPPt.includes(fileType)) {
  315. docType = 'presentation'
  316. }
  317. return docType
  318. },
  319. },
  320. }
  321. </script>
  322. <style>
  323. .extra #header-logo i{
  324. background-image: url('../../assets/logo.png') !important;
  325. }
  326. </style>