<!-- com.encryptedIM.im   com.aiim.app.demo   6OQrPSweuBcdbhg -->
<script setup lang="ts">
import {onHide, onLaunch, onShow} from "@dcloudio/uni-app";
import {getCurrentInstance} from "vue";
import {useSysStore} from "@/store/sysStore";
import Auth from "@/api/Auth";
import {useWsStore} from "@/store/WsStore";
//import app_upgrade from '@/uni_modules/app-upgrade/js_sdk/index.js'
//import packageJson from '../package.json'
//import update from '../update.json'
//import dbApi from '@/api/DBApi';
import dbactApi from '@/api/DBactApi';
 
import {useUserStore} from "@/store/userStore";
import UserApi from '@/api/UserApi';
var jpushModule = uni.requireNativePlugin("JG-JPush")

const wsStore = useWsStore();
onLaunch(() => {
	plus.runtime.setBadgeNumber(0);
	plus.screen.lockOrientation('portrait-primary');
	setInterval(() => {
		uni.getNetworkType({
		  success: function (res) {
		    const networkType = res.networkType;
		    if (networkType === 'none') {
		      //console.log('networkType N')
			  if(Auth.getToken()!=''){
				  //Auth.logout();//无法连接网络，跳转登录页面
			  }
		    } else {
		      // 有网络
		      //console.log('networkType Y')
		    }
		  }
		});
	}, 5000);
  uni.getSystemInfo({
    success: function (e) {
      //@ts-ignore
      const {proxy} = getCurrentInstance();
      // #ifndef MP
      proxy.statusBar = e.statusBarHeight;
      if (e.platform == 'android') {
        //@ts-ignore
        proxy.customBar = e.statusBarHeight + 50;
      } else {
        //@ts-ignore
        proxy.customBar = e.statusBarHeight + 45;
      }
      ;
      // #endif

      // #ifdef MP-WEIXIN
      proxy.statusBar = e.statusBarHeight;
      //@ts-ignore
      let custom = wx.getMenuButtonBoundingClientRect();
      proxy.Custom = custom;
      //@ts-ignore
      proxy.customBar = custom.bottom + custom.top - e.statusBarHeight;
      // #endif

      // #ifdef MP-ALIPAY
      proxy.statusBar = e.statusBarHeight;
      //@ts-ignore
      proxy.customBar = e.statusBarHeight + e.titleBarHeight;
      // #endif
      // #ifdef APP-PLUS
      //自动更新 安卓
      if (e.platform === 'android') {
        // app_upgrade(async () => {
        //   //查询是否更新
        //   const {statusCode, data} = await uni.request({
        //     url: update.checkUrl,
        //     method: 'GET'
        //   })
        //   const version = packageJson.version
        //   if (statusCode === 200) {
        //     return {
        //       changelog: data.changelog,
        //       status: data.android > version ? 1 : 0, // 0 无新版本 | 1 有新版本
        //       path: data.url // 新apk地址
        //     }
        //   }
        // })
      }
      // #endif
    }
  })
  
  //极光推送
  console.log('App Launch')
  var platformType='a';
  Auth.setregisterID('');
  if(uni.getSystemInfoSync().platform == "ios"){
	  platformType='i';
  	// 请求定位权限
  	let locationServicesEnabled = jpushModule.locationServicesEnabled()
  	let locationAuthorizationStatus = jpushModule.getLocationAuthorizationStatus()
  	console.log('locationAuthorizationStatus',locationAuthorizationStatus)	
  	if (locationServicesEnabled == true && locationAuthorizationStatus < 3) {
  		jpushModule.requestLocationAuthorization((result)=>{
  			console.log('定位权限',result.status)
  		})
  	}
  	jpushModule.requestNotificationAuthorization((result)=>{
  		let status = result.status
		console.log('status',status)
  		if (status < 2) {
  			uni.showToast({
  				icon: 'none',
  				title: '您还没有打开通知权限',
  				duration: 3000
  			})
  		}
  	})
  }
  
  jpushModule.initJPushService();
  jpushModule.setLoggerEnable(true);
  jpushModule.addConnectEventListener(result=>{
	  console.log('result',result);
	  jpushModule.getRegistrationID(resultid=>{
	  		let registerID = resultid.registerID;
			console.log('registerID',registerID);
			Auth.setregisterID(registerID);
			if(Auth.getToken()&&Auth.getToken()!=''){
				const currentUser = useUserStore().getUser();
				currentUser.registrationId=registerID;
				var data={
					"userId": currentUser?.id,
					"registrationId":registerID,
					"platformType":platformType//a---(android),i---(ios)
				}
				UserApi.setUser(JSON.stringify(data)).then((res)=>{
					console.log('setuser',res);
				});
			}
	  })	
  	let connectEnable = result.connectEnable
  	uni.$emit('connectStatusChange',connectEnable)
  });
  console.log('22222');
  jpushModule.addNotificationListener(result=>{
  	let notificationEventType = result.notificationEventType
  	let messageID = result.messageID
  	let title = result.title
  	let content = result.content
  	let extras = result.extras
  	console.log('111',result);
  	// uni.showToast({
  	// 	icon: 'none',
  	// 	title: JSON.stringify(result),
  	// 	duration: 3000
  	// })
  });
  
  jpushModule.addCustomMessageListener(result=>{
  	let type = result.type
  	let messageType = result.messageType
  	let content = result.content
	console.log('222',JSON.stringify(result))
  	uni.showToast({
  		icon: 'none',
  		title: JSON.stringify(result),
  		duration: 3000
  	})
  })
  
  jpushModule.addLocalNotificationListener(result=>{
  	let messageID = result.messageID
  	let title = result.title
  	let content = result.content
  	let extras = result.extras
	console.log('333',JSON.stringify(result))
  	uni.showToast({
  		icon: 'none',
  		title: JSON.stringify(result),
  		duration: 3000
  	})
  })
  
  //jpushModule.setIsAllowedInMessagePop(true)
  // jpushModule.pullInMessage(result=>{
  // 	let code = result.code
  // 	console.log(code)
  // })
  
  jpushModule.addInMessageListener(result=>{
  	let eventType = result.eventType
  	let messageType = result.messageType
  	let content = result.content
  	console.log('inMessageListener',eventType,messageType,content)
  	console.log(JSON.stringify(result))
  	// uni.showToast({
  	// 	icon: 'none',
  	// 	title: JSON.stringify(result),
  	// 	duration: 3000
  	// })
  })
  
  
});
onShow(() => {
  if (Auth.getToken()) {
    if (wsStore.wsRequest.socket?.readyState === 1) {
      wsStore.checkStatus()
    } else {
      wsStore.close(true)
      wsStore.init()
    }
  }
  
  
  if(dbactApi.DBisOpen()){
	  console.log('dbactApi.DBisOpen')
	   if (Auth.getToken()){
		   dbactApi.createMSGtable();
	   }
  }else{
  	  dbactApi.openDb().then((res)=>{
  	  	  console.log('-----',res);
		  if (Auth.getToken()){
		  	dbactApi.createMSGtable();
		  }
  	  });
  	  
  }
  // if(dbApi.DBisOpen()){
	 //  dbApi.createMSGtable();
  // }else{
	 //  dbApi.openDb();
	 //  dbApi.createMSGtable();
  // }
  
  useSysStore().setShow(true)
});
onHide(() => {
	console.log('onHide')
  useSysStore().setShow(false)
  dbactApi.closeDB();
});

</script>
<style>
@import "colorui/main.css";
@import "colorui/icon.css";
@import "static/css/font.css";

page {
  height: 100%;
}
.tab-bar-mar {
  margin-bottom: 140upx
}
</style>
