Yansen 11 mēnešus atpakaļ
vecāks
revīzija
339d5e01ea
3 mainītis faili ar 47 papildinājumiem un 19 dzēšanām
  1. 27
    17
      src/components/Uploader/index.jsx
  2. 3
    2
      src/utils/authorize.js
  3. 17
    0
      src/utils/promise.js

+ 27
- 17
src/components/Uploader/index.jsx Parādīt failu

@@ -3,6 +3,7 @@ import Taro from "@tarojs/taro";
3 3
 import { View, Image, Video } from "@tarojs/components";
4 4
 import { Loading } from "@antmjs/vantui";
5 5
 import { uploadFileBase64, uploadFiles } from "@/utils/request";
6
+import { promise } from "@/utils/promise";
6 7
 import icon from "@/assets/icons/uploader.png";
7 8
 import closeIcon from "@/assets/icons/close.png";
8 9
 import style from "./style.modules.less";
@@ -22,23 +23,32 @@ export default (props) => {
22 23
     setLoading(true);
23 24
 
24 25
     // eslint-disable-next-line no-undef
25
-    if (IS_APP_CLIENT) {  
26
-      // eslint-disable-next-line no-undef
27
-      SmartCity.chooseImage({
28
-        count : 1 // 图片张数
29
-      },function(res){
30
-          console.log("--------上传图片---------", res);
31
-          const base64 = res[0].imageData;
32
-          const fileName = res[0].name;
33
-          const fileType = res[0].type;
34
-  
35
-          uploadFileBase64(base64, fileName, fileType).then(resp => {
36
-            setLoading(false);
37
-            onChange((value || []).concat(resp));
38
-          }).catch((err) => {
39
-            console.error(err);
40
-            setLoading(false);
41
-          });
26
+    if (IS_APP_CLIENT) {
27
+      const p = promise((resolve, reject) => {
28
+        // eslint-disable-next-line no-undef
29
+        SmartCity.chooseImage({
30
+          count : 1 // 图片张数
31
+        },function(res){
32
+            console.log("--------上传图片---------", res);
33
+            const base64 = res[0].imageData;
34
+            const fileName = res[0].name;
35
+            const fileType = res[0].type;
36
+    
37
+            uploadFileBase64(base64, fileName, fileType).then(resp => {
38
+              resolve(resp);
39
+              // onChange((value || []).concat(resp));
40
+            }).catch((err) => {
41
+              console.error(err);
42
+              reject(err);
43
+            });
44
+        });
45
+      }, 60 * 1000);
46
+
47
+      p.then((resp) => {
48
+        setLoading(false);
49
+        onChange((value || []).concat(resp));
50
+      }).catch(() => {
51
+        setLoading(false);
42 52
       });
43 53
     } else {
44 54
       Taro.chooseMedia({

+ 3
- 2
src/utils/authorize.js Parādīt failu

@@ -1,4 +1,5 @@
1 1
 import Taro from "@tarojs/taro";
2
+import { promise } from "./promise";
2 3
 import { translate } from "./map";
3 4
 
4 5
 export default function getAuthorize(scope) {
@@ -39,7 +40,7 @@ export const getLocation = () => {
39 40
       },
40 41
     }
41 42
 
42
-    return new Promise((resolve, reject) => {
43
+    return promise((resolve, reject) => {
43 44
       // eslint-disable-next-line no-undef
44 45
       new TMap.service.IPLocation().locate().then(res => {
45 46
         if (res.status == 0) {
@@ -58,7 +59,7 @@ export const getLocation = () => {
58 59
     
59 60
   }
60 61
 
61
-  return new Promise((resolve, reject) => {
62
+  return promise((resolve, reject) => {
62 63
     // eslint-disable-next-line no-undef
63 64
     SmartCity.getLocation(function(res){
64 65
       // 百度坐标

+ 17
- 0
src/utils/promise.js Parādīt failu

@@ -0,0 +1,17 @@
1
+
2
+/**
3
+ * 创建一个带有超时功能的Promise
4
+ * @param {*} fn 
5
+ * @param {*} timeout 
6
+ * @returns 
7
+ */
8
+export function promise(fn, timeout = 3000) {
9
+  return new Promise((resolve, reject) => {
10
+    const timer = setTimeout(() => {
11
+      clearTimeout(timer);
12
+      reject(new Error('Promise timeout'));
13
+    }, timeout);
14
+
15
+    fn(resolve, reject);
16
+  });
17
+}