调用imgseccheck云函数超时,且会阻塞异步函数
发布于 5 年前 作者 wei57 9885 次浏览 来自 官方Issues

华为P9'system': 'Android 8.0.0', 'version': '7.0.8', 'brand': 'HUAWEI', 'model': 'EVA-DL00', 'SDKVersion': '2.9.2', 'platform': 'android'。图片审核接口调用很慢,经常超时,超时结果返回后还会阻塞异步函数的调用。比如demo代码片段中超时后catch中的wx.hideloading一直不能完成。

下面是我根据实际问题做的demo,问题在于catch后wx.hideloading不生效。ios没问题。

Page({

    data: { },

    onLoad() {},

    getFileBuffer(img) {

        return new Promise((resolvereject=> {

            wx.getFileSystemManager().readFile({

                filePath: img,

                success: (res=> {

                    resolve(res.data);

                },

                fail: (err=> {

                    reject(err);

                }

            });

        });

    },

    checkImgPromise(ret) {

        return new Promise((resolvereject=> {

            const file = ret;

            wx.cloud.callFunction({

                name: 'checkImg',

                data: {

                    file

                }

            }).then((red=> {

                if (red.result.errCode === 0) {

                    resolve(red);

                } else if (red.result.errCode === 87014) {

                    wx.hideLoading();

                    reject(red.result.errMsg);

                }

            }).catch((err=> {

                if (err.errCode === -401002 || err.errCode === -404010) {

                    resolve(err);

                } else {

                    reject(err);

                }

            });

        });

    },

    chooseimg() {

        const that = this;

        wx.chooseImage({

            count: 1,

            sizeType: ['compressed'],

            sourceType: ['camera''album'],

            success(res) {

                that.uploadImg(res.tempFilePaths[0]);

            },

            fail() {

            }

        });

    },

    uploadImg(imgPath) {

        const that = this;

        wx.showLoading({

            title: '图片上传中...',

            mask: true

        });

        wx.getImageInfo({

            src: imgPath,

            success() {

                that.getFileBuffer(imgPath)

                    .then((ret=> {

                        that.checkImgPromise(ret)

                            .then((rer=> {

                                wx.hideLoading();

                                console.log(rer);

                            })

                            .catch((rej=> {

                                wx.hideLoading();

                                // 重点在于代码执行后执行catch时,console能执行,hideloading或做一些其它异步的事情会失效

                                console.log(rej);

                            });

                    })

            }

        });

    },

});



2 回复

你代码一直使用promise   当然会出现异步问题了  (大佬  你代码排个版  别人也好回答你的问题)

这代码好歹排个版

回到顶部