spread.page.js 3.06 KB
/**
 * 我的推广
 * @author: yyq<yanqing.yang@yoho.cn>
 * @date: 2017/6/22
 */

var $ = require('yoho-jquery');

var Clipboard = require('clipboard'),
    Dialog = require('../common/dialog').Dialog;

var trendWordTpl = require('../../hbs/home/spread/trend-word.hbs');

var $spreadWord = $('.spread-id'),
    $editBtn = $('.edit-word'),
    $copySuccTip = $('.succ-tip'),
    $qrcodeImg = $('.spread-code');

var reasonHtml = $('.trend-word-tpl').html();

var clipboard;
var defaultWord = $spreadWord.data('word');

require('../common');
require('yoho-jquery-qrcode');

function reseatWordSize(word) {
    if (!word) {
        return;
    }

    $spreadWord.css('font-size', 22 + parseInt((14 - word.length) / 7, 10) * 4);
}

function saveTrendWord(dg) {
    var that = this;
    var $errTip = dg.$el.find('.err-tip');
    var word = $.trim(dg.$el.find('.trend-word-input').val());

    if (this.saving) {
        return;
    }

    if (!word) {
        $errTip.html('请输入潮流口令');
        return;
    }

    this.saving = true;

    $.ajax({
        url: '/home/spread/updateword',
        type: 'POST',
        data: {word: word}
    }).done(function(data) {
        if (data.code === 200) {
            $spreadWord.text(word);
            defaultWord = word;
            reseatWordSize(defaultWord);

            dg.close && dg.close();
        } else {
            $errTip.html(data.message || '网络异常,请稍后重试');
        }
    }).fail(function() {
        $errTip.html('网络异常,请稍后重试');
    }).always(function() {
        that.saving = false;
    });
}

// 生成二维码
$qrcodeImg.qrcode({
    render: 'div', // 显示方式,canvas,image和div
    text: $qrcodeImg.data('url'), // 二维码的内容
    size: parseInt($qrcodeImg.width(), 10) // 大小
});

clipboard = new Clipboard('.copy-url', {
    text: function(trigger) {
        return trigger.value;
    }
});

clipboard.on('success', function() {
    $copySuccTip.show();

    setTimeout(function() {
        $copySuccTip.fadeOut();
    }, 2000);
});

$editBtn.on('click', function(e) {
    var _d = new Dialog({
        content: trendWordTpl({
            img: $spreadWord.data('img'),
            word: defaultWord,
            content: reasonHtml
        }),
        className: 'edit-trendword-dialog',
        btns: [
            {
                id: 'trend-edit-cancel',
                btnClass: ['trend-edit-cancel'],
                name: '暂不',
                cb: function() {
                    if (e.isTrigger && !window.cookie('no_cue_trendword')) {
                        window.setCookie('no_cue_trendword', 1, {expires: 30});
                    }
                    _d.close();
                }
            },
            {
                id: 'trend-edit-sure',
                btnClass: ['trend-edit-sure'],
                name: '设置',
                cb: function() {
                    saveTrendWord(_d);
                }
            }
        ]
    }).show();
});

reseatWordSize(defaultWord);

if (!defaultWord && !window.cookie('no_cue_trendword')) {
    $editBtn.trigger('click');
}