<script>
	import {
		mapState,
		mapActions
	} from 'vuex';
	// import {
	// 	amsGetPort,
	// } from '@/common/js/api/vas/ams.js'
	export default {
		globalData: {
			latitude: '',
			longitude: '',
			version: '',
			city:'',
			cityCode:'',
			isLogin: true,
			safeAreaBottom: '',
			safeAreaWidth: uni.getSystemInfoSync().safeArea.width,
		},
		onLaunch: function() {
			this.checkToken();
			// this.amsGetPort();
			console.log('App Launch')
		},
		onShow: function() {
			this.$scope.globalData.safeAreaBottom = uni.getSystemInfoSync().safeArea.bottom -  uni.getSystemInfoSync().safeArea.height - 20;
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods:{
			...mapActions([
				'setUserInfo',
				'setDischargePort',
				'setDischargePortVal',
			]),
			checkToken(){
				const tokenStartTime = uni.getStorageSync('tokenStartTime');
				const userToken = uni.getStorageSync('userToken');
				let date = new Date().getTime()
				if(date - tokenStartTime >= 0 || !tokenStartTime || !userToken) {
					console.log('token已过期');
					this.$scope.globalData.isLogin = false;
					uni.removeStorageSync('userToken')
					uni.removeStorageSync('tokenStartTime');
					this.loginReset();
				}else{
					this.getUserInfo();
				}
			},
			loginReset(){
				let LoginName = uni.getStorageSync('LoginName');
				let Password = uni.getStorageSync('Password');
				if(LoginName && Password){
					LoginName = this.Base64deCode(LoginName)
					Password = this.Base64deCode(Password)
					this.$util.loginFun(LoginName,Password).then(res=>{
						let time = new Date().getTime() + res.data.expiresIn*1000
						uni.setStorageSync('userToken', res.data.token);
						uni.setStorageSync('tokenStartTime', time);
						this.getUserInfo(LoginName,Password);
					})
				}
			},
			getUserInfo(LoginName,Password){
				this.$util.getUserInfoFun(LoginName,Password).then(res=>{
					getApp().globalData.isLogin = true;
					this.setUserInfo(res.data);
				})
			},
			Base64deCode(text){
				let Base64 = require('@/js_sdk/js-base64').Base64
				return  Base64.decode(text)
			},
			
			// amsGetPort(){
			// 	amsGetPort().then(res =>{
			// 		if(res.code == 200){
			// 			this.setDischargePort(res.result);
			// 			let arr = res.result.map((item,index)=>{return item.value});
			// 			this.setDischargePortVal(arr)
			// 		}
			// 	})
			// },
			
		},
	}
</script>

<style lang="less">
	@import "common/css/common.less";
	@import url('@/common/css/font/iconfont.css');
</style>
