查看: 841|回复: 0

uniapp的异步思想代码参考

[复制链接]

748

主题

39

回帖

3907

积分

管理员

总舵主

积分
3907
QQ
发表于 2024-5-4 17:48:20 | 显示全部楼层 |阅读模式
showConfirm() {
                            this.type = 'showpopup';
                        },
                        hideConfirm() {
                            this.type = '';
                        },
                        async getLocation() {
                            // #ifdef APP-PLUS
                            let status = await this.checkPermission();
                            if (status !== 1) {
                                return;
                            }
                            // #endif
                            // #ifdef MP-WEIXIN || MP-TOUTIAO
                            let status = await this.getSetting();
                            if (status === 2) {
                                this.showConfirm();
                                return;
                            }
                            // #endif
                       
                            this.doGetLocation();
                        },
                        doGetLocation() {
                            uni.getLocation({
                                success: (res) => {
                                    this.hasLocation = true;
                                    this.location = formatLocation(res.longitude, res.latitude);
                                },
                                fail: (err) => {
                                    // #ifdef MP-BAIDU
                                    if (err.errCode === 202 || err.errCode === 10003) { // 202模拟器 10003真机 user deny
                                        this.showConfirm();
                                    }
                                    // #endif
                                    // #ifndef MP-BAIDU
                                    if (err.errMsg.indexOf("auth deny") >= 0) {
                                        uni.showToast({
                                            title: "访问位置被拒绝"
                                        })
                                    } else {
                                        uni.showToast({
                                            title: err.errMsg
                                        })
                                    }
                                    // #endif
                                }
                            })
                        },
                        getSetting: function() {
                            return new Promise((resolve, reject) => {
                                uni.getSetting({
                                    success: (res) => {
                                        if (res.authSetting['scope.userLocation'] === undefined) {
                                            resolve(0);
                                            return;
                                        }
                                        if (res.authSetting['scope.userLocation']) {
                                            resolve(1);
                                        } else {
                                            resolve(2);
                                        }
                                    }
                                });
                            });
                        },
                        openSetting: function() {
                            this.hideConfirm();
                            uni.openSetting({
                                success: (res) => {
                                    if (res.authSetting && res.authSetting['scope.userLocation']) {
                                        this.doGetLocation();
                                    }
                                },
                                fail: (err) => {}
                            })
                        },
                        async checkPermission() {
                            let status = permision.isIOS ? await permision.requestIOS('location') :
                                await permision.requestAndroid('android.permission.ACCESS_FINE_LOCATION');
                       
                            if (status === null || status === 1) {
                                status = 1;
                            } else if (status === 2) {
                                uni.showModal({
                                    content: "系统定位已关闭",
                                    confirmText: "确定",
                                    showCancel: false,
                                    success: function(res) {
                                    }
                                })
                            } else if (status.code) {
                                uni.showModal({
                                    content: status.message
                                })
                            } else {
                                uni.showModal({
                                    content: "需要定位权限",
                                    confirmText: "设置",
                                    success: function(res) {
                                        if (res.confirm) {
                                            permision.gotoAppSetting();
                                        }
                                    }
                                })
                            }
                       
                            return status;
                        },

道不同,不相为谋!理想要有,万一实现了呢?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注公众号

相关侵权、举报、投诉及建议等,请发 E-mail:2513533699@qq.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.|鲁ICP备19000917号|鲁公网安备37078302000320号

在本版发帖
关注公众号
QQ客服返回顶部