<script>
  import config from './config'
  import store from '@/store'
  import { getToken } from '@/utils/auth'
// const jpushModule = uni.requireNativePlugin('JG-JPush')
  import {updateUserProfile,findVersion} from "@/api/system/user.js"
  const baseUrl = config.baseUrl
  export default {
	  data(){
	  		  return{
	  			platform:'Android'  
	  		  }
	  },
    onLaunch: function() {
		// this.platform=uni.getSystemInfoSync().platform;
		// console.log(this.platform,1)
		this.initApp()
		// #ifdef APP
		var url=baseUrl;
		if(url.indexOf('https://qszs.qs163.cn')!=-1){
			this.getJGtuifn()
			this.onlineWgt(); //版本更新
		}else{
		}
		// #endif
		
	  // console.log(jpushModule)
    },
    methods: {
		//热更新
		// 在线跟新
		onlineWgt() {
			let that = this;
			plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
				that.$store.dispatch('SetwgtFn',widgetInfo.version).then(()=>{
				})
				that.comparisonVersionNo(widgetInfo.versionCode);
			});
			// that.comparisonVersionNo()
		},
		comparisonVersionNo(versionCode) {
			let that = this;
			// console.log("aaaaaaa===versionCode", versionCode);
			const parmas = {
				model: that.platform
			}
			findVersion(parmas).then(res => {
				if (res.code == "200") {
					let versionNos = res.data.code;
					console.log(versionNos,'versionNos')
					// console.log(res.data.path);
					let filePath = res.data.path;
					if (versionNos != versionCode) { //服务器返回1更新，0则不更新
						uni.showModal({
							title: '提示',
							content: '发现新版本，是否升级',
							success: function(res) {
								// console.log(widgetInfo.version);
								if (res.confirm) {
									console.log('用户点击确定');
									that.getProperty(filePath);
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						});
					}
				}
			}).catch(error=>{
				uni.hideLoading()
				console.log(error)
			})
		},
		getProperty(getProperty) {
			// console.log("bbbbbbbbb----------->", getProperty)
			let that = this;
			// 在线升级app/热更新
			that.showwri = true;
			const downloadTask = uni.downloadFile({ //下载文件
				url: baseUrl + getProperty,
				success: (downloadResult) => {
					// console.log(downloadResult);
					if (downloadResult.statusCode == 200) {
						// 更新升级
						plus.runtime.install(downloadResult.tempFilePath, {
							force: true
						}, function() {
							// console.log('install success...');
							that.showwri = false;
							plus.nativeUI.alert("应用资源更新完成！", function() {
								plus.runtime.restart();
							});
						}, function(e) {
							that.showwri = false;
							plus.nativeUI.alert("更新失败，请稍后再试");
						});
					}
				}
			});
			downloadTask.onProgressUpdate((res) => { //下载文件的进度
				that.totalBytesWritten = res.totalBytesWritten; //当前下载大小
				that.progress = res.progress; //当前下载比例
				that.totalBytesExpectedToWrite = res.totalBytesExpectedToWrite; //
			});
		},
		// 极光推送
		getJGtuifn(){ 
			//#ifdef APP-PLUS
				var jpushModule = uni.requireNativePlugin("JG-JPush");
				jpushModule.setLoggerEnable(true);
				// 初始化函数
				jpushModule.initJPushService();
				jpushModule.addConnectEventListener(result=>{
					let connectEnable = result.connectEnable
					// console.log("jpush连接", connectEnable)
				})
				jpushModule.getRegistrationID(result => {
					// console.log("注册ID.....",result)
					this.registerID = result.registerID;
					if(getToken()){
						var params={
							jgId:this.registerID,
						}
						updateUserProfile(params).then(res=>{
							if(rtes.code==200){
								console.log(res,9)
							}
						})
					}	
				})
				jpushModule.isPushStopped(result=>{
					let code = result.code
					console.log('连接状态回调',result)
				});
				// 设置别名
				jpushModule.setAlias({
					'alias' : 'coder',
					'sequence': 1
				})
				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('通知事件回调',result)
					// 推送一个本地通知
					jpushModule.addLocalNotification({
						messageID,
						title,
						content,
						extras
					})
				})
				jpushModule.addNotificationListener(result => {
				console.log(result)
				if (result.notificationEventType == "notificationOpened") {
				  //点击窗口通知栏推送的消息 跳转指定页面
					uni.reLaunch({
						url:'/pages/login'
					})
				}
 
			});
			//#endif 
		},
      // 初始化应用
      initApp() {
        // 初始化应用配置
        this.initConfig()
        // 检查用户登录状态
        //#ifdef H5
        this.checkLogin()
        //#endif
      },
      initConfig() {
        this.globalData.config = config
      },
      checkLogin() {
        if (!getToken()) {
          this.$tab.reLaunch('/pages/login') 
        }
      }
    }
  }
</script>

<style lang="scss">
	page{background-color: #ffffff;}
  @import '@/static/scss/index.scss';
  @import "@/static/style.css";
 
  .uni-tabbar .uni-tabbar__label{
  		font-size: 32rpx !important;
  	}
  	// tab图标
  	.uni-tabbar .uni-tabbar__icon {
  		width: 50rpx !important;
          height: 50rpx !important;
  	}
	/* #ifdef APP */
	/deep/ .uni-picker-container .uni-picker-header{background-color: #FA5F03;}
	/* #endif */
// .inform /deep/ .uni-input-input{text-align: right;font-size: 32rpx;color: #343434;}
// .inform /deep/ .uni-easyinput__placeholder-class{text-align: right;font-size: 32rpx !important;color: #808080;}

</style>
