console
<h1>Welcome to 托盘测试页</h1>
<a href="trayapp://"><h1>启动托盘程序</h1></a>
<br>
<button onclick="openFile()">打开文件</button>
<button onclick="openLocalFile()">打开本地文件</button>
<button onclick="editFile()">编辑文件</button>
<button onclick="notification()">显示通知</button>
<button onclick="getPrinters()">获取打印机列表</button>
<button onclick="printPdf()">打印PDF</button>
<button onclick="readFile()">读取文件</button>
<button onclick="exec()">执行命令</button>
<pre id="disp">
</pre>
<script>
var prot;
function init() {
return new Promise(function(res, rej) {
var portList = [16691, 24123, 32717];
var reqs = [];
var promiseList = portList.map(function (port) {
return Vue.http.get('http://localhost:' + port + '/ping', {
before(request) {
reqs.push(request);
},
});
});
Promise.race(promiseList).then(function (res) {
port = res.data.port;
reqs.forEach(function (req) {
req.abort();
});
});
})
}
init();
function getLocalServerPath(path) {
if (!port) {
init();
}
return 'http://localhost:' + port + path;
}
function openFile() {
Vue.http.get('./demo.pdf', { responseType: 'blob' }).then(function (resp) {
var fd = new FormData();
fd.append('filename', 'demo.pdf');
fd.append('file', resp.bodyBlob);
Vue.http
.post(getLocalServerPath('/open-file'), fd, {})
.then(() => {
console.log('打开成功');
})
.catch((error) => {
if (error.status === 0) {
alert('无法访问本地托盘服务,请检查');
} else {
alert(error.body.msg);
}
});
});
}
function openLocalFile() {
var fd = new FormData();
fd.append('path', 'D:\\1.pdf');
Vue.http
.post(getLocalServerPath('/open-file'), fd, {})
.then(() => {
console.log('打开成功');
})
.catch((error) => {
if (error.status === 0) {
alert('无法访问本地托盘服务,请检查');
} else {
alert(error.body.msg);
}
});
}
function editFile() {
Vue.http.get('./测试用例.xlsx', { responseType: 'blob' }).then(function (resp) {
var fd = new FormData();
fd.append('filename', '测试用例.xlsx');
fd.append('file', resp.bodyBlob);
fd.append('type', 'webdav');
fd.append('server', 'http://127.0.0.1:10080');
fd.append('username', 'ADMIN');
fd.append('password', 'admin');
fd.append('path', '/测试用例.xlsx');
Vue.http
.post(getLocalServerPath('/edit-file'), fd, {})
.then(() => {
console.log('打开成功');
})
.catch((error) => {
if (error.status === 0) {
alert('无法访问本地托盘服务,请检查');
} else {
alert(error.body.msg);
}
});
});
}
function notification() {
Vue.http
.post(getLocalServerPath('/notification'), { title: '测试标题', body: `此处为测试消息的内容部分,此处为测试消息的内容部分。`, icon: 'success', timeoutType: 'never' })
.then(() => {
console.log('消息显示成功');
})
.catch((error) => {
if (error.status === 0) {
alert('无法访问本地托盘服务,请检查');
} else {
alert(error.body.msg);
}
});
}
function getPrinters() {
Vue.http
.get(getLocalServerPath('/get-printers'))
.then((resp) => {
disp.innerText = JSON.stringify(resp.data, null,2)
})
.catch((error) => {
if (error.status === 0) {
alert('无法访问本地托盘服务,请检查');
} else {
alert(error.body.msg);
}
});
}
function printPdf() {
Vue.http.get('./demo.pdf', { responseType: 'blob' }).then(function (resp) {
var fd = new FormData();
fd.append('printer', 'Microsoft Print to PDF');
fd.append('file', resp.bodyBlob);
Vue.http
.post(getLocalServerPath('/print-pdf'), fd, {})
.then(() => {
console.log('打印成功');
})
.catch((error) => {
if (error.status === 0) {
alert('无法访问本地托盘服务,请检查');
} else {
alert(error.body.msg);
}
});
});
}
function readFile() {
Vue.http
.post(getLocalServerPath('/read-file'), {
path: 'D:\\1.txt',
encoding: 'utf8',
})
.then((resp) => {
disp.innerText = resp.data.data
})
.catch((error) => {
if (error.status === 0) {
alert('无法访问本地托盘服务,请检查');
} else {
alert(error.body.msg);
}
});
}
function readBinaryFile() {
Vue.http
.post(getLocalServerPath('/read-file'), { path: 'D:\\1.zip' }, { responseType: 'blob' })
.then((resp) => {
console.log(resp);
})
.catch((error) => {
if (error.status === 0) {
alert('无法访问本地托盘服务,请检查');
} else {
alert(error.body.msg);
}
});
}
function exec() {
Vue.http
.post(getLocalServerPath('/exec'), { command: 'dir', options: {encoding: 'GBK'}})
.then((resp) => {
disp.innerText = resp.data.data
})
.catch((error) => {
if (error.status === 0) {
alert('无法访问本地托盘服务,请检查');
} else {
alert(error.body.msg);
}
});
}
</script>