vue前端A请求完毕后执行B请求Promise的写法

自己项目中写的同步请求,笔记下。

// 检查是否开启位置信息权限
      checkOpenPermission() {
    
     //检查权限
        uni.showLoading({
    
    
            title: '加载中',
            mask: true
        })
        Promise.resolve().then(res => {
    
     // 第一步  定位权限
          return new Promise((resolve, reject) => {
    
    
            let result = WaPermision.checkSystemEnableLocation();
            if(!result) {
    
    
              uni.showModal({
    
    
                  title: '提示',
                  content: '请打开定位服务功能',
                showCancel: false,
                  success() {
    
    
                    var main = plus.android.runtimeMainActivity();
                    var Intent = plus.android.importClass('android.content.Intent');
                    var Settings = plus.android.importClass('android.provider.Settings');
                    var intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    main.startActivity(intent); // 打开系统设置GPS服务页面
                  }
              });
              reject()
            }else {
    
    
              console.log('已开启定位服务功能');
              resolve()
            }
          })
        }).then(() => {
    
    
          return new Promise((resolve, reject) => {
    
    // 第二步  相机权限
            WaPermision.requestAndroidPermission('android.permission.CAMERA').then((res=>{
    
    
                if (res == 1) {
    
    
                    console.log('已获得授权')
                    resolve()
                } else if (res == 0) {
    
    
                    console.log('未授权')
                    uni.showModal({
    
    
                      title: '提示',
                      content: '请开启相机权限服务!',
                      showCancel: false,
                      success() {
    
    
                        WaPermision.gotoAppPermissionSetting()
                      }
                    });
                    reject()
                } else {
    
    
                    console.log('被永久拒绝权限')
                }
            }))
          })
        }).then(() => {
    
    
          return new Promise((resolve, reject) => {
    
    // 第三步 获取当前定位的经纬度
            let that = this
            uni.getLocation({
    
    
							type: 'gcj02',
              geocode: true,
              isHighAccuracy: true,
              success(res) {
    
    
                let {
    
     latitude, longitude } = res
                let x = longitude
                let y = latitude
                let x_pi = (3.14159265358979324 * 3000.0) / 180.0
                let z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi)
                let theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi)
                let lngs = z * Math.cos(theta) + 0.0065
                let lats = z * Math.sin(theta) + 0.006
                that.longitude = lngs
								that.latitude = lats
                resolve()
                console.log('获取经纬度成功',lngs,lats)
              },fail: function () {
    
    
					uni.showToast({
    
    
						title: '获取地址失败,将导致部分功能不可用',
						icon:'none'
					});
					reject()
				}
			})
          })
        }).then(() => {
    
     // 第四步 定位位置校验
            let that = this
            let data = {
    
    
              longitude:that.longitude,
              latitude:that.latitude
            }
			checkScope(data).then(res => {
    
    
				if(res.data){
    
    
                	uni.hideLoading()
                	this.$tab.navigateTo('/pages/checking/index')
					resolve()
					console.log('经纬度校验成功')
				}else{
    
    
	                uni.hideLoading()
	                uni.showModal({
    
    
	                  title: '温馨提示',
	                  content: '您未处于考勤范围内,请打卡手机定位尝试更换位置或联系管理员设置考勤范围',
	                  showCancel: false,
	                    success() {
    
    
	                      console.log('确定')
	                    }
	                });
					this.$modal.showToast(res.msg)
					reject()
				}
			})
        })
      },

猜你喜欢

转载自blog.csdn.net/qq_43061933/article/details/131416979