const app = getApp();
/**
 * @param type 位置类型
 *
 * - 0：默认，获取经纬度
 * - 1：获取经纬度和详细到区县级别的逆地理编码数据
 * - 2：获取经纬度和详细到街道级别的逆地理编码数据，不推荐使用
 * - 3：获取经纬度和详细到POI级别的逆地理编码
 */

function getLocationInfo(type = 2) {
  return new Promise((resolve, reject) => {
    const {
      regionData = {}
    } = app.globalData;

    if (regionData.cityAdcode) {
      resolve(regionData);
      return;
    }

    my.getLocation({
      type,

      success(res) {
        const {
          province = '',
          city = '',
          cityAdcode = '',
          district = '',
          latitude,
          longitude
        } = res;
        const street = res.streetNumber.street || '';
        const number = res.streetNumber.number || '';
        app.globalData.regionData = { ...regionData,
          province,
          city,
          cityAdcode,
          address: `${city}${district}${street}${number}`,
          latitude,
          longitude
        };
        resolve(app.globalData.regionData);
      },

      fail(e) {
        app.globalData.regionData = undefined;
        reject(e);
      }

    });
  });
}

export default getLocationInfo;