3party-bind.js 4.51 KB
/**
 * Created by liuchuanyang on 2016/11/23.
 */

var $ = require('yoho-jquery');
var Dialog = require('../plugins/dialog').Dialog;
var labelMap = {
    renren: '人人',
    douban: '豆瓣',
    qq: '腾讯',
    sina: '新浪微博',
    alipay: '支付宝',
    wechat: '微信'
};
var csrfToken = $('input[name=_csrf]').val();

document.domain = 'yohobuy.com';

// MDialog
function MDialog(opts) {
    var that = this;
    var option = $.extend(true, {}, {
        className: 'm-dialog',
        closeIcon: false
    }, opts);

    if (option.title) {
        option.content = '<div class="dialog-title">' + option.title +
                      '<span class="iconfont dialog-close" data-role="dialog-close">&#xe60d;</span>' +
                  '</div>' +
                  '<div class="dialog-content">' + option.content + '</div>';
    }

    Dialog.call(this, option);

    this.$el.on('click', '[data-role=dialog-close]', function() {
        that.close();
    });
}

MDialog.prototype = new Dialog({
    inherit: true
});
MDialog.prototype.constructor = MDialog;

function malert(content, title, cb) {

    var id = '' + new Date().getTime();
    var md = new MDialog({
        content: content,
        title: title || '提示',
        btns: [{
            id: id,

            // btnClass: ['alert-sure'],
            name: '确定',
            cb: function() {
                md.close();
                (typeof cb === 'function') && cb();
            }
        }]
    });

    md.show();
}

function mconfirm(content, title, ok, cancel) {

    var id = '' + new Date().getTime();
    var md = new MDialog({
        content: content,
        title: title || '提示',
        btns: [{
            id: 'ok-' + id,

            // btnClass: ['alert-sure'],
            name: '确定',
            cb: function() {
                md.close();
                (typeof ok === 'function') && ok();
            }
        }, {
            id: 'cancel-' + id,
            btnClass: ['btn-cancel'],
            name: '取消',
            cb: function() {
                md.close();
                (typeof cancel === 'function') && cancel();
            }
        }]
    });

    md.show();
}

function reloadPage() {

    var url = window.location.href;
    var idx = url.lastIndexOf('?');

    if (idx > 0) {
        url = url.substr(0, idx);
    }

    window.location = url + '?pt=bind';
}

$('#Y_bindAccount').on('click', '.account-item .account-item-title', function() {

    var $t = $(this);
    var $b = $t.closest('.account-item').find('.account-item-body');
    var $a = $t.find('a');

    $a.removeClass('del-binding');
    $b.toggle();

    if ($b.is(':visible')) {
        $a.html('收起');
    } else {
        if ($a.attr('data-binding') === '1') {
            $a.html('解绑');
            $a.addClass('del-binding');
        } else {
            $t.find('a').html('绑定');
        }
    }
});


$('#Y_bindAccount').on('click', '.account-item .bind-btn', function() {

    var url = $(this).attr('data-url');

    window.open(url, '3partybind', 'width=600px;height=400px');
});

$('#Y_bindAccount').on('click', '.account-item .cancel-bind-btn', function() {

    var url = $(this).attr('data-url');

    mconfirm(
        '<i class="iconfont tip-icon">&#xe61f;</i>确定要解除绑定' + labelMap[$(this).attr('data-type')] + '?',
        '您确定要解除绑定?',
        function() {
            $.ajax({
                url: url,
                type: 'POST',
                data: {_csrf: csrfToken},
                dataType: 'json'
            })
                .then(function(data) {
                    if (data.code === 200) {
                        malert('解绑成功!', '解绑成功', function() {
                            reloadPage();
                        });
                    } else {
                        malert('解绑失败,请稍后再试!', '解绑失败');
                    }
                })
                .fail();
        }
    );
});

window.bindTip = function(ret, type, message) {

    var typeLabel = labelMap[type] || '';

    if (ret === 'success') {
        malert('<i class="iconfont tip-icon">&#xe638;</i>您已成功绑定' + typeLabel + '账号!', '绑定成功', function() {
            reloadPage();
        });
    } else {
        malert('<i class="iconfont tip-icon">&#xe61f;</i>' + (message || '绑定失败!'), '绑定' + typeLabel + '账号');
    }
};

$(function() {
    var qs = window.queryString();

    if (qs && qs.pt === 'bind') {
        $('#Y_bindAccount h2.sub-title').trigger('click');
    }
});