coupon.mobile.1130.js 8.08 KB
var $tip, tipItime;

/* 领指定券 */
var activityId = $('#coupon-container').attr('param');
var userIsLogged =  $('#coupon-container').attr('isLogged') === 'Y';
var isApp;

$('.get-coupon').click(function() {
    var couponId = $(this).attr('param');
    isApp = $(this).attr('href') !== 'javascript:;';
    if (!isNaN(activityId) && !isNaN(couponId)) {
        getNamedCoupon(activityId, couponId, isApp, userIsLogged);
    }
});
/* 领所有券 */
$('#get-all-coupon').click(function() {
    var isApp = $(this).attr('href') !== 'javascript:;';
    getAllCoupon(activityId, isApp);
});

/* 检查是否有在领取中的券,有则直接领取 */
if (userIsLogged) {
    var cookieCouponId = cookie('_Coupon' + activityId);
    if (cookieCouponId && !isNaN(cookieCouponId)) {
        getNamedCoupon(activityId, cookieCouponId, isApp, userIsLogged);
    }
}

/**
 * 微信分享 
 */
(function($) {
    if (typeof(wx) == "undefined") {
        return;
    }
    var _weChatInterface = 'http://www.yohoshow.com/api/wechat/getSignPackage';
    $.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 shareTitle = $('#shareTitle').val();
        var shareImg = $('#shareImg').val();
        var shareDesc = $('#shareDesc').val();
        var shareLink = $('#shareLink').val();
        var shareData = {
            title: shareTitle,
            desc: shareDesc,
            imgUrl: shareImg,
            link: shareLink
        };
        wx.onMenuShareAppMessage(shareData);
        wx.onMenuShareTimeline(shareData);
        wx.onMenuShareQQ(shareData);
        wx.onMenuShareWeibo(shareData);
    });
}(jQuery));

/**
 * 初始化提示框
 */
(function() {
    var tipHtml = '<div id="yoho-tip" class="yoho-tip"></div>';

    //插入提示HTML
    $('#coupon-container').append(tipHtml);

    $tip = $('#yoho-tip');
    $tip.on('touchend', function() {
        $tip.hide();

        //清除Timeout
        clearTimeout(tipItime);
    });
}());

function cookie(name) {
    var cookies = document.cookie,
        cookieVal,
        offset;

    if (document.cookie && document.cookie !== '') {
        offset = cookies.indexOf(name + '=');
        if (offset > -1) {
            offset += name.length + 1;

            cookieVal = decodeURIComponent($.trim(cookies.substring(offset, cookies.indexOf(';', offset))));
        }
    }

    return cookieVal;
}

function setCookie(name, value, options) {
    var expires = '',
        path,
        domain,
        secure,
        date;

    if (typeof value !== 'undefined') {
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }

        if (options.expires &&
            (typeof options.expires === 'number' || options.expires.toUTCString)) {
            if (typeof options.expires === 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString();
        }
        path = options.path ? '; path=' + options.path : '';
        domain = options.domain ? '; domain=' + options.domain : '';
        secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    }
}

/**
 * 显示提示
 */
function showTip(con, dur) {
    var content, duration;

    if (typeof con === 'undefined') {
        return;
    }

    content = con.toString();
    duration = (dur && dur > 0) ? dur : 3000;

    $tip.html(content).show();

    tipItime = setTimeout(function() {
        if ($tip.css('display') === 'block') {
            $tip.hide();
        }
    }, duration);
}

/**
 * 获取活动的指定优惠券 
 */
function getNamedCoupon(activityId, couponId, isApp, userIsLogged)
{
    $.ajax({
        type: 'POST',
        url: '/cuxiao/coupon/getnamed',
        data: 'activityId=' + activityId + '&couponId=' + couponId,
        success: function(data) {
            setCookie('_Coupon' + activityId, null, {
                expires: -2,
                domain: '.m.yohobuy.com'
            });
            if (isApp && !userIsLogged) {
                return;
            }
            if (data.code == 200) {
                showTip('恭喜你成功领取优惠券!<br><br>稍后即会到账,请耐心等待。');
            }
            else if (data.code == 201) {
                showTip('你已领取过品牌优惠券<br><br>快去选购心仪的商品吧!');
            }
            else if (data.code == 400) {
                setCookie('_Coupon' + activityId, couponId, {
                    expires: 1,
                    domain: '.m.yohobuy.com'
                });
                if (isApp) {
                    //showTip('请先登录!');
                } else {
                    location.href = decodeURIComponent(data.data);
                }
            }
            else if (data.message) {
                //showTip(data.message);
                showTip('领取失败<br><br>请稍候再试');
                setCookie('_Coupon' + activityId, null);
            }
            
        },
        error: function() {
            showTip('网络断开连接啦~');
        }
    });
}

/**
 * 获取活动所有的优惠券 
 */
function getAllCoupon(activityId, isApp)
{
    $.ajax({
        type: 'POST',
        url: '/cuxiao/coupon/getall',
        data: 'activityId=' + activityId,
        dataType: 'json',
        success: function(data) {
            if (data.code == 200) {
                showTip('恭喜您,成功领取');
            }
            else if (data.code == 201) {
                showTip('对不起,您已经领取过');
            }
            else if (data.code == 400) {
                if (isApp) {
                    showTip('请先登录!');
                } else {
                    location.href = decodeURIComponent(data.data);
                }
            }
            else if (data.message) {
                //showTip(data.message);
                showTip('系统繁忙,请稍候再试!');
            }
        },
        error: function() {
            showTip('网络断开连接啦~');
        }
    });
}