students.page.js 20.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
/**
 * stu页面
 * @author: da<hongwei.gao@yoho.cn>
 * @date: 2016/08/8
 */
var $ = require('yoho-jquery'),
    lazyLoad = require('yoho-jquery-lazyload'),
    handlebars = require('yoho-handlebars');

var Dialog = require('../common/dialog').Dialog;

var $sortItem = $('.sort-item'),
    $agreenShow = $('#agreen-show'),
    $stuProv = $('#stu-province-show'),
    $stuSchool = $('#stu-school-show'),
    $stuEdu = $('#stu-edu-show'),
    $stuYear = $('#stu-entrance-year-show'),
    $stuYearWrap = $stuYear.parent(),
    $identityForm = $('#identity-form'),
    $identityWrap = $('#identity-wrap');

var $rightDia = $('#rights-dia'),
    $couponDia = $('#coupon-dia');

var provFn,
    identityFn;

var alertConfig,
    makeAlert;

// j面跳转对象
var redirect = {

    // 去逛逛跳转链接
    gunangSrc: null,

    // 查看优惠券跳转链接
    checkCouponSrc: null,
    goToGuang: function() {
        window.location.href = this.gunangSrc;
    },
    goToCheck: function() {
        window.location.href = this.checkCouponSrc;
    },

    // 处理网络返回
    403: function(url) {
        window.location.href = url;
    },
    200: function(url) {
        this.checkCouponSrc = url;
    }
};

require('../common');

// handlebars模板
provFn = handlebars.compile($('#stu-select').html() || '');
identityFn = handlebars.compile($('#identity-back').html() || '');

require('../plugins/slider');


function getCookie(name) {
    var arr, reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)');

    if (arr === document.cookie.match(reg)) {
        return unescape(arr[2]);
    } else {
        return null;
    }
}

/** *埋点方法***/
function givePoint(op, parameter) {
    var CID = 1;

    if (!window._yas || !window._yas.sendCustomInfo) {
        return false;
    }

    if (!getCookie('_Channel')) {
        // 男:1,女:2,潮童:3,创意生活:4
        switch (getCookie('_Channel')) {
            case 'boys':
                CID = 1;
                break;
            case 'girls':
                CID = 2;
                break;
            case 'kids':
                CID = 3;
                break;
            case 'lifestyle':
                CID = 4;
                break;
            default:
                CID = 1;
        }
    }

    parameter = $.extend({
        REC_POSE: '',
        PRD_ID: '',
        PRD_NUM: 0,
        C_ID: CID,
        ACTION_ID: 0,
        page_num: 1
    }, parameter);

    if (parameter.REC_POSE === '' || parameter.PRD_ID === '') {
        return true;
    }
    window._yas.sendCustomInfo({
        op: op,
        param: JSON.stringify(parameter)
    }, true);
}

function createStuDialog(cName, cont) {
    var opt = {
        className: 'stu-dialog ' + cName,
        content: cont

    };
    var myDialog = new Dialog(opt);

    myDialog.show();
}

// 关掉所有下拉框
function closeStuSelect() {
    $('.ident-select-wrap').removeClass('active');
    $('.ident-select-wrap .select-ul').empty();
}


/**
 * 清除加载的下拉数据,还原点击状态,获取点击数据
 * @param
 * @param
 */
function clearSelectUl($a, code) {
    var $li = $a.parent(),
        $ul = $li.parent();

    var $show = $ul.prev(),
        $wrap = $ul.parent(),
        $input = $wrap.prev();

    $show.html($a.html());
    if (arguments.length === 2) {
        $show.attr(code, $a.attr(code));
        $input.val($a.attr(code));
    } else {
        $input.val($a.html());
    }
    $ul.empty();
    $wrap.removeClass('active');
}

/**
 * 最终提交表单
 */
function submitIdentity(json) {
    // xls-3提交认证埋点
    givePoint('YB_STUDENT_ATTCT_SUBMIT', {SRC_ID: 2, SUBMIT_RES: 1});
    $.ajax({
        type: 'GET',
        url: '/product/students/verify',
        data: json
    }).then(function(data) {
        if (data.code === 200) {
            window.location.href = data.data;

            // xls-4认证成功埋点
            givePoint('YB_STUDENT_ATTCT_RESULT', {SRC_ID: 5, SUBMIT_RES: 1});
        } else {
            $identityForm.addClass('hide');
            $('identity-back-wrap').remove();
            $identityWrap.append(identityFn({msg: data.message}));

            // xls-4认证失败埋点
            givePoint('YB_STUDENT_ATTCT_RESULT', {SRC_ID: 5, SUBMIT_RES: 2, FAILURE_CAUSE: data.message});
        }
    });
}

