define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
var Controller = {
index: function () {
// 初始化表格参数
Table.api.init({
extend: {
index_url: 'commission/agent/index' + location.search,
add_url: '',
multi_url: 'commission/agent/multi',
import_url: 'commission/agent/import',
table: 'commission_agent',
}
});
var table = $("#table");
// 初始化表格
table.bootstrapTable({
url: $.fn.bootstrapTable.defaults.extend.index_url,
pk: 'user_id',
sortName: 'user_id',
fixedColumns: true, // 启用固定列功能
fixedRightNumber: 1, // 固定右侧最后1列(操作列)
columns: [
[
{checkbox: true},
{field: 'user_id', title: __('ID'), width: 90},
{field: 'user.nickname', title: __('分销商信息'), width: 200, operate: 'LIKE', formatter: function(value, row, index) {
var avatarUrl = row.user && row.user.avatar ? Fast.api.cdnurl(row.user.avatar) : Fast.api.cdnurl('/assets/img/avatar.png');
var nickname = row.user && row.user.nickname ? row.user.nickname : 'yimizi';
var mobile = row.user && row.user.mobile ? row.user.mobile : '';
var html = '
';
html += '

';
html += '
';
html += '
' + nickname + '
';
if (mobile) {
html += '
' + mobile + '
';
}
html += '
';
return html;
}},
{field: 'level_info.name', title: __('等级'), width: 120, formatter: function(value, row, index) {
if (row.level_info) {
var html = '' + row.level_info.name + '
';
html += '(等级' + row.level_info.level + ')
';
return html;
}
return '默认等级
(等级' + (row.level || 1) + ')';
}},
{field: 'total_income', title: __('累计佣金'), width: 120, operate: 'BETWEEN', formatter: function(value, row, index) {
return (row.total_income || 0) + '元';
}},
{field: 'user.total_consume', title: __('消费金额'), width: 120, formatter: function(value, row, index) {
return ((row.user && row.user.total_consume) || 0) + '元';
}},
{field: 'child_order_money_0', title: __('自购分销业绩'), width: 140, formatter: function(value, row, index) {
var orderCount = row.child_order_count_first;
var orderMoney = row.child_order_money_first;
var html = '' + orderCount + '单
';
html += '' + orderMoney + '元
';
return html;
}},
{field: 'user.parent_user.nickname', title: __('上级分销商'), width: 150, formatter: function(value, row, index) {
if (row.user && row.user.parent_user) {
var parentAvatar = row.user.parent_user.avatar ? Fast.api.cdnurl(row.user.parent_user.avatar) : Fast.api.cdnurl('/assets/img/avatar.png');
var parentNickname = row.user.parent_user.nickname || ('用户' + row.user.parent_user_id);
var html = '';
html += '

';
html += '
';
html += '
' + parentNickname + '
';
html += '
';
return html;
}
return '平台直推
';
}},
{field: 'child_order_money_second', title: __('一级用户人数'), width: 120, formatter: function(value, row, index) {
var count = row.child_user_count_first;
return count + '人';
}},
{field: 'child_agent_count_1', title: __('一级分销商人数'), width: 160, formatter: function(value, row, index) {
var count = row.child_agent_count_first;
return '' + count + '人
';
}},
{field: 'become_time', title: __('创建时间'), width: 172, operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime},
{field: 'status', title: __('状态'), searchList: {
"normal": __('正常'),
"pending": __('审核中'),
"freeze": __('冻结'),
"forbidden": __('禁用'),
"reject": __('拒绝')
}, formatter: function(value, row, index) {
var colorMap = {
'normal': 'success',
'pending': 'warning',
'freeze': 'info',
'forbidden': 'danger',
'reject': 'danger'
};
var textMap = {
'normal': '正常',
'pending': '审核中',
'freeze': '冻结',
'forbidden': '禁用',
'reject': '拒绝'
};
var color = colorMap[value] || 'default';
var text = textMap[value] || value;
return '' + text + '';
}},
{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
buttons: [
{
name: 'detail',
text: __('详情'),
title: __('详情'),
classname: 'btn btn-xs btn-primary btn-dialog',
icon: 'fa fa-list',
url: 'commission/agent/detail',
callback: function (data) {
table.bootstrapTable('refresh');
}
},
{
name: 'team',
text: __('团队'),
title: __('团队'),
classname: 'btn btn-xs btn-info btn-dialog',
icon: 'fa fa-users',
url: 'commission/agent/team',
callback: function (data) {
table.bootstrapTable('refresh');
}
},
],
formatter: Table.api.formatter.operate}
]
]
});
// 为表格绑定事件
Table.api.bindevent(table);
},
detail: function () {
var agentId = Fast.api.query('id');
var agentData = {};
// 获取分销商详情
function getDetail() {
Fast.api.ajax({
url: 'commission/agent/detail',
data: {id: agentId}
}, function(data) {
agentData = data;
updateDetailView(data);
});
}
// 更新详情视图
function updateDetailView(data) {
if (data.user) {
$('#user-avatar').attr('src', data.user.avatar || '/assets/img/avatar.png');
$('#user-nickname').text(data.user.nickname || data.user_id);
}
// 状态
var statusMap = {
'normal': {text: '正常', class: 'label-success'},
'pending': {text: '审核中', class: 'label-warning'},
'freeze': {text: '冻结', class: 'label-info'},
'forbidden': {text: '禁用', class: 'label-danger'},
'reject': {text: '拒绝', class: 'label-danger'}
};
var status = statusMap[data.status] || {text: data.status, class: 'label-default'};
$('#agent-status').text(status.text).attr('class', 'label ' + status.class);
// 等级
if (data.level_info) {
$('#agent-level').text(data.level_info.name + '(等级' + data.level_info.level + ')');
if (data.level_info.commission_rules) {
$('#commission-1').text(data.level_info.commission_rules.commission_1 || '0.00');
$('#commission-2').text(data.level_info.commission_rules.commission_2 || '0.00');
$('#commission-3').text(data.level_info.commission_rules.commission_3 || '0.00');
}
} else {
$('#agent-level').text(data.level);
}
// 待升级等级
if (data.level_status > 0 && data.level_status_info) {
$('#upgrade-level-group').show();
$('#upgrade-level').text(data.level_status_info.name + '(等级' + data.level_status + ')');
} else {
$('#upgrade-level-group').hide();
}
// 上级分销商
if (data.user && data.user.parent_user) {
$('#parent-agent').text(data.user.parent_user.nickname || '用户' + data.user.parent_user_id);
} else {
$('#parent-agent').text('无');
}
// 允许升级
$('#upgrade-lock').prop('checked', data.upgrade_lock == 0);
// 统计数据
$('#child-user-count-all').text(data.child_user_count_all || 0);
$('#child-user-count-1').text(data.child_user_count_1 || 0);
$('#child-agent-count-all').text(data.child_agent_count_all || 0);
$('#child-agent-count-1').text(data.child_agent_count_1 || 0);
$('#child-order-money-all').text((data.child_order_money_all || 0) + '元');
$('#child-order-count-all').text(data.child_order_count_all || 0);
$('#child-order-money-1').text((data.child_order_money_1 || 0) + '元');
$('#child-order-count-1').text(data.child_order_count_1 || 0);
$('#total-income').text((data.total_income || 0) + '元');
$('#pending-reward').text((data.pending_reward || 0) + '元');
$('#total-consume').text(((data.user && data.user.total_consume) || 0) + '元');
// 申请信息
if (data.apply_info && data.apply_info.length > 0) {
var applyHtml = '';
$('#apply-info').html(applyHtml);
} else {
$('#apply-info').html('暂无申请信息
');
}
}
// 详情页面特有的全局函数
// 修改状态事件委托
$(document).on('click', '.btn-edit-status', function () {
var userId = $(this).data('user-id');
var currentStatus = $(this).data('current-status') || 'normal';
var html = Template('statusedittml', {
currentStatus: currentStatus,
userId: userId
});
layer.open({
type: 1,
skin: 'layui-layer-demo',
title: '修改分销商状态',
anim: 2,
area: ['400px', '280px'],
shadeClose: 1,
content: html,
btn: ['确定', '取消'],
zIndex: 999999,
yes: function (index) {
var newStatus = $('input[name="status"]:checked').val();
if (!newStatus) {
Toastr.error('请选择状态');
return false;
}
// 发送AJAX请求
Fast.api.ajax({
url: 'commission/agent/edit',
data: {
ids: $('#status_user_id').val(),
status: newStatus
}
}, function(data, ret) {
window.location.reload();
Toastr.success('修改成功');
layer.close(index);
});
},
success: function (layero, index) {
// 使用FastAdmin标准方式绑定表单事件
Form.api.bindevent(layero.find("form"));
}
});
});
// 修改到期时间 - 使用事件委托
$(document).on('click', '.btn-edit-expire-time', function () {
var userId = $(this).data('user-id');
var expireTime = $(this).data('expire-time');
// 将时间戳转换为日期字符串
var currentTime = '';
if (expireTime && expireTime != '') {
var date = new Date(expireTime * 1000);
currentTime = date.getFullYear() + '-' +
String(date.getMonth() + 1).padStart(2, '0') + '-' +
String(date.getDate()).padStart(2, '0') + ' ' +
String(date.getHours()).padStart(2, '0') + ':' +
String(date.getMinutes()).padStart(2, '0') + ':' +
String(date.getSeconds()).padStart(2, '0');
}
// 使用Template渲染HTML
var html = Template('expiretimetpl', {
currentTime: currentTime,
userId: userId
});
layer.open({
type: 1,
skin: 'layui-layer-demo',
title: '修改分销商到期时间',
anim: 2,
area: ['500px', '500px'],
shadeClose: 1,
content: html,
btn: ['确定', '取消'],
zIndex: 999999,
yes: function (index) {
var newTime = ($('#expire_time').val()).trim();
// 验证时间格式
if (newTime && !/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(newTime)) {
Toastr.error('时间格式不正确,请使用格式: 2024-01-01 00:00:00');
return;
}
var userId = $('#user_id').val();
Fast.api.ajax({
url: 'commission/agent/updateExpireTime',
data: {
user_id: userId,
expire_time: newTime
}
}, function(data, ret) {
window.location.reload();
Toastr.success('修改成功');
layer.close(index);
});
},
success: function (layero, index) {
// 使用FastAdmin标准方式绑定表单事件
Form.api.bindevent(layero.find("form"));
}
});
});
window.changeLevel = function(userId) {
Toastr.info('等级修改功能待完善');
};
window.changeParentUser = function(userId) {
Fast.api.open('commission/agent/select?id=' + userId, '更换上级分销商', {
callback: function() {
location.reload();
}
});
};
window.approveLevel = function(userId, levelStatus) {
Fast.api.ajax({
url: 'commission/agent/edit',
data: {ids: userId, level_status: levelStatus}
}, function() {
location.reload();
});
};
window.rejectLevel = function(userId) {
Fast.api.ajax({
url: 'commission/agent/edit',
data: {ids: userId, level_status: 0}
}, function() {
location.reload();
});
};
window.toggleUpgradeLock = function(userId, currentLock) {
var newLock = currentLock == 0 ? 1 : 0;
Fast.api.ajax({
url: 'commission/agent/edit',
data: {ids: userId, upgrade_lock: newLock}
}, function() {
location.reload();
});
};
window.viewTeam = function(userId) {
Fast.api.open('commission/agent/team?id=' + userId, '查看团队');
};
// 事件绑定
$(document).ready(function() {
$('.btn-refresh').click(function() {
location.reload();
});
$('#refresh-detail').click(function() {
getDetail();
});
$('#edit-status').click(function() {
Layer.prompt({
title: '修改状态',
formType: 2,
value: agentData.status,
select: ['normal', 'pending', 'freeze', 'forbidden', 'reject'],
selectTips: ['正常', '审核中', '冻结', '禁用', '拒绝']
}, function(value, index) {
Fast.api.ajax({
url: 'commission/agent/edit',
data: {ids: agentId, status: value}
}, function() {
Layer.close(index);
getDetail();
});
});
});
$('#change-parent').click(function() {
Fast.api.open('commission/agent/select?id=' + agentId, '更换上级分销商', {
callback: function() {
getDetail();
}
});
});
$('#upgrade-lock').change(function() {
var value = $(this).is(':checked') ? 0 : 1;
Fast.api.ajax({
url: 'commission/agent/edit',
data: {ids: agentId, upgrade_lock: value}
}, function() {
getDetail();
});
});
$('#approve-level').click(function() {
Fast.api.ajax({
url: 'commission/agent/edit',
data: {ids: agentId, level_status: agentData.level_status}
}, function() {
getDetail();
});
});
$('#reject-level').click(function() {
Fast.api.ajax({
url: 'commission/agent/edit',
data: {ids: agentId, level_status: 0}
}, function() {
getDetail();
});
});
// 初始化
if (agentId) {
getDetail();
}
});
Controller.api.bindevent();
},
select: function () {
var userId = Fast.api.query('id');
var selectedParentId = null;
var currentPage = 1;
var pageSize = 10;
// 获取当前用户信息
function getCurrentUserInfo() {
Fast.api.ajax({
url: 'commission/agent/detail',
data: {id: userId}
}, function(data) {
var html = '';
if (data.user && data.user.parent_user) {
html += '
';
html += (data.user.parent_user.nickname || '用户' + data.user.parent_user_id);
selectedParentId = data.user.parent_user_id;
} else {
html += '无';
selectedParentId = 0;
}
$('#current-parent').html(html);
});
}
// 获取分销商列表
function getAgentList() {
var searchType = $('#search-type').val();
var searchValue = $('#search-value').val();
var searchData = {
page: currentPage,
limit: pageSize
};
if (searchValue) {
searchData[searchType] = searchValue;
}
Fast.api.ajax({
url: 'commission/agent/select',
data: searchData
}, function(ret) {
var html = '';
if (ret.data.rows && ret.data.rows.length > 0) {
ret.data.rows.forEach(function(item) {
var statusClass = '';
var statusText = '';
switch(item.status) {
case 'normal':
statusClass = 'label-success';
statusText = '正常';
break;
case 'pending':
statusClass = 'label-warning';
statusText = '审核中';
break;
case 'freeze':
statusClass = 'label-info';
statusText = '冻结';
break;
case 'forbidden':
statusClass = 'label-danger';
statusText = '禁用';
break;
case 'reject':
statusClass = 'label-danger';
statusText = '拒绝';
break;
default:
statusClass = 'label-default';
statusText = item.status;
}
html += '';
html += ' | ';
html += '' + item.user_id + ' | ';
html += '';
html += ' ';
html += (item.user.nickname || '用户' + item.user_id);
html += ' | ';
html += '';
if (item.level_info) {
html += item.level_info.name + ' 等级' + item.level_info.level + '';
} else {
html += '等级' + item.level;
}
html += ' | ';
html += '' + (item.user.mobile || '-') + ' | ';
html += '' + statusText + ' | ';
html += '';
if (selectedParentId == item.user_id) {
html += '已选择';
} else {
html += '';
}
html += ' | ';
html += '
';
});
} else {
html = '暂无数据 |
';
}
$('#agent-list').html(html);
// 更新分页
updatePagination(ret.data.total);
});
}
// 更新分页
function updatePagination(total) {
var totalPages = Math.ceil(total / pageSize);
var html = '';
$('#pagination').html(html);
}
// 选择页面特有的全局函数
window.searchAgent = function() {
currentPage = 1;
getAgentList();
};
window.selectAgent = function(agentId) {
selectedParentId = agentId;
$('input[name="parent_agent"]').prop('checked', false);
$('input[value="' + agentId + '"]').prop('checked', true);
$('#agent-table tbody tr').removeClass('warning');
$('input[value="' + agentId + '"]').closest('tr').addClass('warning');
$('#set-platform').prop('checked', false);
};
window.changePage = function(page) {
currentPage = page;
getAgentList();
};
window.confirmChange = function() {
if (selectedParentId === null) {
Toastr.error('请选择上级分销商');
return;
}
Fast.api.ajax({
url: 'commission/agent/changeParentUser',
data: {
id: userId,
parent_user_id: selectedParentId
}
}, function() {
Toastr.success('更换成功');
Fast.api.close();
});
};
// 事件绑定
$(document).ready(function() {
// 设为平台直推
$('#set-platform').change(function() {
if ($(this).is(':checked')) {
selectedParentId = 0;
$('input[name="parent_agent"]').prop('checked', false);
$('#agent-table tbody tr').removeClass('warning');
}
});
// 搜索框回车事件
$('#search-value').keypress(function(e) {
if (e.which == 13) {
searchAgent();
}
});
// 初始化
getCurrentUserInfo();
getAgentList();
});
},
team: function () {
// 优先从Config中获取ID,否则从URL参数获取
var agentId = Config.current_agent_id || Fast.api.query('id');
console.log('agentId:', agentId);
// 初始化表格参数
Table.api.init({
extend: {
index_url: 'commission/agent/team?id=' + agentId,
add_url: '',
edit_url: '',
del_url: '',
multi_url: '',
import_url: '',
table: 'user',
}
});
var table = $("#table");
// 初始化表格
table.bootstrapTable({
url: $.fn.bootstrapTable.defaults.extend.index_url,
pk: 'id',
sortName: 'id',
columns: [
[
{checkbox: true},
{field: 'id', title: __('ID'), width: 90},
{field: 'nickname', title: __('团队用户'), width: 150, operate: 'LIKE', formatter: function(value, row, index) {
var avatarUrl = row.avatar ? Fast.api.cdnurl(row.avatar) : Fast.api.cdnurl('/assets/img/avatar.png');
var html = '';
html += '

';
html += '
';
html += '
' + (row.nickname || '用户' + row.id) + '
';
html += '
' + (row.mobile || '') + '
';
html += '
';
return html;
}},
{field: 'agent.level_info.name', title: __('等级'), width: 150, formatter: function(value, row, index) {
if (row.agent && row.agent.level_info) {
var html = '';
if (row.agent.level_info.image) {
html += '
 + ')
';
}
html += '
' + row.agent.level_info.name + '';
html += '
';
return html;
} else if (row.agent) {
return '等级' + (row.agent.level || '-');
}
return '非分销商';
}},
{field: 'agent.status', title: __('状态'), width: 120, align: 'center', searchList: {
"normal": __('正常'),
"pending": __('审核中'),
"freeze": __('冻结'),
"forbidden": __('禁用'),
"reject": __('拒绝')
}, formatter: function(value, row, index) {
if (!row.agent) {
return '非分销商';
}
var status = row.agent.status;
var colorMap = {
'normal': '#5cb85c',
'pending': '#f0ad4e',
'freeze': '#5bc0de',
'forbidden': '#d9534f',
'reject': '#d9534f'
};
var textMap = {
'normal': '正常',
'pending': '审核中',
'freeze': '冻结',
'forbidden': '禁用',
'reject': '拒绝'
};
var color = colorMap[status] || '#777';
var text = textMap[status] || status;
return '' + text + '';
}},
{field: 'agent.child_user_count_all', title: __('团队人数/分销商人数'), width: 160, align: 'center', formatter: function(value, row, index) {
if (!row.agent) return '0人/0人';
var userCount = row.agent.child_user_count_all || 0;
var agentCount = row.agent.child_agent_count_all || 0;
return userCount + '人/' + agentCount + '人';
}},
{field: 'agent.child_user_count_first', title: __('一级团队人数/一级分销商人数'), width: 220, align: 'center', formatter: function(value, row, index) {
if (!row.agent) return '0人/0人';
var userCount = row.agent.child_user_count_first || 0;
var agentCount = row.agent.child_agent_count_first || 0;
return userCount + '人/' + agentCount + '人';
}},
{field: 'agent.child_user_count_second', title: __('二级团队人数/二级分销商人数'), width: 220, align: 'center', formatter: function(value, row, index) {
if (!row.agent) return '0人/0人';
var userCount = row.agent.child_user_count_second || 0;
var agentCount = row.agent.child_agent_count_second || 0;
return userCount + '人/' + agentCount + '人';
}},
{field: 'agent.child_order_money_all', title: __('团队分销总额/团队分销订单'), width: 220, align: 'center', formatter: function(value, row, index) {
if (!row.agent) return '0元/0单';
var money = row.agent.child_order_money_all || 0;
var count = row.agent.child_order_count_all || 0;
return money + '元/' + count + '单';
}},
{field: 'agent.child_order_money_first', title: __('一级分销总额/一级分销订单'), width: 220, align: 'center', formatter: function(value, row, index) {
if (!row.agent) return '0元/0单';
var money = row.agent.child_order_money_first || 0;
var count = row.agent.child_order_count_first || 0;
return money + '元/' + count + '单';
}},
{field: 'agent.child_order_money_second', title: __('二级分销总额/二级分销订单'), width: 220, align: 'center', formatter: function(value, row, index) {
if (!row.agent) return '0元/0单';
var money = row.agent.child_order_money_second || 0;
var count = row.agent.child_order_count_second || 0;
return money + '元/' + count + '单';
}},
{field: 'agent.child_order_money_0', title: __('自购分销总金额/订单数'), width: 220, align: 'center', formatter: function(value, row, index) {
if (!row.agent) return '0元/0单';
// 自购数据可能存储在其他字段中,这里使用0级别或者特定字段
var money = row.agent.self_order_money || 0;
var count = row.agent.self_order_count || 0;
return money + '元/' + count + '单';
}},
{field: 'agent.total_income', title: __('累计佣金'), width: 160, align: 'center', operate: 'BETWEEN', formatter: function(value, row, index) {
return row.agent ? ((row.agent.total_income || 0) + '元') : '0元';
}},
{field: 'total_consume', title: __('消费金额'), width: 160, align: 'center', formatter: function(value, row, index) {
return ((row.total_consume) || 0) + '元';
}},
{field: 'agent.pending_reward', title: __('待入账佣金'), width: 160, align: 'center', formatter: function(value, row, index) {
return row.agent ? ((row.agent.pending_reward || 0) + '元') : '0元';
}},
{field: 'createtime', title: __('加入时间'), width: 172, align: 'center', operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime}
]
]
});
// 为表格绑定事件
Table.api.bindevent(table);
// 团队页面特有的全局函数
window.viewTeam = function(userId) {
Fast.api.open('commission/agent/team?id=' + userId, '查看团队');
};
window.viewDetail = function(userId) {
Fast.api.open('commission/agent/detail?id=' + userId, '分销商详情');
};
Controller.api.bindevent();
},
api: {
bindevent: function () {
Form.api.bindevent($("form[role=form]"));
}
}
};
return Controller;
});