promotion.js 10.1 KB
/* global wx */
// 1. 优惠券领取
// 2. YOHO币领取

import $ from 'jquery';
import utils from './utils';
import user from './user';
import cookies from './cookies';
import jsonp from './jsonp';

let isSendCoin = false;
let _getCoin = function(data) {
    data.app = utils.queryString();
    data.app.uid = data.uid;

    if (isSendCoin) {
        return;
    }

    isSendCoin = true;
    jsonp({
        url: '//m.yohobuy.com/activity/coin/sendCoin?callback=?',
        timeout: 5000, // 5s
        data: data
    }).then(function(res) {
        isSendCoin = false;
        if (res.code === 200 && res.data) {
            if (res.data.code === 200) {
                utils.showCoinTip({
                    title: '恭喜您,成功领取有货币!',
                    content: '特殊情况下到账有延时<br>请耐心等待',
                    close: true,
                    coin: true,
                    img: res.data.popupImg
                });
            } else if (res.data.code === 501) {
                utils.showCoinTip({
                    title: '领取失败',
                    content: '哎呀,你来的有点早,活动还没开始呢<br/>稍后再来哦',
                    close: true,
                    img: res.data.popupImg
                });
            } else if (res.data.code === 502) {
                utils.showCoinTip({
                    title: '领取失败',
                    content: '抱歉,活动已结束了,下次要快哟~~',
                    close: true,
                    img: res.data.popupImg
                });
            } else if (res.data.code === 503) {
                utils.showCoinTip({
                    title: '已经领取',
                    content: '贪心会长胖,你已经领取过了啦~~',
                    close: true,
                    img: res.data.popupImg
                });
            } else if (res.data.code === 504) {
                utils.showCoinTip({
                    title: '领取失败',
                    content: '哎呀,你来晚了,有货币已经领完了,<br/>下次早点来哦',
                    close: true,
                    img: res.data.popupImg
                });
            } else if (res.data.msg) {
                utils.showCoinTip({
                    title: '领取失败',
                    content: '抱歉,系统错误,有货君正奋力解决中...请稍后再来',
                    close: true,
                    img: res.data.popupImg
                });
            }
        } else if (res.message) {
            utils.showCoinTip({
                title: '领取失败',
                content: '抱歉,系统错误,有货君正奋力解决中...请稍后再来',
                close: true,
                img: res.data && res.data.popupImg
            });
        }
    }, function(){
        isSendCoin = false;
        utils.showCoinTip({
            title: '领取失败',
            content: '抱歉,系统错误,有货君正奋力解决中...请稍后再来',
            close: true
        });
    });
};

let isSendCoupon = false;
let _getCoupon = function(data){
    data.app = utils.queryString();

    data.app.uid = data.app.uid || cookies.cookie('app_uid');
    data.app.app_version = data.app.app_version || cookies.cookie('app_version');
    data.app.client_type = data.app.client_type || cookies.cookie('app_client_type');
    data.app.session_key = data.app.session_key || cookies.cookie('app_session_key');

    if (isSendCoupon) {
        return;
    }

    isSendCoupon = true;
    jsonp({
        url: '//m.yohobuy.com/activity/feature/couponSend?callback=?',
        timeout: 5000, // 5s
        data: data
    }).then(function(res) {
        isSendCoupon = false;
        if (res.code === 200) {
            utils.showTip({
                title: '领取成功',
                content: '恭喜您,领取成功',
                tipClassName: 'coupon-tip',
                close: true
            });
        } else if (res.code === 401) {
            utils.showTip({
                title: '已经领取',
                content: '快去分享给更多<br>喜爱<span class="highlight">潮流</span>的小伙伴吧!',
                tipClassName: 'coupon-tip',
                close: true
            });
        } else if (res.message) {
            utils.showTip({
                title: '领取失败<br>请刷新重新领取',
                content: res.message + '<br>如多次领取失败,请联系客服人员<br>带来不便敬请谅解',
                tipClassName: 'coupon-tip',
                close: false
            });
        }
    }, function(){
        isSendCoupon = false;
        utils.showTip({
            title: '领取失败<br>请刷新重新领取',
            content: '如多次领取失败,请联系客服人员<br>带来不便敬请谅解',
            tipClassName: 'coupon-tip',
            close: false
        });
    });
};