lazyLoad($('img.lazy'));

// 根据配置执行展示弹窗
function couponAlert(opt) {
    var newAlert = new Dialog(opt);

    newAlert.show();
}

function loadSelectLi(jsonData, param) {
    var $wrap = jsonData.$ul.parent();

    jsonData.$ul.empty();
    if ($wrap.hasClass('active')) {
        $wrap.removeClass('active');
    } else {
        closeStuSelect();
        $wrap.addClass('active');
        $.ajax({
            type: 'GET',
            url: jsonData.url,
            data: param
        }).then(function(data) {
            var fnJson = {};

            if (data.code === 200) {
                fnJson[jsonData.keyData] = data.data;
                jsonData.$ul.html(provFn(fnJson));
            } else if (data.code === 403) {
                location.href = data.data.url;
            }
        });
    }
}

// 配置弹窗
alertConfig = {
    success: {
        content: '恭喜您,成功领取优惠券',
        subContents: ['特殊情况下到账有延时', '请耐心等待'],
        className: 'subcontent-dialog',
        refreshOnClose: true,
        btns: [
            {
                id: 1,
                name: '去购物啦',
                btnClass: ['black', 'btn-close'],
                cb: function() {
                    redirect.goToGuang();
                }
            },
            {
                id: 2,
                name: '查看优惠券',
                btnClass: ['btn-close'],
                cb: function() {
                    redirect.goToCheck();
                }
            }
        ]
    },
    alreadyGot: {
        content: '您已领取过优惠券',
        subContent: '快去选购心仪的潮品吧',
        className: 'subcontent-dialog',
        btns: [
            {
                id: 1,
                name: '去使用',
                btnClass: ['btn-close'],
                cb: function() {
                    redirect.goToGuang();
                }
            }
        ]
    },
    expired: {
        content: '优惠券已过期',
        subContent: '去领最新的优惠券吧',
        className: 'subcontent-dialog',
        btns: [
            {
                id: 1,
                name: '关闭',
                btnClass: ['btn-close']
            }
        ]
    },
    failed: {
        content: '领取失败',
        subContents: ['请刷新重试,', '多次无效请联系客服'],
        className: 'subcontent-dialog',
        btns: [
            {
                id: 1,
                name: '刷新',
                btnClass: ['btn-close'],
                cb: function() {
                    window.location.reload();
                }
            }
        ]
    }
};

// 对应不同的网络返回码展示不同的弹窗
makeAlert = {
    200: function() {
        couponAlert(alertConfig.success);
    },
    401: function() {
        couponAlert(alertConfig.alreadyGot);
    },
    315: function() {
        couponAlert(alertConfig.expired);
    },
    400: function() {
        couponAlert(alertConfig.failed);
    },
    403: function() {

        // do not make alert when user are not login in
    }
};

function requestCoupon(id) {
    $.ajax({
        type: 'GET',
        url: '/coupon/sendcoupon',
        data: {
            id: id
        },
        success: function(res) {
            var code = res.code;

            // 如果返回的数据里有url,则执行redirect里的方法
            res.url && res.url.length > 0 && redirect[code] &&
                redirect[code](res.url + '?' + location.href.split('?')[1]);

            // 如果能找到对应code则执行相应方法,如果没有的对应code则执行error提示
            makeAlert[code] ? makeAlert[code]() : makeAlert['400']();

        },
        error: function() {
            var opt = {
                className: 'stu-alert',
                content: '<p>网络异常</p>'
            };

            new Dialog(opt).show();
        }
    });
}


function clearForm() {
    $('#stu-name').val('');
    $('#stu-idNum').val('');
    $('#stu-school').val('');
    $('#stu-edu').val('');
    $('#stu-entrance-year').val('');
    $('#prompt').html('');
    $stuProv.html('请选择省份');
    $stuSchool.html('请选择您所在学校');
    $stuEdu.html('请选择您的学历');
    $stuYear.html('请选择您的入学年份');
}

