admin 发表于 2024-5-4 17:48:20

uniapp的异步思想代码参考

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;
                        },

页: [1]
查看完整版本: uniapp的异步思想代码参考