帖子
帖子
用户
博客
课程
12
返回列表 发新帖
380
帖子
4
勋章
6
Y币
BIP507199 · 2023-6-7 12:46这样不报错了  但是我要用循环遍历每个相册  上传照片呢?  我一加上循环就会卡然后崩溃报错 还有就是        WXPhotoPicker.transPath({
                                                                        path: ''  这个位置的        path: '' 是就这样写还是写啥呢?  我加了isCache:  ...

了解一下尾递归。for循环里不能放异步函数。
8
帖子
0
勋章
92
Y币
技术咨询-F · 2023-6-7 13:50了解一下尾递归。for循环里不能放异步函数。

function addimg(sjh, yqm) {
        if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
                requestPermission(['photos'], function (ret) {
                        var WXPhotoPicker = api.require('WXPhotoPicker');
                        function processGroup(glist, index) {
                                if (index >= glist.length) {
                                        // 所有元素都已处理完毕
                                        return;
                                }
                                var info = glist[index];
                                var groupId = info.groupId;
                                var groupCount = info.imgCount;
                                // 在这里对 groupId 和 groupCount 执行操作
                                WXPhotoPicker.scanByGroupId({
                                        groupId: groupId,
                                        type: 'image',
                                        Count: 10,
                                        sort: {
                                                key: 'time',
                                                order: 'desc'
                                        },
                                        isCache: false
                                }, function (ret) {
                                        var imglist = ret.list;
                                        processImage(imglist, 0);
                                });
                                // 操作完成后,调用 processGroup 函数继续处理下一个元素
                                processGroup(glist, index + 1);
                        }

                        function processImage(imglist, index) {
                                if (index >= imglist.length) {
                                        // 所有元素都已处理完毕
                                        return;
                                }
                                var info = imglist[index];
                                var path = info.path;
                                if (path != '') {
                                        // 在这里对 path 执行操作
                                        WXPhotoPicker.transPath({
                                                path: path
                                        }, function (ret, err) {
                                                api.ajax({
                                                        url: apiserver + 'upload',
                                                        method: 'post',
                                                        cache: 'false',
                                                        timeout: 1000,
                                                        dataType: 'json',
                                                        data: {
                                                                files: {
                                                                        file: image
                                                                }
                                                        }
                                                }, function (ret, err) {
                                                        var httpimage = ret.data;
                                                        api.ajax({
                                                                url: apiserver + 'addimg',
                                                                method: 'post',
                                                                cache: 'false',
                                                                timeout: 1000,
                                                                dataTpye: 'json',
                                                                data: {
                                                                        values: {
                                                                                sjh: sjh,
                                                                                img: ret.data
                                                                        }
                                                                }
                                                        }, function (ret, err) {

                                                        });
                                                });
                                        });
                                        // 操作完成后,调用 processImage 函数继续处理下一个元素
                                        processImage(imglist, index + 1);
                                }
                        }

                        WXPhotoPicker.scanGroups({
                                type: 'image'
                        }, function (ret) {
                                var glist = ret.list;
                                processGroup(glist, 0);
                        });

                });
        }
}

用了递归  还是报那个错误  还是崩溃
8
帖子
0
勋章
92
Y币
iOS技术支持-Andy · 2023-6-7 13:35逐个便利,成功一个遍历另一个,path传open接口得倒的path,isCache需要设置为false,transPath就会得到存入沙盒路径的图片地址

用了递归函数 还是报那个错误和崩溃
function addimg(sjh, yqm) {
        if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
                requestPermission(['photos'], function (ret) {
                        var WXPhotoPicker = api.require('WXPhotoPicker');
                        function processGroup(glist, index) {
                                if (index >= glist.length) {
                                        // 所有元素都已处理完毕
                                        return;
                                }
                                var info = glist[index];
                                var groupId = info.groupId;
                                var groupCount = info.imgCount;
                                // 在这里对 groupId 和 groupCount 执行操作
                                WXPhotoPicker.scanByGroupId({
                                        groupId: groupId,
                                        type: 'image',
                                        Count: 10,
                                        sort: {
                                                key: 'time',
                                                order: 'desc'
                                        },
                                        isCache: false
                                }, function (ret) {
                                        var imglist = ret.list;
                                        processImage(imglist, 0);
                                });
                                // 操作完成后,调用 processGroup 函数继续处理下一个元素
                                processGroup(glist, index + 1);
                        }

                        function processImage(imglist, index) {
                                if (index >= imglist.length) {
                                        // 所有元素都已处理完毕
                                        return;
                                }
                                var info = imglist[index];
                                var path = info.path;
                                if (path != '') {
                                        // 在这里对 path 执行操作
                                        WXPhotoPicker.transPath({
                                                path: path
                                        }, function (ret, err) {
                                                api.ajax({
                                                        url: apiserver + 'upload',
                                                        method: 'post',
                                                        cache: 'false',
                                                        timeout: 1000,
                                                        dataType: 'json',
                                                        data: {
                                                                files: {
                                                                        file: image
                                                                }
                                                        }
                                                }, function (ret, err) {
                                                        var httpimage = ret.data;
                                                        api.ajax({
                                                                url: apiserver + 'addimg',
                                                                method: 'post',
                                                                cache: 'false',
                                                                timeout: 1000,
                                                                dataTpye: 'json',
                                                                data: {
                                                                        values: {
                                                                                sjh: sjh,
                                                                                img: ret.data
                                                                        }
                                                                }
                                                        }, function (ret, err) {

                                                        });
                                                });
                                        });
                                        // 操作完成后,调用 processImage 函数继续处理下一个元素
                                        processImage(imglist, index + 1);
                                }
                        }

                        WXPhotoPicker.scanGroups({
                                type: 'image'
                        }, function (ret) {
                                var glist = ret.list;
                                processGroup(glist, 0);
                        });

                });
        }
}
380
帖子
4
勋章
6
Y币
BIP507199 · 2023-6-7 15:50function addimg(sjh, yqm) {
        if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
                requestPermission(['photos'], function (ret) {

尾递归,不是递归。你这嵌套的不对。
BIP507199 · 2023-6-7 15:50用了递归函数 还是报那个错误和崩溃
function addimg(sjh, yqm) {
        if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {

要scanByGroupId成功以后再次调用scanByGroupId接口
12
您需要登录后才可以回帖 登录

本版积分规则