let isSendRedEnevlope = false;
let _getRedEnevlope = function(data){
    data.app = utils.queryString();

    data.app.uid = data.app.uid || cookies.cookie('app_uid');
    data.app.app_version = data.app.app_version || cookies.cookie('app_version');
    data.app.client_type = data.app.client_type || cookies.cookie('app_client_type');
    data.app.session_key = data.app.session_key || cookies.cookie('app_session_key');

    if (isSendRedEnevlope) {
        return;
    }

    isSendRedEnevlope = true;
    jsonp({
        url: '//m.yohobuy.com/activity/feature/redenvelope?callback=?',
        timeout: 5000, // 5s
        data: data
    }).then(function(res) {
        isSendRedEnevlope = false;
        if (res.code === 200) {
            utils.showTip({
                title: '领取成功',
                content: res.message,
                close: true
            });
        } else if (res.message) {
            utils.showTip({
                title: '领取失败<br>请刷新重新领取',
                content: res.message + '<br>如多次领取失败,请联系客服人员<br>带来不便敬请谅解',
                close: false
            });
        }
    }, function(){
        isSendRedEnevlope = false;
        utils.showTip({
            title: '领取失败<br>请刷新重新领取',
            content: '如多次领取失败,请联系客服人员<br>带来不便敬请谅解',
            close: false
        });
    });
};

let _initCoin = function(uid){
    if (uid) {
        let cointoken = cookies.cookie('yoho-coin-token');
        if (cointoken) {
            _getCoin({
                token: cointoken,
                uid: uid
            });

            cookies.setCookie('yoho-coin-token', '');
        }
    }

    $('body').on('click', '.yoho-coin', function() {
        let token = $(this).data('token');
        if (user.uid) {
            _getCoin({
                token: token,
                uid: user.uid
            });
        } else {
            cookies.setCookie('yoho-coin-token', token);

            if (window.__wxjs_environment === 'miniprogram' && typeof wx !== 'undefined' && wx.miniProgram) {
                wx.miniProgram.navigateTo({
                    url: `../bindPhoneNumber/bindPhoneNumber?h5back=${location.href}`
                });
                return;
            }

            if ($('#intimacy-link').length <= 0) {
                $('body').append('<a href=\'' + user.noLoginUrl() + '\' style="display:none;" id="intimacy-link"><span class="intimacy-link"></span></a>');
            }
            $('.intimacy-link').click();
            return;
        }
    });
};

let _initCoupon = function(uid) {
    if (uid) {
        let conpontoken = cookies.cookie('yoho-conpon-token');
        if (conpontoken) {
            _getCoupon({
                token: conpontoken,
                uid: uid
            });

            cookies.setCookie('yoho-conpon-token', '');
        }
    }

    $('body').on('click', '.yoho-conpon', function() {
        let token = $(this).data('token');
        if (user.uid) {
            _getCoupon({
                token: token,
                uid: user.uid
            });
        } else {
            cookies.setCookie('yoho-conpon-token', token);

            if (window.__wxjs_environment === 'miniprogram' && typeof wx !== 'undefined' && wx.miniProgram) {
                wx.miniProgram.navigateTo({
                    url: `../bindPhoneNumber/bindPhoneNumber?h5back=${location.href}`
                });
                return;
            }

            if ($('#intimacy-link').length <= 0) {
                $('body').append('<a href=\'' + user.noLoginUrl() + '\' style="display:none;" id="intimacy-link"><span class="intimacy-link"></span></a>');
            }
            $('.intimacy-link').click();
            return;
        }
    });
};

let _initRedEnvelope = function(uid) {
    if (uid) {
        let redevenlopetoken = cookies.cookie('yoho-redenvelope-token');
        if (redevenlopetoken) {
            _getRedEnevlope({
                token: redevenlopetoken,
                uid: uid
            });

            cookies.setCookie('yoho-redenvelope-token', '');
        }
    }

    $('body').on('click', '.yoho-redenvelope', function() {
        let token = $(this).data('token');
        if (user.uid) {
            _getRedEnevlope({
                token: token,
                uid: user.uid
            });
        } else {
            cookies.setCookie('yoho-redenvelope-token', token);

            if (window.__wxjs_environment === 'miniprogram' && typeof wx !== 'undefined' && wx.miniProgram) {
                wx.miniProgram.navigateTo({
                    url: `../bindPhoneNumber/bindPhoneNumber?h5back=${location.href}`
                });
                return;
            }

            if ($('#intimacy-link').length <= 0) {
                $('body').append('<a href=\'' + user.noLoginUrl() + '\' style="display:none;" id="intimacy-link"><span class="intimacy-link"></span></a>');
            }
            $('.intimacy-link').click();
            return;
        }
    });
};

export default {
    init(uid) {
        _initCoin(uid);  // 初始化有货币
        _initCoupon(uid); // 初始化优惠券
        _initRedEnvelope(uid); // 初始化红包
    }
};