wpsDocX.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. import axios from '@/common/services/axios-instance'
  2. import download from '@/common/services/download'
  3. import { sdLocalStorage } from '@/common/services/storage-service'
  4. import crossWindowWatcher from '@/common/services/cross-window-watcher'
  5. const DOCSUFFIX = ['wps', 'wpt', 'doc', 'docx', 'dot', 'dotx', 'docm', 'dotm']
  6. const EXCELSUFFIX = ['xls', 'xlsx', 'xlsm', 'xlt', 'xltx', 'et', 'xltm']
  7. const PPTSUFFIX = ['ppt', 'pptx', 'pptm', 'ppsx', 'ppsm', 'pps', 'potx', 'potm', 'dpt', 'dps']
  8. // WPS中台:正文附件编辑、清稿、套打、转版等
  9. let wpsDocX = {
  10. version: '2023.0424.01',
  11. createInstance() {
  12. let wpsdocxObj = {
  13. slCtl: {
  14. $$idx: {}, //对象属性
  15. SupportFormat: {
  16. // wps 在线编辑支持的格式
  17. edit: [...DOCSUFFIX, ...EXCELSUFFIX, ...PPTSUFFIX, 'pdf'],
  18. // wps 在线预览支持的格式
  19. view: [...DOCSUFFIX, 'rtf', 'txt', ...EXCELSUFFIX, 'csv', ...PPTSUFFIX, 'pdf', 'ofd'],
  20. },
  21. ErrorMessage: { NotSupport: 'FormatNotSupport' },
  22. checkInstall: function(argsobj) {
  23. wpsdocxObj.slCtl.$$idx = wpsdocxObj.slCtl.formatParams(argsobj)
  24. return Promise.resolve(true)
  25. },
  26. getEnv: function() {
  27. var platform = window.navigator.platform.toLocaleLowerCase()
  28. return platform.includes('win32') || platform.includes('win64') ? 'x86' : 'xc'
  29. },
  30. getRef: function(str, start, substr, seperator) {
  31. let pos, endpos
  32. pos = str.indexOf(substr, start)
  33. if (pos == -1) return ''
  34. endpos = str.indexOf(seperator, pos + 1)
  35. if (endpos == -1) return str.substring(pos + substr.length, str.length)
  36. else return str.substring(pos + substr.length, endpos)
  37. },
  38. setFileList: function(data) {
  39. let strlist = new Array()
  40. for (let i = 0; i < data.length; i++) {
  41. strlist.push(
  42. data[i].properties +
  43. '<FileServerUrl>' +
  44. fnGetFileLink(data[i].code) +
  45. '</FileServerUrl>' +
  46. '<attr>' +
  47. JSON.stringify(data[i].attr || {}) +
  48. '</attr>'
  49. )
  50. }
  51. if (strlist != null && strlist != undefined) {
  52. wpsdocxObj.slCtl.Content.Files._FileInfoString = strlist
  53. wpsdocxObj.slCtl.Content.Files.FileList = wpsdocxObj.slCtl.analyzeFileList(strlist) // 解析filelist
  54. }
  55. },
  56. analyzeFileList: function(FileInfoString) {
  57. let arrFileMap = FileInfoString.map(function(onefile, index) {
  58. let filelink = ''
  59. if (typeof fnGetFileLink === 'function') {
  60. filelink = fnGetFileLink(
  61. wpsdocxObj.slCtl.getRef(onefile, 0, '<file_unid>', '</file_unid>')
  62. )
  63. }
  64. let attr = JSON.parse(wpsdocxObj.slCtl.getRef(onefile, 0, '<attr>', '</attr>') || '{}')
  65. return {
  66. FileName: wpsdocxObj.slCtl.getRef(onefile, 0, '<file_name>', '</file_name>'),
  67. Unid: wpsdocxObj.slCtl.getRef(onefile, 0, '<file_unid>', '</file_unid>'),
  68. CatNum: wpsdocxObj.slCtl.getRef(onefile, 0, '<CatNum>', '</CatNum>'),
  69. Size: wpsdocxObj.slCtl.getRef(onefile, 0, '<file_size>', '</file_size>'),
  70. FileType: wpsdocxObj.slCtl.getRef(onefile, 0, '<filetype>', '</filetype>'),
  71. Completed: parseInt(
  72. wpsdocxObj.slCtl.getRef(onefile, 0, '<completed>', '</completed>')
  73. ),
  74. doAdd:
  75. wpsdocxObj.slCtl.getRef(onefile, 0, '<doAdd>', '</doAdd>') == 'True' ? true : false,
  76. Type: wpsdocxObj.slCtl.getFileType(
  77. wpsdocxObj.slCtl.getRef(onefile, 0, '<file_name>', '</file_name>')
  78. ),
  79. CreateInfo: wpsdocxObj.slCtl.getRef(onefile, 0, '<CreateInfo>', '</CreateInfo>'),
  80. UpdateInfo: wpsdocxObj.slCtl.getRef(onefile, 0, '<UpdateInfo>', '</UpdateInfo>'),
  81. TaodaInfo: wpsdocxObj.slCtl.getRef(onefile, 0, '<TaodaInfo>', '</TaodaInfo>'),
  82. NeedUpdate: wpsdocxObj.slCtl.getRef(onefile, 0, '<NeedUpdate>', '</NeedUpdate>'),
  83. AllowPrint: false,
  84. Editable: attr.editable ?? false,
  85. DocUnid: wpsdocxObj.slCtl.getRef(onefile, 0, '<doc_unid>', '</doc_unid>'),
  86. Uploading:
  87. parseInt(wpsdocxObj.slCtl.getRef(onefile, 0, '<completed>', '</completed>')) == 0 &&
  88. wpsdocxObj.slCtl.getRef(onefile, 0, '<doAdd>', '</doAdd>') == 'False'
  89. ? true
  90. : false,
  91. Link: filelink,
  92. }
  93. })
  94. return arrFileMap
  95. },
  96. formatParams: function(argsobj) {
  97. let $$idxtemp = {}
  98. let options = {}
  99. let UploadParam = argsobj.UploadParam || ''
  100. UploadParam.split('|').forEach((item) => {
  101. let tmpArr = item.split('=')
  102. options[tmpArr[0]] = tmpArr[1]
  103. })
  104. $$idxtemp.options = options
  105. return $$idxtemp
  106. },
  107. getFileType: function(strFileName) {
  108. let arrtmp = strFileName.split('.')
  109. return '.' + arrtmp.pop().toLowerCase()
  110. },
  111. isSupport(fname) {
  112. let ext = fname.match(/\.(?<ext>\w*)$/i).groups.ext
  113. return this.SupportFormat.edit.some((x) => ext == x)
  114. },
  115. Content: {
  116. Files: {
  117. FileList: [],
  118. _FileInfoString: [],
  119. },
  120. Control: {
  121. getFileByName: function(fname) {
  122. let userfile = null
  123. wpsdocxObj.slCtl.Content.Files.FileList.map(function(onefile, index) {
  124. if (onefile.Unid == fname || onefile.FileName == fname) {
  125. userfile = onefile
  126. }
  127. })
  128. return userfile
  129. },
  130. getNewFileName(fname, suffix, index = 1) {
  131. const fileName = fname + (index > 1 ? `(${index})` : '') + '.' + suffix
  132. if (
  133. wpsdocxObj.slCtl.Content.Files.FileList.find((item) => item.FileName === fileName)
  134. ) {
  135. // 存在同名附件,重命名
  136. return wpsdocxObj.slCtl.Content.Control.getNewFileName(fname, suffix, index + 1)
  137. } else {
  138. // 附件名不重复,可使用
  139. return fileName.substring(0, fileName.length - suffix.length - 1)
  140. }
  141. },
  142. // 创建文件
  143. createFile: function(groupId, params) {
  144. return new Promise((resolve, reject) => {
  145. // 默认文件名
  146. if (!params.fileName) {
  147. params.fileName = wpsdocxObj.slCtl.Content.Control.getNewFileName(
  148. '新建文档',
  149. params.type
  150. )
  151. }
  152. // 文字(word/wps)
  153. if (params.type === 'docx') {
  154. axios
  155. .get(`api/framework/v1/wps-doc-middle-end/create-docx/${groupId}`, { params })
  156. .then((res) => resolve(res.data))
  157. .catch((e) => reject(e))
  158. } else {
  159. reject('无法创建文档')
  160. }
  161. })
  162. },
  163. // 编辑
  164. EditFile: function(fname, fileparam, argsobj) {
  165. return new Promise((resolve, reject) => {
  166. if (!wpsdocxObj.slCtl.isSupport(fname)) {
  167. reject(wpsdocxObj.slCtl.ErrorMessage.NotSupport)
  168. } else {
  169. let { activeStepId, mode } = wpsdocxObj.slCtl.$$idx.options
  170. let file = wpsdocxObj.slCtl.Content.Control.getFileByName(fname)
  171. crossWindowWatcher
  172. .waitForChanged(
  173. `/sd-wps-editor?code=${file.Unid}&editable=${file.Editable}&mode=${mode}&activeStepId=${activeStepId}&filename=${fname}&fileStatus=${fileparam.Status}&openAuthType=${fileparam.openAuthType}&openAuthParameter=${fileparam.openAuthParameter}`
  174. )
  175. .then(() => {
  176. resolve(true)
  177. })
  178. }
  179. })
  180. },
  181. // 编辑:按URL
  182. EditFileByUrl: function(fname, url) {
  183. return new Promise((resolve, reject) => {
  184. crossWindowWatcher
  185. .waitForChanged(
  186. `/sd-wps-editor?url=${encodeURIComponent(url)}&filename=${encodeURIComponent(
  187. fname
  188. )}`
  189. )
  190. .then(() => {
  191. resolve(true)
  192. })
  193. })
  194. },
  195. getLockId: function(instId) {
  196. return JSON.parse(sdLocalStorage.getItem(`Lock${instId}`) || '{}').sessionId || 0
  197. },
  198. // 清稿
  199. QingGao: function(fname, fileparam, argsobj) {
  200. return new Promise((resolve, reject) => {
  201. if (!wpsdocxObj.slCtl.isSupport(fname)) {
  202. reject(wpsdocxObj.slCtl.ErrorMessage.NotSupport)
  203. } else {
  204. let { instId, activeStepId, groupId } = wpsdocxObj.slCtl.$$idx.options
  205. let lockId = wpsdocxObj.slCtl.Content.Control.getLockId(instId)
  206. let file = wpsdocxObj.slCtl.Content.Control.getFileByName(fname)
  207. let environment = wpsdocxObj.slCtl.getEnv()
  208. axios
  209. .get(`api/framework/v1/wps-doc-middle-end/clear-draft/${groupId}`, {
  210. params: {
  211. fileCode: file.Unid,
  212. lockId,
  213. instId,
  214. activeStepId,
  215. environment,
  216. },
  217. })
  218. .then(() => {
  219. resolve(true)
  220. })
  221. .catch((e) => {
  222. reject(e)
  223. })
  224. }
  225. })
  226. },
  227. // 套打
  228. TaoDa: function(
  229. ModelUrl,
  230. ContentFieldName,
  231. fname,
  232. fileparam,
  233. OldDocName,
  234. values,
  235. taodainfo,
  236. argsobj
  237. ) {
  238. return new Promise((resolve, reject) => {
  239. if (!wpsdocxObj.slCtl.isSupport(fname)) {
  240. reject(wpsdocxObj.slCtl.ErrorMessage.NotSupport)
  241. } else {
  242. let { instId, activeStepId, groupId } = wpsdocxObj.slCtl.$$idx.options
  243. let lockId = wpsdocxObj.slCtl.Content.Control.getLockId(instId)
  244. let file = wpsdocxObj.slCtl.Content.Control.getFileByName(fname)
  245. let environment = wpsdocxObj.slCtl.getEnv()
  246. axios
  247. .post(`api/framework/v1/wps-doc-middle-end/wrap-header/${groupId}`, values, {
  248. params: {
  249. fileCode: file.Unid,
  250. lockId,
  251. instId,
  252. activeStepId,
  253. readHeadFileCode:
  254. ModelUrl.match(/\/download-attachments\/(?<code>.*)\?/)?.groups?.code ||
  255. '',
  256. environment,
  257. },
  258. })
  259. .then(() => {
  260. resolve(true)
  261. })
  262. .catch((e) => {
  263. reject(e)
  264. })
  265. }
  266. })
  267. },
  268. // 更新正文
  269. UpdateRegion: function(fname, values, picvalues, fileparam, argsobj) {
  270. return wpsdocxObj.slCtl.Content.Control.TaoDa(
  271. '',
  272. '',
  273. fname,
  274. fileparam,
  275. '',
  276. values,
  277. '',
  278. argsobj
  279. )
  280. },
  281. // 盖章
  282. GaiZhang: function(
  283. ModelUrl,
  284. ContentFieldName,
  285. fname,
  286. fileparam,
  287. OldDocName,
  288. values,
  289. argsobj
  290. ) {
  291. return Promise.reject('请自行定制盖章功能')
  292. },
  293. // 转版:PDF
  294. DOCConvertToPDF: function(fname, fileparam, argsobj) {
  295. return new Promise((resolve, reject) => {
  296. if (!wpsdocxObj.slCtl.isSupport(fname)) {
  297. reject(wpsdocxObj.slCtl.ErrorMessage.NotSupport)
  298. } else {
  299. let { instId, activeStepId, groupId } = wpsdocxObj.slCtl.$$idx.options
  300. let lockId = wpsdocxObj.slCtl.Content.Control.getLockId(instId)
  301. let file = wpsdocxObj.slCtl.Content.Control.getFileByName(fname)
  302. let environment = wpsdocxObj.slCtl.getEnv()
  303. // 接口中会自动判断当前环境,信创环境转ofd,x86环境转pdf
  304. axios
  305. .get(`api/framework/v1/wps-doc-middle-end/convert/${groupId}`, {
  306. params: {
  307. fileCode: file.Unid,
  308. lockId,
  309. instId,
  310. activeStepId,
  311. environment,
  312. },
  313. })
  314. .then(() => {
  315. resolve(true)
  316. })
  317. .catch((e) => {
  318. reject(e)
  319. })
  320. }
  321. })
  322. },
  323. // 转版:OFD
  324. DOCConvertToOFD: function(fname, fileparam, argsobj) {
  325. return wpsdocxObj.slCtl.Content.Control.DOCConvertToPDF(fname, fileparam, argsobj)
  326. },
  327. SaveFileToLocal: function(fname, fileparam, argsobj) {
  328. return new Promise(function(resolve, reject) {
  329. var userfile = wpsdocxObj.slCtl.Content.Control.getFileByName(fname)
  330. download(userfile.Link, fname)
  331. .then(function() {
  332. resolve()
  333. })
  334. .catch(reject)
  335. })
  336. },
  337. SaveMultiFiles: function(strFiles, fileparam, argsobj) {
  338. let { groupId } = wpsdocxObj.slCtl.$$idx.options
  339. return new Promise((resolve, reject) => {
  340. var arrFileNames = strFiles.split('|')
  341. var fileName = arrFileNames[0]
  342. var codes = arrFileNames
  343. .map((v) => wpsdocxObj.slCtl.Content.Control.getFileByName(v))
  344. .filter(Boolean)
  345. download(
  346. `api/framework/v1/attachment-extend/attachments-download/multi?groupId=${groupId}&codes=${codes.map(
  347. (x) => x.Unid
  348. )}`,
  349. `${fileName}等${codes.length}个附件.zip`
  350. )
  351. .then((_) => resolve())
  352. .catch(reject)
  353. })
  354. },
  355. },
  356. },
  357. },
  358. }
  359. return wpsdocxObj
  360. },
  361. }
  362. export default wpsDocX