export function downloadFile({url}) {
  return new Promise((resolve, reject) => {
      wx.downloadFile({
          url,
          success: resolve,
          fail: reject
      });
  })
}

export function getQueryString(url, name) {
  let reg = new RegExp('(^|/?|&)' + name + '=([^&]*)(&|$)', 'i');
  let r = url.substr(1).match(reg);
  if (r != null) {
      return unescape(r[2]);
  }
  return null;
}

export function randomString(e) {
  for (var t = "", i = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", n = 0; n < e; n++)
    t += i.charAt(Math.floor(Math.random() * i.length));
  return t
}

export function isPhoneX () {
  return new Promise((resolve) => {
      let screenHeight = wx.getSystemInfoSync().screenHeight
      let windowHeight = wx.getSystemInfoSync().windowHeight
      let bottom = wx.getSystemInfoSync().safeArea.bottom
      
      resolve(screenHeight != bottom || windowHeight > 750)
  })
}

export function getLocation () {
  return new Promise ( (resolve, reject) => {
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
          var latitude = res.latitude;
          var longitude = res.longitude;
          getApp().globalData.qqmapsdk.reverseGeocoder({
              location: {
                  latitude: latitude,
                  longitude: longitude
              },
              success: function (res) {
                  var ad_info = res.result.ad_info;
                  let city = ad_info.city.replace('市', '');
                  resolve(city)
              },
              fail:function (res) {
                  wx.showModal({
                      title: '定位失败',
                  });
              }
          });
      },
      fail(err) {
          wx.showModal({
              title: '定位失败，请手动选择城市',
          });
      }
  })
})
}