SOURCE

console 命令行工具 X clear

                    
>
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 = [];

        // 并发请求三个端口的ping路径
        var promiseList = portList.map(function (port) {
          return Vue.http.get('http://localhost:' + port + '/ping', {
            before(request) {
              reqs.push(request);
            },
          });
        });

        // 最快的一个ping接口返回时,获取到port,取消剩余的两个接口
        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() {
      // ajax获取需要打开的文件二进制数据
      Vue.http.get('./demo.pdf', { responseType: 'blob' }).then(function (resp) {
        // 把文件名和数据构建表单发给守护程序service
        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() {
      // ajax获取需要打开的文件二进制数据
      Vue.http.get('./测试用例.xlsx', { responseType: 'blob' }).then(function (resp) {
        // 把文件名和数据构建表单发给守护程序service
        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'); //回写远端路径

        // fd.append('silent', '1')  // 禁用 成功失败 提示
        // fd.append('showError', '1')  // 显示失败消息

        Vue.http
          .post(getLocalServerPath('/edit-file'), fd, {})
          .then(() => {
            console.log('打开成功');
          })
          .catch((error) => {
            if (error.status === 0) {
              alert('无法访问本地托盘服务,请检查');
            } else {
              alert(error.body.msg);
            }
          });
      });
    }

    // 通知
    function notification() {
      // 发送请求 参数{title, body, icon('success', 'info', 'error', 'warning'), timeoutType('default', 'never'手动关闭), }
      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() {
      // ajax获取需要打开的文件二进制数据
      Vue.http.get('./demo.pdf', { responseType: 'blob' }).then(function (resp) {
        // 把文件名和数据构建表单发给守护程序service
        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>