<script>
	/**
	 * vuex管理登陆状态，具体可以参考官方登陆模板示例
	 */
	import Vue from 'vue'
	import { mapState,mapMutations} from 'vuex'
	import authorize from '@/common/config/authorize.js'
	export default {
		onLaunch: function() {
			let self = this
			//小程序热更新代码
			// if (wx.canIUse('getUpdateManager')) {
			// 	const updateManager = wx.getUpdateManager()
			// 	updateManager.onCheckForUpdate(function (res) {
			//         console.log('onCheckForUpdate====', res)
			//         // 请求完新版本信息的回调
			//         if (res.hasUpdate) {
			// 			console.log('res.hasUpdate====')
			// 			updateManager.onUpdateReady(function () {// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
			// 				self.$util.modal('更新提示','新版本已经准备好，是否重启应用？','确定','取消',true,() =>{
			// 					updateManager.applyUpdate()
			// 				})
			// 			})
			// 			updateManager.onUpdateFailed(function () {  // 新的版本下载失败
			// 				self.$util.modal('已经有新版本了哟~','新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~','确定','取消',true,() =>{
			// 					// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
			// 					updateManager.applyUpdate()
			// 				})
			// 			})
			//         }
			// 	})
			// }
			uni.getSystemInfo({
				success: function(e) {
					let modelmes = e.model
					console.log(e)
					if (modelmes.search('iPhone 11') !== -1 || modelmes.search('iPhone 11 Pro Max') !== -1 ||modelmes.search('iPhone X') != -1) { //XS,XR,XS MAX均可以适配
						self.$store.dispatch('setVariableFun',true)
					}else{
						self.$store.dispatch('setVariableFun',false)
					}
					// #ifndef MP
					Vue.prototype.StatusBar = e.statusBarHeight
					if (e.platform == 'android') {
						Vue.prototype.CustomBar = e.statusBarHeight + 50
						Vue.prototype.platformClass = true
					} else {
						Vue.prototype.CustomBar = e.statusBarHeight + 45
						Vue.prototype.platformClass = false
					};
					// #endif
			
					// #ifdef MP-WEIXIN || MP-QQ
					console.log(e.platform)
					if (e.platform == 'android') {
						Vue.prototype.platformClass = 'left'
						self.$store.dispatch('setVariableFun',false)
					} else {
						Vue.prototype.platformClass = 'center'
						self.$store.dispatch('setIsIphoneFun',true)
					}
					Vue.prototype.StatusBar = e.statusBarHeight
					Vue.prototype.fontSizeSetting = e.fontSizeSetting
					Vue.prototype.screenWidth = e.screenWidth
					let capsule = wx.getMenuButtonBoundingClientRect()
					Vue.prototype.capsule = capsule
					if (capsule) {
						Vue.prototype.Custom = capsule
						// Vue.prototype.capsuleSafe = uni.upx2px(750) - capsule.left + uni.upx2px(750) - capsule.right;
						Vue.prototype.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight
					} else {
						Vue.prototype.CustomBar = e.statusBarHeight + 50
					}
					// #endif		
					// #ifdef MP-ALIPAY
					Vue.prototype.StatusBar = e.statusBarHeight
					Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight
					// #endif
				}
			})
			this.refresh()
		},
		computed: {
			...mapState(['hasLogin'])
		},
		methods:{
			...mapMutations(['login','logout','isWxAuthorize']),
			async getWxAuthorize(){
				const wechatCode = await authorize.getCode('weixin')// 根据微信的code获取用户登录状态:1已登录过 -1未登录过
				const getUserInfo = await authorize.getUserInfo('weixin')
				this.UserService.UserLoginAuthApplets({ 
					code:wechatCode,
					encryptedData:getUserInfo.encryptedData,
					iv:getUserInfo.iv
				})
				.then(response =>{
					this.$store.commit('updateStatus',response.data)
					this.login(response.data)
					uni.setStorageSync('token',response.data.token)
					uni.setStorageSync('unionId',response.data.unionId)
				})
				.catch(error =>{
					this.logout(error.data)
					uni.setStorageSync('unionId',error.data.unionId)
					this.$store.commit('updateStatus',error.data)
					if(!this.hasLogin){
						if(uni.getStorageSync('isActivitySwitch')){
							const  lockTime = uni.getStorageSync('lockTime')
							const  eTime = this.diffTime(lockTime)
							this.$store.dispatch('setActivityFn',eTime)
						}else{
							this.$store.dispatch('setActivityFn',true)
						}
					}
				})
			},		
			refresh(){
				let TIME = (20*60)*1000
				setInterval(()=>{
					this.getWxAuthorize()
				},TIME)
			},
			diffTime(t){
				let date = Date.now()
				return (date -t) < 2*60*1000 ? false : true
			}
		},
		onShow: function() {
			
		},
		onHide: function() {
			console.log('App Hide')
		},
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "@/common/css/common.scss";
	@import "@/common/css/iconfont.scss";
	@import "@/common/css/style/thorui.css";
	@import "@/common/css/style/icon.css";

view,
scroll-view,
swiper,
swiper-item,
cover-view,
cover-image,
icon,
text,
rich-text,
progress,
button,
checkbox,
form,
input,
label,
radio,
slider,
switch,
textarea,
navigator,
audio,
camera,
image,
video {
	// box-sizing: border-box;
}
// page {
//   width: 100%;
//   height: 100%;
//   background: #fff;
//   filter: grayscale(100%);
//   filter: gray;
//   filter: progid:DXImageTransform.Microsoft.BasicImage(grayscale=1);
//   --toast-default-width: 114px;
// }
page{
	height: 100%;
	background-color: #FFFFFF;
}
/* 骨架屏替代方案 */
.Skeleton {
	background: #f3f3f3;
	padding: 20upx 0;
	border-radius: 8upx;
}

.clamp {
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
	display: block;
}

.common-hover {
	background: #f5f5f5;
}
/* input 样式 */
.input-placeholder {
	color: #999999;
}

.placeholder {
	color: #999999;
}
</style>
