suggest.page.js 4.49 KB
/**
 * 个人中心--意见反馈
 */

var $ = require('yoho-jquery'),
    Hammer = require('yoho-hammer'),
    lazyLoad = require('yoho-jquery-lazyload'),
    Handlebars = require('yoho-handlebars');

var diaLog = require('../plugin/dialog');
var saveImage = require('./save-image');

var $uploadImgList = $('.upload-img-list'),
    headerNavHammer,
    formHammer,
    imgTpl,
    imgTemplate,
    $likeBtn = $('.suggest-item .like-btn'),
    $disLikeBtn = $('.suggest-item .dislike-btn'),
    $imgAdd = $('.img-add'),
    imgStr = '',
    uploadImgNum = 0;

require('./jquery.upload');
require('../common');

imgTpl = '{{# imgList}}' +
    '<li>' +
        '{{# imgUrl}}<img src="{{.}}" />' +
        '<span class="upload-img-remove"></span>{{/ imgUrl}}' +
    '</li>' +
    '{{/ imgList}}';

imgTemplate = Handlebars.compile(imgTpl);


$('#upload-img').upload({
    auto: true,
    fileType: 'image/*',
    uploadScript: '/api/upload/image',
    fileObjName: 'filename',
    fileSizeLimit: 300,
    height: '100%',
    width: '100%',
    multi: false,
    formData: {
        bucket: 'suggest'
    },
    onAddQueueItem: function(files) {

        // TODO
        $uploadImgList.html(imgTemplate({
            imgList: true
        }));
    },
    onUploadComplete: function(file, data) {
        data = saveImage.saveImage(data);

        $uploadImgList.html('');
        imgStr = data.imgList[0].imgRelUrl;
        $uploadImgList.html(imgTemplate(data));
        $imgAdd.hide();
        uploadImgNum++;
    }
});

lazyLoad();

headerNavHammer = new Hammer(document.getElementById('yoho-header'));

headerNavHammer.on('tap', function(e) {
    var suggestText = $('#suggest-textarea').val(),
        textReg = /\S+/;


    if ($(e.target).hasClass('nav-btn')) {

        if (!textReg.test(suggestText)) {
            diaLog.showDialog({
                autoHide: true,
                dialogText: '意见不能为空'
            });

            return;
        }

        $.ajax({
            method: 'post',
            url: '/home/savesuggest',
            data: {
                content: suggestText,
                image: imgStr
            }
        }).then(function(data) {
            if (data.code === 200) {
                diaLog.showDialog({
                    autoHide: true,
                    dialogText: '提交成功'
                });
                setTimeout(function() {
                    location.pathname = 'home/suggest';
                }, 2000);
            } else {
                diaLog.showDialog({
                    autoHide: true,
                    dialogText: '提交失败~'
                });
            }
        }).fail(function() {

            diaLog.showDialog({
                autoHide: true,
                dialogText: '网络错误~'
            });
        });
    }
});

if (document.getElementById('img-form') !== null) {
    formHammer = new Hammer(document.getElementById('img-form'));

    formHammer.on('tap', function(e) {
        if ($(e.target).hasClass('upload-img-remove')) {
            $uploadImgList.html('');
            imgStr = '';
            uploadImgNum--;
            setTimeout(function() {
                $imgAdd.show();
            }, 50);

        }
    });
}

// 点赞与取消点赞
$likeBtn.bind('click', function() {

    var id = $(this).closest('.suggest-item').attr('data-id'),
        $that = $(this);

    $.ajax({
        method: 'post',
        url: '/home/upAndDown',
        data: {
            suggest_id: id,
            reliable: 1
        }
    }).then(function(data) {
        if (data.code === 200) {
            $that.closest('.suggest-type').removeClass('show');
            $that.closest('.suggest-item').find('.suggest-good').addClass('show');
        }
    }).fail(function(data) {

        // TODO

        diaLog.showDialog({
            autoHide: true,
            dialogText: '网络错误~'
        });
    });
});

$disLikeBtn.bind('click', function() {

    var id = $(this).closest('.suggest-item').attr('data-id'),
        $that = $(this);

    $.ajax({
        method: 'post',
        url: '/home/upAndDown',
        data: {
            suggest_id: id,
            reliable: 2
        }
    }).then(function(data) {
        if (data.code === 200) {
            $that.closest('.suggest-type').removeClass('show');
            $that.closest('.suggest-item').find('.suggest-bad').addClass('show');
        }
    }).fail(function(data) {

        // TODO

        diaLog.showDialog({
            autoHide: true,
            dialogText: '网络错误~'
        });
    });
});