<script>
	const bgAudioMannager = uni.getBackgroundAudioManager();
	const platform=uni.getSystemInfoSync().platform;
	const time='';
	import {newsRemind} from "@/utils/api/api.js";//获取商家消息
	import permision from "@/utils/permission.js";//App平台的授权判断封装js
	export default {
		onLaunch: function() {
			if(platform=='ios'){
				bgAudioMannager.title = '您已进入鹿马商家助手...';
				bgAudioMannager.singer = '小马将为你进行实时播报语音';
				bgAudioMannager.coverImgUrl = 'https://runimg.jxdsy.cn/bank/logo.png';
				bgAudioMannager.src = 'https://runimg.jxdsy.cn/bank/1703.mp3';
				bgAudioMannager.play();
				bgAudioMannager.onEnded((res)=>{ //自然播放完毕
					bgAudioMannager.pause(); //pause 暂停
				});
			}
			
			// #ifdef APP-PLUS	
			//app关闭默认的启动 方法关闭启动图。但是这个时间不能太晚，6s 超时后依旧会主动关闭。
			setTimeout(()=>{
				plus.navigator.closeSplashscreen();
			},100);
			
			uni.getNetworkType({ //查看是否有网络
			    success: function (res) {
					if(res.networkType=='none'){
						uni.showModal({
							title:'温馨提示',
							content:'检测到当前未打开网络,请打开网络',
							showCancel:false
						})
						
					}else if(res.networkType=='2g'||res.networkType=='3g'){
						uni.showModal({
							title:'温馨提示',
							content:'检测当前为'+res.networkType+'网络,网速较慢可能造成卡顿',
							showCancel:false
						})
					}
			    }
			});
			// #endif
		},
		computed: {
			userInfo(){
				return this.$store.state.userInfo;
			}
		},
		methods:{	
			hgServiceFun(){
				const pushLive = uni.requireNativePlugin('push-live');
				pushLive.startService({ //启动服务
					title: "鹿马播报",
					content: "鹿马商家助手正在进行实时播报...",
					mode: 1  //0省电模式 1流氓模式
				}, function(res) {
					
				});
				
				pushLive.isIgnoringBatteryOptimizations(function(res) { //检测是否限制后台运行
					if(res.flag){ //添加进去了
						pushLive.startPollingTask(30);//5
					}else{
						pushLive.requestIgnoreBatteryOptimizations(function(res) { // 申请后台运行（提高保活几率）
							
						}); 
					}
				})
			},
			async getIosLocation(type,pages){
				let that=this;
				if(pages!='index'){
					uni.showLoading({
						title:'请稍后...'
					})
				}
				let result = permision.judgeIosPermission('location');  //是否授权
				if(result){
					var cllocationManger = plus.ios.import("CLLocationManager");
					var status = cllocationManger.authorizationStatus(); 
					 //4 使用期间  3前后台都执行 0下次询问  2拒绝授权
					if(status==0){
						uni.getLocation({
							type: 'gcj02',
							success:(res)=> {
								that.$store.commit('horseLng',res.longitude)
								that.$store.commit('horseLat',res.latitude)
								that.getIosLocation(2,'');
							}
						});	
					}else if(status==2||status==4){
						uni.hideLoading();
						uni.showModal({
							title:'温馨提示',
							content:'需要您的同意，点击“始终”，打开位置后台运行权限',
							success:(res)=>{
								if(res.confirm){
									permision.gotoAppPermissionSetting(); //打开当前设置
								}
							}
						})
					}else if(status==3){
						uni.setStorageSync('dwstatus',status);
						if(pages!='index'){
							that.$api.msg('开启成功');
						}
						if (that.$store.state.Timer == '') {
							that.$store.commit('Timer',setInterval(()=>{
								uni.hideLoading();
								uni.getLocation({
									type: 'gcj02',
									success:(res)=> {
										that.$store.commit('horseLng',res.longitude)
										that.$store.commit('horseLat',res.latitude)
										that.getnewsRemind(type);
									}
								});	
							},5000));
						}else{
							clearInterval(that.$store.state.Timer);
							that.$store.commit('Timer','');
							
							that.$store.commit('Timer',setInterval(()=>{
								uni.hideLoading();
								uni.getLocation({
									type: 'gcj02',
									success:(res)=> {
										that.$store.commit('horseLng',res.longitude)
										that.$store.commit('horseLat',res.latitude)
										that.getnewsRemind(type);
									}
								});	
							},5000));
						}
					}
					
					plus.ios.deleteObject(cllocationManger);
					
				}else{  
					uni.hideLoading();
					uni.showModal({
						title:'温馨提示',
						content:'定位权限没有开启，是否去开启',
						success:(res)=>{
							if(res.confirm){
								permision.gotoAppPermissionSetting(); //打开当前设置
							}
						}
					})
				}
			},
			//获取 语音消息
			async getnewsRemind(type){
				// 语音消息
				var date = new Date();
				let hours = date.getHours();//当前 时间 小时
				
				if(this.$store.state.token!=""){//判断是否 已登录
					let cid = "";
					//#ifdef APP-PLUS
					cid =  plus.push.getClientInfo().clientid
					//#endif
					let res = await newsRemind({
						uid:this.$store.state.userInfo.user_id,
						cid:cid,
					});
					if(res.code==0){
						if(res.data.length!=0){
							let KJres = res.data.filter(item =>{ //快捷支付
								return item.type==3 || item.type==0
							})
							
							let PSres = res.data.filter(item => { //订单
								return item.type==4 || item.type==5 || item.type==6 || item.type==1
							})
							
							if(platform=='ios'){
								if(bgAudioMannager.paused){ //没有播放或者暂停了播放
									if(PSres.length!=0){
										bgAudioMannager.src = 'https://runimg.jxdsy.cn/bank/run.mp3';
										bgAudioMannager.seek({position:0});  
										bgAudioMannager.play();
										bgAudioMannager.onEnded((res)=>{ //自然播放完毕
											bgAudioMannager.pause(); //pause 暂停
										})
									}
									if(KJres.length!=0){   
										bgAudioMannager.src = 'https://runimg.jxdsy.cn/bank/run.mp3';
										bgAudioMannager.seek({position:0});
										bgAudioMannager.play();
										bgAudioMannager.onEnded((res)=>{ //自然播放完毕
											bgAudioMannager.pause(); //pause 暂停
										})
									}
								}
							}else{
								if(PSres.length!=0 && KJres.length!=0){
									let msgtext = `你有${PSres.length}条鹿马订单,请及时处理`;
									if(this.$store.state.isvoice){
										this.$store.dispatch('AudioVoice',msgtext).then((e) => {
											uni.vibrateLong();//震动 调用//400ms//长震动
											setTimeout(()=>{
												let payText =""
												KJres.map(item=>{
													payText = payText+","+ item.contents
												})
												if(this.$store.state.isvoice){
													this.$store.dispatch('AudioVoice',payText).then((e) => {
														uni.vibrateLong();//震动 调用//400ms//长震动
													})
												}
											},4000)
										})
									}
								}else{
									if(PSres.length!=0){
										let msgtext = `你有${PSres.length}条鹿马订单,请及时处理`;
										if(this.$store.state.isvoice){
											this.$store.dispatch('AudioVoice',msgtext).then((e) => {
												uni.vibrateLong();//震动 调用//400ms//长震动
											})
										}
									}
									
									
									if(KJres.length!=0){
										let payText =""
										
										KJres.map(item=>{
											payText = payText+","+ item.contents
										});
										if(this.$store.state.isvoice){
											this.$store.dispatch('AudioVoice',payText).then((e) => {
												uni.vibrateLong();//震动 调用//400ms//长震动
											})
										}
									}
								}
								
							}
						}
						if(type!=2){
							return true;
						}
						
					}
						
				}
			},
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
    @import "colorui/main.css";
    @import "colorui/icon.css";
    @import "./common/uni.css";
	
	@import './common/qiun.css';
	@import './style.css';//我的
</style>