coupon.mobile.js 5.82 KB
var $tip, tipItime;

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

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

/**
 * 微信分享 
 */
(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 showTip(con, dur) {
    var content, duration;

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

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

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

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

/**
 * 获取活动的指定优惠券 
 */
function getNamedCoupon(activityId, couponId, isApp)
{
    $.ajax({
        type: 'POST',
        url: '/cuxiao/coupon/getnamed',
        data: 'activityId=' + activityId + '&couponId=' + couponId,
        success: function(data) {
            if (data.code == 200) {
                showTip('恭喜你成功领取品牌优惠券<br><br>快去分享给你的小伙伴吧!');
            }
            else if (data.code == 201) {
                showTip('你已领取过品牌优惠券<br><br>快去选购心仪的商品吧!');
            }
            else if (data.code == 400) {
                if (isApp) {
                    showTip('请先登录!');
                } else {
                    location.href = data.data;
                }
            }
            else if (data.message) {
                //showTip(data.message);
                showTip('系统繁忙,请稍候再试!');
            }
        },
        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 = data.data;
                }
            }
            else if (data.message) {
                //showTip(data.message);
                showTip('系统繁忙,请稍候再试!');
            }
        },
        error: function() {
            showTip('网络断开连接啦~');
        }
    });
}