<script>
	import {
		SET_SYSTEM_INFO,
		SET_PAGE_CONFIG
	} from './store/mutation-types'
	import {
		initUser
	} from './utils'
	import store from '@/store'
	import {
		SET_PID,
		SET_CHANNEL,
		SET_ADID,
		SET_CLICKID,
		SET_CALLBACK,
		SET_PAYINFO
	} from '@/store/mutation-types'
	export default {
		onLaunch: function() {
			const res = uni.getSystemInfoSync()
			this.$store.commit(SET_SYSTEM_INFO, res)

			this.updateVersion()

		},
		onShow: function(option) {
			// uni.login({
			//     scopes: 'auth_base',
			//     success: async (result) => {
			//         if (result.code) {
			//             this.$service.user.updateSession(result.code)
			//         }
			//     }
			// })
			this.$event.emit(this.$event.key.APP_SHOW)
			if (this.$common.isLogin()) {
				this.$service.user.info()
			}


			this.pageConfig('magic_luck')
			this.pageConfig('index_banner')
			this.pageConfig('index_icon')
			this.pageConfig('box_banner')
			this.pageConfig('super_ad')
			let page = this.$common.currentPage()
			if (page && page.$vm && page.$vm.poolIn) {
				page.$vm.poolIn()
			}
			if (option.query) {
				if (option.query.pid) {
					store.commit(SET_PID, option.query.pid)
				}
				if (option.query.channel) {
					store.commit(SET_CHANNEL, option.query.channel)
				}
				if (option.query.adid) {
					store.commit(SET_ADID, option.query.adid)
				}
				if (option.query.gdt_vid || option.query.qz_gdt) {
					store.commit(SET_CLICKID, option.query.gdt_vid || option.query.qz_gdt)
				}
				if (option.query.callback) {
					store.commit(SET_CALLBACK, option.query.callback)
				}
			}
			if (option.referrerInfo && option.referrerInfo.extraData && option.referrerInfo.extraData.action ==
				'epPay') {
				store.commit(SET_PAYINFO, option.referrerInfo.extraData)
			}
			if (this.$common.isLogin()) {
				this.$service.user.getHide()
			}
		},
		onHide: function() {
			this.$service.award.poolOut()
		},
		methods: {
			async updateVersion() {
				const local = await this.getLocalVersion()
				const res = await this.$service.base.getLatest()
				if (local !== res.version) {
					this.downloadPackage(res.upgradeUrl)
				}
			},
			getLocalVersion() {
				return new Promise((resolve, reject) => {
					plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
						const version = widgetInfo.version
						resolve(version)
					})
				}).catch(err => {
					console.err(err);
				})
			},
			downloadPackage(url) {
				
				var dtask = plus.downloader.createDownload(url, {}, function(res, status) {
					// 下载完成
					if (status === 200) {
						plus.runtime.install(res.filename)
					}
				});
				//dtask.add
				dtask.start()
			},
			async pageConfig(key) {
				const res = await this.$service.base.pageConfig(key)
				let pageConfig = this.$store.state.pageConfig || {}
				pageConfig[key] = res
				this.$store.commit(SET_PAGE_CONFIG, {
					...pageConfig
				})
			}
		}
	}
</script>

<style lang="scss">
	@import "@/uni_modules/uview-ui/index.scss";

	/*每个页面公共css */
	.wrapper {
		padding: 0 20rpx;
	}

	@import '@/assets/sass/global.scss';
	@import 'colorui/main.css';
	@import 'colorui/icon.css';
</style>