1. 程式人生 > 實用技巧 >小程式的首次登陸 小程式的生命週期開始 授權地理位置的頁面 js 直接呼叫微信官方

小程式的首次登陸 小程式的生命週期開始 授權地理位置的頁面 js 直接呼叫微信官方

//app.js
var QQMapWX = require('utils/qqmap-wx-jssdk.js');
var qqmapsdk;
App({
  onLaunch: function () {
    // 例項化API核心類
    qqmapsdk = new QQMapWX({
      key: 'TCCBZ-J67W4-FWTUL-DQEEN-FHM2K-3CBGZ'
    });
    // 獲取定位
    this.getLocation(function () { });
    // 初始化慧眼實名核身元件
    const Verify = require('/verify_mpsdk/main');
    Verify.init();
  },

  /**
   * 全域性變數
   */
  globalData: {
    // serverUrl: 'https://www.xpms.cn/mini',
    serverUrl: 'https://192.168.1.20:8080/mini',
    uploadUrl: 'https://192.168.1.5:9090/mini/upload',
    imageUrl: 'https://192.168.1.20:8080/file/mini',
    userInfo: {
      address: {}
    },
    servicePhone: '400 0719 828',
    hotel_group_id: 1
  },
  //獲取使用者地理位置
  getLocation: function (callBack) {
    var that = this;

    wx.getLocation({
      success: function (res) {
        //2、根據座標獲取當前位置名稱,顯示在頂部:騰訊地圖逆地址解析
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: function (addressRes) {
            that.globalData.userInfo.address.nation = addressRes.result.address_component.nation;
            that.globalData.userInfo.address.province = addressRes.result.address_component.province;
            that.globalData.userInfo.address.city = addressRes.result.address_component.city;
            that.globalData.userInfo.address.district = addressRes.result.address_component.district;
            that.globalData.userInfo.address.street = addressRes.result.address_component.street;
            that.globalData.userInfo.address.street_number = addressRes.result.address_component.street_number;
            that.globalData.userInfo.address.lat = addressRes.result.location.lat;
            that.globalData.userInfo.address.lng = addressRes.result.location.lng;
            that.globalData.userInfo.address.address = addressRes.result.address;
            callBack();
          }
        })
      },
      fail: function () {
        wx.getSetting({
          success: function (res) {
            var statu = res.authSetting;
            if (!statu['scope.userLocation']) {
              wx.showModal({
                title: '是否授權當前位置',
                content: '需要獲取您的地理位置,請確認授權,否則地圖功能將無法使用',
                success: function (tip) {
                  if (tip.confirm) {
                    wx.openSetting({
                      success: function (data) {
                        if (data.authSetting["scope.userLocation"] === true) {
                          //授權成功之後,再呼叫chooseLocation選擇地方
                          wx.getLocation({
                            success: function (res) {
                              //2、根據座標獲取當前位置名稱,顯示在頂部:騰訊地圖逆地址解析
                              qqmapsdk.reverseGeocoder({
                                location: {
                                  latitude: res.latitude,
                                  longitude: res.longitude
                                },
                                success: function (addressRes) {
                                  that.globalData.userInfo.address.nation = addressRes.result.address_component.nation;
                                  that.globalData.userInfo.address.province = addressRes.result.address_component.province;
                                  that.globalData.userInfo.address.city = addressRes.result.address_component.city;
                                  that.globalData.userInfo.address.district = addressRes.result.address_component.district;
                                  that.globalData.userInfo.address.street = addressRes.result.address_component.street;
                                  that.globalData.userInfo.address.street_number = addressRes.result.address_component.street_number;
                                  that.globalData.userInfo.address.lat = addressRes.result.location.lat;
                                  that.globalData.userInfo.address.lng = addressRes.result.location.lng;
                                  that.globalData.userInfo.address.address = addressRes.result.address;
                                  callBack();
                                }
                              })
                            },
                          })
                        } else {
                          wx.showToast({
                            title: '授權失敗',
                            icon: 'none',
                            duration: 1000
                          })
                        }
                      }
                    })
                  }
                }
              })
            }
          },
          fail: function (res) {
            wx.showToast({
              title: '呼叫授權視窗失敗',
              icon: 'none',
              duration: 1000
            })
          }
        })
      }
    })
  },
})