<script>
import { mapState, mapMutations, mapActions } from 'vuex';
import { GET_USER_INFO, HEALTH_TYPE_STATUS } from 'api/api';
import { showLoading, hideLoading, showToast, showModal } from 'utils/ui';
import { formatQuery } from 'utils/util';

export default {
  async onLaunch(options) {
    console.log('options: ', options);
    // #ifdef MP-WEIXIN
    uni.getSetting({
      success(res) {
        if (!res.authSetting['scope.userLocation']) {
          uni.authorize({
            scope: 'scope.userLocation',
            success() {},
          });
        }
      },
    });
    // #endif

    try {
      if (options.query && options.query.scene) {
        const query = formatQuery(decodeURIComponent(options.query.scene));
        if (options.path === 'pages/index/index' && query && query.d) {
          await this.getTokenByUserId({ params: { userId: query.d } });
          this.initCommon();
        } else {
          await this.login();
          this.initCommon();
          return;
        }
      } else {
        await this.login();
        this.initCommon();
      }
    } catch (error) {
      console.log('onLaunch error: ', error);
    }
  },

  computed: mapState('user', ['token']),

  methods: {
    ...mapActions('user', ['login', 'getUserInfo', 'getHealthTypeStatus', 'getTokenByUserId']),

    async initCommon() {
      const startTime = +this.$moment()
        .startOf('year')
        .format('x');
      const endTime = +this.$moment()
        .endOf('day')
        .format('x');
      const params = {
        param: {
          startTime,
          endTime,
          token: this.token,
        },
      };
      await this.getUserInfo(params);
      await this.getHealthTypeStatus();
    },
  },
};
</script>

<style lang="scss">
@import 'colorui/main.scss';
@import 'colorui/icon.scss';
@import 'colorui/animation.scss';
@import 'common/style/global';
@import 'common/style/iconfont';
</style>
