define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
var Controller = {
// 模板对象
templates: {
// 模板渲染工具方法
render: function(template, data) {
var result = template;
Object.keys(data).forEach(function(key) {
var regex = new RegExp('{{' + key + '}}', 'g');
result = result.replace(regex, data[key] || '');
});
return result;
},
// 文本截断工具
truncateText: function(text, maxLength) {
if (!text || text.length <= maxLength) {
return text;
}
return text.substring(0, maxLength) + '...';
},
// 安全转义HTML
escapeHtml: function(text) {
if (typeof Fast !== 'undefined' && Fast.api && Fast.api.escape) {
return Fast.api.escape(text);
}
return $('
').text(text).html();
},
// 商品信息模板
goodsInfo: function(row) {
var template = '
' +
'' +
'
' +
'

' +
'
' +
'' +
'
' +
'' +
'
' +
'' +
'{{subtitle}}' +
'' +
'
' +
'ID: {{id}}' +
'{{typeInfo}}' +
'
' +
'
' +
'
';
// 数据填充
var data = {
imageUrl: row.image || '/assets/img/goods-default.png',
title: Controller.templates.truncateText(Controller.templates.escapeHtml(row.title || '未命名商品'), 50),
id: row.id || '-',
subtitle: row.subtitle ?
'
' +
Controller.templates.truncateText(Controller.templates.escapeHtml(row.subtitle), 60) +
'
' : '',
specBadge: (row.spec_type == 1 || (row.spec_type_text && row.spec_type_text === '多规格')) ?
'
多规格' : '',
typeInfo: row.status_text ?
' |
' + Controller.templates.escapeHtml(row.status_text) + '' : ''
};
return Controller.templates.render(template, data);
},
// 分销规则状态模板
commissionStatus: function(row) {
if (!row.commission_goods) {
return '
不参与';
}
var participateStatus = row.commission_goods.status;
if (participateStatus == 0) {
return '
不参与';
}
var ruleTypeList = Config.commission_goods_rule_type_list || {
'0': '默认规则',
'1': '独立规则',
'2': '批量规则'
};
var colorMap = {
'0': 'info',
'1': 'success',
'2': 'warning'
};
var value = row.commission_goods.rule_type;
var text = ruleTypeList[value] || '默认规则';
var color = colorMap[value] || 'info';
return '
' + text + '';
},
// 商品状态模板
goodsStatus: function(row) {
var statusMap = {
'0': {text: '仓库中', color: 'default'},
'1': {text: '上架中', color: 'success'},
'2': {text: '已售罄', color: 'warning'},
'3': {text: '已下架', color: 'danger'}
};
var status = statusMap[row.status] || {text: '未知', color: 'default'};
return '
' + status.text + '';
}
},
index: function () {
// 添加自定义样式
Controller.addCustomStyles();
// 初始化表格参数
Table.api.init({
extend: {
index_url: 'commission/goods/index' + location.search,
add_url: '',
commission_url: 'commission/goods/commission',
del_url: '',
multi_url: '',
import_url: '',
table: 'shop_goods',
dragsort_url: ''
}
});
var table = $("#table");
// 初始化表格
table.bootstrapTable({
url: $.fn.bootstrapTable.defaults.extend.index_url,
pk: 'id',
sortName: 'id',
sortable: true,
sort: 'desc',
columns: [
[
{field: 'id', title: __('ID'), width: 80},
{field: 'title', title: __('商品信息'), operate: 'LIKE', width: 400, formatter: function(value, row, index) {
return Controller.templates.goodsInfo(row);
}},
{field: 'price', title: __('价格'), operate: 'BETWEEN', width: 100},
{field: 'commission_goods.rule_type', title: __('分销规则'), width: 120, formatter: function(value, row, index) {
return Controller.templates.commissionStatus(row);
}},
{field: 'status', title: __('商品状态'), width: 100, formatter: function(value, row, index) {
return Controller.templates.goodsStatus(row);
}},
{field: 'operate', title: __('操作'), table: table, events: Table.api.events.operate, width: 150,
buttons: [
{
name: 'commission',
text: __('设置佣金'),
title: __('设置佣金'),
classname: 'btn btn-xs btn-success btn-dialog',
icon: 'fa fa-cog',
url: 'commission/goods/commission',
callback: function(data) {
// 弹窗回传回调,刷新表格
table.bootstrapTable('refresh');
}
}
],
formatter: Table.api.formatter.operate}
]
]
});
// 为表格绑定事件
Table.api.bindevent(table);
},
// 添加自定义样式
addCustomStyles: function() {
var styles = `
`;
// 添加样式到页面
if (!$('#commission-goods-styles').length) {
$('head').append(styles.replace('