<script>
	import {
		BASE_URL
	} from './config.js'
	import {
		useUserStore
	} from '@/store/userStore.js'

	export default {
		onLaunch: function() {
			console.log('App Launch')
			const user = useUserStore()
			uni.addInterceptor('request', {
				invoke(options) {
					if (!options.url.startsWith('http')) {
						options.url = BASE_URL + options.url
					}
					options.timeout = 20000
					options.header = {
						...options.header,
						Token: user.token
					}
					console.log(options)
				},
				success(res) {
					console.log(res)
					if (res.data.code === 2000) {
						user.updateToken('')
						uni.showToast({
							icon: 'none',
							title: '令牌过期，请重新登录'
						})
						setTimeout(() => {
							uni.reLaunch({
								url: "/pages/login/login",
							})
						}, 1000)
					}
				}
			})
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		}
	}
</script>

<style>
	/*每个页面公共css */
	/* 设置全局背景颜色 */
	page {
		background-color: #f4f4f4;
	}
</style>