|
| 1 | +import SuperMap from '../SuperMap'; |
| 2 | +import '../security/SecurityManager'; |
| 3 | + |
| 4 | +/** |
| 5 | + * @class SuperMap.CommonServiceBase common服务基类 |
| 6 | + * @constructs SuperMap.CommonServiceBase |
| 7 | + * @param url - {String} 与客户端交互的服务地址。 |
| 8 | + * @param options - {Object} 参数。 |
| 9 | + */ |
| 10 | +export default class CommonServiceBase { |
| 11 | + |
| 12 | + /* |
| 13 | + * Constant: EVENT_TYPES |
| 14 | + * {Array(String)} |
| 15 | + * 此类支持的事件类型 |
| 16 | + * - *processCompleted* 服务端返回信息成功触发该事件 。 |
| 17 | + * - *processFailed* 服务端返回信息失败触发该事件 。 |
| 18 | + */ |
| 19 | + EVENT_TYPES = ["processCompleted", "processFailed"]; |
| 20 | + |
| 21 | + /** |
| 22 | + * @member events -{SuperMap.Events} |
| 23 | + * @description APIProperty: 处理所有事件的对象,支持 processCompleted 、processFailed 两种事件 |
| 24 | + * 服务端成功返回地图信息结果时触发 processCompleted 事件,服务端返回信息结果时触发 processFailed 事件。 |
| 25 | + */ |
| 26 | + events = null; |
| 27 | + |
| 28 | + /** |
| 29 | + * @member eventListeners -{Object} |
| 30 | + * @description APIProperty: 听器对象,在构造函数中设置此参数(可选),对 MapService 支持的两个事件 processCompleted 、processFailed 进行监听, |
| 31 | + * 相当于调用 SuperMap.Events.on(eventListeners)。 |
| 32 | + */ |
| 33 | + eventListeners = null; |
| 34 | + |
| 35 | + /** |
| 36 | + * @member url -{String|Array} |
| 37 | + * @description APIProperty: 服务访问地址或者服务访问地址数组。 |
| 38 | + * |
| 39 | + * @example |
| 40 | + * var url1 = "http://localhost:8090/iserver/services/map-world/rest/maps/World"; |
| 41 | + * var url2 = ["http://192.168.17.168:8090/iserver/services/map-world/rest/maps/World", |
| 42 | + * "http://192.168.17.169:8091/iserver/services/map-world/rest/maps/World"]; |
| 43 | + */ |
| 44 | + url = null; |
| 45 | + |
| 46 | + /* |
| 47 | + * Property: urls |
| 48 | + * {Array} 服务访问地址数组。 |
| 49 | + */ |
| 50 | + urls = null; |
| 51 | + |
| 52 | + /* |
| 53 | + * Property: serverType |
| 54 | + * {SuperMap.ServerType} 服务器类型,iServer|iPortal|Online |
| 55 | + */ |
| 56 | + serverType = null; |
| 57 | + |
| 58 | + /* |
| 59 | + * Property: index |
| 60 | + * {Int} 服务访问地址在数组中的位置。 |
| 61 | + */ |
| 62 | + index = null; |
| 63 | + |
| 64 | + /* |
| 65 | + * Property: length |
| 66 | + * {String} 服务访问地址数组长度。 |
| 67 | + */ |
| 68 | + length = null; |
| 69 | + |
| 70 | + /* |
| 71 | + * Property: options |
| 72 | + * {Object} 请求参数。 |
| 73 | + */ |
| 74 | + options = null; |
| 75 | + |
| 76 | + /* |
| 77 | + * Property: totalTimes |
| 78 | + * {Int} 实际请求失败次数。 |
| 79 | + */ |
| 80 | + totalTimes = null; |
| 81 | + |
| 82 | + /* |
| 83 | + * Property: POLLING_TIMES |
| 84 | + * {Int} 默认请求失败次数。 |
| 85 | + */ |
| 86 | + POLLING_TIMES = 3; |
| 87 | + |
| 88 | + /* |
| 89 | + * Property: _processSuccess |
| 90 | + * {Function} 请求参数中成功回调函数。 |
| 91 | + */ |
| 92 | + _processSuccess = null; |
| 93 | + |
| 94 | + /* |
| 95 | + * Property: _processFailed |
| 96 | + * {Function} 请求参数中失败回调函数。 |
| 97 | + */ |
| 98 | + _processFailed = null; |
| 99 | + |
| 100 | + |
| 101 | + /* |
| 102 | + * Property: isInTheSameDomain |
| 103 | + * {Boolean} |
| 104 | + */ |
| 105 | + isInTheSameDomain = null; |
| 106 | + |
| 107 | + /** |
| 108 | + * @function initialize |
| 109 | + * @description ServiceBase的构造函数 |
| 110 | + * @param url - {String} 与客户端交互的服务地址。 |
| 111 | + * @param options - {Object} 参数。 |
| 112 | + */ |
| 113 | + constructor(url, options) { |
| 114 | + let me = this; |
| 115 | + |
| 116 | + if (SuperMap.Util.isArray(url)) { |
| 117 | + me.urls = url; |
| 118 | + me.length = url.length; |
| 119 | + me.totalTimes = me.length; |
| 120 | + if (me.length === 1) { |
| 121 | + me.url = url[0]; |
| 122 | + } else { |
| 123 | + me.index = parseInt(Math.random() * me.length); |
| 124 | + me.url = url[me.index]; |
| 125 | + } |
| 126 | + } else { |
| 127 | + me.totalTimes = 1; |
| 128 | + me.url = url; |
| 129 | + } |
| 130 | + |
| 131 | + if (SuperMap.Util.isArray(url) && !me.isServiceSupportPolling()) { |
| 132 | + me.url = url[0]; |
| 133 | + me.totalTimes = 1; |
| 134 | + } |
| 135 | + |
| 136 | + me.serverType = me.serverType || SuperMap.ServerType.ISERVER; |
| 137 | + |
| 138 | + options = options || {}; |
| 139 | + |
| 140 | + if (options) { |
| 141 | + SuperMap.Util.extend(this, options); |
| 142 | + } |
| 143 | + |
| 144 | + me.isInTheSameDomain = SuperMap.Util.isInTheSameDomain(me.url); |
| 145 | + |
| 146 | + me.events = new SuperMap.Events(me, null, me.EVENT_TYPES, true); |
| 147 | + if (me.eventListeners instanceof Object) { |
| 148 | + me.events.on(me.eventListeners); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * @override |
| 154 | + * @function destroy |
| 155 | + * @description APIMethod: 释放资源,将引用的资源属性置空。 |
| 156 | + */ |
| 157 | + destroy() { |
| 158 | + let me = this; |
| 159 | + if (SuperMap.Util.isArray(me.urls)) { |
| 160 | + me.urls = null; |
| 161 | + me.index = null; |
| 162 | + me.length = null; |
| 163 | + me.totalTimes = null; |
| 164 | + } |
| 165 | + me.url = null; |
| 166 | + me.options = null; |
| 167 | + me._processSuccess = null; |
| 168 | + me._processFailed = null; |
| 169 | + me.isInTheSameDomain = null; |
| 170 | + |
| 171 | + me.EVENT_TYPES = null; |
| 172 | + if (me.events) { |
| 173 | + me.events.destroy(); |
| 174 | + me.events = null; |
| 175 | + } |
| 176 | + if (me.eventListeners) { |
| 177 | + me.eventListeners = null; |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * @function request |
| 183 | + * @description APIMethod: 该方法用于向服务发送请求。 |
| 184 | + * |
| 185 | + * Parameters: |
| 186 | + * @param options - {Object} 参数。 |
| 187 | + * method - {String} 请求方式,包括GET,POST,PUT, DELETE。<br> |
| 188 | + * url - {String} 发送请求的地址。<br> |
| 189 | + * params - {Object} 作为查询字符串添加到url中的一组键值对, |
| 190 | + * 此参数只适用于GET方式发送的请求。<br> |
| 191 | + * data - {String } 发送到服务器的数据。<br> |
| 192 | + * success - {function} 请求成功后的回调函数。<br> |
| 193 | + * failure - {function} 请求失败后的回调函数。<br> |
| 194 | + * scope - {Object} 如果回调函数是对象的一个公共方法,设定该对象的范围。<br> |
| 195 | + * isInTheSameDomain - {Boolean} 请求是否在当前域中。<br> |
| 196 | + */ |
| 197 | + request(options) { |
| 198 | + let me = this; |
| 199 | + options.url = options.url || me.url; |
| 200 | + options.isInTheSameDomain = me.isInTheSameDomain; |
| 201 | + //为url添加安全认证信息片段 |
| 202 | + let credential = this.getCredential(options.url); |
| 203 | + if (credential) { |
| 204 | + //当url中含有?,并且?在url末尾的时候直接添加token *网络分析等服务请求url会出现末尾是?的情况* |
| 205 | + //当url中含有?,并且?不在url末尾的时候添加&token |
| 206 | + //当url中不含有?,在url末尾添加?token |
| 207 | + let endStr = options.url.substring(options.url.length - 1, options.url.length); |
| 208 | + if (options.url.indexOf("?") > -1 && endStr === "?") { |
| 209 | + options.url += credential.getUrlParameters(); |
| 210 | + } else if (options.url.indexOf("?") > -1 && endStr !== "?") { |
| 211 | + options.url += "&" + credential.getUrlParameters(); |
| 212 | + } else { |
| 213 | + options.url += "?" + credential.getUrlParameters(); |
| 214 | + } |
| 215 | + } |
| 216 | + me.calculatePollingTimes(); |
| 217 | + me._processSuccess = options.success; |
| 218 | + me._processFailed = options.failure; |
| 219 | + options.scope = me; |
| 220 | + options.success = me.getUrlCompleted; |
| 221 | + options.failure = me.getUrlFailed; |
| 222 | + me.options = options; |
| 223 | + SuperMap.Util.committer(me.options); |
| 224 | + } |
| 225 | + |
| 226 | + /* |
| 227 | + * 获取凭据信息 |
| 228 | + * parameter url |
| 229 | + */ |
| 230 | + getCredential(url) { |
| 231 | + let keyUrl = url, credential, value; |
| 232 | + switch (this.serverType) { |
| 233 | + case SuperMap.ServerType.ISERVER: |
| 234 | + value = SuperMap.SecurityManager.getToken(keyUrl); |
| 235 | + credential = value ? new SuperMap.Credential(value, "token") : null; |
| 236 | + break; |
| 237 | + case SuperMap.ServerType.IPORTAL: |
| 238 | + value = SuperMap.SecurityManager.getToken(keyUrl); |
| 239 | + credential = value ? new SuperMap.Credential(value, "token") : null; |
| 240 | + if (!credential) { |
| 241 | + value = SuperMap.SecurityManager.getKey(keyUrl); |
| 242 | + credential = value ? new SuperMap.Credential(value, "key") : null; |
| 243 | + } |
| 244 | + break; |
| 245 | + case SuperMap.ServerType.ONLINE: |
| 246 | + value = SuperMap.SecurityManager.getKey(keyUrl); |
| 247 | + credential = value ? new SuperMap.Credential(value, "key") : null; |
| 248 | + break; |
| 249 | + default: |
| 250 | + value = SuperMap.SecurityManager.getToken(keyUrl); |
| 251 | + credential = value ? new SuperMap.Credential(value, "token") : null; |
| 252 | + break; |
| 253 | + } |
| 254 | + return credential; |
| 255 | + } |
| 256 | + |
| 257 | + /* |
| 258 | + * Method: getUrlCompleted |
| 259 | + * 请求成功后执行此方法。 |
| 260 | + * |
| 261 | + * Parameters: |
| 262 | + * result - {Object} 服务器返回的结果对象。 |
| 263 | + */ |
| 264 | + getUrlCompleted(result) { |
| 265 | + let me = this; |
| 266 | + me._processSuccess(result); |
| 267 | + } |
| 268 | + |
| 269 | + |
| 270 | + /* |
| 271 | + * Method: getUrlFailed |
| 272 | + * 请求失败后执行此方法。 |
| 273 | + * |
| 274 | + * Parameters: |
| 275 | + * result - {Object} 服务器返回的结果对象。 |
| 276 | + */ |
| 277 | + getUrlFailed(result) { |
| 278 | + let me = this; |
| 279 | + if (me.totalTimes > 0) { |
| 280 | + me.totalTimes--; |
| 281 | + me.ajaxPolling(); |
| 282 | + } else { |
| 283 | + me._processFailed(result); |
| 284 | + } |
| 285 | + } |
| 286 | + |
| 287 | + |
| 288 | + /* |
| 289 | + * Method: ajaxPolling |
| 290 | + * 请求失败后,如果剩余请求失败次数不为0,重新获取url发送请求 |
| 291 | + */ |
| 292 | + ajaxPolling() { |
| 293 | + let me = this, |
| 294 | + url = me.options.url, |
| 295 | + re = /^http:\/\/([a-z]{9}|(\d+\.){3}\d+):\d{0,4}/; |
| 296 | + me.index = parseInt(Math.random() * me.length); |
| 297 | + me.url = me.urls[me.index]; |
| 298 | + url = url.replace(re, re.exec(me.url)[0]); |
| 299 | + let isInTheSameDomain = SuperMap.Util.isInTheSameDomain(url); |
| 300 | + if (isInTheSameDomain) { |
| 301 | + if (url.indexOf(".jsonp") > 0) { |
| 302 | + url = url.replace(/.jsonp/, ".json"); |
| 303 | + } |
| 304 | + } else { |
| 305 | + if (!(url.indexOf(".jsonp") > 0)) { |
| 306 | + url = url.replace(/.json/, ".jsonp"); |
| 307 | + } |
| 308 | + } |
| 309 | + me.options.url = url; |
| 310 | + me.options.isInTheSameDomain = isInTheSameDomain; |
| 311 | + SuperMap.Util.committer(me.options); |
| 312 | + } |
| 313 | + |
| 314 | + |
| 315 | + /* |
| 316 | + * Method: calculatePollingTimes |
| 317 | + * 计算剩余请求失败执行次数。 |
| 318 | + */ |
| 319 | + calculatePollingTimes() { |
| 320 | + let me = this; |
| 321 | + if (me.times) { |
| 322 | + if (me.totalTimes > me.POLLING_TIMES) { |
| 323 | + if (me.times > me.POLLING_TIMES) { |
| 324 | + me.totalTimes = me.POLLING_TIMES; |
| 325 | + } else { |
| 326 | + me.totalTimes = me.times; |
| 327 | + } |
| 328 | + } else { |
| 329 | + if (me.times < me.totalTimes) { |
| 330 | + me.totalTimes = me.times; |
| 331 | + } |
| 332 | + } |
| 333 | + |
| 334 | + } else { |
| 335 | + if (me.totalTimes > me.POLLING_TIMES) { |
| 336 | + me.totalTimes = me.POLLING_TIMES; |
| 337 | + } |
| 338 | + } |
| 339 | + me.totalTimes--; |
| 340 | + } |
| 341 | + |
| 342 | + /* |
| 343 | + * Method: isServiceSupportPolling |
| 344 | + * 判断服务是否支持轮询。 |
| 345 | + */ |
| 346 | + isServiceSupportPolling() { |
| 347 | + let me = this; |
| 348 | + return !( |
| 349 | + me.CLASS_NAME === "SuperMap.REST.ThemeService" || |
| 350 | + me.CLASS_NAME === "SuperMap.REST.EditFeaturesService" |
| 351 | + ); |
| 352 | + } |
| 353 | + |
| 354 | + /* |
| 355 | + * Method: serviceProcessCompleted |
| 356 | + * 状态完成,执行此方法。 |
| 357 | + * |
| 358 | + * Parameters: |
| 359 | + * result - {Object} 服务器返回的结果对象。 |
| 360 | + */ |
| 361 | + serviceProcessCompleted(result) { |
| 362 | + result = SuperMap.Util.transformResult(result); |
| 363 | + this.events.triggerEvent("processCompleted", {result: result}); |
| 364 | + } |
| 365 | + |
| 366 | + /* |
| 367 | + * Method: serviceProcessFailed |
| 368 | + * 状态失败,执行此方法。 |
| 369 | + * |
| 370 | + * Parameters: |
| 371 | + * result - {Object} 服务器返回的结果对象。 |
| 372 | + */ |
| 373 | + serviceProcessFailed(result) { |
| 374 | + result = SuperMap.Util.transformResult(result); |
| 375 | + let error = result.error || result; |
| 376 | + this.events.triggerEvent("processFailed", {error: error}); |
| 377 | + } |
| 378 | + |
| 379 | + CLASS_NAME = "SuperMap.CommonServiceBase"; |
| 380 | +} |
| 381 | + |
| 382 | +SuperMap.CommonServiceBase = CommonServiceBase; |
0 commit comments