$('.slide-container').slider({
    orient: true
});

$sortItem.on('click', function() {
    $(this).siblings('.active').removeClass('active');
    $(this).addClass('active');
});

// 加载学校地区数据
$stuProv.on('click', function() {
    var data = {
        $ul: $(this).next(),
        keyData: 'provData',
        url: '/product/students/schoolArea'
    };

    loadSelectLi(data, {});
});

//
$stuSchool.on('click', function() {
    var data = {
        $ul: $(this).next(),
        keyData: 'schoolData',
        url: '/product/students/schoolList'
    };

    loadSelectLi(data, {areaCode: $stuProv.attr('areaCode')});

});

//
$stuEdu.on('click', function() {
    var data = {
        $ul: $(this).next(),
        keyData: 'eduData',
        url: '/product/students/eduLevel'
    };

    loadSelectLi(data, {});
});

//
$stuYear.on('click', function() {
    var $ul = $(this).next(),
        year = new Date().getFullYear(),
        yearArr = [],
        i;

    $ul.empty();
    if ($stuYearWrap.hasClass('active')) {
        $stuYearWrap.removeClass('active');
    } else {
        closeStuSelect();
        $stuYearWrap.addClass('active');
        for (i = year - 7; i <= year; i++) {
            yearArr.push(i);
        }
        $ul.html(provFn({
            yearData: yearArr
        }));
    }
});

$('.ident-select-wrap > .arrow-down').click(function() {
    $(this).next().click();
});

$couponDia.on('click', function() {
    var cont = '<h3 class="dia-title">认证协议</h3>' +
    '<p>(1) 全日制大学生及硕士博士研究生;</p>' +
    '<p>(2) 学校在可选的范围内,有部分学校可能暂未收录,后期尽快增加;</p>' +
    '<p>(3) 每个学号只能认证一个账户;</p>';

    createStuDialog('stu-agree-dialog', cont);
});

$rightDia.on('click', function() {
    var cont = '<h3 class="dia-title">特权详细说明</h3>' +
    '<p>权益1:新品立享9折</p>' +
    '<p>学生购买原价新品时,可立即享受9折优惠,与VIP折扣不可同时享受。</p><br>' +
    '<p>权益2:每1元返1个有货币</p>' +
    '<p>学生购买商品时,以商品的实际成交金额计算,每1元返1个有货币;</p>' +
    '<p>返有货币时间:确认收货7日后,系统将自动将对应数量的有货币返至购买账户;</p>' +
    '<p>有货币有效期:获得当日至次年12月31日,逾期自动作废;</p>' +
    '<p>查看有货币:登录后,点击“个人中心”在“我的有货币”中可以查看有货币余额及明细。</p><br>' +
    '<p>权益3:免单抽奖</p>' +
    '<p>每月将在累计购物金额最高的学校中,抽取3名幸运学生用户,获得免单资格;</p>' +
    '<p>免单用户名单将在每月第1个工作日公布在有货微信公众号上,可关注【有货YOHOBUY】;</p>' +
    '<p>免单用户将在中奖次日获得与实付金额等额的现金券,使用时间:中奖当月。</p><br>' +
    '<p>权益4:学生专享活动</p>' +
    '<p>每月不定期的开展学生专享活动,可打开有货APP推送,及时查收学生专享活动通知。</p>';

    createStuDialog('stu-rights-dialog', cont);

    // xls-5学生营销,页面加载埋点
    givePoint('YB_STUDENT_VIP_FLR', {F_ID: 'stu-rights-box', F_NAME: '学生权益',
        F_URL: '', F_INDEX: 2, I_INDEX: 0});
});

$agreenShow.on('click', function() {
    var $ck = $(this);

    $ck.hasClass('active') === true ? $ck.removeClass('active') : $ck.addClass('active');
});


$('#stu-province-ul').delegate('li > a', 'click', function() {
    var $a = $(this);

    if ($a.html() !== $stuProv.html()) {
        $stuSchool.html('请选择学校名称');
        $('#stu-school').val('');
    }
    clearSelectUl($a, 'areacode');
});

$('#stu-school-ul').delegate('li > a', 'click', function() {
    clearSelectUl($(this));
});

$('#stu-edu-ul').delegate('li > a', 'click', function() {
    clearSelectUl($(this));
});

