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

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

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

/**
 * 微信分享 
 */
(function($) {
    if (!wx) {
        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.text(content).show();

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

/**
 * 获取活动的指定优惠券 
 */
function getNamedCoupon(activityId, couponId)
{
    $.ajax({
        type: 'POST',
        url: '/cuxiao/coupon/getnamed',
        data: 'activityId=' + activityId + '&couponId=' + couponId,
        success: function(data) {
            if (data.code == 200) {
                showTip('领取成功');
            }
            else if (data.code == 400) {
                location.href = data.data;
            }
            else if (data.message) {
                showTip('领取失败');
            }
        },
        error: function() {
            showTip('网络断开连接啦~');
        }
    });
}

/**
 * 获取活动所有的优惠券 
 */
function getAllCoupon(activityId)
{
    $.ajax({
        type: 'POST',
        url: '/cuxiao/coupon/getall',
        data: 'activityId=' + activityId,
        dataType: 'json',
        success: function(data) {
            if (data.code == 200) {
                showTip('领取成功');
            }
            else if (data.code == 400) {
                location.href = data.data;
            }
            else if (data.message) {
                showTip('领取失败');
            }
        },
        error: function() {
            showErrTip('网络断开连接啦~');
        }
    });
}