You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

600 lines
18KB

  1. // -------------------------- 通用常量 ---------------------------
  2. //OA门户网站用接口,配置默认服务器接口
  3. var OA_DOOR = {
  4. templateDataUrl: undefined, //正文模板列表接口
  5. templateBaseURL: undefined, //指定正文模板基础接口
  6. redHeadsPath: undefined, //默认红头模板列表获取路径
  7. getRedHeadPath: undefined, //默认获取红头文件路径
  8. bookmarkPath: undefined, //书签列表接口
  9. redHeadsPath: undefined, //默认红头模板列表获取路径
  10. }
  11. // -------------------------- 通用方法 ---------------------------
  12. //去除字符串左边空格
  13. String.prototype.ltrim = function () {
  14. return this.replace(/(^\s*)/g, "");
  15. }
  16. //去除字符串右边空格
  17. String.prototype.rtrim = function () {
  18. return this.replace(/(\s*$)/g, "");
  19. }
  20. //扩展js string endwith,startwith方法
  21. String.prototype.endWith = function (str) {
  22. if (str == null || str == "" || this.length == 0 || str.length > this.length)
  23. return false;
  24. if (this.substring(this.length - str.length) == str)
  25. return true;
  26. else
  27. return false;
  28. }
  29. String.prototype.startWith = function (str) {
  30. if (str == null || str == "" || this.length == 0 || str.length > this.length)
  31. return false;
  32. if (this.substr(0, str.length) == str)
  33. return true;
  34. else
  35. return false;
  36. }
  37. //UTF-16转UTF-8
  38. function utf16ToUtf8(s) {
  39. if (!s) {
  40. return;
  41. }
  42. var i, code, ret = [],
  43. len = s.length;
  44. for (i = 0; i < len; i++) {
  45. code = s.charCodeAt(i);
  46. if (code > 0x0 && code <= 0x7f) {
  47. //单字节
  48. //UTF-16 0000 - 007F
  49. //UTF-8 0xxxxxxx
  50. ret.push(s.charAt(i));
  51. } else if (code >= 0x80 && code <= 0x7ff) {
  52. //双字节
  53. //UTF-16 0080 - 07FF
  54. //UTF-8 110xxxxx 10xxxxxx
  55. ret.push(
  56. //110xxxxx
  57. String.fromCharCode(0xc0 | ((code >> 6) & 0x1f)),
  58. //10xxxxxx
  59. String.fromCharCode(0x80 | (code & 0x3f))
  60. );
  61. } else if (code >= 0x800 && code <= 0xffff) {
  62. //三字节
  63. //UTF-16 0800 - FFFF
  64. //UTF-8 1110xxxx 10xxxxxx 10xxxxxx
  65. ret.push(
  66. //1110xxxx
  67. String.fromCharCode(0xe0 | ((code >> 12) & 0xf)),
  68. //10xxxxxx
  69. String.fromCharCode(0x80 | ((code >> 6) & 0x3f)),
  70. //10xxxxxx
  71. String.fromCharCode(0x80 | (code & 0x3f))
  72. );
  73. }
  74. }
  75. return ret.join('');
  76. }
  77. var Base64 = {
  78. _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
  79. encode: function(e) {
  80. var t = "";
  81. var n, r, i, s, o, u, a;
  82. var f = 0;
  83. e = Base64._utf8_encode(e);
  84. while (f < e.length) {
  85. n = e.charCodeAt(f++);
  86. r = e.charCodeAt(f++);
  87. i = e.charCodeAt(f++);
  88. s = n >> 2;
  89. o = (n & 3) << 4 | r >> 4;
  90. u = (r & 15) << 2 | i >> 6;
  91. a = i & 63;
  92. if (isNaN(r)) {
  93. u = a = 64
  94. } else if (isNaN(i)) {
  95. a = 64
  96. }
  97. t = t + this._keyStr.charAt(s) + this._keyStr.charAt(o) + this._keyStr.charAt(u) + this._keyStr.charAt(a)
  98. }
  99. return t
  100. },
  101. decode: function(e) {
  102. var t = "";
  103. var n, r, i;
  104. var s, o, u, a;
  105. var f = 0;
  106. e = e.replace(/[^A-Za-z0-9+/=]/g, "");
  107. while (f < e.length) {
  108. s = this._keyStr.indexOf(e.charAt(f++));
  109. o = this._keyStr.indexOf(e.charAt(f++));
  110. u = this._keyStr.indexOf(e.charAt(f++));
  111. a = this._keyStr.indexOf(e.charAt(f++));
  112. n = s << 2 | o >> 4;
  113. r = (o & 15) << 4 | u >> 2;
  114. i = (u & 3) << 6 | a;
  115. t = t + String.fromCharCode(n);
  116. if (u != 64) {
  117. t = t + String.fromCharCode(r)
  118. }
  119. if (a != 64) {
  120. t = t + String.fromCharCode(i)
  121. }
  122. }
  123. t = Base64._utf8_decode(t);
  124. return t
  125. },
  126. _utf8_encode: function(e) {
  127. e = e.replace(/rn/g, "n");
  128. var t = "";
  129. for (var n = 0; n < e.length; n++) {
  130. var r = e.charCodeAt(n);
  131. if (r < 128) {
  132. t += String.fromCharCode(r)
  133. } else if (r > 127 && r < 2048) {
  134. t += String.fromCharCode(r >> 6 | 192);
  135. t += String.fromCharCode(r & 63 | 128)
  136. } else {
  137. t += String.fromCharCode(r >> 12 | 224);
  138. t += String.fromCharCode(r >> 6 & 63 | 128);
  139. t += String.fromCharCode(r & 63 | 128)
  140. }
  141. }
  142. return t
  143. },
  144. _utf8_decode: function(e) {
  145. var t = "";
  146. var n = 0;
  147. var r = c1 = c2 = 0;
  148. while (n < e.length) {
  149. r = e.charCodeAt(n);
  150. if (r < 128) {
  151. t += String.fromCharCode(r);
  152. n++
  153. } else if (r > 191 && r < 224) {
  154. c2 = e.charCodeAt(n + 1);
  155. t += String.fromCharCode((r & 31) << 6 | c2 & 63);
  156. n += 2
  157. } else {
  158. c2 = e.charCodeAt(n + 1);
  159. c3 = e.charCodeAt(n + 2);
  160. t += String.fromCharCode((r & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
  161. n += 3
  162. }
  163. }
  164. return t
  165. }
  166. }
  167. //UTF-8转UTF-16
  168. function utf8ToUtf16(s) {
  169. if (!s) {
  170. return;
  171. }
  172. var i, codes, bytes, ret = [],
  173. len = s.length;
  174. for (i = 0; i < len; i++) {
  175. codes = [];
  176. codes.push(s.charCodeAt(i));
  177. if (((codes[0] >> 7) & 0xff) == 0x0) {
  178. //单字节 0xxxxxxx
  179. ret.push(s.charAt(i));
  180. } else if (((codes[0] >> 5) & 0xff) == 0x6) {
  181. //双字节 110xxxxx 10xxxxxx
  182. codes.push(s.charCodeAt(++i));
  183. bytes = [];
  184. bytes.push(codes[0] & 0x1f);
  185. bytes.push(codes[1] & 0x3f);
  186. ret.push(String.fromCharCode((bytes[0] << 6) | bytes[1]));
  187. } else if (((codes[0] >> 4) & 0xff) == 0xe) {
  188. //三字节 1110xxxx 10xxxxxx 10xxxxxx
  189. codes.push(s.charCodeAt(++i));
  190. codes.push(s.charCodeAt(++i));
  191. bytes = [];
  192. bytes.push((codes[0] << 4) | ((codes[1] >> 2) & 0xf));
  193. bytes.push(((codes[1] & 0x3) << 6) | (codes[2] & 0x3f));
  194. ret.push(String.fromCharCode((bytes[0] << 8) | bytes[1]));
  195. }
  196. }
  197. return ret.join('');
  198. }
  199. function currentTime() {
  200. var now = new Date();
  201. var year = now.getFullYear(); //年
  202. var month = now.getMonth() + 1; //月
  203. var day = now.getDate(); //日
  204. var hh = now.getHours(); //时
  205. var mm = now.getMinutes(); //分
  206. var ss = now.getSeconds();
  207. var clock = year + "";
  208. if (month < 10)
  209. clock += "0";
  210. clock += month + "";
  211. if (day < 10)
  212. clock += "0";
  213. clock += day + "";
  214. if (hh < 10)
  215. clock += "0";
  216. clock += hh + "";
  217. if (mm < 10) clock += '0';
  218. clock += mm;
  219. if (ss < 10) clock += '0';
  220. clock += ss;
  221. return (clock);
  222. }
  223. /**
  224. * 获取文件路径
  225. * @param {*} html 文件全称
  226. */
  227. function getHtmlURL(html) {
  228. //弹出辅助窗格框
  229. var GetUrlPath = ()=> {
  230. var e = document.location.toString();
  231. return -1 != (e = decodeURI(e)).indexOf("/") && (e = e.substring(0, e.lastIndexOf("/"))), e
  232. }
  233. var url = GetUrlPath();
  234. if (url.length != 0) {
  235. url = url.concat("/" + html);
  236. } else {
  237. url = url.concat("./" + html);
  238. }
  239. return url;
  240. }
  241. /**
  242. * wps内弹出web页面
  243. * @param {*} html 文件名
  244. * @param {*} title 窗口标题
  245. * @param {*} hight 窗口高
  246. * @param {*} width 窗口宽
  247. */
  248. function OnShowDialog(html, title, height, width, bModal) {
  249. var l_ActiveDoc = wps.WpsApplication().ActiveDocument;
  250. if (!l_ActiveDoc) {
  251. alert("WPS当前没有可操作文档!")
  252. return;
  253. }
  254. if (typeof bModal == "undefined" || bModal == null) {
  255. bModal = true;
  256. }
  257. width *= window.devicePixelRatio;
  258. height *= window.devicePixelRatio;
  259. var url = getHtmlURL(html);
  260. wps.ShowDialog(url, title, height, width, bModal);
  261. }
  262. /**
  263. * 解析返回response的参数
  264. * @param {*} resp
  265. * @return {*} body
  266. */
  267. function handleResultBody(resp) {
  268. var result = "";
  269. if (resp.Body) {
  270. // 解析返回response的参数
  271. }
  272. return result;
  273. }
  274. /**
  275. * 判断WPS中的文件个数是否为0,若为0则关闭WPS函数
  276. * @param {*} name
  277. */
  278. function closeWpsIfNoDocument() {
  279. var wpsApp = wps.WpsApplication();
  280. var docs = wpsApp.Documents;
  281. if (!docs || docs.Count == 0) {
  282. wps.ApiEvent.Cancel = true;
  283. //根据业务可以选择是否退出进程 wpsApp.Quit();
  284. }
  285. }
  286. function activeTab() {
  287. //启动WPS程序后,默认显示的工具栏选项卡为ribbon.xml中某一tab
  288. if (wps.ribbonUI)
  289. wps.ribbonUI.ActivateTab('WPSWorkExtTab');
  290. }
  291. function showOATab() {
  292. wps.PluginStorage.setItem("ShowOATabDocActive", pCheckIfOADoc()); //根据文件是否为OA文件来显示OA菜单
  293. wps.ribbonUI.Invalidate(); // 刷新Ribbon自定义按钮的状态
  294. }
  295. function getDemoTemplatePath() {
  296. var url = document.location.toString();
  297. url = decodeURI(url);
  298. if (url.indexOf("/") != -1) {
  299. url = url.substring(0, url.lastIndexOf("/"));
  300. }
  301. if (url.length !== 0)
  302. url = url.concat("/template/红头文件.docx");
  303. if (url.startsWith("file:///"))
  304. url = url.substr("file:///".length);
  305. return url;
  306. }
  307. function getDemoSealPath() {
  308. var url = document.location.toString();
  309. url = decodeURI(url);
  310. if (url.indexOf("/") != -1) {
  311. url = url.substring(0, url.lastIndexOf("/"));
  312. }
  313. if (url.length !== 0)
  314. url = url.concat("/template/OA模板:公章.png");
  315. if (url.startsWith("file:///"))
  316. url = url.substr("file:///".length);
  317. return url;
  318. }
  319. function pGetParamName(data, attr) {
  320. var start = data.indexOf(attr);
  321. data = data.substring(start + attr.length);
  322. return data;
  323. }
  324. /**
  325. * 从requst中获取文件名(确保请求中有filename这个参数)
  326. * @param {*} request
  327. * @param {*} url
  328. */
  329. function pGetFileName(request, url) {
  330. var disposition = request.getResponseHeader("Content-Disposition");
  331. var filename = "";
  332. if (disposition) {
  333. var matchs = pGetParamName(disposition, "filename=");
  334. if (matchs) {
  335. filename = decodeURIComponent(matchs);
  336. } else {
  337. filename = "petro" + Date.getTime();
  338. }
  339. } else {
  340. filename = url.substring(url.lastIndexOf("/") + 1);
  341. filename=filename.split("?")[0]
  342. }
  343. return filename;
  344. }
  345. function StringToUint8Array(string) {
  346. var binLen, buffer, chars, i, _i;
  347. binLen = string.length;
  348. buffer = new ArrayBuffer(binLen);
  349. chars = new Uint8Array(buffer);
  350. for (var i = 0; i < binLen; ++i) {
  351. chars[i] = String.prototype.charCodeAt.call(string, i);
  352. }
  353. return buffer;
  354. }
  355. /**
  356. * WPS下载文件到本地打开(业务系统可根据实际情况进行修改)
  357. * @param {*} url 文件流的下载路径
  358. * @param {*} callback 下载后的回调
  359. */
  360. function DownloadFile(url,fileName, callback) {
  361. var xhr = new XMLHttpRequest();
  362. xhr.onreadystatechange = function () {
  363. if (this.readyState == 4 && this.status == 200) {
  364. //需要业务系统的服务端在传递文件流时,确保请求中的参数有filename
  365. // var fileName = pGetFileName(xhr, url)
  366. // fileName = fileName
  367. console.log(fileName);
  368. //落地打开模式下,WPS会将文件下载到本地的临时目录,在关闭后会进行清理
  369. var path = wps.Env.GetTempPath() + "/" + fileName
  370. var reader = new FileReader();
  371. reader.onload = function () {
  372. wps.FileSystem.writeAsBinaryString(path, reader.result);
  373. callback(path);
  374. };
  375. reader.readAsBinaryString(xhr.response);
  376. }
  377. }
  378. xhr.open('GET', url);
  379. xhr.responseType = 'blob';
  380. xhr.send();
  381. }
  382. /**
  383. * WPS上传文件到服务端(业务系统可根据实际情况进行修改,为了兼容中文,服务端约定用UTF-8编码格式)
  384. * @param {*} strFileName 上传到服务端的文件名称(包含文件后缀)
  385. * @param {*} strPath 上传文件的文件路径(文件在操作系统的绝对路径)
  386. * @param {*} uploadPath 上传文件的服务端地址
  387. * @param {*} strFieldName 业务调用方自定义的一些内容可通过此字段传递,默认赋值'file'
  388. * @param {*} OnSuccess 上传成功后的回调
  389. * @param {*} OnFail 上传失败后的回调
  390. */
  391. function UploadFile(strFileName, strPath, uploadPath, strFieldName, OnSuccess, OnFail) {
  392. var xhr = new XMLHttpRequest();
  393. xhr.open('POST', uploadPath);
  394. const xx = sessionStorage.getItem('cehsi');
  395. console.log(xx)
  396. var fileData = wps.FileSystem.readAsBinaryString(strPath);
  397. var data = new FakeFormData();
  398. if (strFieldName == "" || typeof strFieldName == "undefined"){//如果业务方没定义,默认设置为'file'
  399. strFieldName = 'file';
  400. }
  401. data.append(strFieldName, {
  402. name: utf16ToUtf8(strFileName), //主要是考虑中文名的情况,服务端约定用utf-8来解码。
  403. type: "application/octet-stream",
  404. getAsBinary: function () {
  405. return fileData;
  406. }
  407. });
  408. xhr.onreadystatechange = function () {
  409. if (xhr.readyState == 4) {
  410. if (xhr.status == 200){
  411. console.log(xhr.response)
  412. const data = JSON.parse(xhr.response).data
  413. upLoadReport(data.id,data.originalFileName,OnSuccess)
  414. }
  415. // OnSuccess(xhr.response)
  416. // let width = 400 * window.devicePixelRatio
  417. // let height = 300 * window.devicePixelRatio
  418. // wps.Application.ShowDialog(JSON.stringify(xhr.response), "wps网站", 500, 500, true)
  419. // "data": {
  420. // "contentType": "application/msword",
  421. // "fileType": "DOC",
  422. // "id": 8890,
  423. // "originalFileName": "测试 (3).doc",
  424. // "size": 15872,
  425. // "suffix": "doc",
  426. // "url": ""
  427. // },
  428. else
  429. OnFail(xhr.response);
  430. }
  431. };
  432. xhr.setRequestHeader("Cache-Control", "no-cache");
  433. xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
  434. if (data.fake) {
  435. xhr.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + data.boundary);
  436. var arr = StringToUint8Array(data.toString());
  437. xhr.send(arr);
  438. } else {
  439. xhr.send(data);
  440. }
  441. }
  442. function upLoadReport(fileId,fileName,OnSuccess){
  443. const options = JSON.parse(sessionStorage.getItem('cehsi'));
  444. const url = options.extra.uploadPath
  445. const postData ={
  446. projectCode: options.extra.projectCode,
  447. appraisalId: options.extra.appraisalId,
  448. reportFile: JSON.stringify({
  449. fileId:fileId,
  450. fileName:fileName
  451. })
  452. }
  453. // 创建 XMLHttpRequest 对象
  454. var xhr = new XMLHttpRequest();
  455. // 设置请求的类型和URL
  456. xhr.open('POST', url, true);
  457. xhr.withCredentials = true;
  458. // 设置请求完成后的回调函数
  459. xhr.onload = function () {
  460. if (xhr.status >= 200 && xhr.status < 300) {
  461. // 请求成功,处理响应数据
  462. var response = JSON.parse(xhr.response);
  463. console.log(response);
  464. OnSuccess(response)
  465. } else {
  466. // 请求失败,处理错误
  467. console.error('Request failed. Returned status of ' + xhr.status);
  468. }
  469. };
  470. //设置请求出错时的回调函数
  471. xhr.onerror = function () {
  472. console.error('Request failed.');
  473. };
  474. //设置请求头,告诉服务器发送的是JSON格式的数据
  475. xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
  476. // 发送POST请求,并附上JSON数据
  477. xhr.send(JSON.stringify(postData));
  478. }
  479. /**
  480. * 打开WPS后通知到业务系统,可根据需求扩展
  481. * @param {*} p_Url 业务方接受请求的地址
  482. */
  483. function NotifyToServer(p_Url) {
  484. $.ajax({
  485. url: p_Url, // URL + '/wps/wpsCanOpen',
  486. async: true,
  487. method: "post",
  488. dataType: 'json'
  489. });
  490. }
  491. /**
  492. * 更新编辑状态
  493. * @param {*} p_Url 要传入OA端,通知业务系统,当前文档所处的编辑状态的URL地址路径
  494. * @param {*} p_OpenUrl 当前文档从业务系统打开时的入口URL,这个URL包含业务系统开发者需要传入的ID等参数
  495. * @param {*} docId 文档id
  496. * @param {*} state 0-正在编辑中 1-文件保存 2-文件关闭 状态可根据需要进行自定义扩展
  497. */
  498. function UpdateEditState(p_Url, p_OpenUrl, docId, state) {
  499. var formData = {
  500. "openUrl": p_OpenUrl,
  501. "docId": docId,
  502. "state": state
  503. };
  504. $.ajax({
  505. url: p_Url, //URL + '/document/stateMonitor',
  506. async: false,
  507. data: formData,
  508. method: "post",
  509. dataType: 'json',
  510. success: function (response) {
  511. if (response == "success") {
  512. console.log(response);
  513. }
  514. },
  515. error: function (response) {
  516. console.log(response);
  517. }
  518. });
  519. }
  520. /**
  521. * 作用:判断文档关闭后,如果系统已经没有打开的文档了,则设置回初始用户名
  522. */
  523. function pSetWPSAppUserName() {
  524. //文档全部关闭的情况下,把WPS初始启动的用户名设置回去
  525. if (wps.WpsApplication().Documents.Count == 1) {
  526. var l_strUserName = wps.PluginStorage.getItem(constStrEnum.WPSInitUserName);
  527. wps.WpsApplication().UserName = l_strUserName;
  528. }
  529. }
  530. /**
  531. * 设置文档参数的属性值
  532. * @param {*} Doc
  533. * @param {*} Key
  534. * @param {*} Value
  535. */
  536. function SetDocParamsValue(Doc, Key, Value) {
  537. if (!Doc || !Key) {
  538. return;
  539. }
  540. var l_Params = wps.PluginStorage.getItem(Doc.DocID);
  541. if (!l_Params) {
  542. return;
  543. }
  544. var l_objParams = JSON.parse(l_Params);
  545. if (!(typeof(l_objParams) == "undefined")) {
  546. l_objParams[Key] = Value;
  547. }
  548. //把属性值整体再写回原来的文档ID中
  549. wps.PluginStorage.setItem(Doc.DocID, JSON.stringify(l_objParams));
  550. }