getpos.js 3.7 KB
// components/userinfo/getpos.js
let failCount=0;
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    hasAllowFail:{//强制获取定位
      type:Boolean,
      value:false,
    },
    hasAllowaAuthDeny:{//是否允许用户拒绝
      type:Boolean,
      value:true,
    },
    hasAllowLoading:{//包间查找中
      type:Boolean,
      value:true,
    },
  },

  /**
   * 组件的初始数据
   */
  data: {},
  attached(){
    //this.getPos()
  },
  /**
   * 组件的方法列表
   */
  methods: {
    getPos(fouce=false){
      console.log('getPos')
      if(getApp().location && getApp().location.latitude){
        return this.triggerEvent("pos",{
          latitude: getApp().location.latitude,
          longitude: getApp().location.longitude,
        });
      }
      this._getPos().then(res => {
        console.log('wx.getLocation.then', res);
        failCount=0;
        //this.triggerEvent("pos",res);
      }).catch((e) => {
        console.log('wx.getLocation.catch', failCount,e);
        if(this.data.hasAllowFail){
          this.triggerEvent("pos",{
            latitude: '',
            longitude: '',
          });
          if(this.data.hasAllowLoading) wx.hideLoading()
          return;
        }
        if(failCount>2){//定位失败
          failCount=0;
          wx.showModal({
            title:'定位失败',
            content:'请开启位置服务',
            showCancel:false,
            success:()=>{
              console.log('wx.showModal::success,给默认坐标');
              this.triggerEvent("pos",{
                //latitude: 39.6846701,
                //longitude: 118.1763001,
                latitude:'',
                longitude:'',
              });
              if(this.data.hasAllowLoading) wx.hideLoading()
            },
          })
          return;
        }
        if ('getLocation:fail auth deny' == e.errMsg
          || 'getLocation:fail:auth denied' == e.errMsg
          || 'getLocation:fail authorize no response' == e.errMsg) {
          failCount=0;
          if(this.data.hasAllowaAuthDeny && !fouce){
            this.triggerEvent("pos",{
              latitude: '',
              longitude: '',
            });
            if(this.data.hasAllowLoading) wx.hideLoading()
            return;
          }
          this.selectComponent('#confirm').options({
            header: '定位失败',
            body: '请开启定位,为您查询附近餐厅。'
          }).show();
        } else {
          failCount=failCount+1;

          setTimeout(() => {
            this.getPos()
          }, 500)
        }
      })
    },
    _getPos(){
      return new Promise((success, fail) => {
        let rex={latitude:'',longitude:''};
        if(this.data.hasAllowLoading){
          wx.showLoading({
            title: '包间查找中',
          })
        }
        let hasSuccess=false;
        wx.getLocation({
          type: 'gcj02',
          success: (res) => {
            hasSuccess=true;
            rex=res;
            this.triggerEvent("pos",JSON.parse(JSON.stringify(res)));
            getApp().location=res;
            success(res)
          },
          fail: (res) => {
            fail(res)
          },
          complete: (res) => {
            console.log('wx.getLocation', res)
          }
        });
        /*
        setTimeout(()=>{
          fail({errMsg:'getLocation:fail auth deny'})
        },3000)
        */
        setTimeout(()=>{
          if(!hasSuccess){
            success(rex)
            if(this.data.hasAllowLoading) wx.hideLoading()
          }
        },2000)
      })
    },
    confirmEvent(e){
      console.log('confirmEvent.type',e.detail.type)
      if(e.detail.type=='confirm') {
        wx.openSetting()
      }
    },
  }
})