TMRAppBle/script/req.js

56 lines
1.4 KiB
JavaScript
Raw Normal View History

2023-10-19 15:00:17 +08:00
const config = {
// schema: 'http',
// host: '192.168.0.107:8088',
schema: 'https',
host: 'tmr.nxcyx.com/api/',
path: 'app/tmr',
secret:'776eca99-******-11e9-9897-*******'
}
function req(options) {
const baseUrl = `${config.schema}://${config.host}/${config.path}/`;
options.url = baseUrl + options.url;
return new Promise((resolve, reject) => {
var data = options.data?JSON.stringify(options.data.body):'';
api.ajax(options, (ret, err) => {
console.log('[' + options.method + '] ' + options.url +data+ ' [' + api.winName + '/' + api.frameName + ']')
if (ret) {
resolve(ret);
api.hideProgress();
} else {
reject(err);
api.hideProgress();
}
});
})
}
/**
* GET请求快捷方法
* @constructor
* @param url {string} 地址
* @param options {Object} 附加参数
*/
function GET(url, options = {}) {
return req({
...options, url, method: 'GET'
});
}
/**
* POST 请求快捷方法
* @param url
* @param data
* @param options {Object} 附加参数
* @returns {Promise<Object>}
* @constructor
*/
function POST(url, data, options = {}) {
options.headers= {'Content-Type': 'application/json;charset=utf-8'};
return req({
...options, url, method: 'POST', data: {body:data}
});
}
export {
req, GET, POST, config
}