index-controller.js 13.7 KB
/**
 * 用户中心首页控制器
 */
define(
    "user/index-controller",
    [
        'mk7/controller',
        'mk7/url',
        'mk7/utils',
        'mk7/modals',
        'mk7/agreement-checker'
    ],
    function (ctrl, url, utils, modals, agreementChecker) {
        var ctrl = new ctrl();
        var $$ = Dom7;
        var t7 = Template7;
        var STATUS_NEVER_AUTH = 0;
        var STATUS_PHONE = 1;
        var STATUS_REALNAME_AUTHING = 2;
        var STATUS_REALNAME_AUTH = 3;
        var STATUS_REALNAME_AUTH_FILE = 4;
        var STATUS_SKILL_AUTH = 6;
        var STATUS_SKILL_FAIL = 7;
        var STATUS_DISABLE = 101;

        var DEFAULT_QRCODE_TEMPLATE_ID = 11; // 默认专属二维码模板ID定义

        ctrl.run = function () {
            clearInterval(window.pollingTimer);
            var me = this;
            me.setPageTitle('个人中心');
            me.getUserData();
            agreementChecker.initServiceVersion();
            me.hideAllNonBaseMenuItem(window.$site);
            // 初始化订单tab小红点
            me.initOrderCountBadge();
        };

        ctrl.bindEvents = function () {
            var me = this;
            console.log("bindEvents");
            $$('.order_index').on('click', function () {
                console.log('order_index');
                window.location.replace('/order#index');
            })
            $$('.order_center').on('click', function () {
                console.log('order_center');
                window.location.replace('/order#order-center');
            })
            $$('.user_index').on('click', function () {
                console.log('user_index');
                window.location.replace('/user#index');
            })
            $$('.dispatch_index').on('click', function () {
                window.location.replace('/dispatchorder#index');
            })
        };

        ctrl.getUserData = function () {
            var me = this;
            $$.ajax({
                method: "POST",
                url: url.to('user/default/get-profile'),
                data: {},
                dataType: "json",
                beforeSend: function () {
                    me.showIndicator();
                },
                success: function (e) {
                    if (e.success) {
                        me.render(e);
                        me.bindEventsAfterAjax(e.engineer);
                        /*
                        if(e.pending_order_count > 0){
                            $$('.index-view-footer .total-count').addClass('bg-red').addClass('badge');
                            $$('.index-view-footer .total-count').text(e.pending_order_count);
                        }
                        */
                    }
                },
                error: function (e) {
                },
                complete: function (e) {
                    me.hideIndicator();
                }
            });
        };

        ctrl.bindEventsAfterAjax = function (engineer) {
            var me = this;
            $$('.my-balance').on('click', function (e) {
                me.mustRepairTypeAuth(engineer, '#wallet');
            });
            $$('.order-setting').on('click', function (e) {
                me.mustPhoneAuth(engineer, '/user#order-setting');
            });
            $$('.realNameLi').on('click', function (e) {
                me.mustRepairTypeAuth(engineer, '/tauth#real-name');
            });
            $$('.jiwoShop').on('click', function (e) {
                me.mustRepairTypeAuth(engineer, '/shop#index');
            });
            $$('.userData').on('click', function (e) {
                me.mustRepairTypeAuth(engineer, '/user#data');
            });
            $$('#levelScoreDetail').on('click', function (e) {
                me.mustSkillAuth(engineer, '/user#level-score-detail');
            });
            $$('.userVip').on('click', function (e) {
                me.mustRealNameAuth(engineer, '/user#vip');
            });
            $$('#btnTauthList').on('click', function (e) {
                me.mustRealNameAuth(engineer, '/tauth#list');
            });
            $$('.Course').on('click', function (e) {
                me.mustRepairTypeAuth(engineer, '/course#home');
            });
            $$('.my-order').on('click', function (e) {
                if (engineer.status <= STATUS_SKILL_FAIL) {
                    if (engineer.status == STATUS_SKILL_AUTH) {
                        window.location.href = '#orders/working';
                    } else {
                        utils.toast({content: '请先完成认证'});
                    }
                } else {
                    utils.toast({content: '您的账号已被禁用'});
                }
            });

            $$('.realname-btn').on('click', function (e) {
                if (engineer.status <= STATUS_SKILL_FAIL) {
                    if (engineer.status == STATUS_NEVER_AUTH) {
                        utils.toast({content: '请先通过手机验证'});
                        setTimeout(function () {
                            window.location.href = '/tauth#mobile';
                        }, 1500);
                    } else if (engineer.status == STATUS_SKILL_AUTH) {
                        window.location.href = url.to('/tauth#real-name');
                    } else {
                        utils.toast({content: '请先完成认证'});
                    }
                } else {
                    utils.toast({content: '您的账号已被禁用'});
                }
            });

            $$('.label-switch').on('click', function (e) {
                var item_after_switch = $$(this).parents(".item-after-switch");
                var switch_text = item_after_switch.children(".switch").text();
                if (switch_text == '开') {
                    item_after_switch.children(".switch").text("关");
                } else {
                    item_after_switch.children(".switch").text("开");
                }
            });

            var me = this;
            $$('#applyQrcode').click(function () {
                if (!engineer.canApplyQrcode) {
                    me.showTipModal();
                    return ;
                }

                window.location.href = url.to("/applyqcode#apply-code");
            })

            $$('#btnCreateQrcode').click(function () {
                if (engineer.status <= STATUS_SKILL_FAIL) {
                    var tplId = DEFAULT_QRCODE_TEMPLATE_ID;
                    if (engineer.qrcode_template_id != "" && engineer.qrcode_template_id != null && engineer.qrcode_template_id != undefined) {
                        tplId = engineer.qrcode_template_id;
                    }
                    window.location.href = url.to('/applyqcode#apply-diy-code/' + tplId);
                } else {
                    utils.toast({content: '您的账号已被禁用'});
                }
            })
            // 联系客服打电话
            $$(".contactService").on('click', function (e) {
                e.preventDefault();
                e.stopPropagation();
                var me = this;
                var pageDom = $$('#index')
                var params = {'trace':'orderHelper'};
                utils.httpPost(url.to('order/call/get-service-call'), params, function (res) {
                    if (!res.success) {
                        utils.toast({content: res.error});
                        return false;
                    }
                    if ('' != res.phone) {
                        utils.makePhone(pageDom, res.phone);
                        return false;
                    } else {
                        me.app.alert('系统异常无法拨号');
                        return false;
                    }
                }, true)
            })
        };



        ctrl.showTipModal = function () {
            var me = this;
            var ele = $$('script#tip-box-modal');
            var params = {
                text: "学习【绑定设备】教程<br/>并通过考试后可申领二维码"
            };
            var compiled = t7.compile(ele.html());
            compiled = $$(compiled(params));
            var content = compiled.html();

            me.app.modal({
                text: content,
                cssClass: 'myModal'
            });

            $$('body').on('click', '#user-modal-close', function () {
                me.app.closeModal();
            });
            $$('body').on('click', '#user-modal-confirm', function () {
                me.app.closeModal();
                window.location.href = url.to("/course"); // 进入接单教程首页
            });
        }

        ctrl.mustRealNameAuth = function (engineer, toUrl) {
            if (engineer.status <= STATUS_SKILL_FAIL) {
                if (engineer.status == STATUS_REALNAME_AUTH || engineer.status > STATUS_REALNAME_AUTH_FILE) {
                    window.location.href = toUrl;
                    return '';
                }
                if (engineer.status < STATUS_REALNAME_AUTH  || engineer.status == STATUS_REALNAME_AUTH_FILE) {
                    //alert(url.to("/tauth#index"));
                    if (engineer.status == STATUS_NEVER_AUTH) {
                        window.location.href = url.to("/tauth#mobile");
                    } else {
                        if (engineer.selectTags) {
                            window.location.href = url.to("/tauth#real-name");
                        } else {
                            window.location.href = url.to("/user#repair-type");
                        }
                    }
                    return '';
                }
                // 后面统一做状态处理
                utils.toast({content: '请先完成认证'});
            } else {
                utils.toast({content: '您的账号已被禁用'});
            }
        }

        ctrl.mustRepairTypeAuth = function (engineer, toUrl) {
            if (engineer.status <= STATUS_SKILL_FAIL) {
                if (engineer.status >= STATUS_REALNAME_AUTHING) {
                    if (engineer.selectTags) {
                        window.location.href = toUrl;
                    } else {
                        window.location.href = url.to("/user#repair-type");
                    }
                    return false;
                }
                if (engineer.status < STATUS_REALNAME_AUTHING) {
                    //alert(url.to("/tauth#index"));
                    if (engineer.status == STATUS_NEVER_AUTH) {
                        window.location.href = url.to("/tauth#mobile");
                    } else {
                        if (engineer.selectTags) {
                            window.location.href = toUrl;
                        } else {
                            window.location.href = url.to("/user#repair-type");
                        }
                    }
                } else {
                    // 后面统一做状态处理
                    utils.toast({content: '请先完成认证'});
                }
            } else {
                utils.toast({content: '您的账号已被禁用'});
            }
        }

        ctrl.mustSkillAuth = function (engineer, toUrl) {
            if (engineer.status <= STATUS_SKILL_FAIL) {
                if (engineer.status == STATUS_SKILL_AUTH) {
                    if (engineer.selectTags) {
                        window.location.href = toUrl;
                    } else {
                        window.location.href = url.to("/user#repair-type");
                    }
                } else {
                    if (engineer.status < STATUS_SKILL_AUTH) {
                        //alert(url.to("/tauth#index"));
                        if (engineer.status == STATUS_NEVER_AUTH) {
                            window.location.href = url.to("/tauth#mobile");
                        } else {
                            if (engineer.selectTags) {
                                if (engineer.status < STATUS_REALNAME_AUTH || engineer.status == STATUS_REALNAME_AUTH_FILE) {
                                    window.location.href = url.to("/tauth#real-name");
                                } else {
                                    window.location.href = url.to("/tauth#list");
                                }
                            } else {
                                window.location.href = url.to("/user#repair-type");
                            }
                        }
                    } else {
                        // 后面统一做状态处理
                        utils.toast({content: '请先完成认证'});
                    }
                }
            } else {
                utils.toast({content: '您的账号已被禁用'});
            }
        }

        ctrl.mustPhoneAuth = function (engineer, toUrl) {
            if (engineer.status <= STATUS_SKILL_FAIL) {
                if (engineer.status >= STATUS_PHONE) {
                    window.location.href = toUrl;
                } else {
                    if (engineer.status == STATUS_NEVER_AUTH) {
                        window.location.href = url.to("/tauth#mobile");
                    } else {
                        window.location.href = toUrl;
                    }
                }
            } else {
                utils.toast({content: '您的账号已被禁用'});
            }
        }

        /**
         * 初始化订单tab小红点
         */
        ctrl.initOrderCountBadge = function(){
            utils.httpPost( url.to('order/default/get-each-orders-count'),{},function(res){
                if (res.success) {
                    if (res.orders.totalOrderCount > 0) {
                        $$("#totalOrderBadge").css('display', 'block');
                        $$("#totalOrderBadge").html(res.orders.totalOrderCount);
                    }
                }
            })
        }

        return ctrl;
    }
);