@ccos/webos-core
Version:
A Web-Ui Framework for Skyworth/Coocaa TV
80 lines (70 loc) • 2.4 kB
JavaScript
let tv_protocol = 'http';
let tv_server = 'localhost';
let tv_port = 32758;
function setDebugModeServer(ipAddr) {
if (ipAddr) {
tv_server = ipAddr;
}
}
export function getUrlBase() {
return `${tv_protocol}://${tv_server}:${tv_port}`;
}
function call_uapi_inner(path, str) {
return new Promise((resolve) => {
try {
let xhr = new XMLHttpRequest();
let url = getUrlBase() + path;
xhr.open("POST", url, true); //异步
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.responseType = 'json'; //如果为text则response为文本
xhr.timeout = 60 * 60 * 1000;
xhr.onreadystatechange = function () { //绑定响应状态事件监听函数
if (xhr.readyState == 4) { //监听readyState状态
if (xhr.status == 200 || xhr.status == 0) { //监听HTTP状态码
//console.log(xhr.responseText); //接收数据
//设置responseType为 json 则xhr.response 返回json对象,则不能通过 responseText 取得对象
resolve(xhr.response);
}
else {
resolve('error: ' + xhr.status);
}
} else {
//
}
}
xhr.onerror = function() {
//console.error(xhr.statusText);
resolve('error: ' + xhr.statusText);
}
xhr.ontimeout = function() {
//console.error('The request for ' + url + ' timed out.');
resolve('error: ' + 'timeout');
};
xhr.send(str); //发送请求
}
catch (e) {
resolve('error: ' + e);
}
});
}
export async function call_uapi(path, str) {
let res = await call_uapi_inner(path, str || '');
if (typeof res == 'object') {
return res;
} else {
return {
code: -100000,
msg: res
}
}
}
async function getBaseInfo() {
return call_uapi('/api/base/api_1', null);
}
function registerEventHandler(callback) {
}
export default {
setDebugModeServer,
getBaseInfo,
registerEventHandler,
};