index.page.js 14.2 KB

var $ = require('yoho-jquery'),
    phone,
    reg,
    orderCode = $('#orderCode').html(),
    oldUserCouponPic = $('#oldUserCouponPic').html(),
    newUserCouponPic = $('#newUserCouponPic').html(),
    tipMessage = $('#tipMessage').html(),
    activityID = $('#activityID').html(),
    pageInfo = $('#pageInfo').html(),
    wx,
    _weChatInterface = 'http://www.yohoshow.com/api/wechat/getSignPackage',
    shareTitle = pageInfo.shareTitle,
    shareImg = pageInfo.shareImg,
    shareDesc = pageInfo.shareDesc,
    shareLink = pageInfo.shareUrl;

$('#phone').bind('input propertychange', function() {
    if ($(this).val().length === 11) {
        $('.input-content div').eq('0').addClass('verification-code');
    } else {
        $('.input-content div').eq('0').removeClass();
        if ($(this).val().length >= 1) {
            $('.input-content i').eq('0').removeClass('hidden');
        } else {
            $('.input-content i').eq('0').addClass('hidden');
        }
    }
});
$('#verification').bind('input propertychange', function() {
    if ($(this).val().length === 4) {
        $('.input-content div').eq('1').addClass('get');
    } else {
        $('.input-content div').eq('1').removeClass();
        if ($(this).val().length >= 1) {
            $('.input-content i').eq('1').removeClass('hidden');
        } else {
            $('.input-content i').eq('1').addClass('hidden');
        }
    }
});

if ($('#activityEnded').html()) {
    $('.tip-wrap').removeClass('hidden');
    $('.tip-wrap .title').html(tipMessage);
}

if (!$('#newUser').html()) {
    $('.gain-coupon-centent .coupon img').attr('src', oldUserCouponPic);
}

/**
 * 微信分享
 */
(function() {
    if (typeof(wx) === 'undefined') {
        return;
    }

    $.getJSON(_weChatInterface + '?pageurl=' +
            encodeURIComponent(location.href.split('#')[0]) + '&callback=?', function(json) {
        var _appId, _timestamp, _nonceStr, _signature;
        if (json !== undefined && json !== '') {
            _appId = json.appId.toString();
            _timestamp = json.timestamp;
            _nonceStr = json.nonceStr.toString();
            _signature = json.signature.toString();

            wx.config({
                debug: false,
                appId: _appId,
                timestamp: _timestamp,
                nonceStr: _nonceStr,
                signature: _signature,
                jsApiList: [
                    'checkJsApi',
                    'onMenuShareTimeline',
                    'onMenuShareAppMessage',
                    'onMenuShareQQ',
                    'onMenuShareWeibo',
                    'hideMenuItems',
                    'showMenuItems',
                    'hideAllNonBaseMenuItem',
                    'showAllNonBaseMenuItem',
                    'translateVoice',
                    'startRecord',
                    'stopRecord',
                    'onRecordEnd',
                    'playVoice',
                    'pauseVoice',
                    'stopVoice',
                    'uploadVoice',
                    'downloadVoice',
                    'chooseImage',
                    'previewImage',
                    'uploadImage',
                    'downloadImage',
                    'getNetworkType',
                    'openLocation',
                    'getLocation',
                    'hideOptionMenu',
                    'showOptionMenu',
                    'closeWindow',
                    'scanQRCode',
                    'chooseWXPay',
                    'openProductSpecificView',
                    'addCard',
                    'chooseCard',
                    'openCard'
                ]
            });
        }
    });
    wx.ready(function() {

        var shareData = {
            title: shareTitle,
            desc: shareDesc,
            imgUrl: shareImg,
            link: shareLink
        };
        wx.onMenuShareAppMessage(shareData);
        wx.onMenuShareTimeline(shareData);
        wx.onMenuShareQQ(shareData);
        wx.onMenuShareWeibo(shareData);
    });
}());

