// 格式化数字
function formatNumber(n) {
  const str = n.toString();
  return str[1] ? str : `0${str}`;
}
// object转url参数
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) {
  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: '/pagesA/h5Page/index?src='+item.url
			})
		}else{
			uni.navigateTo({
				url: '/' + item.url
			})
		}
	}
}
// 去登陆页面
export function toLogin(){
	uni.navigateTo({
		url: '/pages/login/login'
	})
}
// 去认证页面
export function toAuthStore(){
	uni.showModal({
		title: '提示',
		content: '您还未认证门店，暂无法继续使用！',
		confirmText: '去认证',
		success(ret) {
			if(ret.confirm){
				uni.navigateTo({
					url: '/pages/storeManage/storeAuth'
				})
			}
		}
	})
}
// vin 扫码
export function openCamera(hasLogin,sysUserFlag,maxNums){
	if(hasLogin){
		// 游客身份
		if(sysUserFlag){
			// 最多扫10次
			if(maxNums){
				uni.navigateTo({
					url: "/pages/scan-frame/scan-frame"
				})
			}else{
				uni.showModal({
					title: '提示',
					content: '个人用户扫描VIN仅限10次，您的次数已用完！',
					confirmText: '好的',
					showCancel: false,
					success(res) {}
				})
			}
		}else{
			// 非游客，直接扫
			uni.navigateTo({
				url: "/pages/scan-frame/scan-frame"
			})
		}
	}else{
		toLogin()
	}
}
// 单击底部tab
export function clickTab(row,hasLogin,isNoAuth,isMax){
	// 点击促销刷新列表
	if(row.text == '促销'){
		if(!hasLogin){
			toLogin()
			return false
		}
		// 游客去认证
		if(isNoAuth){
			toAuthStore()
			return false
		}
		uni.$emit('refashProm')
	}
	// 点击购物车
	if(row.text == '购物车'){
		if(!hasLogin){
			toLogin()
			return true
		}
		// 游客去认证
		if(isNoAuth){
			toAuthStore()
			return true
		}
		// 打开购物车页面
		uni.navigateTo({
			url:'/pagesB/cart/index',
			target: 'page'
		})
	}
	// 打开vin扫码页面
	if(row.text == '扫描VIN'){
		openCamera(hasLogin,isNoAuth,isMax)
	}else{
		return true
	}
}
// 单击banner
export function clickBannerList(row,hasLogin,isAuth){
	 // 红包
	 if(row.activeType == 'redPacket'){
	 	uni.navigateTo({url:"/pages/morePage/redPacket"})
	 }
	 // 图文
	 if(row.contentType == 'IMAGE_CONTENT'){
	 	uni.navigateTo({url:"/pagesA/activeDetail/index?sn="+row.bannerSn})
	 }
	 // 视频
	 if(row.contentType == 'VIDEO'){
	 	uni.navigateTo({url:"/pagesB/videos/detail?form=banner&&sn="+row.bannerSn})
	 }
	 // 链接
	 if(row.contentType == 'LINK'){
	 	openWebView({url:row.content})
	 }
	 // 促销活动
	 if(row.contentType == 'PROMO_LINK'){
		 if(hasLogin){
			 if(isAuth){
				 uni.navigateTo({
				 	url: '/pagesB/promoProduct?promoSn='+row.bizSn+'&title='+row.bannerName
				 })
			 }else{
				 // 游客去认证
				 toAuthStore()
			 }
		 }else{
			 toLogin()
		 }
	 }
}