
<script>
	import config from './config'
	import store from '@/store'
	import {
		getToken
	} from '@/utils/auth'
	import {getTenantInfo} from '@/api/login'

	export default {
		onLaunch: function() {
			this.initApp()
			
		},
		methods: {
			// 初始化应用
			initApp() {
				// 初始化应用配置
				this.initConfig()
				// 检查用户登录状态
				//#ifdef H5
				// this.checkLogin()
				//#endif
				
				//bug 切换租户id颜色没有变，得刷新才行
				let tenantId = ''
				let urls = window.location.href.split('?')
				if (urls.length > 1) {
					let strs = urls[urls.length - 1]
					let arr = strs.split('&')
					let arr2 = arr.filter(item => item.indexOf('tenantId') > -1)
					if (arr2.length > 0) {
						tenantId = arr2[0].split('=')[1]
					} else {
						tenantId = uni.getStorageSync('tenantId') || ''
					}
				} else {
					tenantId = uni.getStorageSync('tenantId') || ''
				}
				
				//刷新页面时更新用户信息
				if(getToken()){
					store.dispatch('GetInfo')
				}
				
				this.initThemeSetting(tenantId)
			},
			initConfig() {
				this.globalData.config = config
			},
			initThemeSetting(tenantId){
				if(!uni.$u.test.isEmpty(tenantId)){
					getTenantInfo(tenantId).then(res => {
						if (res.code == 200) {
							let themeColor = res.data.themeColor
							if(!uni.$u.test.isEmpty(themeColor)){
								document.documentElement.style.setProperty('--phone-theme-color', themeColor);
							}
						}
					}).catch(err => {
					
					})
				}
				
				
			},
			checkLogin() {
				if (!getToken()) {
					this.$tab.reLaunch('/pages/index')
				}
			}
		}
	}
</script>

<style lang="scss">
	/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
	@import "@/uni_modules/uview-ui/index.scss";
	@import '@/static/scss/index.scss';
	
	page{
		background-color: #ffffff;
		width: 100%;
		height: 100%;
	}
	
	:root{
		--phone-theme-color: #165DFF
	}
</style>