export default {
    methods:{
        initEvent() {

            // #ifdef APP-PLUS
            uni.onNativeEventReceive((event, data)=>{
                if(event === 'getTenantList'){
                    this.$store.commit("tenanList",JSON.parse(data))
                    // this.$store.tenanList = JSON.parse(data);
                }else if(event === 'encryptToken'){
                    // this.token = data;
                    uni.setStorageSync('encryptToken', data);
                    this.login()
                }
            })
            // #endif
        },
        getTenanList(){
            // 向宿主App发送事件
            uni.sendNativeEvent('getTenantList', {}, ret => {})
        },
        encryptToken(){
            const active = this.list[this.selectedIndex];
            uni.sendNativeEvent('encryptToken',JSON.stringify(
                {
                    globalId: active.globalUserId,
                    tenantId: active.tenantId,
                    subId: active.id,
                }
            ), ret => {})
        },
        triggerSelectItem(item){
            this.selectedIndex = item;
        },
        login(){
            // 核对
            this.$http(
                "auth.checkSecretKey",
                uni.getStorageSync('encryptToken')
            ).then(res =>{
                let {
                    code, data
                } = res;
                if(code === 200){
                    let {
                        userId,      // 用户id
                        enterpriseId // 企业id
                    } = data;
                    this.userData = data;
                    uni.setStorageSync('tenantId', data.tenantId);
                    if(userId && enterpriseId){
                        this.createToken(data);
                    }else{
                        this.toRegister();
                    }
                }
            })
        },
        // 跳转到注册
        toRegister(){
            let {userId,enterpriseId,subId,tenantId} = this.userData;
            this.$Router.push({
                path: "/pages/user/register/stepSelectType",
                query:{
                    pcUserId: userId || "",
                    pcBasicsId: enterpriseId || "",
                    tenantUserId:subId,
                    tenantId,
                }
            })
        },
        // 跳转到首页
        toIndexPage(){
            this.$Router.push({
                path: "/pages/home/index"
            })
        },
        checkStatus(data){
            this.$http(
                "user.checkMagStatus",{
                    pcUserId: data.userId,
                    pcBasicsId: data.enterpriseId,
                }).then(res =>{
                let { code ,data} = res;
                if(code === 200){
                    // 1-面料供应商, 2-品牌商, 3-设计机构, 4-检测机构
                    let { identityType} = data.userLastIdentity;
                    if(identityType){
                        this.assignType = identityType;
                        this.filterStatus(data)
                    }
                }
            })
        },
        filterStatus(data){
            // 0-未完善 1-已完善
            let{
                isBrandOwner,   // 我要选款
                isDesignAgency, // 完善设计机构信息
                isFabricSupplier, //完善面料供应商信息
                isTestingFacility, // 完善检测机构信息
            } = data;
            let { assignType} = this;
            switch (assignType){
                case "1":
                    isFabricSupplier  ? this.toIndexPage() : this.toRegister();
                    return ;
                case "2":
                    isBrandOwner  ? this.toIndexPage() : this.toRegister();
                    return ;
                case "3":
                    isDesignAgency   ? this.toIndexPage() : this.toRegister();
                    return ;
                case "4":
                    isTestingFacility ? this.toIndexPage() : this.toRegister();
                    return ;
                default:
                    this.toRegister();
                    return;
            }
        },
        createToken(data){

            this.$http("auth.createTokenByApp" ,{
                ...{
                    identityType: uni.getStorageSync("identityType")
                },
                ...data,
            }).then(res =>{
                let {
                    code, data
                } = res;
                if(code === 200){
                    uni.setStorageSync('token', data.accessToken);
                    uni.setStorageSync('pcUserId', data.userId);
                    uni.setStorageSync('pcBasicsId', data.enterpriseId);
                    // uni.setStorageSync('tenantId', this.userData.tenantId);

                    this.checkStatus(data);
                }
            })
        },
        next(){
            this.encryptToken();
        }
    }
}
