function formatNumber(n) {
  const str = n.toString();
  return str[1] ? str : `0${str}`;
}

export function objToUrl(obj) {
  let uri = '';
  let keys = Object.keys(obj);
  keys.forEach(item => {
    uri += '&' + item + '=' + obj[item];
  });

  uri = uri.substr(1);

  return uri;
}

export function formatTime(date) {
  date = new Date(date.substr(0, 19));
  let year1 = date.getFullYear();
  let month1 = date.getMonth() + 1;
  let day1 = date.getDate();
  let hour1 = date.getHours();
  let minute1 = date.getMinutes();
  let second1 = date.getSeconds();

  let [year, month, day, hour, minute, second] = [year1, month1, day1, hour1, minute1, second1].map(formatNumber);

  return {
    year, month, day, hour, minute, second
  };
}

// 计算经纬度距离
export function getDistance(lat1, lng1, currentPosition) {
  const lat2 = currentPosition.lat;
  const lng2 = currentPosition.lng;
  if (lat1 && lng1 && lat2 && lng2) {
	const radLat1 = lat1 * Math.PI / 180.0;
	const radLat2 = lat2 * Math.PI / 180.0;
	const a = radLat1 - radLat2;
	const b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
	let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
	  Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
	s = s * 6378.137;// EARTH_RADIUS;
	s = Math.round(s * 10000) / 10000;
	return s;
  }
  return null;
}
// 打开位置
export function openLocation(item) {
  uni.openLocation({
	latitude: item.lat - 0,
	longitude: item.lng - 0,
	name: item.name,
	address: item.addrProvinceName + item.addrCityName + item.addrDistrictName + item.addrDetail
  });
}
// 评分
export function getStarNum(level) {
  console.log(level,'level')
  if(level){
	  const star = level.toString();
	  return star.indexOf('.') > -1 ? star : star + '.0';
  }
  return 0
}
// webview 打开新页面
export function openWebView(item){
	if (item.url) {
		// 外链
		if(item.url.indexOf("http")>=0){
			uni.navigateTo({
				url: '/pages/h5Page/index?src='+item.url
			})
		}else{
			uni.navigateTo({
				url: '/' + item.url
			})
		}
	}
}