import axios from 'axios';
axios.jsonp = (url, data) => {
if (!url) throw new Error('url is necessary');
const callback = 'jsonpcallback';
const JSONP = document.createElement('script');
JSONP.setAttribute('type', 'text/javascript');
const headEle = document.getElementsByTagName('head')[0];
let ret = '';
if (data) {
if (typeof data === 'string') {
ret = '&' + data;
} else if (typeof data === 'object') {
for (let key in data) ret += '&' + key + '=' + encodeURIComponent(data[key]);
}
ret += '&_time=' + Date.now();
}
JSONP.src = `${url}?callback=${callback}${ret}`;
function remove() {
headEle.removeChild(JSONP);
delete window[callback];
}
return new Promise((resolve, reject) => {
window[callback] = r => {
resolve(r);
remove();
};
JSONP.onerror = function () {
reject();
remove();
};
headEle.appendChild(JSONP);
});
};
var sJson1 = JSON.stringify({ 'function': 'SetLock', 'sn': 0,'mini': 0, 'id': 8 });
await axios
.jsonp('http://127.0.0.1:17681', `json=${sJson1}`)
.then(res => {
allRes.usbkey = res[0].sn ?? '';
})
.catch(err => {
allRes.usbkey = '';
});