<template>
  <view>
    <u-button @click="exportPDF">预览pdf</u-button>
    <u-button @click="handlePrint">打印pdf</u-button>
  </view>
</template>

<script>
import io from '@hyoga/uni-socket.io';
// import { SOCKET_IO_CLIENT, OPTIONS } from '@/uni_modules/bunny-socket-io-client';
import uButton from '../../uview-ui/components/u-button/u-button.vue';
export default {
  components: { uButton },
  data() {
    return {
      socket: {},
      url: 'http://192.168.3.91:8082/static/123.pdf'
    };
  },
  created() {
    this.socket = io('http://192.168.21.104:17521', {
      transports: ['websocket'],
      auth: {
        token: 'Bearer ' + this.vuex_token // 在此处填入你 client 设置的 token，缺省可留空
      }
    });
    this.socket.on('connect', () => {
      globalThis.connect = true;
      console.log('连接成功444');
      // TODO: Do something for your project
    });
    this.socket.on('clients', (clients) => {
      globalThis.clients = clients;
      console.log(clients, 'clients');
    });
    this.socket.on('printerList', (printerList) => {
      globalThis.printerList = printerList;
      console.log(printerList, 'printerList');
    });
  },
  methods: {
    handlePrint() {
      console.log(this.socket, 'this.$socket');
      this.socket.emit('news', {
        client: null,
        printer: 'HP LaserJet 1022n',
        type: 'url_pdf',
        pdf_path: 'http://192.168.3.91:8082/static/123.pdf'
      });
    },
    previewPdf() {
      // 预览pdf文件
      // uni.openDocument({
      //   filePath: 'http://192.168.3.91:8082/static/123.pdf',
      //   showMenu: true,
      //   success: function(file) {
      //     console.log("file-success", file)
      //   }
      // })
      let fileType = ['doc', 'xls', 'ppt', 'pdf', 'docx', 'xlsx', 'pptx'];
      //下载文件资源到本地
      const url = '/api/static/123.pdf';
      uni.downloadFile({
        url,
        success: function (res) {
          console.log(res);
          var filePath = res.tempFilePath;
          uni.openDocument({
            filePath: filePath,
            showMenu: true,
            success: function (res) {}
          });
        },
        fail: function (err) {
          console.log('下载失败：', err);
        }
      });
    },
    saveFile(tempFilePath) {
      //保存到本地
      //文件保存到本地
      const url = '/api/static/123.pdf';
      uni.saveFile({
        tempFilePath: url, //临时路径
        success: function (res) {
          uni.showToast({
            icon: 'none',
            mask: true,
            title: '文件已保存:' + res.savedFilePath, //保存路径
            duration: 3000
          });
          setTimeout(() => {
            //打开文档查看
            uni.openDocument({
              filePath: res.savedFilePath,
              success: function (res) {
                // console.log('打开文档成功');
              }
            });
          }, 3000);
        }
      });
    },
    exportPDF() {
      let url = this.url;
      // #ifdef APP-PLUS
      uni.downloadFile({
        url,
        success: (res) => {
          console.log(res);
          if (res.statusCode === 200) {
            // 保存pdf文件至手机，一般安卓端存储路径为：手机存储/dcim/camera文件夹下
            // uni.showToast({
            //   title: '文件已保存至/DCIM/CAMERA文件夹下',
            //   icon: 'none'
            // });
            // 预览pdf文件
            uni.openDocument({
              filePath: res.tempFilePath,
              showMenu: true,
              success: function (file) {
                console.log('file-success', file);
              }
            });
          }
        }
      });
      // #endif
    },
    downLoadFile(fileName, fileType) {
      console.log('开始下载');
      //这里填写接口地址
      let url = '/api/static/123.pdf';

      uni.downloadFile({
        //调用官方提供的uni.downloadFile
        url: url, //将刚刚提前准备好的url传过来
        header: {
          //如果需要填写请求头信息则在此处填写
        },
        success: function (res) {
          console.log(res);
          if (res.statusCode === 200) {
            console.log('下载成功：', res.tempFilePath);
            //下载成功，并把下载好的文件保存在临时区域中
            uni.saveFile({
              //使用该功能，以将临时文件保存在手机中可长久存储的区域
              tempFilePath: res.tempFilePath,
              success: (saveResult) => {
                // 在保存文件成功后，使用 uni.openDocument 方法打开文件
                console.log('文件保存成功:', saveResult);
                uni.openDocument({
                  filePath: saveResult.savedFilePath,
                  fileType: fileType, //这里把预先限定好的文件类型传来
                  success: function () {
                    console.log('文件打开成功!');
                    //至此，手机会从app中跳转到文件预览界面
                    //之后可从预览界面返回app中
                  },
                  fail: function (err) {
                    console.log('打开失败', err);
                  }
                });
              },
              fail: function (err) {
                console.log('保存文件失败', err);
              }
            });
          }
        },
        fail: function (err) {
          console.log('下载失败：', err);
        }
      });
    },
    //文件预览
    prefile(e) {
      let that = this;
      let url = '/api/static/123.pdf';
      uni.downloadFile({
        url: url,
        success: function (res) {
          let filepathss = plus.io.convertLocalFileSystemURL(res.tempFilePath);
          setTimeout(
            () =>
              uni.openDocument({
                filePath: filepathss,
                showMenu: false,
                success: function () {
                  console.log('打开文档成功');
                },
                fail: function () {
                  uni.showToast({
                    title: '暂不支持此类型',
                    duration: 2000,
                    icon: 'none'
                  });
                }
              }),
            1000
          );
        },
        fail: function (res) {
          console.log(res); //失败
        }
      });
    }
  }
};
</script>

<style lang="scss"></style>
