Commit ccb9cd2a authored by rockyl's avatar rockyl

引擎启动参数改为engineConfig和customConfig

parent 524b14e4
/**
* Created by hwj on 2018/11/22.
* 网络组件
*/
import {utils} from "scilla";
// 请求路径 root
let baseUrl: string;
/**
* 调用接口
* @param uri
* @param params
* @param method
* @param responseType
* @param ignoreSuccessField
*/
export function callApi(uri: string, params: any = null, method: string = 'post', responseType = 'json', ignoreSuccessField = false): Promise<any> {
let ts = Date.now() + Math.floor(Math.random() * 9999999);
let url = uri.indexOf('//') === 0 ? uri : `${uri}?_=${ts}`;
params = params || {};
let options: any = {
method,
};
if (!baseUrl) {
options.credentials = 'include';
}
let temp = typeof params === 'string' ? params : utils.objectStringify(params);
switch (method.toUpperCase()) {
case 'GET':
if(temp && temp.length > 0){
url += (url.indexOf('?') < 0 ? '?' : '') + '&' + temp;
}
break;
case 'POST':
options.body = temp;
options.headers = {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
};
break;
}
const fetchMethod = responseType == 'jsonp' ? window['fetchJsonp'] : fetch;
url = baseUrl ? baseUrl + url : url
return fetchMethod(url, options)
.then((response) => {
if (response.type === 'opaque') {
return null;
}
return response.text();
})
.then((response) => {
//console.log('fetch ==>', url, response);
if (response) {
let data;
switch (responseType) {
case 'json':
try {
/*console.log('debug', GameConfig.debug);
console.log(url, options);
console.log(response);*/
data = JSON.parse(response);
} catch (e) {
console.log('decode json failed: ' + url);
return Promise.reject({});
}
if (ignoreSuccessField || data.success) {
return {
data: data.hasOwnProperty('data') ? data.data : data,
origin: data,
};
} else {
return Promise.reject(data.code);
}
case 'html':
let html = null;//DOMParser.parseFromString(response, 'text/html');
return html;
case 'txt':
return response;
}
}
return Promise.reject();
})
}
/**
* 轮训请求
* @param successFunc
* @param maxTimes
* @param delay
* @param uri
* @param params
* @param method
* @param responseType
*/
export function polling(successFunc, uri, params, maxTimes = 10, delay = 500, method = 'POST', responseType = 'json'): Promise<any> {
let p = Promise.resolve();
for (let i = 0; i < maxTimes; i++) {
p = p.then(func);
p = p.then(() => {
return utils.waitPromise(delay)
})
}
let lastData;
return p.then(
() => {
return Promise.reject(null);
},
(e) => {
if (e === 'success') {
return Promise.resolve(lastData);
}
return Promise.reject(e);
}
);
function func() {
return callApi(uri, params, method, responseType).then(
(data) => {
if (successFunc(data)) {
lastData = data;
return Promise.reject('success');
}
},
(e) => {
return Promise.reject(e);
}
)
}
}
/**
* 获取token并发送
* @param uri
* @param params
* @param method
* @param responseType
*/
export async function getToken(uri: string, params: any, method: string = 'POST', responseType: string = 'json') {
if (window['getDuibaToken']) {
window['getDuibaToken'](async (tokenObj: any) => {
params.token = tokenObj.token;
await this.send(uri, params, method, responseType);
}, () => {
});
} else {
await this.send(uri, params, method, responseType);
}
}
// /**
// * 通讯底层错误
// */
// protected onError(key,msgObj): void {
// const msg = `${key}:${msgObj}`
// this.dispatchEvent('Error', msg );
// }
/**
* 生成签名
* @param {number} ticketId
* @param {number} score
* @param {any} gameData
* @param {string} submitToken 提交
* @returns {string} 签名
*/
export function createSgin(ticketId: number, score: number, gameData: any, submitToken: string): string {
return window['duiba_md5'](ticketId + '' + score + '' + gameData + '' + submitToken);
}
/**
* @desc 请求root补全
* @param {string} u
*/
export function enjectBaseUrl(u) {
baseUrl = u;
}
\ No newline at end of file
/**
* Created by hwj on 2018/11/22.
* 网络组件
*/
import {utils, customConfig} from "scilla";
/**
* 调用接口
* @param uri
* @param params
* @param method
* @param responseType
* @param ignoreSuccessField
*/
export function callApi(uri: string, params: any = null, method: string = 'post', responseType = 'json', ignoreSuccessField = false): Promise<any> {
let ts = Date.now() + Math.floor(Math.random() * 9999999);
let url = uri.indexOf('//') === 0 ? uri : `${uri}?_=${ts}`;
params = params || {};
let options: any = {
method,
};
let baseUrl = customConfig.webServiceUrl;
if (!baseUrl) {
options.credentials = 'include';
}
let temp = typeof params === 'string' ? params : utils.objectStringify(params);
switch (method.toUpperCase()) {
case 'GET':
if(temp && temp.length > 0){
url += (url.indexOf('?') < 0 ? '?' : '') + '&' + temp;
}
break;
case 'POST':
options.body = temp;
options.headers = {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
};
break;
}
const fetchMethod = responseType == 'jsonp' ? window['fetchJsonp'] : fetch;
url = baseUrl ? baseUrl + url : url
return fetchMethod(url, options)
.then((response) => {
if (response.type === 'opaque') {
return null;
}
return response.text();
})
.then((response) => {
//console.log('fetch ==>', url, response);
if (response) {
let data;
switch (responseType) {
case 'json':
try {
/*console.log('debug', GameConfig.debug);
console.log(url, options);
console.log(response);*/
data = JSON.parse(response);
} catch (e) {
console.log('decode json failed: ' + url);
return Promise.reject({});
}
if (ignoreSuccessField || data.success) {
return {
data: data.hasOwnProperty('data') ? data.data : data,
origin: data,
};
} else {
return Promise.reject(data.code);
}
case 'html':
let html = null;//DOMParser.parseFromString(response, 'text/html');
return html;
case 'txt':
return response;
}
}
return Promise.reject();
})
}
/**
* 轮训请求
* @param successFunc
* @param maxTimes
* @param delay
* @param uri
* @param params
* @param method
* @param responseType
*/
export function polling(successFunc, uri, params, maxTimes = 10, delay = 500, method = 'POST', responseType = 'json'): Promise<any> {
let p = Promise.resolve();
for (let i = 0; i < maxTimes; i++) {
p = p.then(func);
p = p.then(() => {
return utils.waitPromise(delay)
})
}
let lastData;
return p.then(
() => {
return Promise.reject(null);
},
(e) => {
if (e === 'success') {
return Promise.resolve(lastData);
}
return Promise.reject(e);
}
);
function func() {
return callApi(uri, params, method, responseType).then(
(data) => {
if (successFunc(data)) {
lastData = data;
return Promise.reject('success');
}
},
(e) => {
return Promise.reject(e);
}
)
}
}
/**
* 获取token并发送
* @param uri
* @param params
* @param method
* @param responseType
*/
export async function getToken(uri: string, params: any, method: string = 'POST', responseType: string = 'json') {
if (window['getDuibaToken']) {
window['getDuibaToken'](async (tokenObj: any) => {
params.token = tokenObj.token;
await this.send(uri, params, method, responseType);
}, () => {
});
} else {
await this.send(uri, params, method, responseType);
}
}
// /**
// * 通讯底层错误
// */
// protected onError(key,msgObj): void {
// const msg = `${key}:${msgObj}`
// this.dispatchEvent('Error', msg );
// }
/**
* 生成签名
* @param {number} ticketId
* @param {number} score
* @param {any} gameData
* @param {string} submitToken 提交
* @returns {string} 签名
*/
export function createSgin(ticketId: number, score: number, gameData: any, submitToken: string): string {
return window['duiba_md5'](ticketId + '' + score + '' + gameData + '' + submitToken);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment