<template>
    <div class="fruit"></div>
</template>

<script>
import {judgeUserType,queryOffLineQrCodeInfo} from '@/api/fruit.js'
export default {
    name: 'fruit',
    data() {
        return {
            userInfo: {},
            params: {
                mobile: '',
                qrCode: '',
                uid: '',
            },
            judgeUserType: {},
            queryOffLineQrCodeInfo: {},
        }
    },
    mounted() {
        window.getUserInfo = this.getUserInfo;
    },
    created() {
        if (window.android !== undefined) {
            const res = window.android.getUserInfo(); // 这是一个json字符串
            sessionStorage.setItem("ZJCD-UserInfo", res);
            this.userInfo = JSON.parse(res);
            this.params.mobile = this.userInfo.mobileNo;
            this.params.uid = this.userInfo.userId;
            this.params.qrCode = this.$route.query.code;
            if (this.params.qrCode) {
                this.init() 
            }
        } else if (window.webkit) {
            window.webkit && window.webkit.messageHandlers.getUserInfo.postMessage("getUserInfo");
        } else {
            this.$toast("请先登录镇合意app");
        }
       
    },
    methods: {
        init() {
            this.judgeUserType = {}
            judgeUserType(this.params).then((res) => {
                this.judgeUserType = res;
                let param = {
                    code: this.params.qrCode,
                    userType: this.judgeUserType.type
                }
                queryOffLineQrCodeInfo(param).then((res) => {
                    this.queryOffLineQrCodeInfo = res;
                    if(
                        this.judgeUserType.type == 1 &&
                        this.queryOffLineQrCodeInfo.useState == 0 &&
                        this.queryOffLineQrCodeInfo.qrCodeType == 0
                    ) {
                        this.$router.replace({
                            name: 'Fruit3',
                            query: this.queryOffLineQrCodeInfo,
                            params: {
                                code: this.judgeUserType.code,
                                qrCode: this.$route.query.code,
                            }
                        })
                    } else if(
                        this.queryOffLineQrCodeInfo.useState == 0 &&
                        this.queryOffLineQrCodeInfo.qrCodeType == 1
                    ) {
                        this.$router.replace({
                            name: 'Fruit4',
                            query: this.queryOffLineQrCodeInfo,
                            params: {
                                judgeUserType: this.judgeUserType,
                                qrCode: this.$route.query.code,
                            }
                        })
                    } else if(
                        this.judgeUserType.type == 1 &&
                        this.queryOffLineQrCodeInfo.useState == 0 &&
                        this.queryOffLineQrCodeInfo.qrCodeType == 2
                    ) {
                        this.$router.replace({
                            name: 'Fruit7',
                            query: this.queryOffLineQrCodeInfo,
                            params: {
                                code: this.judgeUserType.code,
                                qrCode: this.$route.query.code,
                            }
                        })
                    } else if(
                        this.queryOffLineQrCodeInfo.useState == 0 &&
                        this.queryOffLineQrCodeInfo.qrCodeType == 3
                    ) {
                        this.$router.replace({
                            name: 'Fruit4',
                            query: this.queryOffLineQrCodeInfo,
                            params: {
                                judgeUserType: this.judgeUserType,
                                qrCode: this.$route.query.code,
                            }
                        })
                    } else if (
                        this.judgeUserType.type == 0 &&
                        this.queryOffLineQrCodeInfo.useState == 0
                    ) {
                        this.$router.replace({
                            name: 'Fruit1',
                            query: this.queryOffLineQrCodeInfo,
                        })
                    } else if (
                        this.queryOffLineQrCodeInfo.useState == 1
                    ) {
                        this.$router.replace({
                            name: 'Fruit2',
                            query: this.queryOffLineQrCodeInfo,
                        })
                    }
                })
            })
        },
        getUserInfo(res) {
            sessionStorage.setItem("ZJCD-UserInfo", res);
            this.userInfo = JSON.parse(res);
            if(window.webkit){
                this.params.mobile = this.userInfo.mobileNo;
                this.params.uid = this.userInfo.userId;
                this.params.qrCode = this.$route.query.code;
                if (this.params.qrCode) {
                    this.init() 
                }
            }
           
        },
    },
}
</script>

<style lang="stylus" scoped>

</style>