coupon-option.page.js 4.92 KB
require('admin/coupon-option.page.css');
require('bootstrap-daterangepicker');
require('bootpag/lib/jquery.bootpag.min');

let uploadedFn;
let $upload;

function initUpload() {
    $upload = $('#upload-btn');

    if (!$upload.length) {
        return;
    }

    const imgArr = ['image/jpeg', 'image/jpg', 'image/png'];

    // why? reference: https://github.com/qiniu/js-sdk/issues/266
    Promise.all([
        require('mOxie/bin/js/moxie'),
        require('plupload/js/plupload.dev'),
        require('qiniu-js')
    ]).then(([moxie, plupload]) => {
        window.moxie = moxie;
        window.plupload = plupload;

        Qiniu.uploader({  // eslint-disable-line
            runtimes: 'html5,flash,html4',
            browse_button: 'upload-btn',
            uptoken_url: '/api/getToken',
            unique_names: true,
            multi_selection: false,
            filters: {
                mime_types: [{
                    extensions: 'jpg,jpeg,png,mp4'
                }]
            },
            dragdrop: false,
            domain: 'http://img01.yohoboys.com/',
            max_file_size: '10mb',
            max_retries: 3,
            chunk_size: '10mb',
            auto_start: true,
            init: {
                BeforeUpload: function(up) {
                    window.checkup = up;
                },
                UploadProgress: function() {
                    console.log('上传中...');
                },
                FileUploaded: function(up, file, info) {
                    var domain = up.getOption('domain');
                    var res = JSON.parse(info.response);
                    var url = domain + res.key;

                    uploadedFn && uploadedFn(url, imgArr.indexOf(file.type) >= 0);
                },
                Error: function(up, err, errTip) {
                    console.log(err, errTip);
                }
            }
        });
    });
}

function bindEditPageEvent() {
    initUpload();

    function getStrFormat(strValue) {
        return strValue.replace(/\r\n/g, '<br/>').replace(/\n/g, '<br/>').replace(/\s/g, '&nbsp;');
    }
    function packInfo() {
        let resData = {};
        let error;

        $('.form-imput').each(function() {
            let $this = $(this);
            let val = $this.val();
            let data = $this.data();

            if (!val && !error && data.type !== 'id' && data.type !== 'couponNo' && data.type !== 'shopName') {
                error = data.empty || `请填写${data.type}`;
            }
            if (data.type === 'couponDesc') {
                resData[data.type] = getStrFormat(val);
            } else {
                resData[data.type] = val;
            }
        });

        if (error) {
            alert(error);// eslint-disable line
            return;
        }


        resData.shopLogoUrl = $('.btn-upload-thumb').data('cover');
        resData.status = $('input[name=status]:checked').val();
        resData.type = $('input[name=type]:checked').val();
        if (!resData.shopLogoUrl) {
            alert('请上传商家logo图');
            return;
        }
        return resData;
    }

    function GenUploadedFn($dom) {
        return function(url, isImg) {
            if (!isImg) {
                alert('请上传图片');
            }

            $dom.prev().attr('src', url).removeClass('hide');
            $dom.data('cover', url);
        };

    }
    function initialStr(str) {
        return str.replace(new RegExp('<br/>', 'gm'), '\r\n').replace(new RegExp('&nbsp;', 'gm'), '\s');
    }
    function radioInit() {
        let type = $('#type').val();
        let status = $('#status').val();

        if (type) {
            $($('input[name=type]')[parseInt(type) - 1]).attr('checked', true);
        } else {
            $($('input[name=type]')[0]).attr('checked', true);
        }
        if (status) {
            $($('input[name=status]')[parseInt(status)]).attr('checked', true);
        } else {
            $($('input[name=status]')[0]).attr('checked', true);
        }
        if ($('#id').val()) {
            $('input[name=type]').each(function(index, el) {
                $(el).attr('disabled', true);
            });
            $('textarea').val(initialStr($('#couponDesc').val()));
        }
    }

    radioInit();
    $('.btn-upload-thumb').on('click', function() {
        uploadedFn = new GenUploadedFn($(this));
        $upload[0].click();
    });


    $('.btn-save').on('click', function() {
        let info = packInfo();

        if (!info || window.saveing) {
            return;
        }

        window.saveing = true;

        $.ajax({
            method: 'post',
            url: '/admin/api/coupon/modify',
            data: info
        }).then(res => {
            window.saveing = false;
            if (res.code === 200) {
                alert('保存成功');
            } else {
                alert(res.message || '保存失败');
            }
        });
    });
}

(function() {
    bindEditPageEvent();
}());