define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
var Controller = {
index: function () {
// 初始化表格参数
Table.api.init({
extend: {
index_url: 'commission/order/index' + location.search,
add_url: '',
edit_url: 'commission/order/edit',
del_url: 'commission/order/del',
multi_url: 'commission/order/multi',
import_url: 'commission/order/import',
table: 'commission_order',
}
});
var table = $("#table");
// 获取后台配置
var orderStatusTypes = Controller.api.parseConfigJson('commission_order_status_types', {
'-2': __('已扣除'),
'-1': __('已取消'),
'0': __('不计入'),
'1': __('已计入')
});
var rewardStatusTypes = Controller.api.parseConfigJson('commission_reward_status_types', {
'-2': __('已退回'),
'-1': __('已取消'),
'0': __('未结算'),
'1': __('已结算')
});
var displayConfig = Controller.api.parseConfigJson('commission_order_user_display_config', {
showAvatar: true,
avatarSize: 40,
showMobile: true,
showId: true,
defaultAvatarUrl: '/assets/img/avatar.png'
});
// 格式化订单状态
function formatOrderStatus(value, colorMap) {
var statusConfig = colorMap[value];
if (!statusConfig) return value;
return '' + statusConfig.text + '';
}
// 格式化用户信息(买家或代理商)
function formatUserInfo(user, userId, userType) {
if (!user) return '-';
return Controller.api.formatUserInfo(user, userId || user.id, userType, displayConfig);
}
// 统计信息处理
function updateStats(data) {
if (data.extend && data.extend.count) {
var count = data.extend.count;
// 格式化数字显示
function formatNumber(number) {
return (number || 0).toFixed(2);
}
$('#total-count').text(count.total || 0);
$('#total-amount').text(formatNumber(count.total_amount));
$('#total-commission').text(formatNumber(count.total_commission));
$('#total-pending').text(formatNumber(count.total_commission_pending));
$('#total-accounted').text(formatNumber(count.total_commission_accounted));
$('#total-back').text(formatNumber(count.total_commission_back));
$('#total-cancel').text(formatNumber(count.total_commission_cancel));
}
}
// 初始化表格
table.bootstrapTable({
url: $.fn.bootstrapTable.defaults.extend.index_url,
pk: 'id',
sortName: 'id',
fixedColumns: true, // 启用固定列功能
fixedRightNumber: 1, // 固定右侧最后1列(操作列)
responseHandler: function(res) {
updateStats(res);
return res;
},
columns: [
[
{checkbox: true},
{field: 'id', title: __('ID'), width: 60},
{field: 'order.order_sn', title: __('订单号'), operate: 'LIKE', formatter: function(value, row, index) {
if (row.order && row.order.order_sn) {
return '' + row.order.order_sn + '';
}
return '-';
}},
{field: 'order_item.goods_title', title: __('商品信息'), operate: 'LIKE', width: 300, formatter: function(value, row, index) {
if (row.order_item) {
var item = row.order_item;
var html = '
';
// 商品图片
var imageUrl = item.goods_image || item.image || '/assets/img/goods-default.png';
if (imageUrl && !imageUrl.startsWith('http') && !imageUrl.startsWith('//')) {
imageUrl = Fast.api.cdnurl ? Fast.api.cdnurl(imageUrl) : imageUrl;
}
html += '
';
html += '

';
html += '
';
// 商品信息
html += '
';
// 商品标题
var goodsTitle = item.goods_title || item.title || '-';
html += '
';
// 商品规格
if (item.goods_sku || item.sku_name || item.specs) {
var specs = item.goods_sku || item.sku_name || item.specs;
html += '
' + specs + '
';
}
// 商品价格和数量
var price = item.price || item.goods_price || 0;
var nums = item.nums || item.quantity || 1;
html += '
';
html += '¥' + parseFloat(price).toFixed(2) + ' × ' + nums;
html += '
';
html += '
';
return html;
}
return '-';
}},
{
field: 'buyer.nickname',
title: __('下单用户'),
operate: 'LIKE',
formatter: function (value, row, index) {
return formatUserInfo(row.buyer, row.user_id, '用户');
}
},
{
field: 'agent.nickname',
title: __('推广分销商'),
operate: 'LIKE',
formatter: function (value, row, index) {
return formatUserInfo(row.agent, row.agent_id, '分销商');
}
},
{field: 'amount', title: __('结算金额'), operate: 'BETWEEN'},
{field: 'commission_order_status', title: __('业绩状态'), searchList: orderStatusTypes, formatter: function(value, row, index) {
var statusMap = {
'-2': {text: orderStatusTypes['-2'] || '已扣除', color: 'danger'},
'-1': {text: orderStatusTypes['-1'] || '已取消', color: 'warning'},
'0': {text: orderStatusTypes['0'] || '不计入', color: 'info'},
'1': {text: orderStatusTypes['1'] || '已计入', color: 'success'}
};
return formatOrderStatus(value, statusMap);
}},
{field: 'commission_reward_status', title: __('佣金状态'), searchList: rewardStatusTypes, formatter: function(value, row, index) {
var statusMap = {
'-2': {text: rewardStatusTypes['-2'] || '已退回', color: 'danger'},
'-1': {text: rewardStatusTypes['-1'] || '已取消', color: 'warning'},
'0': {text: rewardStatusTypes['0'] || '未结算', color: 'info'},
'1': {text: rewardStatusTypes['1'] || '已结算', color: 'success'}
};
return formatOrderStatus(value, statusMap);
}},
{field: 'commission_time', title: __('结算时间'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime},
{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
width: 150,
buttons: [
{
name: 'back',
text: __('手动退回'),
title: __('手动退回佣金'),
classname: 'btn btn-xs btn-danger',
icon: 'fa fa-undo',
visible: function (row) {
return row.commission_reward_status == 1;
},
click: function (data) {
Layer.confirm('确认手动退回此订单的佣金?', function(index) {
Fast.api.ajax({
url: 'commission/order/back',
data: {commission_order_id: data.id, deduct_order_money: 1}
}, function(data, ret) {
table.bootstrapTable('refresh');
Layer.close(index);
});
});
}
},
{
name: 'detail',
text: __('订单详情'),
title: __('查看订单详情'),
classname: 'btn btn-xs btn-info',
icon: 'fa fa-eye',
visible: function (row) {
return true; // 始终显示
},
click: function (data) {
if (data.order_id) {
Fast.api.open('order/order/detail?id=' + data.order_id, '订单详情', {
area: ['90%', '90%']
});
} else {
Layer.msg('订单ID不存在');
}
}
}
],
formatter: Table.api.formatter.operate}
]
]
});
// 为表格绑定事件
Table.api.bindevent(table);
// 绑定订单详情点击事件
$(document).on('click', '.order-detail', function() {
var orderId = $(this).data('order-id');
Fast.api.open('order/order/detail?id=' + orderId, '订单详情');
});
// 绑定商品详情点击事件
$(document).on('click', '.goods-detail', function() {
var goodsId = $(this).data('goods-id');
Fast.api.open('goods/goods/detail?id=' + goodsId, '商品详情');
});
},
api: {
// 解析Config中的JSON字符串的辅助函数
parseConfigJson: function(configKey, defaultValue) {
var configValue = Config[configKey] || defaultValue || {};
// 如果是字符串,尝试解析JSON
if (typeof configValue === 'string') {
try {
return JSON.parse(configValue);
} catch (e) {
return defaultValue || {};
}
}
return configValue;
},
// 统一的用户信息展示方法
formatUserInfo: function(user, userId, userType, config) {
if (!user) return '-';
config = config || {
showAvatar: true,
avatarSize: 40,
showMobile: true,
showId: true,
defaultAvatarUrl: '/assets/img/avatar.png'
};
var avatar = user.avatar ? user.avatar : config.defaultAvatarUrl;
var nickname = user.nickname || user.username || (userType || '用户');
var mobile = user.mobile || '';
// 处理头像URL
var avatarUrl = avatar;
if (avatar && !avatar.startsWith('http') && !avatar.startsWith('//')) {
avatarUrl = Fast.api.cdnurl ? Fast.api.cdnurl(avatar) : avatar;
}
var typeColor = userType === '分销商' ? '#28a745' : (userType === '用户' ? '#17a2b8' : '#337ab7');
var html = '';
if (config.showAvatar) {
html += '

';
}
html += '
' +
'
' + nickname + '
' +
'
';
var info = [];
if (config.showMobile && mobile) {
info.push(mobile);
}
if (config.showId && userId) {
info.push('ID: ' + userId);
}
html += info.join(' ') + '
';
return html;
},
bindevent: function () {
Form.api.bindevent($("form[role=form]"));
}
}
};
return Controller;
});