Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

935 Zeilen
28KB

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