define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) { var Controller = { index: function () { Table.api.init({ extend: { index_url: 'service/index' + location.search, add_url: 'service/add', edit_url: 'service/edit', del_url: 'service/del', multi_url: 'service/multi', dragsort_url: '', table: 'service', } }); var table = $("#table"); var customFilter = { cat_id: '', name: '', price_type: '', status: '', work_code: '', has_spec: '', has_addon: '' }; // 监听高级筛选表单【筛选】按钮 $('#btn-srv-search').on('click', function () { customFilter.cat_id = $.trim($('#f-cat_id').val()); customFilter.name = $.trim($('#f-name').val()); customFilter.price_type = $.trim($('#f-price_type').val()); customFilter.status = $.trim($('#f-status').val()); customFilter.work_code = $.trim($('#f-work_code').val()); customFilter.has_spec = $.trim($('#f-has_spec').val()); customFilter.has_addon = $.trim($('#f-has_addon').val()); table.bootstrapTable('refresh', { pageNumber: 1 }); }); // 回车键快捷查询 $('#f-name').on('keypress', function (e) { if (e.which === 13) { $('#btn-srv-search').trigger('click'); } }); // 监听高级筛选表单【重置】按钮 $('#btn-srv-reset').on('click', function () { $('.srv-filter-field').val(''); customFilter = { cat_id: '', name: '', price_type: '', status: '', work_code: '', has_spec: '', has_addon: '' }; table.bootstrapTable('refresh', { pageNumber: 1 }); }); // 弹窗宽高设置自适应 table.on('post-body.bs.table', function () { $(".btn-pricing-main", this).attr("data-area", '["92%", "88%"]').data("area", ["92%", "88%"]); $(".btn-pricing-addon", this).attr("data-area", '["90%", "85%"]').data("area", ["90%", "85%"]); $(".btn-editone, .btn-gallery-srv", this).attr("data-area", '["88%", "85%"]').data("area", ["88%", "85%"]); $(".btn-detail-srv", this).attr("data-area", '["88%", "85%"]').data("area", ["88%", "85%"]); }); $(".btn-add, .btn-edit").attr("data-area", '["88%", "85%"]').data("area", ["88%", "85%"]); table.bootstrapTable({ url: $.fn.bootstrapTable.defaults.extend.index_url, pk: 'id', sortName: 'weigh', sortOrder: 'desc', commonSearch: false, search: false, fixedColumns: true, fixedRightNumber: 1, queryParams: function (params) { var filter = params.filter ? JSON.parse(params.filter) : {}; var op = params.op ? JSON.parse(params.op) : {}; if (customFilter.cat_id) { filter.cat_id = customFilter.cat_id; op.cat_id = '='; } if (customFilter.name) { filter.name = customFilter.name; op.name = 'LIKE'; } if (customFilter.price_type) { filter.price_type = customFilter.price_type; op.price_type = '='; } if (customFilter.status) { filter.status = customFilter.status; op.status = '='; } if (customFilter.work_code) { params.work_code = customFilter.work_code; } if (customFilter.has_spec !== '') { params.has_spec = customFilter.has_spec; } if (customFilter.has_addon !== '') { params.has_addon = customFilter.has_addon; } params.filter = JSON.stringify(filter); params.op = JSON.stringify(op); return params; }, columns: [ [ {checkbox: true}, {field: 'index', title: '序号', operate: false, formatter: Table.api.formatter.index, width: '55px'}, {field: 'cat.name', title: '所属类目', operate: false, width: '105px', formatter: function (val) { return '' + (val || '未归类') + ''; }}, {field: 'image', title: '图片', operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image, width: '65px'}, {field: 'name', title: '商品名称', operate: false, align: 'left', formatter: function (val) { return '' + val + ''; }}, {field: 'price_type', title: '计价模式', operate: false, width: '85px', formatter: function (val) { if (val === 'fixed') { return '一口价'; } else if (val === 'quote') { return '师傅报价'; } return '' + (val || '未知') + ''; }}, {field: 'unit', title: '单位', operate: false, width: '55px'}, { field: 'specs_list', title: '细分规格', operate: false, align: 'left', formatter: function (val, row) { var list = row.specs_list || val || []; if (!Array.isArray(list) || !list.length) { return '标准单品 (无规格)'; } var preview = list.slice(0, 2).join('、'); if (list.length > 2) { preview += ' 等' + list.length + '款'; } return '' + preview + ''; } }, { field: 'service_types_list', title: '主服务定价', operate: false, align: 'left', formatter: function (val, row) { var list = row.service_types_list || []; if (!Array.isArray(list) || !list.length) { return '-'; } var html = []; for (var i = 0; i < list.length; i++) { var st = list[i]; var isFixed = (typeof st.support_fixed !== 'undefined') ? (Number(st.support_fixed) === 1) : (Number(st.price) > 0); if (isFixed && Number(st.price) > 0) { html.push('' + st.name + ' ¥' + Number(st.price || 0).toFixed(0) + ''); } else { html.push('' + st.name + ' (报价)'); } } return html.join(''); } }, { field: 'addons_list', title: '增值服务加价', operate: false, align: 'left', formatter: function (val, row) { var list = row.addons_list || []; if (!Array.isArray(list) || !list.length) { return '无增项'; } var html = []; for (var i = 0; i < list.length; i++) { var ad = list[i]; html.push('' + ad.name + ' +¥' + Number(ad.price).toFixed(0) + '/' + (ad.unit || '台') + ''); } return html.join(''); } }, {field: 'weigh', title: '排序', operate: false, width: '60px', sortable: true}, {field: 'status', title: __('Status'), width: '75px', searchList: {"normal": __('Normal'), "hidden": __('Hidden')}, formatter: Table.api.formatter.status}, { field: 'operate', title: __('Operate'), table: table, class: 'col-operate-fixed', width: '260px', events: Table.api.events.operate, buttons: [ { name: 'pricing_main', text: '主服务定价', title: function (row) { return '配置【' + row.name + '】主服务价格'; }, icon: 'fa fa-wrench', classname: 'btn btn-xs btn-primary btn-dialog btn-pricing-main', url: 'service/pricing_main', extend: 'data-area=\'["92%","88%"]\'' }, { name: 'pricing_addon', text: '增值服务', title: function (row) { return '配置【' + row.name + '】增值服务加价'; }, icon: 'fa fa-plus-circle', classname: 'btn btn-xs btn-warning btn-dialog btn-pricing-addon', url: 'service/pricing_addon', extend: 'data-area=\'["90%","85%"]\' style="background:#ea580c;border-color:#ea580c;"' }, { name: 'edit', text: '编辑商品', title: function (row) { return '编辑【' + row.name + '】商品档案与规格'; }, icon: 'fa fa-pencil', classname: 'btn btn-xs btn-success btn-dialog btn-editone', url: 'service/edit', extend: 'data-area=\'["88%","85%"]\'' }, { name: 'gallery', text: '图库', title: function (row) { return '【' + row.name + '】系统图库管理'; }, icon: 'fa fa-picture-o', classname: 'btn btn-xs btn-info btn-dialog btn-gallery-srv', url: function (row) { return 'service/gallery/index?service_id=' + row.id + '&cat_id=' + row.cat_id; } } ], formatter: Table.api.formatter.operate } ] ] }); Table.api.bindevent(table); }, // ==================== 1. 主服务定价独立弹窗 ==================== pricing_main: function () { // 获取已开启的主服务工项列表 function getEnabledMainServices() { var list = []; $('#table-parent-main tbody tr.main-pool-row').each(function () { var $r = $(this); if ($r.find('.st-enable').is(':checked')) { var code = String($r.data('code') || ''); var name = String($r.data('name') || ''); var price = parseFloat($r.find('.st-price').val()) || 0; var isDef = $r.find('.st-default').is(':checked') ? 1 : 0; var supportFixed = parseInt($r.data('support-fixed'), 10) === 1 ? 1 : 0; list.push({ code: code, name: name, price: price, is_default: isDef, support_fixed: supportFixed }); } }); return list; } // 读取已有的规格数据 var specsData = []; try { var rawSpecs = $('#specs-data-source').html(); if (rawSpecs) specsData = JSON.parse(rawSpecs) || []; } catch (e) { specsData = []; } // 动态渲染下级规格主服务专属定价表 function renderMainSpecsTable() { if (!specsData.length) return; var enabledMain = getEnabledMainServices(); // 表头 var theadHtml = []; if (!enabledMain.length) { theadHtml.push('请先在上方开启主服务工项并设置一口价'); } else { theadHtml.push(''); theadHtml.push(' 序号'); theadHtml.push(' 细分规格/款式名称'); theadHtml.push(' 参考图'); for (var m = 0; m < enabledMain.length; m++) { var ms = enabledMain[m]; if (ms.support_fixed === 0) { theadHtml.push(' ' + ms.name + '
(师傅报价)'); } else { theadHtml.push(' ' + ms.name + (ms.is_default ? ' *' : '') + '
基准:¥' + Number(ms.price).toFixed(2) + ''); } } theadHtml.push(' 规格施工说明'); theadHtml.push(''); } $('#specs-main-thead').html(theadHtml.join('')); // 表体 var tbodyHtml = []; for (var i = 0; i < specsData.length; i++) { var sp = specsData[i]; var imgFull = sp.image ? (sp.image.indexOf('http') === 0 || sp.image.indexOf('/assets/') === 0 ? sp.image : Fast.api.cdnurl(sp.image)) : ''; var sPrices = sp.prices_map || sp.service_prices || {}; if (typeof sPrices === 'string') { try { sPrices = JSON.parse(sPrices) || {}; } catch(e) { sPrices = {}; } } tbodyHtml.push(''); tbodyHtml.push(' ' + (i + 1) + ''); tbodyHtml.push(' ' + (sp.name || '') + ''); tbodyHtml.push(' '); if (imgFull) { tbodyHtml.push(' '); } else { tbodyHtml.push(' 无图'); } tbodyHtml.push(' '); if (!enabledMain.length) { tbodyHtml.push(' 未启用工项'); } else { for (var j = 0; j < enabledMain.length; j++) { var s = enabledMain[j]; var pCode = s.code; if (s.support_fixed === 0) { tbodyHtml.push(' '); tbodyHtml.push(' 师傅报价'); tbodyHtml.push(' '); } else { var parentPrice = Number(s.price || 0).toFixed(2); var hasCustom = typeof sPrices[pCode] !== 'undefined' && Number(sPrices[pCode]) > 0; var customVal = hasCustom ? Number(sPrices[pCode]).toFixed(2) : ''; tbodyHtml.push(' '); tbodyHtml.push('
'); tbodyHtml.push(' ¥'); tbodyHtml.push(' '); tbodyHtml.push('
'); tbodyHtml.push(' '); } } } tbodyHtml.push(' ' + (sp.description || '-') + ''); tbodyHtml.push(''); } $('#specs-main-tbody').html(tbodyHtml.join('')); } renderMainSpecsTable(); // 监听工项开关与价格变动 $(document).on('change', '.st-enable', function () { var $row = $(this).closest('tr'); var checked = $(this).is(':checked'); $row.toggleClass('is-row-disabled', !checked); if (checked) { $row.find('.st-active-panel').css('display', 'inline-flex'); $row.find('.st-off-hint').hide(); } else { $row.find('.st-active-panel').hide(); $row.find('.st-off-hint').show(); } $('#stat-main-cnt').text($('.st-enable:checked').length); renderMainSpecsTable(); }); $(document).on('input change', '.st-price', function () { renderMainSpecsTable(); }); $(document).on('change', '.st-default', function () { $('.srv-default-label').removeClass('is-def-active'); $(this).closest('.srv-default-label').addClass('is-def-active'); renderMainSpecsTable(); }); // 规格输入框专属价高亮与数据缓存 $(document).on('input change', '.smi-main-spec-input', function () { var specId = $(this).data('spec-id'); var code = $(this).data('code'); var val = $.trim($(this).val()); var numVal = parseFloat(val); var $box = $(this).closest('.srv-matrix-input-box'); for (var i = 0; i < specsData.length; i++) { if (specsData[i].id == specId) { var pMap = specsData[i].prices_map || specsData[i].service_prices || {}; if (typeof pMap === 'string') { try { pMap = JSON.parse(pMap) || {}; } catch(e) { pMap = {}; } } if (!isNaN(numVal) && numVal > 0) { pMap[code] = numVal; $(this).addClass('is-custom-main'); $box.addClass('is-active-main'); } else { delete pMap[code]; $(this).removeClass('is-custom-main'); $box.removeClass('is-active-main'); } specsData[i].prices_map = pMap; break; } } }); // 提交前收集并注入表单 Form.api.bindevent($("form[role=form]"), null, null, function () { var enabledMain = getEnabledMainServices(); $('#service_types_raw').val(JSON.stringify(enabledMain)); // 注入各规格专属价字典 $('input.spec-price-submit-hidden').remove(); var $form = $('#pricing-main-form'); for (var i = 0; i < specsData.length; i++) { var sp = specsData[i]; var pMap = sp.prices_map || sp.service_prices || {}; if (typeof pMap === 'string') { try { pMap = JSON.parse(pMap) || {}; } catch(e) { pMap = {}; } } var clean = {}; for (var c in pMap) { var v = parseFloat(pMap[c]); if (!isNaN(v) && v > 0) clean[c] = v; } $form.append(''); } return true; }); }, // ==================== 2. 增值服务加价独立弹窗 ==================== pricing_addon: function () { // 获取已开启的增值服务列表 function getEnabledAddonServices() { var list = []; $('#table-parent-addon tbody tr.addon-pool-row').each(function () { var $r = $(this); if ($r.find('.ad-bind').is(':checked')) { var code = String($r.data('code') || ''); var name = String($r.data('name') || ''); var unit = String($r.data('unit') || '台'); var price = parseFloat($r.find('.ad-price').val()) || 0; list.push({ code: code, name: name, unit: unit, price: price }); } }); return list; } var specsData = []; try { var rawSpecs = $('#specs-data-source').html(); if (rawSpecs) specsData = JSON.parse(rawSpecs) || []; } catch (e) { specsData = []; } // 动态渲染下级规格增值服务专属加价表 function renderAddonSpecsTable() { if (!specsData.length) return; var enabledAddon = getEnabledAddonServices(); // 表头 var theadHtml = []; if (!enabledAddon.length) { theadHtml.push('请先在上方开启增值服务并设置基准加价'); } else { theadHtml.push(''); theadHtml.push(' 序号'); theadHtml.push(' 细分规格/款式名称'); theadHtml.push(' 参考图'); for (var a = 0; a < enabledAddon.length; a++) { var ad = enabledAddon[a]; theadHtml.push(' ' + ad.name + '
基准:+¥' + Number(ad.price).toFixed(2) + '/' + ad.unit + ''); } theadHtml.push(' 规格施工说明'); theadHtml.push(''); } $('#specs-addon-thead').html(theadHtml.join('')); // 表体 var tbodyHtml = []; for (var i = 0; i < specsData.length; i++) { var sp = specsData[i]; var imgFull = sp.image ? (sp.image.indexOf('http') === 0 || sp.image.indexOf('/assets/') === 0 ? sp.image : Fast.api.cdnurl(sp.image)) : ''; var aPrices = sp.addon_prices_map || sp.addon_prices || {}; if (typeof aPrices === 'string') { try { aPrices = JSON.parse(aPrices) || {}; } catch(e) { aPrices = {}; } } tbodyHtml.push(''); tbodyHtml.push(' ' + (i + 1) + ''); tbodyHtml.push(' ' + (sp.name || '') + ''); tbodyHtml.push(' '); if (imgFull) { tbodyHtml.push(' '); } else { tbodyHtml.push(' 无图'); } tbodyHtml.push(' '); if (!enabledAddon.length) { tbodyHtml.push(' 未启用增值服务'); } else { for (var k = 0; k < enabledAddon.length; k++) { var as = enabledAddon[k]; var aCode = as.code; var parentAddonPrice = Number(as.price || 0).toFixed(2); var hasCustomAddon = typeof aPrices[aCode] !== 'undefined' && Number(aPrices[aCode]) > 0; var customAddonVal = hasCustomAddon ? Number(aPrices[aCode]).toFixed(2) : ''; tbodyHtml.push(' '); tbodyHtml.push('
'); tbodyHtml.push(' +¥'); tbodyHtml.push(' '); tbodyHtml.push('
'); tbodyHtml.push(' '); } } tbodyHtml.push(' ' + (sp.description || '-') + ''); tbodyHtml.push(''); } $('#specs-addon-tbody').html(tbodyHtml.join('')); } renderAddonSpecsTable(); // 监听增项开关与价格变动 $(document).on('change', '.ad-bind', function () { var $row = $(this).closest('tr'); var checked = $(this).is(':checked'); $row.toggleClass('is-row-disabled', !checked); if (checked) { $row.find('.ad-active-panel').css('display', 'inline-flex'); $row.find('.ad-off-hint').hide(); } else { $row.find('.ad-active-panel').hide(); $row.find('.ad-off-hint').show(); } $('#stat-addon-cnt').text($('.ad-bind:checked').length); renderAddonSpecsTable(); }); $(document).on('input change', '.ad-price', function () { renderAddonSpecsTable(); }); // 规格专属加价输入高亮与数据缓存 $(document).on('input change', '.smi-addon-spec-input', function () { var specId = $(this).data('spec-id'); var code = $(this).data('code'); var val = $.trim($(this).val()); var numVal = parseFloat(val); var $box = $(this).closest('.srv-matrix-input-box'); for (var i = 0; i < specsData.length; i++) { if (specsData[i].id == specId) { var aMap = specsData[i].addon_prices_map || specsData[i].addon_prices || {}; if (typeof aMap === 'string') { try { aMap = JSON.parse(aMap) || {}; } catch(e) { aMap = {}; } } if (!isNaN(numVal) && numVal > 0) { aMap[code] = numVal; $(this).addClass('is-custom-addon'); $box.addClass('is-active-addon'); } else { delete aMap[code]; $(this).removeClass('is-custom-addon'); $box.removeClass('is-active-addon'); } specsData[i].addon_prices_map = aMap; break; } } }); // 提交前收集并注入表单 Form.api.bindevent($("form[role=form]"), null, null, function () { var enabledAddon = getEnabledAddonServices(); $('#addons_raw').val(JSON.stringify(enabledAddon)); $('input.spec-addon-submit-hidden').remove(); var $form = $('#pricing-addon-form'); for (var i = 0; i < specsData.length; i++) { var sp = specsData[i]; var aMap = sp.addon_prices_map || sp.addon_prices || {}; if (typeof aMap === 'string') { try { aMap = JSON.parse(aMap) || {}; } catch(e) { aMap = {}; } } var clean = {}; for (var c in aMap) { var v = parseFloat(aMap[c]); if (!isNaN(v) && v > 0) clean[c] = v; } $form.append(''); } return true; }); }, // ==================== 3. 商品与规格基础档案编辑 ==================== add: function () { Controller.api.bindSpecSimpleEvents(); Form.api.bindevent($("form[role=form]")); }, edit: function () { Controller.api.bindSpecSimpleEvents(); Form.api.bindevent($("form[role=form]")); }, detail: function () { }, api: { bindSpecSimpleEvents: function () { var specIndex = 1000; // 重新排号 function reindexSpecs() { $('#specs-simple-tbody tr.spec-simple-row').each(function (idx) { $(this).find('.col-spec-seq').text(idx + 1); }); } // 新增规格 $(document).off('click.srv', '#btn-add-spec-simple').on('click.srv', '#btn-add-spec-simple', function () { specIndex++; var idx = $('#specs-simple-tbody tr.spec-simple-row').length + 1; var html = '' + '' + idx + '' + '' + ' ' + '' + '' + '
' + ' ' + ' ' + ' ' + ' ' + ' ' + '
' + '' + '' + ' ' + ' ' + ' ' + ' ' + '' + '' + ' ' + '' + ''; $('#specs-simple-tbody').append(html); reindexSpecs(); }); // 上传规格图 $(document).off('click.srv', '.btn-upload-spec-img').on('click.srv', '.btn-upload-spec-img', function () { $(this).closest('.spec-img-cell').find('.spec-img-file').trigger('click'); }); $(document).off('change.srv', '.spec-img-file').on('change.srv', '.spec-img-file', function () { var file = this.files && this.files[0]; if (!file) return; var $cell = $(this).closest('.spec-img-cell'); var $btn = $cell.find('.btn-upload-spec-img'); var fd = new FormData(); fd.append('file', file); fd.append('category', 'service'); $btn.prop('disabled', true).html(''); $.ajax({ url: Fast.api.fixurl('ajax/upload'), type: 'POST', data: fd, processData: false, contentType: false, dataType: 'json', success: function (res) { if (res && res.code === 1 && res.data) { var rawUrl = res.data.url || ''; var fullUrl = res.data.fullurl || rawUrl; $cell.find('.spec-img-input').val(rawUrl); $cell.find('.spec-img-preview').attr('src', fullUrl).show(); $cell.find('.btn-clear-spec-img').show(); Toastr.success('规格参考图已上传'); } else { Toastr.error((res && res.msg) ? res.msg : '上传失败'); } }, error: function () { Toastr.error('上传请求异常'); }, complete: function () { $btn.prop('disabled', false).html(''); } }); $(this).val(''); }); $(document).off('click.srv', '.btn-clear-spec-img').on('click.srv', '.btn-clear-spec-img', function () { var $cell = $(this).closest('.spec-img-cell'); $cell.find('.spec-img-input').val(''); $cell.find('.spec-img-preview').attr('src', '').hide(); $(this).hide(); }); // 删除规格 $(document).off('click.srv', '.btn-del-spec-simple').on('click.srv', '.btn-del-spec-simple', function () { $(this).closest('tr').remove(); reindexSpecs(); }); } } }; return Controller; });