<script>
  import config from './config'
  import store from '@/store'
  import { getToken, setToken } from '@/utils/auth'
  import  getPageParams  from '@/utils/tools.js'
  import { loginByUserName } from '@/api/login.js'
  const pageParams = getPageParams();
  export default {
    onLaunch: function() {
      this.initApp()
	  console.log(getPageParams())
    },
    methods: {
		//免登录
		 async freeLogin () {
			 let res = await loginByUserName({
				 userName: pageParams.username
			 });
			 if (res.code == 200) {
				setToken(res.data.token);
				this.$store.commit('SET_TOKEN', res.data.token);
				this.$store.commit('SET_USERINFO',res.data.user);
				uni.switchTab({
					url: '/pages/index'
				})
			 }
		 },
		
      // 初始化应用
      initApp() {
        // 初始化应用配置
        this.initConfig()
        // 检查用户登录状态
        //#ifdef H5
		console.log('pageParams.username', !pageParams.username)
		if (!pageParams.username) this.checkLogin();
		else this.freeLogin()
        //#endif
      },
      initConfig() {
        this.globalData.config = config
      },
      checkLogin() {
		  console.log('getToken()', getToken())
        if (!getToken()) {
          this.$tab.reLaunch('/pages/login') 
        } else {
			console.log(window.location.href)
			if (!window.location.href.split('#')[1]) {
				uni.switchTab({
					url: '/pages/index'
				})
			}
			
		}
      }
    }
  }
</script>

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