谢杰标 2 years ago
parent
commit
1ac9942a21
4 changed files with 96 additions and 89 deletions
  1. 1 1
      common/methodTool.js
  2. 78 0
      common/util.js
  3. 10 86
      pages2/invoice/index.vue
  4. 7 2
      pages2/learn/my_learn.vue

+ 1 - 1
common/methodTool.js

@@ -671,7 +671,7 @@ export default {
       uni.downloadFile({
         url,
         success: (res) => {
-          if (res.statusCode === 200 || res.statusCode === 400) {
+          if (res.statusCode === 200) {
             that.fileToBase64(res.tempFilePath).then((res) => {
               resolve(res);
             });

+ 78 - 0
common/util.js

@@ -0,0 +1,78 @@
+import methods from "@/common/methodTool";
+export function download(url) {
+  // #ifdef H5
+  window.location.href = url;
+  // #endif
+  // #ifdef MP-WEIXIN
+  const _s = saveImgToLoca;
+  uni.getSetting({
+    success: (res) => {
+      if (!res.authSetting["scope.writePhotosAlbum"]) {
+        uni.authorize({
+          scope: "scope.writePhotosAlbum",
+          success() {
+            _s(url);
+          },
+          fail() {},
+        });
+      } else {
+        _s(url);
+      }
+    },
+  });
+  // #endif
+}
+
+function saveImgToLoca(url) {
+  uni.downloadFile({
+    url: url, //图片地址
+    success: ({ statusCode, tempFilePath }) => {
+      if (statusCode === 200) {
+        const type = tempFilePath.substr(tempFilePath.lastIndexOf(".") + 1);
+        console.log("🚀 ~ file: util.js:32 ~ saveImgToLoca ~ type:", type);
+        if (type === "pdf") {
+          uni.getFileSystemManager().saveFile({
+            tempFilePath,
+            success: function (res) {
+              console.log("🚀 ~ file: util.js:37 ~ uni.getFileSystemManager ~ res:", res)
+              uni.showToast({
+                title: "保存成功",
+                icon: "none",
+              });
+            },
+            fail: function () {
+              uni.showToast({
+                title: "保存失败",
+                icon: "none",
+              });
+            },
+          });
+          return;
+        }
+        uni.showModal({
+          title: "提示",
+          content: "确定保存到相册吗",
+          success: (res) => {
+            if (res.confirm) {
+              uni.saveImageToPhotosAlbum({
+                filePath: tempFilePath,
+                success: function () {
+                  uni.showToast({
+                    title: "保存成功",
+                    icon: "none",
+                  });
+                },
+                fail: function () {
+                  uni.showToast({
+                    title: "保存失败",
+                    icon: "none",
+                  });
+                },
+              });
+            }
+          },
+        });
+      }
+    },
+  });
+}

+ 10 - 86
pages2/invoice/index.vue

@@ -631,7 +631,7 @@
                         invoiceDetail.invoiceImg
                       "
                     >
-                      <view slot="title">发票预览:</view>
+                      <!-- <view slot="title">发票预览:</view>
                       <view>
                         <image
                           class="preview"
@@ -643,7 +643,12 @@
                           @click="download(invoiceDetail)"
                           >下载电子发票</view
                         >
-                      </view>
+                      </view> -->
+                      <view
+                        class="download-btn"
+                        @click="download(invoiceDetail)"
+                        >下载电子发票</view
+                      >
                     </u-cell-item>
 
                     <u-cell-item
@@ -728,6 +733,7 @@
 
 <script>
 import { mapGetters } from "vuex";
+import { download } from "../../common/util";
 export default {
   components: {},
   data() {
@@ -773,10 +779,6 @@ export default {
             validator: (rule, value, callback) => {
               // 上面有说,返回true表示校验通过,返回false表示不通过
               // this.$u.test.mobile()就是返回true或者false的
-              console.log(
-                this.$u.test.mobile(value),
-                "this.$u.test.mobile(value)"
-              );
               return this.$u.test.mobile(value);
             },
             message: "手机号码格式不正确",
@@ -998,7 +1000,6 @@ export default {
       this.invoiceDetail = item;
     },
     toFixed(number) {
-      console.log(number.toFixed(2));
       return number.toFixed(2);
     },
     /**
@@ -1027,8 +1028,6 @@ export default {
      */
     formSubmit() {
       this.$refs.uForm.validate((valid) => {
-        console.log(valid, "valid");
-        console.log(this.form, "this.form");
         if (valid) {
           if (this.invoicePirce > 10000) {
             uni.showModal({
@@ -1091,7 +1090,6 @@ export default {
      * 发票类型修改
      */
     formTypeChange(e) {
-      console.log(e);
       if (e == "2") {
         this.$set(this.form, "subject", "2");
       } else {
@@ -1102,9 +1100,7 @@ export default {
      * tabs 切换
      */
     change(e) {
-      console.log(e);
       this.current = e;
-
       if (this.current == 1) {
         this.orderInvoiceList();
       } else {
@@ -1162,7 +1158,6 @@ export default {
       this.$nextTick(() => {
         this.$refs.orderGoodsIds.onFieldChange();
       });
-      console.log(this.$refs.orderGoodsIds);
     },
 
     deleteOrder(index) {
@@ -1189,15 +1184,7 @@ export default {
         urls: [this.$method.splitImgHost(item.invoiceImg, true, 1000)],
         longPressActions: {
           itemList: ["发送给朋友", "保存图片", "收藏"],
-          success: function (data) {
-            console.log(
-              "选中了第" +
-                (data.tapIndex + 1) +
-                "个按钮,第" +
-                (data.index + 1) +
-                "张图片"
-            );
-          },
+          success: function (data) {},
           fail: function (err) {
             console.log(err.errMsg);
           },
@@ -1206,70 +1193,7 @@ export default {
     },
 
     download(item) {
-      // #ifdef H5
-      window.location.href = this.$method.splitImgHost(
-        item.certificatePath,
-        true,
-        1000
-      );
-      // #endif
-      // #ifdef MP-WEIXIN
-      //获取相册授权
-      uni.getSetting({
-        success: (res) => {
-          console.log("🚀 ~ file: index.vue:1221 ~ download ~ res:", res);
-          if (!res.authSetting["scope.writePhotosAlbum"]) {
-            uni.authorize({
-              scope: "scope.writePhotosAlbum",
-              success() {
-                //这里是用户同意授权后的回调
-                this.saveImgToLocal(item);
-              },
-              fail() {
-                //这里是用户拒绝授权后的回调
-              },
-            });
-          } else {
-            //用户已经授权过了
-            this.saveImgToLocal(item);
-          }
-        },
-      });
-      // #endif
-    },
-
-    saveImgToLocal(item) {
-      uni.showModal({
-        title: "提示",
-        content: "确定保存到相册吗",
-        success: (res) => {
-          if (res.confirm) {
-            uni.downloadFile({
-              url: this.$method.splitImgHost(item.invoiceImg, true, 1000), //图片地址
-              success: (res) => {
-                if (res.statusCode === 200 || res.statusCode === 400) {
-                  uni.saveImageToPhotosAlbum({
-                    filePath: res.tempFilePath,
-                    success: function () {
-                      uni.showToast({
-                        title: "保存成功",
-                        icon: "none",
-                      });
-                    },
-                    fail: function () {
-                      uni.showToast({
-                        title: "保存失败",
-                        icon: "none",
-                      });
-                    },
-                  });
-                }
-              },
-            });
-          } else if (res.cancel) {
-          }
-        },
-      });
+      download(this.$method.splitImgHost(item.invoiceImg));
     },
   },
   computed: {

+ 7 - 2
pages2/learn/my_learn.vue

@@ -519,9 +519,14 @@ export default {
         success: (res) => {
           if (res.confirm) {
             uni.downloadFile({
-              url: this.$method.splitImgHost(item.certificatePath, true, 1000), //图片地址
+              url: this.$method.splitImgHost(item.certificatePath), //图片地址
               success: (res) => {
-                if (res.statusCode === 200 || res.statusCode === 400) {
+                console.log(
+                  res,
+                  res.tempFilePath,
+                  this.$method.splitImgHost(item.certificatePath)
+                );
+                if (res.statusCode === 200) {
                   uni.saveImageToPhotosAlbum({
                     filePath: res.tempFilePath,
                     success: function () {