wpsjsrpcsdk.js 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592
  1. (function (global, factory) {
  2. "use strict";
  3. if (typeof module === "object" && typeof module.exports === "object") {
  4. module.exports = factory(global, true);
  5. } else {
  6. factory(global);
  7. }
  8. })(typeof window !== "undefined" ? window : this, function (window, noGlobal) {
  9. "use strict";
  10. var bFinished = true;
  11. /**
  12. * 兼容IE低版本Array没有indexOf方法的场景
  13. */
  14. if (!Array.prototype.indexOf) {
  15. Array.prototype.indexOf = function (elt /*, from*/) {
  16. var len = this.length >>> 0;
  17. var from = Number(arguments[1]) || 0;
  18. from = (from < 0)
  19. ? Math.ceil(from)
  20. : Math.floor(from);
  21. if (from < 0)
  22. from += len;
  23. for (; from < len; from++) {
  24. if (from in this &&
  25. this[from] === elt)
  26. return from;
  27. }
  28. return -1;
  29. };
  30. }
  31. /**
  32. * 兼容IE低版本的创建httpobj对象的方法
  33. * @returns httpobj,可用于进行数据传输的http的对象
  34. */
  35. function getHttpObj() {
  36. var httpobj = null;
  37. if (IEVersion() < 10) {
  38. try {
  39. httpobj = new XDomainRequest();
  40. } catch (e1) {
  41. httpobj = new createXHR();
  42. }
  43. } else {
  44. httpobj = new createXHR();
  45. }
  46. return httpobj;
  47. }
  48. //兼容IE低版本的创建xmlhttprequest对象的方法
  49. /**
  50. * 兼容IE低版本的创建xmlhttprequest对象的方法
  51. * @returns xmlhttprequest对象,兼容低版本IE
  52. */
  53. function createXHR() {
  54. if (typeof XMLHttpRequest != 'undefined') { //兼容高版本浏览器
  55. return new XMLHttpRequest();
  56. } else if (typeof ActiveXObject != 'undefined') { //IE6 采用 ActiveXObject, 兼容IE6
  57. var versions = [ //由于MSXML库有3个版本,因此都要考虑
  58. 'MSXML2.XMLHttp.6.0',
  59. 'MSXML2.XMLHttp.3.0',
  60. 'MSXML2.XMLHttp'
  61. ];
  62. for (var i = 0; i < versions.length; i++) {
  63. try {
  64. return new ActiveXObject(versions[i]);
  65. } catch (e) {
  66. //跳过
  67. }
  68. }
  69. } else {
  70. throw new Error('您的浏览器不支持XHR对象');
  71. }
  72. }
  73. /**
  74. * 加载项本地服务返回的错误信息,统一为data : message的json格式
  75. * 如果有新增错误信息,添加到下面
  76. * 也可以通过请求状态码返回错误信息
  77. */
  78. var errorMsg = [
  79. "{\"data\": \"Failed to send message to WPS.\"}",
  80. "{\"data\": \"Json parse failed.\"}",
  81. "{\"error\": \"Json parse failed.\"}"
  82. ]
  83. /**
  84. * 通过该接口给服务器发请求
  85. * @param {*} options 参数对象,具体包含的参数如下:
  86. * @param {*} url 网页路径,传输地址
  87. * @param {*} type 传输类型,POST / GET / PUT
  88. * @param {*} sendData 传输的数据
  89. * @param {*} callBack 回调函数
  90. * @param {*} tryCount 请求失败后的尝试次数
  91. * @param {*} bPop 是否弹出浏览器提示对话框
  92. * @param {*} timeout 请求等待响应的时间,超过时间请求实效
  93. * @param {*} concurrent 请求是否同步发送
  94. * @param {*} wpsclient wpsclient对象
  95. * @returns NULL
  96. */
  97. function startWps(options) {
  98. if (!bFinished && !options.concurrent) {
  99. if (options.callBack)
  100. options.callBack({
  101. status: 1,
  102. message: "上一次请求没有完成"
  103. });
  104. return;
  105. }
  106. if (!options.concurrent)
  107. bFinished = false;
  108. else
  109. options.bFinished = false;
  110. function startWpsInnder(tryCount) {
  111. if (tryCount <= 0) {
  112. if (!options.concurrent) {
  113. if (bFinished)
  114. return;
  115. bFinished = true;
  116. } else {
  117. if (options.bFinished) {
  118. return;
  119. }
  120. options.bFinished = true;
  121. }
  122. if (options.callBack)
  123. options.callBack({
  124. status: 2,
  125. message: "请允许浏览器打开WPS Office"
  126. });
  127. return;
  128. }
  129. var xmlReq = getHttpObj();
  130. //WPS客户端提供的接收参数的本地服务,HTTP服务端口为58890,HTTPS服务端口为58891
  131. //这俩配置,取一即可,不可同时启用
  132. xmlReq.open('POST', options.url);
  133. xmlReq.onload = function (res) {
  134. var responseStr = IEVersion() < 10 ? xmlReq.responseText : res.target.response;
  135. var respStatus = IEVersion() < 10 ? xmlReq.status : res.target.status;
  136. if (!options.concurrent)
  137. bFinished = true;
  138. else
  139. options.bFinished = true;
  140. if (options.callBack) {
  141. if ((respStatus != undefined && respStatus != 200) || errorMsg.indexOf(responseStr) != -1) {
  142. var errorMessage = JSON.parse(responseStr)
  143. options.callBack({
  144. status: 1,
  145. message: errorMessage.data
  146. });
  147. if (errorMessage.data == "Subserver not available." && tryCount == options.tryCount && options.bPop) {
  148. InitWpsCloudSvr();
  149. setTimeout(function () {
  150. startWpsInnder(tryCount - 1)
  151. }, 3000);
  152. }
  153. }
  154. else {
  155. options.callBack({
  156. status: 0,
  157. response: responseStr
  158. });
  159. }
  160. }
  161. }
  162. xmlReq.ontimeout = xmlReq.onerror = function (res) {
  163. xmlReq.bTimeout = true;
  164. if (tryCount == options.tryCount && options.bPop) { //打开wps并传参
  165. InitWpsCloudSvr();
  166. }
  167. setTimeout(function () {
  168. startWpsInnder(tryCount - 1)
  169. }, 1000);
  170. }
  171. if (IEVersion() < 10) {
  172. xmlReq.onreadystatechange = function () {
  173. if (xmlReq.readyState != 4)
  174. return;
  175. if (xmlReq.bTimeout) {
  176. return;
  177. }
  178. if (xmlReq.status === 200)
  179. xmlReq.onload();
  180. else
  181. xmlReq.onerror();
  182. }
  183. }
  184. xmlReq.timeout = options.timeout;
  185. xmlReq.send(options.sendData)
  186. }
  187. startWpsInnder(options.tryCount);
  188. return;
  189. }
  190. var fromCharCode = String.fromCharCode;
  191. // encoder stuff
  192. var cb_utob = function (c) {
  193. if (c.length < 2) {
  194. var cc = c.charCodeAt(0);
  195. return cc < 0x80 ? c :
  196. cc < 0x800 ? (fromCharCode(0xc0 | (cc >>> 6)) +
  197. fromCharCode(0x80 | (cc & 0x3f))) :
  198. (fromCharCode(0xe0 | ((cc >>> 12) & 0x0f)) +
  199. fromCharCode(0x80 | ((cc >>> 6) & 0x3f)) +
  200. fromCharCode(0x80 | (cc & 0x3f)));
  201. } else {
  202. var cc = 0x10000 +
  203. (c.charCodeAt(0) - 0xD800) * 0x400 +
  204. (c.charCodeAt(1) - 0xDC00);
  205. return (fromCharCode(0xf0 | ((cc >>> 18) & 0x07)) +
  206. fromCharCode(0x80 | ((cc >>> 12) & 0x3f)) +
  207. fromCharCode(0x80 | ((cc >>> 6) & 0x3f)) +
  208. fromCharCode(0x80 | (cc & 0x3f)));
  209. }
  210. };
  211. var re_utob = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;
  212. var utob = function (u) {
  213. return u.replace(re_utob, cb_utob);
  214. };
  215. var _encode = function (u) {
  216. var isUint8Array = Object.prototype.toString.call(u) === '[object Uint8Array]';
  217. if (isUint8Array)
  218. return u.toString('base64')
  219. else
  220. return btoa(utob(String(u)));
  221. }
  222. if (typeof window.btoa !== 'function') window.btoa = func_btoa;
  223. function func_btoa(input) {
  224. var str = String(input);
  225. var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  226. for (
  227. // initialize result and counter
  228. var block, charCode, idx = 0, map = chars, output = '';
  229. // if the next str index does not exist:
  230. // change the mapping table to "="
  231. // check if d has no fractional digits
  232. str.charAt(idx | 0) || (map = '=', idx % 1) ;
  233. // "8 - idx % 1 * 8" generates the sequence 2, 4, 6, 8
  234. output += map.charAt(63 & block >> 8 - idx % 1 * 8)
  235. ) {
  236. charCode = str.charCodeAt(idx += 3 / 4);
  237. if (charCode > 0xFF) {
  238. throw new InvalidCharacterError("'btoa' failed: The string to be encoded contains characters outside of the Latin1 range.");
  239. }
  240. block = block << 8 | charCode;
  241. }
  242. return output;
  243. }
  244. if (typeof window.atob !== 'function') window.atob = func_atob;
  245. function func_atob(input) {
  246. var output = input.replace(/[\s\S]{1,4}/g, cb_decode);
  247. return output
  248. }
  249. function cb_decode(cccc) {
  250. var len = cccc.length
  251. , padlen = len % 4
  252. , n = (len > 0 ?
  253. b64tab[cccc.charAt(0)] << 18 : 0) |
  254. (len > 1 ? b64tab[cccc.charAt(1)] << 12 : 0)
  255. | (len > 2 ? b64tab[cccc.charAt(2)] << 6 : 0)
  256. | (len > 3 ? b64tab[cccc.charAt(3)] : 0)
  257. , chars = [
  258. fromCharCode(n >>> 16),
  259. fromCharCode((n >>> 8) & 0xff),
  260. fromCharCode(n & 0xff)
  261. ]
  262. ;
  263. chars.length -= [0, 0, 2, 1][padlen];
  264. return chars.join('');
  265. };
  266. function btou(b) {
  267. return b.replace(re_btou, cb_btou);
  268. };
  269. // decoder stuff
  270. var re_btou = new RegExp([
  271. '[\xC0-\xDF][\x80-\xBF]',
  272. '[\xE0-\xEF][\x80-\xBF]{2}',
  273. '[\xF0-\xF7][\x80-\xBF]{3}'
  274. ].join('|'), 'g');
  275. function cb_btou(cccc) {
  276. switch (cccc.length) {
  277. case 4:
  278. var cp = ((0x07 & cccc.charCodeAt(0)) << 18)
  279. | ((0x3f & cccc.charCodeAt(1)) << 12)
  280. | ((0x3f & cccc.charCodeAt(2)) << 6)
  281. | (0x3f & cccc.charCodeAt(3)),
  282. offset = cp - 0x10000;
  283. return (fromCharCode((offset >>> 10) + 0xD800)
  284. + fromCharCode((offset & 0x3FF) + 0xDC00));
  285. case 3:
  286. return fromCharCode(
  287. ((0x0f & cccc.charCodeAt(0)) << 12)
  288. | ((0x3f & cccc.charCodeAt(1)) << 6)
  289. | (0x3f & cccc.charCodeAt(2))
  290. );
  291. default:
  292. return fromCharCode(
  293. ((0x1f & cccc.charCodeAt(0)) << 6)
  294. | (0x3f & cccc.charCodeAt(1))
  295. );
  296. }
  297. };
  298. /**
  299. * 将字符串进行Base64编码
  300. * @param {*} u 需要编码的数据
  301. * @param {*} urisafe 返回值,编码后的数据
  302. * @returns urisafe
  303. */
  304. var encode = function (u, urisafe) {
  305. return !urisafe ?
  306. _encode(u) :
  307. _encode(String(u)).replace(/[+\/]/g, function (m0) {
  308. return m0 == '+' ? '-' : '_';
  309. }).replace(/=/g, '');
  310. };
  311. var decode = function (u) {
  312. return btou(atob(String(u)));
  313. }
  314. /**
  315. * 获取IE浏览器版本
  316. * @returns IE浏览器版本
  317. */
  318. function IEVersion() {
  319. var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
  320. var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器
  321. var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器
  322. var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;
  323. if (isIE) {
  324. var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
  325. reIE.test(userAgent);
  326. var fIEVersion = parseFloat(RegExp["$1"]);
  327. if (fIEVersion == 7) {
  328. return 7;
  329. } else if (fIEVersion == 8) {
  330. return 8;
  331. } else if (fIEVersion == 9) {
  332. return 9;
  333. } else if (fIEVersion == 10) {
  334. return 10;
  335. } else {
  336. return 6; //IE版本<=7
  337. }
  338. } else if (isEdge) {
  339. return 20; //edge
  340. } else if (isIE11) {
  341. return 11; //IE11
  342. } else {
  343. return 30; //不是ie浏览器
  344. }
  345. }
  346. /**
  347. * 启动wps客户端,加载项执行操作,无返回值
  348. * @param {*} options 参数对象,详情见下:
  349. * @param {*} clientType 加载项类型, wps / wpp / et
  350. * @param {*} name 加载项名称
  351. * @param {*} func 客户端加载项要执行的方法
  352. * @param {*} param 客户端家乡执行方法的参数
  353. * @param {*} urlBase 网页路径前缀
  354. * @param {*} callBack 回调函数
  355. * @param {*} tryCount 请求失败后的尝试次数
  356. * @param {*} bPop 是否弹出浏览器提示对话框
  357. * @param {*} wpsclient wpsclient对象
  358. */
  359. function WpsStart(options) {
  360. var startInfo = {
  361. "name": options.name,
  362. "function": options.func,
  363. "info": options.param.param,
  364. "jsPluginsXml": options.param.jsPluginsXml
  365. };
  366. var strData = JSON.stringify(startInfo);
  367. if (IEVersion() < 10) {
  368. try {
  369. eval("strData = '" + JSON.stringify(startInfo) + "';");
  370. } catch (err) {
  371. }
  372. }
  373. var baseData = encode(strData);
  374. var url = options.urlBase + "/" + options.clientType + "/runParams";
  375. var data = "ksowebstartup" + options.clientType + "://" + baseData;
  376. startWps({
  377. url: url,
  378. sendData: data,
  379. callBack: options.callBack,
  380. tryCount: options.tryCount,
  381. bPop: options.bPop,
  382. timeout: 5000,
  383. concurrent: false,
  384. wpsclient: options.wpsclient
  385. });
  386. }
  387. /**
  388. * 服务端版本为空时,通过该接口启动wps
  389. * @param {*} options 参数对象,详情见下:
  390. * @param {*} clientType 加载项类型, wps / wpp / et
  391. * @param {*} name 加载项名称
  392. * @param {*} func 客户端加载项要执行的方法
  393. * @param {*} param 客户端家乡执行方法的参数
  394. * @param {*} urlBase 网页路径前缀
  395. * @param {*} callBack 回调函数
  396. * @param {*} wpsclient wpsclient对象
  397. * @param {*} concurrent 请求是否同步发送
  398. */
  399. function WpsStartWrap(options) {
  400. WpsStart({
  401. clientType: options.clientType,
  402. name: options.name,
  403. func: options.func,
  404. param: options.param,
  405. urlBase: options.urlBase,
  406. callBack: options.callBack,
  407. tryCount: 4,
  408. bPop: true,
  409. wpsclient: options.wpsclient,
  410. })
  411. }
  412. /**
  413. * 支持浏览器触发,WPS有返回值的启动
  414. *
  415. * @param {*} clientType 组件类型
  416. * @param {*} name WPS加载项名称
  417. * @param {*} func WPS加载项入口方法
  418. * @param {*} param 参数:包括WPS加载项内部定义的方法,参数等
  419. * @param {*} callBack 回调函数
  420. * @param {*} tryCount 重试次数
  421. * @param {*} bPop 是否弹出浏览器提示对话框
  422. */
  423. function WpsStartWrapExInner(options) {
  424. var infocontent = options.param.param;
  425. var cmdId = guid();
  426. if (!options.wpsclient || options.wpsclient.single) {
  427. infocontent = JSON.stringify(options.param.param);
  428. var rspUrl = options.urlBase + "/transferEcho/runParams";
  429. var funcEx = "var res = " + options.func;
  430. var cbCode = "var xhr = new XMLHttpRequest();xhr.open('POST', '" + rspUrl + "');xhr.send(JSON.stringify({id: '" + cmdId + "', response: res}));" //res 为func执行返回值
  431. var infoEx = infocontent + ");" + cbCode + "void(0";
  432. options.func = funcEx;
  433. infocontent = infoEx;
  434. }
  435. var startInfo = {
  436. "name": options.name,
  437. "function": options.func,
  438. "info": infocontent,
  439. "showToFront": options.param.showToFront,
  440. "jsPluginsXml": options.param.jsPluginsXml,
  441. };
  442. var strData = JSON.stringify(startInfo);
  443. if (IEVersion() < 10) {
  444. try {
  445. eval("strData = '" + JSON.stringify(startInfo) + "';");
  446. } catch (err) {
  447. }
  448. }
  449. var baseData = encode(strData);
  450. var wrapper;
  451. if (!options.wpsclient || options.wpsclient.single) {
  452. var url = options.urlBase + "/transfer/runParams";
  453. var data = "ksowebstartup" + options.clientType + "://" + baseData;
  454. wrapper = {
  455. id: cmdId,
  456. app: options.clientType,
  457. data: data,
  458. serverId: serverId,
  459. mode: options.silentMode ? true : false,
  460. timeout: options.timeout,
  461. startparam: options.startparam,
  462. };
  463. }
  464. else {
  465. var url = options.urlBase + "/transferEx/runParams";
  466. wrapper = {
  467. id: options.wpsclient.clientId,
  468. app: options.clientType,
  469. data: baseData,
  470. mode: options.wpsclient.silentMode ? "true" : "false",
  471. serverId: serverId,
  472. timeout: options.timeout,
  473. startparam: options.startparam,
  474. };
  475. }
  476. wrapper = JSON.stringify(wrapper);
  477. startWps({
  478. url: url,
  479. sendData: wrapper,
  480. callBack: options.callBack,
  481. tryCount: options.tryCount,
  482. bPop: options.bPop,
  483. timeout: 0,
  484. concurrent: options.concurrent,
  485. wpsclient: options.wpsclient
  486. });
  487. }
  488. var serverVersion = "wait"
  489. /**
  490. * 获取服务端版本号的接口
  491. * @param {*} options 参数对象,详情见下:
  492. * @param {*} clientType 加载项类型, wps / wpp / et
  493. * @param {*} name 加载项名称
  494. * @param {*} func 客户端加载项要执行的方法
  495. * @param {*} param 客户端家乡执行方法的参数
  496. * @param {*} urlBase 网页路径前缀
  497. * @param {*} callBack 回调函数
  498. * @param {*} wpsclient wpsclient对象
  499. * @param {*} concurrent 请求是否同步发送
  500. */
  501. function WpsStartWrapVersionInner(options) {
  502. if (serverVersion == "wait") {
  503. if (g_isSdkInited == true) {
  504. InitWpsCloudSvr();
  505. }
  506. startWps({
  507. url: options.urlBase + '/version',
  508. sendData: JSON.stringify({ serverId: serverId }),
  509. callBack: function (res) {
  510. if (res.status !== 0) {
  511. options.callBack(res)
  512. return;
  513. }
  514. serverVersion = res.response;
  515. if (options.isPublish != true) {
  516. options.tryCount = 1
  517. options.bPop = false
  518. if (serverVersion === "") {
  519. WpsStart(options)
  520. } else if (serverVersion < "1.0.1" && options.wpsclient) {
  521. options.wpsclient.single = true;
  522. WpsStartWrapExInner(options);
  523. } else {
  524. WpsStartWrapExInner(options);
  525. }
  526. }
  527. else {
  528. if (serverVersion === "") {
  529. callBack({
  530. status: 4,
  531. message: "当前客户端不支持,请升级客户端"
  532. })
  533. } else {
  534. WpsAddonGetAllConfigInner(options.callBack)
  535. }
  536. }
  537. },
  538. tryCount: 4,
  539. bPop: !g_isSdkInited,
  540. timeout: 5000,
  541. concurrent: options.concurrent
  542. });
  543. } else {
  544. if (options.isPublish != true) {
  545. options.tryCount = 4
  546. options.bPop = true
  547. if (serverVersion === "") {
  548. WpsStartWrap(options)
  549. } else if (serverVersion < "1.0.1" && options.wpsclient) {
  550. options.wpsclient.single = true;
  551. WpsStartWrapExInner(options);
  552. } else {
  553. WpsStartWrapExInner(options);
  554. }
  555. }
  556. else {
  557. if (serverVersion === "") {
  558. callBack({
  559. status: 4,
  560. message: "当前客户端不支持,请升级客户端"
  561. })
  562. } else {
  563. WpsAddonGetAllConfigInner(options.callBack)
  564. }
  565. }
  566. }
  567. }
  568. var HeartBeatCode =
  569. "function getHttpObj() {\n"
  570. + " var httpobj = null;\n"
  571. + " if (IEVersion() < 10) {\n"
  572. + " try {\n"
  573. + " httpobj = new XDomainRequest();\n"
  574. + " } catch (e1) {\n"
  575. + " httpobj = new createXHR();\n"
  576. + " }\n"
  577. + " } else {\n"
  578. + " httpobj = new createXHR();\n"
  579. + " }\n"
  580. + " return httpobj;\n"
  581. + " }\n"
  582. + " \n"
  583. + " function createXHR() {\n"
  584. + " if (typeof XMLHttpRequest != 'undefined') {\n"
  585. + " return new XMLHttpRequest();\n"
  586. + " } else if (typeof ActiveXObject != 'undefined') {\n"
  587. + " var versions = [\n"
  588. + " 'MSXML2.XMLHttp.6.0',\n"
  589. + " 'MSXML2.XMLHttp.3.0',\n"
  590. + " 'MSXML2.XMLHttp'\n"
  591. + " ];\n"
  592. + " \n"
  593. + " for (var i = 0; i < versions.length; i++) {\n"
  594. + " try {\n"
  595. + " return new ActiveXObject(versions[i]);\n"
  596. + " } catch (e) {\n"
  597. + " \n"
  598. + " }\n"
  599. + " }\n"
  600. + " } else {\n"
  601. + " throw new Error('您的浏览器不支持XHR对象');\n"
  602. + " }\n"
  603. + " }\n"
  604. + " \n"
  605. + " function IEVersion() {\n"
  606. + " var userAgent = navigator.userAgent; \n"
  607. + " var isIE = userAgent.indexOf('compatible') > -1 && userAgent.indexOf('MSIE') > -1;\n"
  608. + " var isEdge = userAgent.indexOf('Edge') > -1 && !isIE; \n"
  609. + " var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf('rv:11.0') > -1;\n"
  610. + " if (isIE) {\n"
  611. + " var reIE = new RegExp('MSIE (\\d+\\.\\d+);');\n"
  612. + " reIE.test(userAgent);\n"
  613. + " var fIEVersion = parseFloat(RegExp['$1']);\n"
  614. + " if (fIEVersion == 7) {\n"
  615. + " return 7;\n"
  616. + " } else if (fIEVersion == 8) {\n"
  617. + " return 8;\n"
  618. + " } else if (fIEVersion == 9) {\n"
  619. + " return 9;\n"
  620. + " } else if (fIEVersion == 10) {\n"
  621. + " return 10;\n"
  622. + " } else {\n"
  623. + " return 6; \n"
  624. + " }\n"
  625. + " } else if (isEdge) {\n"
  626. + " return 20; \n"
  627. + " } else if (isIE11) {\n"
  628. + " return 11; \n"
  629. + " } else {\n"
  630. + " return 30; \n"
  631. + " }\n"
  632. + " }\n"
  633. + " var heartBeatStart = false;\n"
  634. + " function checkLastRegTime() {\n"
  635. + " var now = new Date().valueOf();\n"
  636. + " var TimeGap = now - LastRegTime;\n"
  637. + " if (TimeGap > 5000 && !heartBeatStart) {\n"
  638. + " HeartBeat();\n"
  639. + " heartBeatStart = true;\n"
  640. + " }\n"
  641. + " }\n"
  642. + " \n"
  643. + " function HeartBeat() {\n"
  644. + " var heartBeatItem = function () {\n"
  645. + " var xhr = getHttpObj();\n"
  646. + " xhr.onload = function (e) {\n"
  647. + " self.setTimeout(heartBeatItem, 5000);\n"
  648. + " }\n"
  649. + " xhr.onerror = function (e) {\n"
  650. + " self.setTimeout(heartBeatItem, 5000);\n"
  651. + " }\n"
  652. + " xhr.ontimeout = function (e) {\n"
  653. + " self.setTimeout(heartBeatItem, 5000);\n"
  654. + " }\n"
  655. + " xhr.open('POST', 'http://127.0.0.1:58890/askwebnotify', true);\n"
  656. + " xhr.timeout = 2000;\n"
  657. + " xhr.send(JSON.stringify(paramStr));\n"
  658. + " }\n"
  659. + " heartBeatItem();\n"
  660. + " }\n"
  661. + " \n"
  662. + " var paramStr;\n"
  663. + " var startCheck = false;\n"
  664. + " self.addEventListener('message', function (event) {\n"
  665. + " var data = event.data;\n"
  666. + " paramStr = data.param\n"
  667. + " paramStr.heartBeat = true\n"
  668. + " LastRegTime = data.LastRegTime;\n"
  669. + " if (!startCheck) {\n"
  670. + " startCheck = true;\n"
  671. + " self.setInterval(checkLastRegTime, 5000)\n"
  672. + " }\n"
  673. + " }, false);\n"
  674. /**
  675. * 生成guid的接口
  676. * @returns guid
  677. */
  678. function guid() {
  679. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  680. var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
  681. return v.toString(16);
  682. });
  683. }
  684. /**
  685. * 开启多用户的接口
  686. */
  687. var serverId = undefined
  688. function EnableMultiUser() {
  689. serverId = getServerId();
  690. InitSdk(true)
  691. }
  692. /**
  693. * 自定义协议启动服务端
  694. * 默认不带参数serverId,linux未升级之前不要使用多用户
  695. */
  696. function InitWpsCloudSvr() {
  697. if (serverId == undefined)
  698. window.location.href = "ksoWPSCloudSvr://start=RelayHttpServer"//是否启动wps弹框
  699. else
  700. window.location.href = "ksoWPSCloudSvr://start=RelayHttpServer" + "&serverId=" + serverId //是否启动wps弹框
  701. }
  702. /**
  703. * 获取serverId的接口
  704. * @returns serverId
  705. */
  706. function getServerId() {
  707. if (window.localStorage) {
  708. if (localStorage.getItem("serverId")) {
  709. //
  710. }
  711. else {
  712. localStorage.setItem("serverId", guid());
  713. }
  714. return localStorage.getItem("serverId");
  715. }
  716. else {
  717. return guid();
  718. }
  719. }
  720. /**
  721. * 将字符串转成二进制,这里用来将字符串化后的js代码转成二进制文件
  722. * @param {*} code
  723. * @returns js文件对象的url
  724. */
  725. function codeToBlob(code) {
  726. var blob = new Blob([code], { type: 'text/javascript' }); // 生成js文件对象
  727. var objectURL = window.URL.createObjectURL(blob); // 生成js文件的url
  728. return objectURL;
  729. }
  730. var RegWebNotifyMap = { wps: {}, wpp: {}, et: {}, pdf: {} }
  731. var bWebNotifyUseTimeout = true
  732. function WebNotifyUseTimeout(value) {
  733. bWebNotifyUseTimeout = value ? true : false
  734. }
  735. var g_businessId = Number(Math.random().toString().substr(3, 5) + Date.parse(new Date())).toString(36);
  736. var HeartBeatWorker
  737. if (window.Worker) {
  738. try {
  739. HeartBeatWorker = new Worker(codeToBlob(HeartBeatCode));
  740. } catch (error) {
  741. //
  742. }
  743. }
  744. var g_LastRegTime;
  745. /**
  746. * 注册一个前端页面接收WPS传来消息的方法
  747. * @param {*} clientType wps | et | wpp
  748. * @param {*} name WPS加载项的名称
  749. * @param {*} callBack 回调函数
  750. * @param {*} wpsclient wpsclient对象
  751. */
  752. function RegWebNotify(clientType, name, callBack, wpsclient) {
  753. if (clientType != "wps" && clientType != "wpp" && clientType != "et" && clientType != "pdf")
  754. return;
  755. var paramStr = {}
  756. if (wpsclient) {
  757. if (wpsclient.notifyRegsitered == true) {
  758. return
  759. }
  760. wpsclient.notifyRegsitered = true;
  761. paramStr = {
  762. clientId: wpsclient.clientId,
  763. name: name,
  764. type: clientType,
  765. serverId: serverId
  766. }
  767. if (HeartBeatWorker)
  768. paramStr.businessId = g_businessId
  769. }
  770. else {
  771. if (typeof callBack != 'function')
  772. return
  773. if (RegWebNotifyMap[clientType][name]) {
  774. RegWebNotifyMap[clientType][name] = callBack;
  775. return
  776. }
  777. var RegWebNotifyID = new Date().valueOf() + ''
  778. paramStr = {
  779. id: RegWebNotifyID,
  780. name: name,
  781. type: clientType,
  782. serverId: serverId
  783. }
  784. if (HeartBeatWorker)
  785. paramStr.businessId = g_businessId
  786. RegWebNotifyMap[clientType][name] = callBack
  787. }
  788. var askItem = function () {
  789. var xhr = getHttpObj()
  790. xhr.onload = function (e) {
  791. if (xhr.responseText == "WPSInnerMessage_quit" || (xhr.status != undefined && xhr.status != 200)) {
  792. if (!wpsclient || wpsclient.single) {
  793. window.setTimeout(askItem, 2000);
  794. }
  795. return;
  796. }
  797. window.setTimeout(askItem, 300)
  798. if (serverVersion < "1.0.3") {
  799. try {
  800. if (!HeartBeatWorker)
  801. throw new Error();
  802. var resText = JSON.parse(xhr.responseText);
  803. paramStr.messageId = resText.msgId;
  804. if (wpsclient) {
  805. if (resText.data != undefined && paramStr.messageId != undefined) // 如果发的数据是字符串化后的json对象,这里的resText.data就是一个json对象,可以输出自己想要的json数据
  806. if (typeof resText.data == 'object')
  807. wpsclient.OnRegWebNotify(JSON.stringify(resText.data))
  808. else
  809. wpsclient.OnRegWebNotify(resText.data)
  810. else
  811. wpsclient.OnRegWebNotify(xhr.responseText)
  812. } else {
  813. var func = RegWebNotifyMap[clientType][name]
  814. if (resText.data != undefined && paramStr.messageId != undefined) // 如果发的数据是字符串化后的json对象,这里的resText.data就是一个json对象,可以输出自己想要的json数据
  815. if (typeof resText.data == 'object')
  816. func(JSON.stringify(resText.data))
  817. else
  818. func(resText.data)
  819. else
  820. func(xhr.responseText)
  821. }
  822. }
  823. catch (e) {
  824. // 这里做一个容错,即使json解析失败,也要把msgId提取出来,发回给服务端,避免消息清不掉一直重复发送
  825. // 同时把data也取出来,但是格式无法保证
  826. var data
  827. var str = xhr.responseText
  828. var idx1 = str.indexOf("\"msgId\"")
  829. var idx2
  830. var idx3
  831. var idx4
  832. var data
  833. if (idx1 != -1) {
  834. idx1 = idx1 + 8
  835. idx2 = str.indexOf("\"data\"") - 2
  836. paramStr.messageId = parseInt(str.substring(idx1, idx2))
  837. idx3 = str.indexOf("\"data\"") + 8
  838. idx4 = str.length - 2
  839. data = str.substring(idx3, idx4)
  840. }
  841. if (wpsclient) {
  842. if (paramStr.messageId !== undefined && data != undefined)
  843. wpsclient.OnRegWebNotify(data)
  844. else
  845. wpsclient.OnRegWebNotify(xhr.responseText)
  846. } else {
  847. var func = RegWebNotifyMap[clientType][name]
  848. if (paramStr.messageId !== undefined && data != undefined)
  849. func(data)
  850. else
  851. func(xhr.responseText)
  852. }
  853. }
  854. }
  855. else {
  856. var resText = JSON.parse(xhr.responseText);
  857. paramStr.messageId = resText.msgId;
  858. var resData = decode(resText.data);
  859. if (wpsclient) {
  860. wpsclient.OnRegWebNotify(resData)
  861. }
  862. else {
  863. var func = RegWebNotifyMap[clientType][name]
  864. func(resData)
  865. }
  866. }
  867. }
  868. xhr.onerror = function (e) {
  869. if (bWebNotifyUseTimeout)
  870. window.setTimeout(askItem, 1000)
  871. else
  872. window.setTimeout(askItem, 10000)
  873. }
  874. xhr.ontimeout = function (e) {
  875. if (bWebNotifyUseTimeout)
  876. window.setTimeout(askItem, 300)
  877. else
  878. window.setTimeout(askItem, 10000)
  879. }
  880. if (IEVersion() < 10) {
  881. xhr.onreadystatechange = function () {
  882. if (xhr.readyState != 4)
  883. return;
  884. if (xhr.bTimeout) {
  885. return;
  886. }
  887. if (xhr.status === 200)
  888. xhr.onload();
  889. else
  890. xhr.onerror();
  891. }
  892. }
  893. xhr.open('POST', GetUrlBase() + '/askwebnotify', true)
  894. if (bWebNotifyUseTimeout)
  895. xhr.timeout = 2000;
  896. if (HeartBeatWorker) {
  897. g_LastRegTime = new Date().valueOf();
  898. var param = {
  899. param: {
  900. name: name,
  901. type: clientType,
  902. businessId: g_businessId,
  903. serverId: serverId
  904. },
  905. LastRegTime: g_LastRegTime
  906. }
  907. HeartBeatWorker.postMessage(param)
  908. }
  909. xhr.send(JSON.stringify(paramStr));
  910. }
  911. askItem()
  912. }
  913. /**
  914. * 获取网页路径前缀
  915. * @returns url前缀
  916. */
  917. function GetUrlBase() {
  918. if (location.protocol == "https:")
  919. return "https://127.0.0.1:58891"
  920. return "http://127.0.0.1:58890"
  921. }
  922. /**
  923. * 获取服务端版本号的接口,这里主要是初始化一些参数
  924. * @param {*} clientType 加载项类型, wps / wpp / et
  925. * @param {*} name 加载项名称
  926. * @param {*} func 客户端加载项要执行的方法
  927. * @param {*} param 客户端加载项执行方法的参数
  928. * @param {*} callBack 回调函数
  929. * @param {*} showToFront 设置客户端是否显示到前面
  930. * @param {*} jsPluginsXml 设置加载项路径
  931. * @param {*} silentMode 是否是静默启动
  932. * @param {*} timeout 等待wps客户端启动的超时时间,单位为ms
  933. * @param {*} startparam 传递给wps客户端的启动参数
  934. */
  935. function WpsStartWrapVersion(clientType, name, func, param, callBack, showToFront, jsPluginsXml, silentMode, timeout, startparam) {
  936. var paramEx = {
  937. jsPluginsXml: jsPluginsXml ? jsPluginsXml : "",
  938. showToFront: typeof (showToFront) == 'boolean' ? showToFront : true,
  939. param: (typeof (param) == 'object' ? param : JSON.parse(param))
  940. }
  941. var options = {
  942. clientType: clientType,
  943. name: name,
  944. func: func,
  945. param: paramEx,
  946. urlBase: GetUrlBase(),
  947. callBack: callBack,
  948. wpsclient: undefined,
  949. concurrent: true,
  950. silentMode: silentMode,
  951. timeout: timeout,
  952. startparam: startparam,
  953. }
  954. WpsStartWrapVersionInner(options);
  955. }
  956. //从外部浏览器远程调用 WPS 加载项中的方法
  957. var WpsInvoke = {
  958. InvokeAsHttp: WpsStartWrapVersion,
  959. InvokeAsHttps: WpsStartWrapVersion,
  960. RegWebNotify: RegWebNotify,
  961. ClientType: {
  962. wps: "wps",
  963. et: "et",
  964. wpp: "wpp",
  965. pdf: "pdf"
  966. },
  967. CreateXHR: getHttpObj,
  968. IsClientRunning: IsClientRunning,
  969. GetVersion: GetVersion
  970. }
  971. /**
  972. * @constructor WpsClient wps客户端
  973. * @param {string} clientType 必传参数,加载项类型,有效值为"wps","wpp","et";分别表示文字,演示,电子表格
  974. */
  975. function WpsClient(clientType) {
  976. /**
  977. * 设置RegWebNotify的回调函数,加载项给业务端发消息通过该函数
  978. * @memberof WpsClient
  979. * @member onMessage
  980. */
  981. this.onMessage;
  982. /**
  983. * 设置加载项路径
  984. * @memberof WpsClient
  985. * @member jsPluginsXml
  986. */
  987. this.jsPluginsXml;
  988. /**
  989. * 内部成员,外部无需调用
  990. */
  991. this.notifyRegsitered = false;
  992. this.clientId = "";
  993. this.concurrent = true;
  994. this.clientType = clientType;
  995. /**
  996. * 内部函数,外部无需调用
  997. * @param {*} options
  998. */
  999. this.initWpsClient = function (options) {
  1000. options.clientType = this.clientType
  1001. options.wpsclient = this
  1002. options.concurrent = this.concurrent
  1003. WpsStartWrapVersionInner(options)
  1004. }
  1005. /**
  1006. * 以http启动
  1007. * @param {string} name 加载项名称
  1008. * @param {string} func 要调用的加载项中的函数行
  1009. * @param {string} param 在加载项中执行函数func要传递的数据
  1010. * @param {function({int, string})} callBack 回调函数,status = 0 表示成功,失败请查看message信息
  1011. * @param {bool} showToFront 设置wps是否显示到前面来
  1012. * @param {*} timeout 等待wps客户端启动的超时时间,单位为ms
  1013. * @param {*} startparam 传递给wps客户端的启动参数
  1014. * @return {string} "Failed to send message to WPS." 发送消息失败,客户端已关闭;
  1015. * "WPS Addon is not response." 加载项阻塞,函数执行失败
  1016. */
  1017. this.InvokeAsHttp = function (name, func, param, callBack, showToFront, timeout, startparam) {
  1018. function clientCallback(res) {
  1019. //this不是WpsClient,是options对象
  1020. if (res.status !== 0 || serverVersion < "1.0.1" || this.wpsclient.single == true) {
  1021. if (callBack)
  1022. callBack(res);
  1023. if (serverVersion < "1.0.1" || this.wpsclient.single == true)
  1024. RegWebNotify(clientType, name, this.wpsclient.onMessage)
  1025. return;
  1026. }
  1027. if (serverVersion < "1.0.3") {
  1028. try {
  1029. var resObject = JSON.parse(res.response);
  1030. if (this.wpsclient.clientId == "") {
  1031. this.wpsclient.clientId = resObject.clientId;
  1032. }
  1033. if (typeof resObject.data == "object")
  1034. res.response = JSON.stringify(resObject.data);
  1035. else
  1036. res.response = resObject.data;
  1037. }
  1038. catch (e) {
  1039. var str = res.response
  1040. var idx1 = str.indexOf("\"clientId\":\"{")
  1041. var idx2
  1042. var idx3
  1043. var idx4
  1044. if (idx1 != -1) {
  1045. idx1 = idx1 + ("\"clientId\":\"{").length - 1
  1046. idx2 = str.indexOf("\"data\":") - 3
  1047. if (this.wpsclient.clientId == "") {
  1048. this.wpsclient.clientId = str.substring(idx1, idx2);
  1049. }
  1050. idx3 = str.indexOf("\"data\":") + ("\"data\":").length
  1051. idx4 = str.length - 1
  1052. if (idx3 < idx4)
  1053. res.response = str.substring(idx3, idx4)
  1054. else
  1055. res.response = "";
  1056. }
  1057. }
  1058. }
  1059. else {
  1060. var resObject = JSON.parse(res.response);
  1061. if (this.wpsclient.clientId == "") {
  1062. this.wpsclient.clientId = resObject.clientId;
  1063. }
  1064. res.response = decode(resObject.data);
  1065. }
  1066. if (IEVersion() < 10)
  1067. eval(" res.response = '" + res.response + "';");
  1068. if (callBack)
  1069. callBack(res);
  1070. this.wpsclient.RegWebNotify(name);
  1071. }
  1072. var paramEx = {
  1073. jsPluginsXml: this.jsPluginsXml ? this.jsPluginsXml : "",
  1074. showToFront: typeof (showToFront) == 'boolean' ? showToFront : true,
  1075. param: (typeof (param) == 'object' ? param : JSON.parse(param))
  1076. }
  1077. this.initWpsClient({
  1078. name: name,
  1079. func: func,
  1080. param: paramEx,
  1081. urlBase: GetUrlBase(),
  1082. callBack: clientCallback,
  1083. timeout: timeout,
  1084. startparam: startparam,
  1085. })
  1086. }
  1087. /**
  1088. * 以https启动
  1089. * @param {string} name 加载项名称
  1090. * @param {string} func 要调用的加载项中的函数行
  1091. * @param {string} param 在加载项中执行函数func要传递的数据
  1092. * @param {function({int, string})} callBack 回调函数,status = 0 表示成功,失败请查看message信息
  1093. * @param {*} timeout 等待wps客户端启动的超时时间,单位为ms
  1094. * @param {*} startparam 传递给wps客户端的启动参数
  1095. * @param {bool} showToFront 设置wps是否显示到前面来
  1096. */
  1097. this.InvokeAsHttps = function (name, func, param, callBack, showToFront, timeout, startparam) {
  1098. var paramEx = {
  1099. jsPluginsXml: this.jsPluginsXml ? this.jsPluginsXml : "",
  1100. showToFront: typeof (showToFront) == 'boolean' ? showToFront : true,
  1101. param: (typeof (param) == 'object' ? param : JSON.parse(param))
  1102. }
  1103. this.initWpsClient({
  1104. name: name,
  1105. func: func,
  1106. param: paramEx,
  1107. urlBase: GetUrlBase(),
  1108. callBack: callBack,
  1109. timeout: timeout,
  1110. startparam: startparam,
  1111. })
  1112. }
  1113. /**
  1114. * 内部函数,外部无需调用
  1115. * @param {*} name
  1116. */
  1117. this.RegWebNotify = function (name) {
  1118. RegWebNotify(this.clientType, name, null, this);
  1119. }
  1120. /**
  1121. * 消息注册函数的回调函数
  1122. * @param {*} message 客户端发来的消息
  1123. */
  1124. this.OnRegWebNotify = function (message) {
  1125. if (this.onMessage)
  1126. this.onMessage(message)
  1127. }
  1128. /**
  1129. * 以静默模式启动客户端
  1130. * @param {string} name 必传参数,加载项名称
  1131. * @param {*} timeout 等待wps客户端启动的超时时间,单位为ms
  1132. * @param {*} startparam 传递给wps客户端的启动参数
  1133. * @param {function({int, string})} [callBack] 回调函数,status = 0 表示成功,失败请查看message信息
  1134. */
  1135. this.StartWpsInSilentMode = function (name, callBack, timeout, startparam) {
  1136. function initCallback(res) {
  1137. //this不是WpsClient,是options对象
  1138. if (res.status !== 0 || serverVersion < "1.0.1" || this.wpsclient.single == true) {
  1139. if (callBack)
  1140. callBack(res);
  1141. if (serverVersion < "1.0.1" || this.wpsclient.single == true)
  1142. RegWebNotify(clientType, name, this.wpsclient.onMessage)
  1143. return;
  1144. }
  1145. var jsonObj = JSON.parse(res.response);
  1146. if (this.wpsclient.clientId == "") {
  1147. this.wpsclient.clientId = jsonObj.clientId;
  1148. }
  1149. if (serverVersion < "1.0.3") {
  1150. res.response = JSON.stringify(jsonObj.data);
  1151. }
  1152. else {
  1153. res.response = decode(jsonObj.data);
  1154. }
  1155. if (callBack) {
  1156. callBack(res);
  1157. }
  1158. this.wpsclient.RegWebNotify(name);
  1159. }
  1160. var paramEx = {
  1161. jsPluginsXml: this.jsPluginsXml,
  1162. showToFront: false,
  1163. param: { status: "InitInSilentMode" }
  1164. }
  1165. this.silentMode = true;
  1166. this.initWpsClient({
  1167. name: name,
  1168. func: "",
  1169. param: paramEx,
  1170. urlBase: GetUrlBase(),
  1171. callBack: initCallback,
  1172. timeout: timeout,
  1173. startparam: startparam,
  1174. })
  1175. }
  1176. /**
  1177. * 显示客户端到最前面
  1178. * @param {string} name 必传参数,加载项名称
  1179. * @param {function({int, string})} [callBack] 回调函数
  1180. */
  1181. this.ShowToFront = function (name, callBack) {
  1182. if (serverVersion < "1.0.1") {
  1183. if (callBack) {
  1184. callBack({
  1185. status: 4,
  1186. message: "当前客户端不支持,请升级客户端"
  1187. });
  1188. return;
  1189. }
  1190. return;
  1191. }
  1192. if (this.clientId == "") {
  1193. if (callBack) callBack({
  1194. status: 3,
  1195. message: "没有静默启动客户端"
  1196. });
  1197. return;
  1198. }
  1199. var paramEx = {
  1200. jsPluginsXml: "",
  1201. showToFront: true,
  1202. param: { status: "ShowToFront" }
  1203. }
  1204. this.initWpsClient({
  1205. name: name,
  1206. func: "",
  1207. param: paramEx,
  1208. urlBase: GetUrlBase(),
  1209. callBack: callBack
  1210. })
  1211. }
  1212. /**
  1213. * 关闭未显示出来的静默启动客户端
  1214. * @param {string} name 必传参数,加载项名称
  1215. * @param {function({int, string})} [callBack] 回调函数
  1216. */
  1217. this.CloseSilentClient = function (name, callBack) {
  1218. if (serverVersion < "1.0.1") {
  1219. if (callBack) {
  1220. callBack({
  1221. status: 4,
  1222. message: "当前客户端不支持,请升级客户端"
  1223. });
  1224. return;
  1225. }
  1226. return;
  1227. }
  1228. if (this.clientId == "") {
  1229. if (callBack) callBack({
  1230. status: 3,
  1231. message: "没有静默启动客户端"
  1232. });
  1233. return;
  1234. }
  1235. var paramEx = {
  1236. jsPluginsXml: "",
  1237. showToFront: false,
  1238. param: undefined
  1239. }
  1240. var func;
  1241. if (this.clientType == "wps")
  1242. func = "wps.WpsApplication().Quit"
  1243. else if (this.clientType == "et")
  1244. func = "wps.EtApplication().Quit"
  1245. else if (this.clientType == "wpp")
  1246. func = "wps.WppApplication().Quit"
  1247. function closeSilentClient(res) {
  1248. if (res.status == 0)
  1249. this.wpsclient.clientId = ""
  1250. if (callBack) callBack(res);
  1251. return;
  1252. }
  1253. this.initWpsClient({
  1254. name: name,
  1255. func: func,
  1256. param: paramEx,
  1257. urlBase: GetUrlBase(),
  1258. callBack: closeSilentClient
  1259. })
  1260. }
  1261. /**
  1262. * 当前客户端是否在运行,使用WpsClient.IsClientRunning()进行调用
  1263. * @param {function({int, string})} [callBack] 回调函数,"Client is running." 客户端正在运行
  1264. * "Client is not running." 客户端没有运行
  1265. */
  1266. this.IsClientRunning = function (callBack) {
  1267. if (serverVersion < "1.0.1") {
  1268. if (callBack) {
  1269. callBack({
  1270. status: 4,
  1271. message: "当前客户端不支持,请升级客户端"
  1272. });
  1273. return;
  1274. }
  1275. return;
  1276. }
  1277. IsClientRunning(this.clientType, callBack, this)
  1278. }
  1279. }
  1280. /**
  1281. * 初始化sdk,用来减少在服务进程启动时自定义协议弹框出现的次数
  1282. */
  1283. var g_isSdkInited = false;
  1284. function InitSdk(bMultiUser) {
  1285. g_isSdkInited = false;
  1286. var url = GetUrlBase() + "/version";
  1287. startWps({
  1288. url: url,
  1289. sendData: JSON.stringify({ serverId: serverId }),
  1290. callBack: function (res) {
  1291. if ((!serverId && !bMultiUser) || bMultiUser)
  1292. g_isSdkInited = true;
  1293. if (res.status !== 0 && res.message !== "Subserver not available.") {
  1294. serverVersion = "wait"
  1295. return;
  1296. }
  1297. if (res.response && serverVersion == "wait")
  1298. serverVersion = res.response;
  1299. },
  1300. tryCount: 1,
  1301. bPop: false,
  1302. concurrent: true,
  1303. timeout: 1000
  1304. });
  1305. }
  1306. InitSdk(false);
  1307. function GetVersion(callBack) {
  1308. var url = GetUrlBase() + "/version";
  1309. startWps({
  1310. url: url,
  1311. data: "",
  1312. callBack: function (res) {
  1313. callBack && callBack(res);
  1314. },
  1315. tryCount: 4,
  1316. bPop: true,
  1317. timeout: 5000
  1318. });
  1319. }
  1320. if (typeof noGlobal === "undefined") {
  1321. window.WpsInvoke = WpsInvoke;
  1322. window.WpsClient = WpsClient;
  1323. window.WebNotifyUseTimeout = WebNotifyUseTimeout;
  1324. window.EnableMultiUser = EnableMultiUser;
  1325. }
  1326. /**
  1327. * 当前客户端是否在运行,使用WpsInvoke.IsClientRunning()进行调用
  1328. * @param {string} clientType 加载项类型
  1329. * @param {function} [callBack] 回调函数,"Client is running." 客户端正在运行
  1330. * "Client is not running." 客户端没有运行
  1331. */
  1332. function IsClientRunning(clientType, callBack, wpsclient) {
  1333. var url = GetUrlBase() + "/isRunning";
  1334. var wrapper = {
  1335. id: wpsclient == undefined ? undefined : wpsclient.clientId,
  1336. app: clientType,
  1337. serverId: serverId
  1338. }
  1339. wrapper = JSON.stringify(wrapper);
  1340. startWps({
  1341. url: url,
  1342. sendData: wrapper,
  1343. callBack: callBack,
  1344. tryCount: 1,
  1345. bPop: false,
  1346. timeout: 2000,
  1347. concurrent: true,
  1348. wpsclient: wpsclient
  1349. });
  1350. }
  1351. /**
  1352. * 获取publish.xml的内容
  1353. * @param {*} callBack 回调函数
  1354. */
  1355. function WpsAddonGetAllConfigInner(callBack) {
  1356. var options = {
  1357. url: GetUrlBase() + "/publishlist",
  1358. type: "POST",
  1359. sendData: JSON.stringify({ serverId: serverId }),
  1360. callBack: callBack,
  1361. tryCount: 3,
  1362. bPop: true,
  1363. timeout: 5000,
  1364. concurrent: true,
  1365. }
  1366. startWps(options);
  1367. }
  1368. /**
  1369. * 先获取一下版本号
  1370. * @param {*} callBack 回调函数
  1371. */
  1372. function WpsAddonGetAllConfig(callBack) {
  1373. var options = {
  1374. callBack: callBack,
  1375. isPublish: true,
  1376. urlBase: GetUrlBase()
  1377. }
  1378. WpsStartWrapVersionInner(options);
  1379. }
  1380. /**
  1381. * 检查ribbon.xml文件是否有效
  1382. * @param {*} element 参数对象
  1383. * @param {*} callBack 回调函数
  1384. */
  1385. function WpsAddonVerifyStatus(element, callBack) {
  1386. var xmlReq = getHttpObj();
  1387. var offline = element.online === "false";
  1388. var url = offline ? element.url : element.url + "ribbon.xml";
  1389. xmlReq.open("POST", GetUrlBase() + "/redirect/runParams");
  1390. xmlReq.onload = function (res) {
  1391. if (offline && !res.target.response.startsWith("7z")) {
  1392. callBack({ status: 1, msg: "不是有效的7z格式" + url });
  1393. } else if (!offline && !res.target.response.startsWith("<customUI")) {
  1394. callBack({ status: 1, msg: "不是有效的ribbon.xml, " + url })
  1395. } else {
  1396. callBack({ status: 0, msg: "OK" })
  1397. }
  1398. }
  1399. xmlReq.onerror = function (res) {
  1400. xmlReq.bTimeout = true;
  1401. callBack({ status: 2, msg: "网页路径不可访问,如果是跨域问题,不影响使用" + url })
  1402. }
  1403. xmlReq.ontimeout = function (res) {
  1404. xmlReq.bTimeout = true;
  1405. callBack({ status: 3, msg: "访问超时" + url })
  1406. }
  1407. if (IEVersion() < 10) {
  1408. xmlReq.onreadystatechange = function () {
  1409. if (xmlReq.readyState != 4)
  1410. return;
  1411. if (xmlReq.bTimeout) {
  1412. return;
  1413. }
  1414. if (xmlReq.status === 200)
  1415. xmlReq.onload();
  1416. else
  1417. xmlReq.onerror();
  1418. }
  1419. }
  1420. xmlReq.timeout = 5000;
  1421. var data = {
  1422. method: "get",
  1423. url: url,
  1424. data: ""
  1425. }
  1426. var sendData = FormatSendData(data)
  1427. xmlReq.send(sendData);
  1428. }
  1429. /**
  1430. * 部署加载项,包括启动enable / disable禁用 / disableall禁用所有
  1431. * @param {*} element 参数对象
  1432. * @param {*} cmd 具体操作,enable / disable / disableall
  1433. * @param {*} callBack 回调函数
  1434. */
  1435. function WpsAddonHandleEx(element, cmd, callBack) {
  1436. var data = FormatData(element, cmd);
  1437. startWps({
  1438. url: GetUrlBase() + "/deployaddons/runParams",
  1439. type: "POST",
  1440. sendData: data,
  1441. callBack: callBack,
  1442. tryCount: 3,
  1443. bPop: true,
  1444. timeout: 0,
  1445. concurrent: true
  1446. });
  1447. }
  1448. /**
  1449. * 启用加载项
  1450. * @param {*} element 参数对象
  1451. * @param {*} callBack 回调函数
  1452. */
  1453. function WpsAddonEnable(element, callBack) {
  1454. WpsAddonHandleEx(element, "enable", callBack)
  1455. }
  1456. /**
  1457. * 禁用加载项
  1458. * @param {*} element 参数对象
  1459. * @param {*} callBack 回调函数
  1460. */
  1461. function WpsAddonDisable(element, callBack) {
  1462. WpsAddonHandleEx(element, "disable", callBack)
  1463. }
  1464. /**
  1465. * 禁用所有加载项
  1466. * @param {*} element
  1467. * @param {*} callBack
  1468. */
  1469. function WpsAddonDisableAll(element, callBack) {
  1470. WpsAddonHandleEx(element, "disableall", callBack)
  1471. }
  1472. /**
  1473. * 生成json格式的数据
  1474. * @param {*} element 参数对象
  1475. * @param {*} cmd 具体操作,enable / disable / disableall
  1476. * @returns base64编码后的数据
  1477. */
  1478. function FormatData(element, cmd) {
  1479. var data = {
  1480. "cmd": cmd, //"enable", 启用, "disable", 禁用, "disableall", 禁用所有
  1481. "name": element.name,
  1482. "url": element.url,
  1483. "addonType": element.addonType,
  1484. "online": element.online,
  1485. "version": element.version,
  1486. "time": new Date().getTime()
  1487. }
  1488. return FormatSendData(data);
  1489. }
  1490. /**
  1491. * 将json格式的数据字符串化,并进行base64编码
  1492. * @param {*} data 数据
  1493. * @returns base64编码后的数据
  1494. */
  1495. function FormatSendData(data) {
  1496. var strData = JSON.stringify(data);
  1497. if (IEVersion() < 10)
  1498. eval("strData = '" + JSON.stringify(strData) + "';");
  1499. if (serverVersion >= "1.0.2" && serverId != undefined) {
  1500. var base64Data = encode(strData);
  1501. return JSON.stringify({
  1502. serverId: serverId,
  1503. data: base64Data
  1504. })
  1505. }
  1506. else {
  1507. return encode(strData);
  1508. }
  1509. }
  1510. //管理 WPS 加载项
  1511. var WpsAddonMgr = {
  1512. getAllConfig: WpsAddonGetAllConfig,
  1513. verifyStatus: WpsAddonVerifyStatus,
  1514. enable: WpsAddonEnable,
  1515. disable: WpsAddonDisable,
  1516. disableall: WpsAddonDisableAll
  1517. }
  1518. if (typeof noGlobal === "undefined") {
  1519. window.WpsAddonMgr = WpsAddonMgr;
  1520. }
  1521. return { WpsInvoke: WpsInvoke, WpsClient: WpsClient, WebNotifyUseTimeout: WebNotifyUseTimeout, EnableMultiUser: EnableMultiUser, WpsAddonMgr: WpsAddonMgr, version: "1.0.32" };
  1522. });