tsp-component
Version:
提供多端和react版本的UI组件
165 lines (164 loc) • 6.48 kB
JavaScript
import assign from 'object-assign';
import ajax from './index';
import Cache from './cache';
var WebApi = (function () {
function WebApi(ajaxUrl, dbParams) {
this.ajaxUrl = ajaxUrl;
try {
if ('open' in indexedDB) {
this.isSupportIndexDB = true;
}
}
catch (e) {
this.isSupportIndexDB = false;
}
if (this.isSupportIndexDB && dbParams && dbParams.dbName && dbParams.tableName) {
this.cache = new Cache(dbParams.dbName, dbParams.tableName);
this.cache.open();
}
}
WebApi.abort = function (noAbortUrl, abortUrl) {
var length = WebApi.requested.length;
var noAbortUrlLength = noAbortUrl ? noAbortUrl.length : 0;
var abortUrlLength = abortUrl ? abortUrl.length : 0;
var i;
var j;
if (abortUrlLength) {
for (i = 0; i < length; i++) {
if (WebApi.requested[i].readyState === 1) {
for (j = 0; j < abortUrlLength; j++) {
if (WebApi.requested[i]['url'] === abortUrl[j]) {
WebApi.requested[i]['isAbort'] = true;
WebApi.requested[i].abort();
break;
}
}
}
}
}
else {
for (i = 0; i < length; i++) {
var flag = true;
if (WebApi.requested[i].readyState === 1) {
for (j = 0; j < noAbortUrlLength; j++) {
if (WebApi.requested[i]['url'] !== noAbortUrl[j]) {
flag = false;
break;
}
}
if (flag) {
WebApi.requested[i]['isAbort'] = true;
WebApi.requested[i].abort();
}
}
}
}
};
WebApi.prototype.post = function (options) {
var _this = this;
var apiUrl;
var requested;
var cacheResult;
if (options.api.indexOf('://') >= 0) {
apiUrl = options.api;
}
else {
apiUrl = WebApi.proxy ? WebApi.proxy + options.api : this.ajaxUrl + options.api;
}
var sendData = WebApi.defaultParams ? assign({}, WebApi.defaultParams, options.params) : options.params;
var xhrType = options.type ? options.type : 'POST';
var apiInfo = {
api: apiUrl,
type: xhrType,
params: sendData
};
var ajaxSendObj = {
url: apiUrl,
type: xhrType,
contentType: options.contentType,
customHeader: options.customHeader,
timeout: options.timeout,
data: sendData,
success: function (result, xhr) {
if (WebApi.response) {
WebApi.response(result, function (status) {
if (options.isCache && _this.isSupportIndexDB) {
if ((JSON.stringify(cacheResult.value) !== JSON.stringify(result)) && status) {
var value = {
api: JSON.stringify({ url: apiUrl, params: options.params }),
value: result
};
var cacheUpdate = cacheResult.value ? true : false;
options.success(result, status, cacheUpdate);
if (cacheResult.value) {
_this.cache.put(value);
}
else {
_this.cache.add(value);
}
}
else if (!cacheResult.value) {
options.success(result, status);
}
}
else {
options.success(result, status);
}
}, JSON.stringify(assign({}, apiInfo, { result: result })));
}
else {
console.error('缺少WebApi.response');
}
if (WebApi.alwaysDone) {
WebApi.alwaysDone();
}
},
error: function (xhr) {
if (xhr['isAbort']) {
return;
}
if (WebApi.alwaysDone) {
WebApi.alwaysDone();
}
if (options.isCache && _this.isSupportIndexDB) {
if (options.timeoutCallback && !cacheResult.value) {
options.timeoutCallback(xhr);
}
}
else if (options.timeoutCallback) {
options.timeoutCallback(xhr);
}
},
complete: function (xhr) {
if (options.complete) {
options.complete(xhr);
}
}
};
if (options.isCache && this.isSupportIndexDB) {
this.cache.read({
key: JSON.stringify({ url: apiUrl, params: options.params }),
onsuccess: function (result) {
cacheResult = result;
requested = ajax(ajaxSendObj);
if (options.cacheReadSuccess) {
options.cacheReadSuccess(result.value);
}
if (result.value) {
WebApi.response(result.value, function (params) {
options.success(result.value, params, false);
}, JSON.stringify(assign({}, apiInfo, { result: result })));
}
WebApi.requested.push(requested);
}
});
}
else {
requested = ajax(ajaxSendObj);
WebApi.requested.push(requested);
}
};
WebApi.requested = [];
return WebApi;
}());
export default WebApi;