<script>
import { mapState, mapMutations, mapActions } from 'vuex';
import { GET_USER_INFO } from 'api/api';

export default {
  async onLaunch() {
    uni.getSetting({
      success(res) {
        if (!res.authSetting['scope.userLocation']) {
          uni.authorize({
            scope: 'scope.userLocation',
            success() {},
          });
        }
      },
    });

    await this.login();
    await this.getUserInfo();
    this.getSites();
  },

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

  methods: {
	...mapMutations('user', ['setUserInfo','setHealthCode']),
    ...mapActions('user', ['login']),
	...mapActions('site', ['getSites']),

	
	/**
	 * 查询个人信息
	 */
	async getUserInfo() {
		try {
			const { token } = this;
			const params = {
				param: {
					token
				}
			};
			const res = await this.$http.post(GET_USER_INFO, params);
			const {
				success,
				code,
				msg,
				data
			} = res.data;
			if (success && code === 200) {
				this.setUserInfo(data);
				if(data.healthCodeList && data.healthCodeList.length>0){
					const oldCode = data.healthCodeList[0].healthCode;
					this.setHealthCode(oldCode)
				}
				this.success = true;
			} else {
				uni.showToast({
					title: msg || '查询个人信息成功',
					icon: 'none'
				});
			}
		} catch (error) {
			console.log('error: ', error);
			if (error.msg) {
				uni.showToast({
					title: error.msg || '查询个人信息失败',
					icon: 'none'
				});
			}
		}
	},
	
  },
};
</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>
