// 播放音频
export function playAudio(type){
	const innerAudioContext = uni.createInnerAudioContext();
	// innerAudioContext.autoplay = true;
	innerAudioContext.src = '/static/'+type+'.mp3';
	innerAudioContext.onCanplay(res => {
		innerAudioContext.play()
	})
	innerAudioContext.onPlay(() => {
	  console.log('开始播放');
	});
	innerAudioContext.onError((res) => {
	  console.log(res.errCode);
	});
	innerAudioContext.onEnded(res => {
		innerAudioContext.destroy()
	})
}
/**
 * //设置应用版本号对应的缓存信息
 * @param {Object} currTimeStamp 当前获取的时间戳
 */
export const  setStorageForAppVersion = function(currTimeStamp){
	uni.setStorage({
		key: 'tip_version_update_time',
		data: currTimeStamp,
		success: function () {
			console.log('setStorage-success');
		}
	});
}
/**
 * 进行版本型号的比对 以及下载更新请求
 * @param {Object} server_version 服务器最新 应用版本号
 * @param {Object} curr_version 当前应用版本号
 * isQzgx 是否强制更新
 * callback 登录
 */
export const checkVersionToLoadUpdate = function(newVersion,type,callback){
	if(newVersion.version == '2001'||newVersion.version == '2002'){
		newVersion.version = 201
	}
	const curVer = getApp().globalData.version  // 当前版本信息
	console.log(newVersion)
	console.log(curVer)
	if(!newVersion || !curVer){
		callback()
		return
	}
	const curr_version = curVer && curVer.version ? curVer.version.replace(/\./g,'') : ''
	const server_version = newVersion && newVersion.version ? newVersion.version : '' // 最新版本信息
	const isQzgx = newVersion && newVersion.forceUpgrade != 1
	let isWifi = plus.networkinfo.getCurrentType()!=3 ? '有新的版本发布,检测到您目前非Wifi连接,' : '有新的版本发布,'
	let msg = isWifi + (!isQzgx ? '请立即更新?' : '是否立即更新版本?')
	// 下载app并安装
	let downloadApp = function(downloadApkUrl){
		uni.showLoading({
			title: '正在更新中...',
			mask: true,
		});
		var dtask = plus.downloader.createDownload( downloadApkUrl, {}, function ( d, status ) {
			// 下载完成  
			if ( status == 200 ) {   
				plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename),{},{},function(error){  
					uni.showToast({
						icon: 'none',
						title: '安装失败', 
						duration: 1500  
					});  
				})
				uni.hideLoading()
			} else {  
				 uni.showToast({  
					icon: 'none',
					title: '更新失败',
					duration: 1500  
				 });  
			}
		});  
		dtask.start();
	}
	// 打开ios 应用市场
	let openIosAppSotre = function(downloadUrl){
		//在App Store Connect中的App Store下的app信息,可找到appleId
		plus.runtime.launchApplication({
			action: downloadUrl
		}, function(e) {
			console.log('Open system default browser failed: ' + e.message);
		})
	}
	console.log(server_version,curr_version)
	if(Number(server_version) > Number(curr_version)){
		uni.showModal({
			title: msg,
			content: '更新内容:'+newVersion.upgradeContent,
			showCancel: isQzgx,
			confirmText:'立即更新',
			cancelText:'稍后进行',
			success: function (res) {
				if (res.confirm) {
					// 清空缓存token
					let tmp = uni.getStorageSync('lifeData');
					tmp = tmp ? tmp : {};
					tmp['vuex_token'] = '';
					uni.setStorageSync('lifeData', tmp);
					if(curVer.platform == 'android'){
						//设置 最新版本apk的下载链接
						downloadApp(newVersion.attachment);
					}else{
						openIosAppSotre(newVersion.downloadUrl)
					}
				} else if (res.cancel) {
					console.log('稍后更新');
					callback()
				}
			}
		});
		uni.hideLoading();
	}else{
		if(type){
			uni.showToast({
				icon: 'none',
				title: '已是最新版本',
				duration: 1500  
			}); 
		}else{
			callback()
		}
	}
}

// 处理数字千位分隔符  num 数值,decimal要保留的小数位数
export const toThousands = (num, decimal) => {
  if (decimal) {
    num = formatDecimal(num, decimal)
  }
  return num.toString().replace(/\d+/, function (n) { // 先提取整数部分
    return n.replace(/(\d)(?=(\d{3})+$)/g, function ($1) {
      return $1 + ','
    })
  })
}
// 保留decimal位小数(不四舍五入)  num 数值,decimal要保留的小数位数
export const formatDecimal = function (num, decimal) {
  num = num.toString()
  const index = num.indexOf('.')
  if (index !== -1) {
    num = num.substring(0, decimal + index + 1)
  } else {
    num = num.substring(0)
  }
  return parseFloat(num).toFixed(decimal)
}