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.

wps_sdk.js 33KB

5 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  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. function getHttpObj() {
  12. var httpobj = null;
  13. if (IEVersion() < 10) {
  14. try {
  15. httpobj = new XDomainRequest();
  16. } catch (e1) {
  17. httpobj = new createXHR();
  18. }
  19. } else {
  20. httpobj = new createXHR();
  21. }
  22. return httpobj;
  23. }
  24. //兼容IE低版本的创建xmlhttprequest对象的方法
  25. function createXHR() {
  26. if (typeof XMLHttpRequest != 'undefined') { //兼容高版本浏览器
  27. return new XMLHttpRequest();
  28. } else if (typeof ActiveXObject != 'undefined') { //IE6 采用 ActiveXObject, 兼容IE6
  29. var versions = [ //由于MSXML库有3个版本,因此都要考虑
  30. 'MSXML2.XMLHttp.6.0',
  31. 'MSXML2.XMLHttp.3.0',
  32. 'MSXML2.XMLHttp'
  33. ];
  34. for (var i = 0; i < versions.length; i++) {
  35. try {
  36. return new ActiveXObject(versions[i]);
  37. } catch (e) {
  38. //跳过
  39. }
  40. }
  41. } else {
  42. throw new Error('您的浏览器不支持XHR对象');
  43. }
  44. }
  45. function startWps(options) {
  46. if (!bFinished && !options.concurrent) {
  47. if (options.callback)
  48. options.callback({
  49. status: 1,
  50. message: "上一次请求没有完成"
  51. });
  52. return;
  53. }
  54. bFinished = false;
  55. function startWpsInnder(tryCount) {
  56. if (tryCount <= 0) {
  57. if (bFinished)
  58. return;
  59. bFinished = true;
  60. if (options.callback)
  61. options.callback({
  62. status: 2,
  63. message: "请允许浏览器打开WPS Office"
  64. });
  65. return;
  66. }
  67. var xmlReq = getHttpObj();
  68. //WPS客户端提供的接收参数的本地服务,HTTP服务端口为58890,HTTPS服务端口为58891
  69. //这俩配置,取一即可,不可同时启用
  70. xmlReq.open('POST', options.url);
  71. xmlReq.onload = function (res) {
  72. bFinished = true;
  73. if (options.callback) {
  74. options.callback({
  75. status: 0,
  76. response: IEVersion() < 10 ? xmlReq.responseText : res.target.response
  77. });
  78. }
  79. }
  80. xmlReq.ontimeout = xmlReq.onerror = function (res) {
  81. xmlReq.bTimeout = true;
  82. if (tryCount == options.tryCount && options.bPop) { //打开wps并传参
  83. window.location.href = "ksoWPSCloudSvr://start=RelayHttpServer" //是否启动wps弹框
  84. }
  85. setTimeout(function () {
  86. startWpsInnder(tryCount - 1)
  87. }, 1000);
  88. }
  89. if (IEVersion() < 10) {
  90. xmlReq.onreadystatechange = function () {
  91. if (xmlReq.readyState != 4)
  92. return;
  93. if (xmlReq.bTimeout) {
  94. return;
  95. }
  96. if (xmlReq.status === 200)
  97. xmlReq.onload();
  98. else
  99. xmlReq.onerror();
  100. }
  101. }
  102. xmlReq.timeout = options.timeout;
  103. xmlReq.send(options.sendData)
  104. }
  105. startWpsInnder(options.tryCount);
  106. return;
  107. }
  108. var fromCharCode = String.fromCharCode;
  109. // encoder stuff
  110. var cb_utob = function (c) {
  111. if (c.length < 2) {
  112. var cc = c.charCodeAt(0);
  113. return cc < 0x80 ? c :
  114. cc < 0x800 ? (fromCharCode(0xc0 | (cc >>> 6)) +
  115. fromCharCode(0x80 | (cc & 0x3f))) :
  116. (fromCharCode(0xe0 | ((cc >>> 12) & 0x0f)) +
  117. fromCharCode(0x80 | ((cc >>> 6) & 0x3f)) +
  118. fromCharCode(0x80 | (cc & 0x3f)));
  119. } else {
  120. var cc = 0x10000 +
  121. (c.charCodeAt(0) - 0xD800) * 0x400 +
  122. (c.charCodeAt(1) - 0xDC00);
  123. return (fromCharCode(0xf0 | ((cc >>> 18) & 0x07)) +
  124. fromCharCode(0x80 | ((cc >>> 12) & 0x3f)) +
  125. fromCharCode(0x80 | ((cc >>> 6) & 0x3f)) +
  126. fromCharCode(0x80 | (cc & 0x3f)));
  127. }
  128. };
  129. var re_utob = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;
  130. var utob = function (u) {
  131. return u.replace(re_utob, cb_utob);
  132. };
  133. var _encode = function (u) {
  134. var isUint8Array = Object.prototype.toString.call(u) === '[object Uint8Array]';
  135. if (isUint8Array)
  136. return u.toString('base64')
  137. else
  138. return btoa(utob(String(u)));
  139. }
  140. if (typeof window.btoa !== 'function') window.btoa = func_btoa;
  141. function func_btoa(input) {
  142. var str = String(input);
  143. var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  144. for (
  145. // initialize result and counter
  146. var block, charCode, idx = 0, map = chars, output = '';
  147. // if the next str index does not exist:
  148. // change the mapping table to "="
  149. // check if d has no fractional digits
  150. str.charAt(idx | 0) || (map = '=', idx % 1);
  151. // "8 - idx % 1 * 8" generates the sequence 2, 4, 6, 8
  152. output += map.charAt(63 & block >> 8 - idx % 1 * 8)
  153. ) {
  154. charCode = str.charCodeAt(idx += 3 / 4);
  155. if (charCode > 0xFF) {
  156. throw new InvalidCharacterError("'btoa' failed: The string to be encoded contains characters outside of the Latin1 range.");
  157. }
  158. block = block << 8 | charCode;
  159. }
  160. return output;
  161. }
  162. var encode = function (u, urisafe) {
  163. return !urisafe ?
  164. _encode(u) :
  165. _encode(String(u)).replace(/[+\/]/g, function (m0) {
  166. return m0 == '+' ? '-' : '_';
  167. }).replace(/=/g, '');
  168. };
  169. function IEVersion() {
  170. var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
  171. var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器
  172. var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器
  173. var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;
  174. if (isIE) {
  175. var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
  176. reIE.test(userAgent);
  177. var fIEVersion = parseFloat(RegExp["$1"]);
  178. if (fIEVersion == 7) {
  179. return 7;
  180. } else if (fIEVersion == 8) {
  181. return 8;
  182. } else if (fIEVersion == 9) {
  183. return 9;
  184. } else if (fIEVersion == 10) {
  185. return 10;
  186. } else {
  187. return 6; //IE版本<=7
  188. }
  189. } else if (isEdge) {
  190. return 20; //edge
  191. } else if (isIE11) {
  192. return 11; //IE11
  193. } else {
  194. return 30; //不是ie浏览器
  195. }
  196. }
  197. function WpsStart(options) {
  198. var startInfo = {
  199. "name": options.name,
  200. "function": options.func,
  201. "info": options.param.param,
  202. "jsPluginsXml": options.param.jsPluginsXml
  203. };
  204. var strData = JSON.stringify(startInfo);
  205. if (IEVersion() < 10) {
  206. try {
  207. eval("strData = '" + JSON.stringify(startInfo) + "';");
  208. } catch (err) {
  209. }
  210. }
  211. var baseData = encode(strData);
  212. var url = options.urlBase + "/" + options.clientType + "/runParams";
  213. var data = "ksowebstartup" + options.clientType + "://" + baseData;
  214. startWps({
  215. url: url,
  216. sendData: data,
  217. callback: options.callback,
  218. tryCount: options.tryCount,
  219. bPop: options.bPop,
  220. timeout: 5000,
  221. concurrent: false,
  222. client: options.wpsclient
  223. });
  224. }
  225. function WpsStartWrap(options) {
  226. WpsStart({
  227. clientType: options.clientType,
  228. name: options.name,
  229. func: options.func,
  230. param: options.param,
  231. urlBase: options.urlBase,
  232. callback: options.callback,
  233. tryCount: 4,
  234. bPop: true,
  235. wpsclient: options.wpsclient,
  236. })
  237. }
  238. /**
  239. * 支持浏览器触发,WPS有返回值的启动
  240. *
  241. * @param {*} clientType 组件类型
  242. * @param {*} name WPS加载项名称
  243. * @param {*} func WPS加载项入口方法
  244. * @param {*} param 参数:包括WPS加载项内部定义的方法,参数等
  245. * @param {*} callback 回调函数
  246. * @param {*} tryCount 重试次数
  247. * @param {*} bPop 是否弹出浏览器提示对话框
  248. */
  249. var exId = 0;
  250. function WpsStartWrapExInner(options) {
  251. var infocontent = options.param.param;
  252. if (!options.wpsclient) {
  253. infocontent = JSON.stringify(options.param.param);
  254. var rspUrl = options.urlBase + "/transferEcho/runParams";
  255. var time = new Date();
  256. var cmdId = "js" + time.getTime() + "_" + exId;
  257. var funcEx = "var res = " + options.func;
  258. var cbCode = "var xhr = new XMLHttpRequest();xhr.open('POST', '" + rspUrl + "');xhr.send(JSON.stringify({id: '" + cmdId + "', response: res}));" //res 为func执行返回值
  259. var infoEx = infocontent + ");" + cbCode + "void(0";
  260. options.func = funcEx;
  261. infocontent = infoEx;
  262. }
  263. var startInfo = {
  264. "name": options.name,
  265. "function": options.func,
  266. "info": infocontent,
  267. "showToFront": options.param.showToFront,
  268. "jsPluginsXml": options.param.jsPluginsXml,
  269. };
  270. var strData = JSON.stringify(startInfo);
  271. if (IEVersion() < 10) {
  272. try {
  273. eval("strData = '" + JSON.stringify(startInfo) + "';");
  274. } catch (err) {
  275. }
  276. }
  277. var baseData = encode(strData);
  278. var wrapper;
  279. if (!options.wpsclient) {
  280. var url = options.urlBase + "/transfer/runParams";
  281. var data = "ksowebstartup" + options.clientType + "://" + baseData;
  282. wrapper = {
  283. id: cmdId,
  284. app: options.clientType,
  285. data: data
  286. };
  287. }
  288. else {
  289. var url = options.urlBase + "/transferEx/runParams";
  290. wrapper = {
  291. id: options.wpsclient.clientId,
  292. app: options.clientType,
  293. data: baseData,
  294. mode: options.wpsclient.silentMode ? "true" : "false"
  295. };
  296. }
  297. wrapper = JSON.stringify(wrapper);
  298. startWps({
  299. url: url,
  300. sendData: wrapper,
  301. callback: options.callback,
  302. tryCount: options.tryCount,
  303. bPop: options.bPop,
  304. timeout: 0,
  305. concurrent: options.concurrent,
  306. client: options.wpsclient
  307. });
  308. }
  309. var serverVersion = "wait"
  310. var cloudSvrStart = true;
  311. function WpsStartWrapVersionInner(options) {
  312. if (serverVersion == "wait") {
  313. if (cloudSvrStart == false) {
  314. window.location.href = "ksoWPSCloudSvr://start=RelayHttpServer" //是否启动wps弹框
  315. }
  316. startWps({
  317. url: options.urlBase + '/version',
  318. data: "",
  319. callback: function (res) {
  320. if (res.status !== 0) {
  321. options.callback(res)
  322. return;
  323. }
  324. serverVersion = res.response;
  325. cloudSvrStart = true;
  326. options.tryCount = 1
  327. options.bPop = false
  328. if (serverVersion === "") {
  329. WpsStart(options)
  330. } else if (serverVersion < "1.0.1" && options.wpsclient) {
  331. if (options.callback) {
  332. options.callback({
  333. status: 4,
  334. message: "当前客户端不支持,请升级客户端"
  335. })
  336. }
  337. } else {
  338. WpsStartWrapExInner(options);
  339. }
  340. },
  341. tryCount: 4,
  342. bPop: true,
  343. timeout: 5000,
  344. concurrent: options.concurrent
  345. });
  346. } else {
  347. if (serverVersion === "") {
  348. WpsStartWrap(options)
  349. } else if (serverVersion < "1.0.1" && options.wpsclient) {
  350. if (options.callback) {
  351. options.callback({
  352. status: 4,
  353. message: "当前客户端不支持,请升级客户端"
  354. })
  355. }
  356. } else {
  357. WpsStartWrapExInner(options);
  358. }
  359. }
  360. }
  361. var RegWebNotifyMap = { wps: {}, wpp: {}, et: {} }
  362. var bWebNotifyUseTimeout = true
  363. function WebNotifyUseTimeout(value) {
  364. bWebNotifyUseTimeout = value ? true : false
  365. }
  366. /**
  367. * 注册一个前端页面接收WPS传来消息的方法
  368. * @param {*} clientType wps | et | wpp
  369. * @param {*} name WPS加载项的名称
  370. * @param {*} callback 回调函数
  371. */
  372. function RegWebNotify(clientType, name, callback, wpsclient) {
  373. if (clientType != "wps" && clientType != "wpp" && clientType != "et")
  374. return;
  375. var paramStr = {}
  376. if (wpsclient) {
  377. if (wpsclient.notifyRegsitered == true) {
  378. return
  379. }
  380. wpsclient.notifyRegsitered = true;
  381. paramStr = {
  382. clientId: wpsclient.clientId,
  383. name: name,
  384. type: clientType
  385. }
  386. }
  387. else {
  388. if (typeof callback != 'function')
  389. return
  390. if (RegWebNotifyMap[clientType][name]) {
  391. RegWebNotifyMap[clientType][name] = callback;
  392. return
  393. }
  394. var RegWebNotifyID = new Date().valueOf() + ''
  395. paramStr = {
  396. id: RegWebNotifyID,
  397. name: name,
  398. type: clientType
  399. }
  400. RegWebNotifyMap[clientType][name] = callback
  401. }
  402. var askItem = function () {
  403. var xhr = getHttpObj()
  404. xhr.onload = function (e) {
  405. if (xhr.responseText == "WPSInnerMessage_quit") {
  406. return;
  407. }
  408. if (wpsclient) {
  409. wpsclient.OnRegWebNotify(xhr.responseText)
  410. } else {
  411. var func = RegWebNotifyMap[clientType][name]
  412. func(xhr.responseText)
  413. }
  414. window.setTimeout(askItem, 300)
  415. }
  416. xhr.onerror = function (e) {
  417. if (bWebNotifyUseTimeout)
  418. window.setTimeout(askItem, 1000)
  419. else
  420. window.setTimeout(askItem, 10000)
  421. }
  422. xhr.ontimeout = function (e) {
  423. if (bWebNotifyUseTimeout)
  424. window.setTimeout(askItem, 300)
  425. else
  426. window.setTimeout(askItem, 10000)
  427. }
  428. if (IEVersion() < 10) {
  429. xhr.onreadystatechange = function () {
  430. if (xhr.readyState != 4)
  431. return;
  432. if (xhr.bTimeout) {
  433. return;
  434. }
  435. if (xhr.status === 200)
  436. xhr.onload();
  437. else
  438. xhr.onerror();
  439. }
  440. }
  441. xhr.open('POST', GetUrlBase() + '/askwebnotify', true)
  442. if (bWebNotifyUseTimeout)
  443. xhr.timeout = 2000;
  444. xhr.send(JSON.stringify(paramStr))
  445. }
  446. window.setTimeout(askItem, 2000)
  447. }
  448. function GetUrlBase() {
  449. if (location.protocol == "http:")
  450. return "http://127.0.0.1:58890"
  451. return "https://127.0.0.1:58891"
  452. }
  453. function WpsStartWrapVersion(clientType, name, func, param, callback, showToFront, jsPluginsXml) {
  454. var paramEx = {
  455. jsPluginsXml: jsPluginsXml ? jsPluginsXml : "",
  456. showToFront: typeof (showToFront) == 'boolean' ? showToFront : true,
  457. param: (typeof (param) == 'object' ? param : JSON.parse(param))
  458. }
  459. var options = {
  460. clientType: clientType,
  461. name: name,
  462. func: func,
  463. param: paramEx,
  464. urlBase: GetUrlBase(),
  465. callback: callback,
  466. wpsclient: undefined,
  467. concurrent: true
  468. }
  469. WpsStartWrapVersionInner(options);
  470. }
  471. //从外部浏览器远程调用 WPS 加载项中的方法
  472. var WpsInvoke = {
  473. InvokeAsHttp: WpsStartWrapVersion,
  474. InvokeAsHttps: WpsStartWrapVersion,
  475. RegWebNotify: RegWebNotify,
  476. ClientType: {
  477. wps: "wps",
  478. et: "et",
  479. wpp: "wpp"
  480. },
  481. CreateXHR: getHttpObj,
  482. IsClientRunning: IsClientRunning
  483. }
  484. window.wpsclients = [];
  485. /**
  486. * @constructor WpsClient wps客户端
  487. * @param {string} clientType 必传参数,加载项类型,有效值为"wps","wpp","et";分别表示文字,演示,电子表格
  488. */
  489. function WpsClient(clientType) {
  490. /**
  491. * 设置RegWebNotify的回调函数,加载项给业务端发消息通过该函数
  492. * @memberof WpsClient
  493. * @member onMessage
  494. */
  495. this.onMessage;
  496. /**
  497. * 设置加载项路径
  498. * @memberof WpsClient
  499. * @member jsPluginsXml
  500. */
  501. this.jsPluginsXml;
  502. /**
  503. * 内部成员,外部无需调用
  504. */
  505. this.notifyRegsitered = false;
  506. this.clientId = "";
  507. this.concurrent = false;
  508. this.clientType = clientType;
  509. this.firstRequest = true;
  510. /**
  511. * 内部函数,外部无需调用
  512. * @param {*} options
  513. */
  514. this.initWpsClient = function (options) {
  515. options.clientType = this.clientType
  516. options.wpsclient = this
  517. options.concurrent = this.firstRequest ? true : this.concurrent
  518. this.firstRequest = false;
  519. WpsStartWrapVersionInner(options)
  520. }
  521. /**
  522. * 以http启动
  523. * @param {string} name 加载项名称
  524. * @param {string} func 要调用的加载项中的函数行
  525. * @param {string} param 在加载项中执行函数func要传递的数据
  526. * @param {function({int, string})} callback 回调函数,status = 0 表示成功,失败请查看message信息
  527. * @param {bool} showToFront 设置wps是否显示到前面来
  528. * @return {string} "Failed to send message to WPS." 发送消息失败,客户端已关闭;
  529. * "WPS Addon is not response." 加载项阻塞,函数执行失败
  530. */
  531. this.InvokeAsHttp = function (name, func, param, callback, showToFront) {
  532. function clientCallback(res) {
  533. //this不是WpsClient
  534. if (res.status !== 0 || serverVersion < "1.0.1") {
  535. if (callback) callback(res);
  536. return;
  537. }
  538. var resObject = JSON.parse(res.response);
  539. if (this.client.clientId == "") {
  540. this.client.clientId = resObject.clientId;
  541. }
  542. this.client.concurrent = true;
  543. if (typeof resObject.data == "object")
  544. res.response = JSON.stringify(resObject.data);
  545. else
  546. res.response = resObject.data;
  547. if (IEVersion() < 10)
  548. eval(" res.response = '" + res.response + "';");
  549. if (callback)
  550. callback(res);
  551. this.client.RegWebNotify(name);
  552. }
  553. var paramEx = {
  554. jsPluginsXml: this.jsPluginsXml ? this.jsPluginsXml : "",
  555. showToFront: typeof (showToFront) == 'boolean' ? showToFront : true,
  556. param: (typeof (param) == 'object' ? param : JSON.parse(param))
  557. }
  558. this.initWpsClient({
  559. name: name,
  560. func: func,
  561. param: paramEx,
  562. urlBase: GetUrlBase(),
  563. callback: clientCallback
  564. })
  565. }
  566. /**
  567. * 以https启动
  568. * @param {string} name 加载项名称
  569. * @param {string} func 要调用的加载项中的函数行
  570. * @param {string} param 在加载项中执行函数func要传递的数据
  571. * @param {function({int, string})} callback 回调函数,status = 0 表示成功,失败请查看message信息
  572. * @param {bool} showToFront 设置wps是否显示到前面来
  573. */
  574. this.InvokeAsHttps = function (name, func, param, callback, showToFront) {
  575. var paramEx = {
  576. jsPluginsXml: this.jsPluginsXml ? this.jsPluginsXml : "",
  577. showToFront: typeof (showToFront) == 'boolean' ? showToFront : true,
  578. param: (typeof (param) == 'object' ? param : JSON.parse(param))
  579. }
  580. this.initWpsClient({
  581. name: name,
  582. func: func,
  583. param: paramEx,
  584. urlBase: GetUrlBase(),
  585. callback: callback
  586. })
  587. }
  588. /**
  589. * 内部函数,外部无需调用
  590. * @param {*} name
  591. */
  592. this.RegWebNotify = function (name) {
  593. RegWebNotify(this.clientType, name, null, this);
  594. }
  595. this.OnRegWebNotify = function (message) {
  596. if (this.onMessage)
  597. this.onMessage(message)
  598. }
  599. /**
  600. * 以静默模式启动客户端
  601. * @param {string} name 必传参数,加载项名称
  602. * @param {function({int, string})} [callback] 回调函数,status = 0 表示成功,失败请查看message信息
  603. */
  604. this.StartWpsInSilentMode = function (name, callback) {
  605. function initCallback(res) {
  606. //this不是WpsClient
  607. if (res.status !== 0 || serverVersion < "1.0.1") {
  608. if (callback) callback(res);
  609. return;
  610. }
  611. if (this.client.clientId == "") {
  612. this.client.clientId = JSON.parse(res.response).clientId;
  613. window.wpsclients[window.wpsclients.length] = { name: name, client: this.client };
  614. }
  615. res.response = JSON.stringify(JSON.parse(res.response).data);
  616. this.client.concurrent = true;
  617. if (callback) {
  618. callback(res);
  619. }
  620. this.client.RegWebNotify(name);
  621. }
  622. var paramEx = {
  623. jsPluginsXml: this.jsPluginsXml,
  624. showToFront: false,
  625. param: { status: "InitInSilentMode" }
  626. }
  627. this.silentMode = true;
  628. this.initWpsClient({
  629. name: name,
  630. func: "",
  631. param: paramEx,
  632. urlBase: GetUrlBase(),
  633. callback: initCallback
  634. })
  635. }
  636. /**
  637. * 显示客户端到最前面
  638. * @param {string} name 必传参数,加载项名称
  639. * @param {function({int, string})} [callback] 回调函数
  640. */
  641. this.ShowToFront = function (name, callback) {
  642. if (serverVersion < "1.0.1") {
  643. if (callback) {
  644. callback({
  645. status: 4,
  646. message: "当前客户端不支持,请升级客户端"
  647. });
  648. return;
  649. }
  650. return;
  651. }
  652. if (this.clientId == "") {
  653. if (callback) callback({
  654. status: 3,
  655. message: "没有静默启动客户端"
  656. });
  657. return;
  658. }
  659. var paramEx = {
  660. jsPluginsXml: "",
  661. showToFront: true,
  662. param: { status: "ShowToFront" }
  663. }
  664. this.initWpsClient({
  665. name: name,
  666. func: "",
  667. param: paramEx,
  668. urlBase: GetUrlBase(),
  669. callback: callback
  670. })
  671. }
  672. /**
  673. * 关闭未显示出来的静默启动客户端
  674. * @param {string} name 必传参数,加载项名称
  675. * @param {function({int, string})} [callback] 回调函数
  676. */
  677. this.CloseSilentClient = function (name, callback) {
  678. if (serverVersion < "1.0.1") {
  679. if (callback) {
  680. callback({
  681. status: 4,
  682. message: "当前客户端不支持,请升级客户端"
  683. });
  684. return;
  685. }
  686. return;
  687. }
  688. if (this.clientId == "") {
  689. if (callback) callback({
  690. status: 3,
  691. message: "没有静默启动客户端"
  692. });
  693. return;
  694. }
  695. var paramEx = {
  696. jsPluginsXml: "",
  697. showToFront: false,
  698. param: undefined
  699. }
  700. var func;
  701. if (this.clientType == "wps")
  702. func = "wps.WpsApplication().Quit"
  703. else if (this.clientType == "et")
  704. func = "wps.EtApplication().Quit"
  705. else if (this.clientType == "wpp")
  706. func = "wps.WppApplication().Quit"
  707. function closeSilentClient(res) {
  708. if (res.status == 0)
  709. this.client.clientId = ""
  710. if (callback) callback(res);
  711. return;
  712. }
  713. this.initWpsClient({
  714. name: name,
  715. func: func,
  716. param: paramEx,
  717. urlBase: GetUrlBase(),
  718. callback: closeSilentClient
  719. })
  720. }
  721. /**
  722. * 当前客户端是否在运行,使用WpsClient.IsClientRunning()进行调用
  723. * @param {function({int, string})} [callback] 回调函数,"Client is running." 客户端正在运行
  724. * "Client is not running." 客户端没有运行
  725. */
  726. this.IsClientRunning = function (callback) {
  727. if (serverVersion < "1.0.1") {
  728. if (callback) {
  729. callback({
  730. status: 4,
  731. message: "当前客户端不支持,请升级客户端"
  732. });
  733. return;
  734. }
  735. return;
  736. }
  737. IsClientRunning(this.clientType, callback, this)
  738. }
  739. }
  740. function InitSdk() {
  741. var url = GetUrlBase() + "/version";
  742. startWps({
  743. url: url,
  744. data: "",
  745. callback: function (res) {
  746. if (res.status !== 0) {
  747. cloudSvrStart = false;
  748. return;
  749. }
  750. if (serverVersion == "wait") {
  751. serverVersion = res.response;
  752. cloudSvrStart = true;
  753. }
  754. },
  755. tryCount: 1,
  756. bPop: false,
  757. timeout: 5000
  758. });
  759. }
  760. if (typeof noGlobal === "undefined") {
  761. window.WpsInvoke = WpsInvoke;
  762. window.WpsClient = WpsClient;
  763. window.WebNotifyUseTimeout = WebNotifyUseTimeout;
  764. InitSdk();
  765. }
  766. /**
  767. * 当前客户端是否在运行,使用WpsInvoke.IsClientRunning()进行调用
  768. * @param {string} clientType 加载项类型
  769. * @param {function} [callback] 回调函数,"Client is running." 客户端正在运行
  770. * "Client is not running." 客户端没有运行
  771. */
  772. function IsClientRunning(clientType, callback, wpsclient) {
  773. var url = GetUrlBase() + "/isRunning";
  774. var wrapper = {
  775. id: wpsclient == undefined ? undefined : wpsclient.clientId,
  776. app: clientType
  777. }
  778. wrapper = JSON.stringify(wrapper);
  779. startWps({
  780. url: url,
  781. sendData: wrapper,
  782. callback: callback,
  783. tryCount: 1,
  784. bPop: false,
  785. timeout: 2000,
  786. concurrent: true,
  787. client: wpsclient
  788. });
  789. }
  790. function WpsAddonGetAllConfig(callBack) {
  791. var baseData;
  792. startWps({
  793. url: GetUrlBase() + "/publishlist",
  794. type: "GET",
  795. sendData: baseData,
  796. callback: callBack,
  797. tryCount: 3,
  798. bPop: true,
  799. timeout: 5000,
  800. concurrent: true
  801. });
  802. }
  803. function WpsAddonVerifyStatus(element, callBack) {
  804. var xmlReq = getHttpObj();
  805. var offline = element.online === "false";
  806. var url = offline ? element.url : element.url + "ribbon.xml";
  807. xmlReq.open("POST", GetUrlBase() + "/redirect/runParams");
  808. xmlReq.onload = function (res) {
  809. if (offline && !res.target.response.startsWith("7z")) {
  810. callBack({ status: 1, msg: "不是有效的7z格式" + url });
  811. } else if (!offline && !res.target.response.startsWith("<customUI")) {
  812. callBack({ status: 1, msg: "不是有效的ribbon.xml, " + url })
  813. } else {
  814. callBack({ status: 0, msg: "OK" })
  815. }
  816. }
  817. xmlReq.onerror = function (res) {
  818. xmlReq.bTimeout = true;
  819. callBack({ status: 2, msg: "网页路径不可访问,如果是跨域问题,不影响使用" + url })
  820. }
  821. xmlReq.ontimeout = function (res) {
  822. xmlReq.bTimeout = true;
  823. callBack({ status: 3, msg: "访问超时" + url })
  824. }
  825. if (IEVersion() < 10) {
  826. xmlReq.onreadystatechange = function () {
  827. if (xmlReq.readyState != 4)
  828. return;
  829. if (xmlReq.bTimeout) {
  830. return;
  831. }
  832. if (xmlReq.status === 200)
  833. xmlReq.onload();
  834. else
  835. xmlReq.onerror();
  836. }
  837. }
  838. xmlReq.timeout = 5000;
  839. var data = {
  840. method: "get",
  841. url: url,
  842. data: ""
  843. }
  844. var sendData = FormatSendData(data)
  845. xmlReq.send(sendData);
  846. }
  847. function WpsAddonHandleEx(element, cmd, callBack) {
  848. var data = FormatData(element, cmd);
  849. startWps({
  850. url: GetUrlBase() + "/deployaddons/runParams",
  851. type: "POST",
  852. sendData: data,
  853. callback: callBack,
  854. tryCount: 3,
  855. bPop: true,
  856. timeout: 5000,
  857. concurrent: true
  858. });
  859. }
  860. function WpsAddonEnable(element, callBack) {
  861. WpsAddonHandleEx(element, "enable", callBack)
  862. }
  863. function WpsAddonDisable(element, callBack) {
  864. WpsAddonHandleEx(element, "disable", callBack)
  865. }
  866. function FormatData(element, cmd) {
  867. var data = {
  868. "cmd": cmd, //"enable", 启用, "disable", 禁用, "disableall", 禁用所有
  869. "name": element.name,
  870. "url": element.url,
  871. "addonType": element.addonType,
  872. "online": element.online,
  873. "version": element.version
  874. }
  875. return FormatSendData(data);
  876. }
  877. function FormatSendData(data) {
  878. var strData = JSON.stringify(data);
  879. if (IEVersion() < 10)
  880. eval("strData = '" + JSON.stringify(strData) + "';");
  881. return encode(strData);
  882. }
  883. window.onbeforeunload = function () {
  884. for (var i = 0; i < window.wpsclients.length; ++i) {
  885. var item = window.wpsclients[i]
  886. item.client.CloseSilentClient(item.name)
  887. }
  888. }
  889. //管理 WPS 加载项
  890. var WpsAddonMgr = {
  891. getAllConfig: WpsAddonGetAllConfig,
  892. verifyStatus: WpsAddonVerifyStatus,
  893. enable: WpsAddonEnable,
  894. disable: WpsAddonDisable,
  895. }
  896. if (typeof noGlobal === "undefined") {
  897. window.WpsAddonMgr = WpsAddonMgr;
  898. }
  899. return { WpsInvoke: WpsInvoke, WpsAddonMgr: WpsAddonMgr, version: "1.0.12" };
  900. });