/** * 账号相关js * * @author CaiAoLin * @date 2018/1/26 * @version */ $(document).ready(function() { autoFlashHeight(); $('#addCert').click(function() { postData('/profile/cert/save', { type: 'add_cert' }, function(result) { console.log(result); const html = insertCertHtml(result.total, result.data); $('#certs').prepend(html); }); }); $('body').on('click', '#certs .add-jx-btn', function() { const _self = $(this); postData('/profile/cert/save', { type: 'add_jx', id: $(this).parents('.one-cert').data('cid') }, function(result) { console.log(result); const html = insertJxHtml(result.data, result.jxData); _self.parents('.pull-right').siblings('.all-jx').prepend(html); console.log(_self.parents('.pull-right').siblings('.all-jx').children('.one-jx').eq(0).find('.jx-date')); _self.parents('.pull-right').siblings('.all-jx').children('.one-jx').eq(0).find('.jx-date').datepicker({ onShow: function (res) { res.$el.attr('readOnly', true); }, onHide: function (res, animationCompleted) { if(animationCompleted) { res.$el.attr('readOnly', false); const val = res.$el.val(); const oldVal = res.$el.attr('data-old-date') || ''; // 日期格式判断 if (val !== '' && !(isNaN(val) && !isNaN(Date.parse(val)))) { toastr.error('日期格式有误!'); res.$el.val(oldVal); if (oldVal === '') { res.clear(); } else { res.selectDate(new Date(oldVal)); } } else if(val !== oldVal) { const data = { id: res.$el.parents('.one-cert').data('cid'), jxid: res.$el.parents('.one-jx').data('jxid'), key: 'date', value: val, }; updateJxDataFun(data); res.$el.attr('data-old-date', val); } } } }); }); }); $('body').on('change', '#certs select', function() { const value = parseInt($(this).val()); const key = $(this).data('type'); const update_data = { id: $(this).parents('.one-cert').data('cid'), key, value, }; postData('/profile/cert/save', { type: 'update_cert', update_data }, function(result) { console.log(result); }); }); $('body').on('change', '#certs .one-cert-msg input[type="text"]', function() { const value = $(this).val(); const key = $(this).data('type'); const update_data = { id: $(this).parents('.one-cert').data('cid'), key, value, }; console.log(update_data); postData('/profile/cert/save', { type: 'update_cert', update_data }, function(result) { console.log(result); }); }); $('body').on('change', '#certs .one-jx input[type="text"]', function() { const value = $(this).val(); const key = $(this).data('type'); const update_data = { id: $(this).parents('.one-cert').data('cid'), jxid: $(this).parents('.one-jx').data('jxid'), key, value, }; console.log(update_data); updateJxDataFun(update_data); }); // 设置默认值 $('.jx-date').each(function() { const defaultValue = $(this).val(); // 获取当前元素的 value 属性作为默认值 if (defaultValue) { // 初始化日期选择器,并设置默认值 $(this).datepicker().data('datepicker').selectDate(new Date(defaultValue)); } }); $('body .jx-date').datepicker({ onShow: function (res) { res.$el.attr('readOnly', true); }, onHide: function (res, animationCompleted) { if(animationCompleted) { res.$el.attr('readOnly', false); const val = res.$el.val(); const oldVal = res.$el.attr('data-old-date') || ''; // 日期格式判断 if (val !== '' && !(isNaN(val) && !isNaN(Date.parse(val)))) { toastr.error('日期格式有误!'); res.$el.val(oldVal); if (oldVal === '') { res.clear(); } else { res.selectDate(new Date(oldVal)); } } else if(val !== oldVal) { const data = { id: res.$el.parents('.one-cert').data('cid'), jxid: res.$el.parents('.one-jx').data('jxid'), key: 'date', value: val, }; updateJxDataFun(data); res.$el.attr('data-old-date', val); } } } }); $('body').on('click', '#certs .del-cert-btn', function() { $('#delete-cert-cid').val($(this).attr('data-cid')); $('#delete-cert-jxid').val($(this).attr('data-jxid')); if ($(this).attr('data-filename')) { console.log($(this).attr('data-filename'), $(this).attr('data-jxid')); const txt = $(this).attr('data-jxid') ? '培训证明:' : '证书附件:'; $('#delete-cert-title').text(txt + $(this).attr('data-filename')); $('#delete-cert-type').val('file'); } else { $('#delete-cert-title').text($(this).parents('.modal-header').find('b').text()); $('#delete-cert-type').val('cert'); } }); $('#delete-cert-btn').click(function() { const data = { id: $('#delete-cert-cid').val(), jxid: $('#delete-cert-jxid').val(), type: $('#delete-cert-type').val(), }; console.log(data); const _self = $(this); postData('/profile/cert/save', { type: 'del_cert', delete_data: data }, function(result) { _self.parents('.modal').modal('hide'); if (data.type === 'file') { if (data.jxid) { $(`.one-cert[data-cid="${data.id}"] .one-jx[data-jxid="${data.jxid}"]`).find('.file-show').html(''); } else { $(`.one-cert[data-cid="${data.id}"]`).find('.one-cert-msg').find('.file-show').html(''); } } else { if (data.jxid) { $(`.one-cert[data-cid="${data.id}"] .one-jx[data-jxid="${data.jxid}"]`).remove(); // 教育信息重新排序 for (let i = 0; i < $(`.one-cert[data-cid="${data.id}"] .one-jx`).length; i++) { $(`.one-cert[data-cid="${data.id}"] .one-jx`).eq(i).find('.jx-num').text($(`.one-cert[data-cid="${data.id}"] .one-jx`).length - i); } } else { $(`.one-cert[data-cid="${data.id}"]`).remove(); // 证书信息重新排序 for (let i = 0; i < $(`#certs .one-cert`).length; i++) { $(`#certs .one-cert`).eq(i).find('.cert-num').text($(`#certs .one-cert`).length - i); } } } }); }); // 上传证书附件 $('body').on('change', '#certs .cert-file-upload', function () { const file = this.files[0]; const formData = new FormData(); if (file === undefined) { toastr.error('未选择上传文件!'); $(this).val(''); return false; } const ext = file.name.toLowerCase().split('.').splice(-1)[0]; const imgStr = /(jpg|jpeg|png|bmp|BMP|JPG|PNG|JPEG|pdf|PDF)$/; if (!imgStr.test(ext)) { toastr.error('请上传正确的图片或pdf格式文件'); $(this).val(''); return } const filesize = file.size; if (filesize > 30 * 1024 * 1024) { toastr.error('上传的文件大小不能超过30MB!'); $(this).val(''); return false; } const id = $(this).parents('.one-cert').data('cid'); formData.append('type', 'upload_cert'); formData.append('id', id); formData.append('file', file); console.log(formData); $(this).val(''); const _self = $(this).parents('.file-show'); postDataWithFile('/profile/cert/upload', formData, function (result) { _self.html(`
${result.file_name}  删除
`); }); }); // 上传证书附件 $('body').on('change', '#certs .jx-file-upload', function () { const file = this.files[0]; const formData = new FormData(); if (file === undefined) { toastr.error('未选择上传文件!'); $(this).val(''); return false; } const ext = file.name.toLowerCase().split('.').splice(-1)[0]; const imgStr = /(jpg|jpeg|png|bmp|BMP|JPG|PNG|JPEG|pdf|PDF)$/; if (!imgStr.test(ext)) { toastr.error('请上传正确的图片或pdf格式文件'); $(this).val(''); return } const filesize = file.size; if (filesize > 30 * 1024 * 1024) { toastr.error('上传的文件大小不能超过30MB!'); $(this).val(''); return false; } const id = $(this).parents('.one-cert').data('cid'); const jxid = $(this).parents('.one-jx').data('jxid'); formData.append('type', 'upload_jx'); formData.append('id', id); formData.append('jxid', jxid); formData.append('file', file); console.log(formData); $(this).val(''); const _self = $(this).parents('.file-show'); postDataWithFile('/profile/cert/upload', formData, function (result) { _self.html(`
${result.file_name}  删除
`); }); }) function updateJxDataFun(data) { console.log(data); postData('/profile/cert/save', { type: 'update_jx', update_data: data }, function(result) { // console.log(result); }); } function insertJxHtml(data, jxdata) { const html = `
`; return html; } function insertCertHtml(i, data) { let regHtml = ``; for (const r of certRegConst) { regHtml += ``; } let qualHtml = ``; for (const q of certQualConst) { qualHtml += ``; } const html = `
持证情况:
继续教育情况:
`; return html; } });