$('#stu-entrance-year-ul').delegate('li > a', 'click', function() {
    clearSelectUl($(this));
});

// 重新验证
$identityWrap.delegate('.backForm', 'click', function() {
    clearForm();
    $identityForm.removeClass('hide').siblings().addClass('hide');
});

$('.enable .info').on('click', function(e) {
    if ($(this).children('.normal').length !== 1) {
        return;
    }

    e.preventDefault();
    requestCoupon($(this).closest('a').data('id'));
    redirect.gunangSrc = $(this).closest('a').get(0).href;

     // xls-5学生营销,页面加载埋点
    givePoint('YB_STUDENT_VIP_FLR', {F_ID: 'stu-coupon-box', F_NAME: '优惠券',
        F_URL: '', F_INDEX: 4, I_INDEX: 0});
});

$('#identity-Btn').on('click', function() {
    var identData = {
        name: $('#stu-name').val(),
        certNo: $('#stu-idNum').val(),
        provName: $('#stu-province').val(),
        collegeName: $('#stu-school').val(),
        educationDegree: $('#stu-edu').val(),
        enrollmentYear: $('#stu-entrance-year').val()
    };

    var $prompt = $('#prompt'),
        $agreen = $('#agreen-show'),
        failText = '';

    if (identData.name === '') {
        failText = '真实姓名不能为空';
        $prompt.html(failText);

        // xls-3埋点
        givePoint('YB_STUDENT_ATTCT_SUBMIT', {SRC_ID: 2, SUBMIT_RES: 2, FAILURE_CAUSE: failText});
        return;
    }
    if (identData.certNo === '') {
        failText = '身份证号不能为空';
        $prompt.html(failText);

        // xls-3埋点
        givePoint('YB_STUDENT_ATTCT_SUBMIT', {SRC_ID: 2, SUBMIT_RES: 2, FAILURE_CAUSE: failText});
        return;
    }
    if (!/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(identData.certNo)) {
        failText = '身份证号格式错误';
        $prompt.html(failText);

        // xls-3埋点
        givePoint('YB_STUDENT_ATTCT_SUBMIT', {SRC_ID: 2, SUBMIT_RES: 2, FAILURE_CAUSE: failText});
        return;
    }
    if (identData.provName === '') {
        failText = '请选择省份';
        $prompt.html(failText);

        // xls-3埋点
        givePoint('YB_STUDENT_ATTCT_SUBMIT', {SRC_ID: 2, SUBMIT_RES: 2, FAILURE_CAUSE: failText});
        return;
    }
    if (identData.collegeName === '') {
        failText = '请选择学校名称';
        $prompt.html(failText);

        // xls-3埋点
        givePoint('YB_STUDENT_ATTCT_SUBMIT', {SRC_ID: 2, SUBMIT_RES: 2, FAILURE_CAUSE: failText});
        return;
    }
    if (identData.educationDegree === '') {
        failText = '请选择学历程度';
        $prompt.html(failText);

        // xls-3埋点
        givePoint('YB_STUDENT_ATTCT_SUBMIT', {SRC_ID: 2, SUBMIT_RES: 2, FAILURE_CAUSE: failText});
        return;
    }
    if (identData.enrollmentYear === '') {
        failText = '请选择入学年份';
        $prompt.html(failText);

        // xls-3埋点
        givePoint('YB_STUDENT_ATTCT_SUBMIT', {SRC_ID: 2, SUBMIT_RES: 2, FAILURE_CAUSE: failText});
        return;
    }
    if (!$agreen.hasClass('active')) {
        failText = '未同意Yoho!有货学生认证协议';
        $prompt.html(failText);

        // xls-3埋点
        givePoint('YB_STUDENT_ATTCT_SUBMIT', {SRC_ID: 2, SUBMIT_RES: 2, FAILURE_CAUSE: failText});
        return;
    }
    $prompt.html('');

    submitIdentity(identData);
});

// 首次点击去掉灰色
$('.ident-select-wrap').one('click', function() {
    $(this).removeClass('gray-p');
});

