<script>
  import config from './config'
  import store from '@/store'
  import { getToken } from '@/utils/auth'
import {updateUserProfile,findVersion} from "@/api/system/user.js"
  const baseUrl = config.baseUrl
  export default {
	data(){
		return{
			platform:'Android'  
		}
	},
    onLaunch: function() {
		// #ifdef APP
		this.onlineWgt(); //版本更新
		// #endif
		this.initApp()
    },
    methods: {
		//极光推送
		// this.getJGtuifn()
		// 在线跟新
		onlineWgt() {
			let that = this;
			plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
				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 => {
				console.log(res)  
				if (res.code == "200") {
					let versionNos = res.data.code;
					// console.log(versionNos,'versionNos')
					let filePath = res.data.path;
					if (Number(versionNos) > Number(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;
			// console.log(baseUrl + getProperty)
			// 在线升级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>
/deep/ .uni-forms-item__error{left:auto !important;right: 0;margin-top: 10rpx;z-index: 1;}
 /deep/ .uniui-calendar::before{display: none;}
/deep/ .uni-date__x-input{text-align: right;color: #272727;}
/deep/ .uni-easyinput__content-textarea{margin: 0;}
</style>
<style lang="scss">
@import "@/static/style.css";
  @import '@/static/scss/index.scss'
</style>