$('.input-content  div').on('click', function() {
    phone = $(this).siblings('input').val();
    reg = /[0-9]{11}/;
    if (!reg.test(phone)) {
        $('#dialog').removeClass('hidden');
        $('.mask').removeClass('hidden');
        $('#dialog .content').html('<p class="phone-error">手机号错误,请重新输入。<p>');
        setTimeout(function() {
            $('.messages').addClass('hidden');
            $('.mask').addClass('hidden');
            $('#dialog .content').html(' ');
        }, 1400);
        return;
    }
    $.ajax({
        url: '/coupon/phone',
        data: {
            mobile: $('#phone').val(),
            ordercode: orderCode
        },
        success: function(data) {
            if (data.result.code === 200) {
                if (window._yas) {
                    window._yas.sendCustomInfo({
                        activityID: activityID,
                        returnCode: data.result.returnCode
                    }, false);
                }
                if (!data.result.newUser) {
                    $('.gain-coupon-centent .coupon img').attr('src', oldUserCouponPic);
                } else {
                    $('.gain-coupon-centent .coupon img').attr('src', newUserCouponPic);
                }
                if (data.result.ended) {
                    $('.tip-wrap').removeClass('hidden');
                    $('.tip-wrap .title').html(data.result.message);
                } else if (data.result.geted) {
                    $('.page .gain-coupon-centent').removeClass('hidden');
                    $('.page .coupon-centent').addClass('hidden');
                    $('#mobile').html('优惠券已放至' + data.result.mobile);
                } else if (data.result.newUser) {
                    $('.coupon-centent .phone-input-content').addClass('hidden');
                    $('.coupon-centent .vertify-input-content').removeClass('hidden');
                } else if (data.result.wrongNumb) {
                    $('#dialog').removeClass('hidden');
                    $('.mask').removeClass('hidden');
                    $('#dialog .content').html('<p class="phone-error">手机号错误,请重新输入。<p>');
                    setTimeout(function() {
                        $('.messages').addClass('hidden');
                        $('.mask').addClass('hidden');
                        $('#dialog .content').html(' ');
                    }, 1400);
                    return;
                } else if (data.result.oldUserAskCouponOnceMore) {
                    $('#dialog').removeClass('hidden');
                    $('.mask').removeClass('hidden');
                    $('#dialog .content').html('<p class="phone-error">您今天已经领过咯,速度去有货购潮流!<p>');
                    setTimeout(function() {
                        $('.messages').addClass('hidden');
                        $('.mask').addClass('hidden');
                        $('#dialog .content').html(' ');
                        $('.page .gain-coupon-centent').removeClass('hidden');
                        $('.page .coupon-centent').addClass('hidden');
                        $('#mobile').html('优惠券已放至' + data.result.mobile);
                    }, 2500);
                    return;
                }
            } else {
                $('#dialog').removeClass('hidden');
                $('.mask').removeClass('hidden');
                $('#dialog .content').html('<p class="phone-error">网络错误<p>');
                setTimeout(function() {
                    $('.messages').addClass('hidden');
                    $('.mask').addClass('hidden');
                    $('#dialog .content').html(' ');
                }, 1400);
            }
        },
        error: function() {
            $('#dialog').removeClass('hidden');
            $('.mask').removeClass('hidden');
            $('#dialog .content').html('<p class="phone-error">网络错误,稍后再试!<p>');
            setTimeout(function() {
                $('.messages').addClass('hidden');
                $('.mask').addClass('hidden');
                $('#dialog .content').html(' ');
            }, 1400);
        }
    });
});
$('.input-content').on('click', '.get', function() {
    $('.input-content div').eq('1').removeClass('get');
    if ($(this).siblings('input').val().length === 4) {
        $.ajax({
            url: '/coupon/verify',
            data: {
                identifycode: $('#verification').val(),
                mobile: $('#phone').val(),
                ordercode: orderCode
            },
            dataType: 'json',
            success: function(data) {
                if (data.result.code === 200) {
                    if (window._yas) {
                        window._yas.sendCustomInfo({
                            activityID: activityID,
                            returnCode: data.result.returnCode
                        }, false);
                    }
                    if (!data.result.newUser) {
                        $('.gain-coupon-centent .coupon img').attr('src', oldUserCouponPic);
                    } else {
                        $('.gain-coupon-centent .coupon img').attr('src', newUserCouponPic);
                    }
                    if (data.result.ended) {
                        $('.tip-wrap').removeClass('hidden');
                        $('.tip-wrap .title').html(data.result.message);
                    } else if (data.result.geted) {
                        $('.page .gain-coupon-centent').removeClass('hidden');
                        $('.page .coupon-centent').addClass('hidden');
                        $('#mobile').html('优惠券已发放至账户' + data.result.mobile);
                    } else if (data.result.newUser) {
                        $('.coupon-centent .phone-input-content').addClass('hidden');
                        $('.coupon-centent .vertify-input-content').removeClass('hidden');
                    } else if (data.result.wrongNumb) {
                        $('#dialog').removeClass('hidden');
                        $('.mask').removeClass('hidden');
                        $('#dialog .content').html('<p class="phone-error">手机号错误,请重新输入。<p>');
                        setTimeout(function() {
                            $('.messages').addClass('hidden');
                            $('.mask').addClass('hidden');
                            $('#dialog .content').html(' ');
                        }, 1400);
                        return;
                    } else if (data.result.vertifyWrong) {
                        $('#dialog').removeClass('hidden');
                        $('.mask').removeClass('hidden');
                        $('#dialog .content').html('<p class="phone-error">验证码错误,请重新输入。<p>');
                        setTimeout(function() {
                            $('.messages').addClass('hidden');
                            $('.mask').addClass('hidden');
                            $('#dialog .content').html(' ');
                        }, 1400);
                        return;
                    } else if (data.result.oldUserAskCouponOnceMore) {
                        $('#dialog').removeClass('hidden');
                        $('.mask').removeClass('hidden');
                        $('#dialog .content').html('<p class="phone-error">您今天已经领过咯,速度去有货购潮流!<p>');
                        setTimeout(function() {
                            $('.messages').addClass('hidden');
                            $('.mask').addClass('hidden');
                            $('#dialog .content').html(' ');
                            $('.page .gain-coupon-centent').removeClass('hidden');
                            $('.page .coupon-centent').addClass('hidden');
                            $('#mobile').html('优惠券已发放至账户' + data.result.mobile);
                        }, 2500);
                        return;
                    }

                    // oldUserAskCouponOnceMore
                } else {
                    $('#dialog').removeClass('hidden');
                    $('.mask').removeClass('hidden');
                    $('#dialog .content').html('<p class="phone-error">' + data.result.message + '<p>');
                    setTimeout(function() {
                        $('.messages').addClass('hidden');
                        $('.mask').addClass('hidden');
                        $('#dialog .content').html(' ');
                    }, 1400);
                }
            },
            error: function() {
                $('#dialog').removeClass('hidden');
                $('.mask').removeClass('hidden');
                $('#dialog .content').html('<p class="phone-error">网络错误,稍后再试!<p>');
                setTimeout(function() {
                    $('.messages').addClass('hidden');
                    $('.mask').addClass('hidden');
                    $('#dialog .content').html(' ');
                }, 1400);
            }
        });
    } else {
        $('#dialog').removeClass('hidden');
        $('.mask').removeClass('hidden');
        $('#dialog .content').html('<p class="phone-error">验证码错误,请重新输入。<p>');
        setTimeout(function() {
            $('.messages').addClass('hidden');
            $('.mask').addClass('hidden');
            $('#dialog .content').html(' ');
        }, 1400);
    }
});
$('.close').on('click', function() {
    $('.dialog').addClass('hidden');
    $('.mask').addClass('hidden');
    $('#dialog .content').html(' ');
});
$('.coupon-description span').on('click', function() {
    $('#message').removeClass('hidden');
    $('.mask').removeClass('hidden');
});
$('.description').on('click', function() {
    $('#message').removeClass('hidden');
    $('.mask').removeClass('hidden');
});

$('.input-content').on('click', '.get', function() {
    if (window._yas) {
        window._yas.sendCustomInfo({
            activityID: activityID
        }, true);
    }
});

$('.input-content .clear-mobile').on('click', function() {
    $('.input-content input').eq('0').val('');
    $(this).addClass('hidden');
    $('.input-content div').eq('0').removeClass();
});

$('.input-content .clear-verify').on('click', function() {
    $('.input-content input').eq('1').val('');
    $(this).addClass('hidden');
    $('.input-content div').eq('1').removeClass();
});