Jelajahi Sumber

优化发票打印

“shengjie.huang” 1 tahun lalu
induk
melakukan
f0b15e0262
2 mengubah file dengan 58 tambahan dan 72 penghapusan
  1. 56 70
      src/pages/print/index.vue
  2. 2 2
      src/service/api/index.ts

+ 56 - 70
src/pages/print/index.vue

@@ -21,7 +21,7 @@
             <view v-for="(item, index) of fileList"
                   :key="item.filePath"
                   class="file-item"
-                  @click="previewFile(item.filePath)">
+                  @click="previewFile(item.name, item.filePath)">
               <view class="item-icon">
                 <wd-img
                   :src="getFileIcon(item.name)"
@@ -160,10 +160,6 @@ const message = useMessage()
 // 系统文件--all, 聊天文件--msg, 相册--image, 发票--invoice
 const accept = ref("all")
 const fileList = ref([])
-// 测试发票
-// const fileList = ref([
-//   {name: "测试发票.pdf", filePath: "https://pay.weixin.qq.com/invoicing/invoicepdf/getpdf?type=invoice&card_id=pmB7twzboHW_C66OLCIqz4VYkpjw&wx_invoice_token=FCFimpsJEytCHNpnEzqafLRmT_5NV5Mk0evaCTOrJeB3J_EI2Wvn0jSQAJnMrd0xVG8vxpcfhpeNOSn5XGEYfg.%3D"}
-// ])
 const form = ref()
 const formData: any = ref({
   printer: "",
@@ -343,13 +339,7 @@ function selectFile() {
                   console.log('InvoiceBatch res.body: ', res.body);
                   // 遍历下载pdf 存储在 fileList 打印
                   res.body.forEach(item => {
-                    downloadFile(`微信发票-${item.payee}.png`, item.userInfo.pdfUrl)
-
-                    // 直接下会有白名单问题
-                    // fileList.value.push({
-                    //   name: `微信发票-${item.payee}.pdf`,
-                    //   filePath: item.userInfo.pdfUrl
-                    // })
+                    downloadFile(`发票-${item.payee}.png`, item.userInfo.pdfUrl)
                   });
                 }
               })
@@ -366,18 +356,13 @@ function selectFile() {
 
 // 下载文件(用于处理发票)
 function downloadFile(fileName, fileUrl) {
-  console.log('fileUrl: ', fileUrl);
   uni.downloadFile({
     url: fileUrl,
-    header: {
-      'Content-Type': "application/png"
-    },
+    filePath: uni.env.USER_DATA_PATH + '/' + fileName,
     success: (res) => {
-      console.log('download res: ', res);
-      // res.tempFilePath
       fileList.value.push({
         name: fileName,
-        filePath: res.tempFilePath.replace(".bin", ".png")
+        filePath: res.filePath,
       })
     },
     fail () {
@@ -387,38 +372,25 @@ function downloadFile(fileName, fileUrl) {
 }
 
 // 预览文件 (区分文件&图片)
-function previewFile(filePath) {
-  uni.openDocument({
-    filePath: filePath,
-    success () {},
-    fail () {
-      toast.warning("打开文件失败, 请重试")
-    }
-  })
-
-  // uni.downloadFile({
-  //   url: filePath,
-  //   success: (res) => {
-  //     console.log('previewFile res: ', res.tempFilePath);
-  //     uni.openDocument({
-  //       filePath: res.tempFilePath,
-  //       success () {},
-  //       fail () {
-  //         toast.warning("打开文件失败, 请重试")
-  //       }
-  //     })
-  //   },
-  //   fail () {
-  //     toast.warning("下载文件失败, 请重试")
-  //   }
-  // })
-
-  // 图片预览
-  // let imgUrl = props.url || props.url
-  // uni.previewImage({
-  //   current: 0,
-  //   urls: [imgUrl],
-  // })
+function previewFile(fileName, filePath) {
+  let fileType = getFileType(fileName)
+  if (['png', 'jpg', 'jpeg', 'webp'].includes(fileType)) {
+    // 图片预览
+    let imgUrl = filePath
+    uni.previewImage({
+      current: 0,
+      urls: [imgUrl],
+    })
+  } else {
+    // 文件预览
+    uni.openDocument({
+      filePath: filePath,
+      success () {},
+      fail () {
+        toast.warning("打开文件失败, 请重试")
+      }
+    })
+  }
 }
 
 // 移除文件
@@ -465,27 +437,34 @@ function handlePrint(filePath) {
     filePath: filePath,
     name: 'file', // 这里根据后端需要的字段来定义
     success: (res: any) => {
-      if (res.statusCode === 200 && JSON.parse(res.data).code === 0) {
-        message.confirm({
-          msg: "打印成功, 是否继续打印?",
-          closeOnClickModal: false,
-        }).then(() => {
-          fileList.value = []
-          printerOptions.value = []
-          formData.value = {
-            printer: ""
-          }
-          if (accept.value == "all") {
-            redirectToUpload()
-          }
-        }).catch((error) => {
-          reLaunchToHome()
-        })
+      if (fileList.value.length == 1) {
+        // 单个打印完成
+        if (res.statusCode === 200 && JSON.parse(res.data).code === 0) {
+          message.confirm({
+            msg: "打印成功, 是否继续打印?",
+            closeOnClickModal: false,
+          }).then(() => {
+            fileList.value = []
+            printerOptions.value = []
+            formData.value = {
+              printer: ""
+            }
+            if (accept.value == "all") {
+              redirectToUpload()
+            }
+          }).catch((error) => {
+            reLaunchToHome()
+          })
+        } else {
+          console.log("打印异常 res", res);
+          toast.warning('上传打印异常, 请重试')
+        }
       } else {
-        toast.warning('上传打印失败, 请重试')
+        // 批量打印完成
       }
     },
     fail: (error) => {
+      console.log('打印失败 error: ', error);
       toast.error('上传打印失败, 请重试')
     },
     complete: () => {
@@ -502,8 +481,15 @@ function handleSubmit() {
   }
   form.value.validate().then(({ valid }) => {
     if (valid) {
-      // TODO: 批量打印
-      handlePrint(fileList.value[0].filePath)
+      // TODO: 批量打印提示
+      if (fileList.value.length == 1) {
+        handlePrint(fileList.value[0].filePath)
+      } else {
+        message.alert(`本次打印文件 ${fileList.value.length} 个`)
+        fileList.value.forEach(item => {
+          handlePrint(item.filePath)
+        })
+      }
     }
   })
 }

+ 2 - 2
src/service/api/index.ts

@@ -132,7 +132,7 @@ export const printFile = (params, data, contentType) => {
 export const getInvoiceInfo = (data) => {
   return http<any>({
     url: `/api/wx/mini/invoice-info`,
-    method: 'POST',
+    method: 'GET',
     query: data
   })
 }
@@ -141,7 +141,7 @@ export const getInvoiceInfo = (data) => {
 export const getInvoiceBatch = (data) => {
   return http<any>({
     url: `/api/wx/mini/invoice-batch`,
-    method: 'POST',
+    method: 'GET',
     query: data
   })
 }