/*
*埋点
*/
$(function() {
    // 加载优惠券状态
    var couponIds = [];

    $('.coupon > a').each(function() {
        couponIds.push($(this).attr('data-id'));
    });

    if (couponIds.length > 0) {

        $.ajax({
            type: 'GET',
            url: '/product/students/userAcquireStatus',
            data: {
                couponIds: couponIds
            },
            success: function(res) {
                var code = res.code,
                    i,
                    text,
                    $a;

                if (code === 200) {
                    for (i = 0; i < res.data.length; i++) {
                        $a = $('[data-id=\'' + res.data[i].couponId + '\']');

                        text = '';

                        if (res.data[i].status === 1) {
                            text = '<div class="normal">' +
                                    '<p>点击</p>' +
                                    '<p>领取</p>' +
                                    '</div>';
                        }
                        if (res.data[i].status === 2) {
                            $a.removeClass('enable');
                            text = '<div class="empty">' +
                                    '<p>已抢光</p>' +
                                    '<p class="guang">去逛逛</p>' +
                                    '</div>';
                        }
                        if (res.data[i].status === 3) {
                            $a.removeClass('enable');
                            text = '<div class="got">' +
                                    '<p>已领取</p>' +
                                    '<p class="guang">去使用</p>' +
                                    '</div>';
                        }
                        if (res.data[i].status === 4) {
                            $a.removeClass('enable');
                            text = '<div class="normal">' +
                                    '<p>已过期</p>' +
                                    '<p class="guang">去逛逛</p>' +
                                    '</div>';
                        }

                        $a.find('.info').html(text);
                    }

                }
            },
            error: function() {
            }
        });
    }

    if ($('#identity-success').length > 0) {
        // xls-4学生认证成功埋点
        givePoint('YB_STUDENT_ATTCT_RESULT', {SRC_ID: 5, SUBMIT_RES: 1});
    }
    if ($('#identity-fail').length > 0) {
        // xls-4学生认证失败埋点
        givePoint('YB_STUDENT_ATTCT_RESULT', {SRC_ID: 5, SUBMIT_RES: 2,
            FAILURE_CAUSE: $('#identity-fail-text').html()});
    }

    // xls-5学生营销,页面加载埋点
    givePoint('YB_STUDENT_HOME', {SRC_ID: 5});

    $('.banner-img').click(function() {
        var url = $(this).children().attr('href'),
            index = $(this).index();

        // xls-5学生营销,页面加载埋点
        givePoint('YB_STUDENT_VIP_FLR', {F_ID: 'banner', F_NAME: 'banner',
            F_URL: url, F_INDEX: 1, I_INDEX: parseInt(index, 0) + 1});
    });

    $('#stu-coupon-more').click(function() {
        var url = $(this).attr('href');

        // xls-5学生营销,页面加载埋点
        givePoint('YB_STUDENT_VIP_FLR', {F_ID: 'stu-coupon-box', F_NAME: '优惠券',
            F_URL: url, F_INDEX: 4, I_INDEX: 0});
    });

    $('#stu-good-more').click(function() {
        var url = $(this).attr('href');

        // xls-5学生营销,页面加载埋点
        givePoint('YB_STUDENT_VIP_FLR', {F_ID: 'stu-good-box', F_NAME: '热门单品',
            F_URL: url, F_INDEX: 5, I_INDEX: 0});
    });

    $('.good-thumb').click(function() {
        var url = $(this).attr('href'),
            index = $(this).closest('.good-info').index();

        // xls-5学生营销,页面加载埋点
        givePoint('YB_STUDENT_VIP_FLR', {F_ID: 'stu-good-box', F_NAME: '热门单品',
            F_URL: url, F_INDEX: 5, I_INDEX: parseInt(index, 0) + 1});
    });

    $('.stu-good-detail a').click(function() {
        var url = $(this).attr('href'),
            index = $(this).closest('.good-info').index();

        // xls-5学生营销,页面加载埋点
        givePoint('YB_STUDENT_VIP_FLR', {F_ID: 'stu-good-box', F_NAME: '热门单品',
            F_URL: url, F_INDEX: 5, I_INDEX: parseInt(index, 0) + 1});
    });

    $('.commodity-list a').click(function() {
        var url = $(this).attr('href'),
            index = $(this).index();

        // xls-5学生营销,页面加载埋点
        givePoint('YB_STUDENT_VIP_FLR', {F_ID: 'stu-activity-box', F_NAME: '活动推荐',
            F_URL: url, F_INDEX: 6, I_INDEX: parseInt(index, 0) + 1});
    });
});