<script>
	//#ifdef APP-PLUS
	import skJGPush from '@/libs/jgPush.js'
	//#endif
	import {openMessagePage} from '@/libs/tools.js'
	export default {
		globalData: {  
			baseUrl: '',
			token: '',
			changeOrg:'',
			version: '', // 当前版本号
			buildType: 'pro', // 打包环境对应类型，pro 生产 uat 预发布 dev 本地开发
			envTips: '', // 环境文字提示
			theme: 'default', // 主题，default
		},
		onLaunch: function() {
			console.log('App launch')
			const App = getApp({ allowDefault: true })
			App.globalData.token = this.$store.state.vuex_token;
			App.globalData.changeOrg = this.$store.state.vuex_changeOrg;
			const theme = App.globalData.theme
			const envText = {pro:'生产环境',dev:'开发环境',uat:'预发布环境'}
			let buildType = App.globalData.buildType
			let baseUrl = this.$config(buildType+'_URL')
			App.globalData.baseUrl = baseUrl
			App.globalData.envTips = envText[buildType]
			this.$store.state.vuex_theme = App.globalData.theme
			this.$store.state.vuex_tempData = ''
			
			// 消息推送
			//#ifdef APP-PLUS
			let bgAudioManager = uni.getBackgroundAudioManager();
			if(uni.getSystemInfoSync().platform == "ios"){
				// 请求定位权限
				let locationServicesEnabled = skJGPush.locationServicesEnabled()
				let locationAuthorizationStatus = skJGPush.getLocationAuthorizationStatus()
				console.log('locationAuthorizationStatus',locationAuthorizationStatus)	
				if (locationServicesEnabled == true && locationAuthorizationStatus < 3) {
					skJGPush.requestLocationAuthorization((result)=>{
						console.log('定位权限',result.status)
					})
				}
				skJGPush.requestNotificationAuthorization((result)=>{
					let status = result.status
					console.log(result,'通知权限')
					if (status < 2) {
						uni.showModal({
							title: '提示',
							content: '您未打开通知权限，应用将无法收到消息通知，确定开启通知权限吗？',
							success: function (res) {
								if (res.confirm) {
									skJGPush.openSettingsForNotification()
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						});
					}
				})
			}else{
				// 极光推送-init
				skJGPush.initJPushService()
			}
			 
			// 极光推送-打开debug
			skJGPush.openDebug()
			skJGPush.initCrashHandler()
			 
			// 极光推送-获取rid
			skJGPush.getRegistrationID(result => {
				console.log('【业务getRegistrationID】', result)
			})
			
			// 极光推送-通知事件回调
			skJGPush.addNotificationListener(result => {
				console.log('【业务addNotificationListener】', result)
				// 点击通知
				if(result.notificationEventType == 'notificationOpened'){
					this.setRead(result)
				}else if(result.notificationEventType == 'notificationArrived'){
					// 安卓铃声
					if(bgAudioManager&&uni.getSystemInfoSync().platform != "ios"){
						bgAudioManager.src = '/static/notes.mp3';
						bgAudioManager.play()
					}else{
						uni.vibrateLong({
							success: function () {
								console.log('vibrateLong success');
							}
						});
					}
				}else{
					// 清空角标
					skJGPush.setBadge(0)
				}
			})
			
			// 极光推送-标签别名事件回调
			skJGPush.addTagAliasListener(result => {
				console.log('【业务addTagAliasListener】', result)
			})	
			
			// 连接状态
			skJGPush.addConnectEventListener(result => {
				console.log('【连接状态】', result)
			})
		//#endif
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods:{
			async setRead(item){
				const api = await import("@/api/user.js")
				const token = getApp({ allowDefault: true }).globalData.token
				if(token){
					api.setReadNotice({'noticeId': item.extras.noticeId}).then(res => {
						console.log(res,'setRead')                                                                                     
						openMessagePage(item)
					})
				}else{
					uni.$emit("openMessage",item)
				}
			},
		}
	}
</script>

<style lang="scss">
	@import "./static/iconfont/iconfont.css";
	page{
		background-color: #F8F8F8;
	}
</style>
