coupon.js 4.26 KB
/**
 * 领券频道
 * @author: 赵彪<bill.zhao@yoho.cn>
 * @date: 2016/04/14
 */
var $ = require('yoho.jquery'),
    lazyLoad = require('yoho.lazyload'),
    Dialog = require('../common/dialog').Dialog;


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;
    }
};

//加载底部图片
lazyLoad($('img.lazy'));

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


$('.slide-container').slider();

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

    newAlert.show();
}

// 配置弹窗
alertConfig = {
    success: {
        content: '恭喜您,成功领取优惠券',
        subContents: ['特殊情况下到账有延时', '请耐心等待'],
        className: 'subcontent-dialog',
        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(err) {
            var Alert = require('../common/dialog').Alert;

            new Alert('网络异常').show();
        }
    });
}

$('.enable .info').on('click', function(e) {
    e.preventDefault();
    requestCoupon($(this).closest('a').data('id'));
    redirect.gunangSrc = $(this).closest('a').get(0).href;
});