define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) { var Controller = { index: function () { // 初始化表格参数 Table.api.init({ extend: { index_url: 'lottery/userchance/index' + location.search, add_url: 'lottery/userchance/add', edit_url: 'lottery/userchance/edit', del_url: 'lottery/userchance/del', multi_url: 'lottery/userchance/multi', table: 'shop_lottery_user_chance', } }); var table = $("#table"); // 初始化表格 table.bootstrapTable({ url: $.fn.bootstrapTable.defaults.extend.index_url, pk: 'id', sortName: 'id', sortOrder: 'desc', columns: [ [ {checkbox: true}, {field: 'id', title: __('Id'), operate: false}, {field: 'activity.name', title: __('活动名称'), operate: 'LIKE'}, {field: 'user.nickname', title: __('用户昵称'), operate: 'LIKE'}, {field: 'user.mobile', title: __('用户手机'), operate: 'LIKE'}, {field: 'chance_count', title: __('抽奖次数'), operate: 'BETWEEN'}, {field: 'used_count', title: __('已使用'), operate: 'BETWEEN'}, {field: 'remain_count', title: __('剩余次数'), operate: false, formatter: function(value, row, index) { var remain = row.chance_count - row.used_count; var color = remain > 0 ? 'success' : 'default'; return '' + remain + ''; }}, {field: 'source_type', title: __('来源类型'), searchList: {"1":"购买商品","2":"订单消费","3":"充值","4":"累计消费","5":"管理员发放","6":"签到奖励","7":"邀请奖励","8":"活动奖励"}, formatter: Table.api.formatter.normal}, {field: 'source_value', title: __('来源描述'), operate: 'LIKE'}, {field: 'expire_time', title: __('过期时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: function(value, row, index) { if (!value || value == 0) { return '永不过期'; } var now = Math.floor(Date.now() / 1000); var color = value > now ? 'info' : 'danger'; return '' + Table.api.formatter.datetime(value) + ''; }}, {field: 'status', title: __('状态'), searchList: {"0":"已失效","1":"有效"}, formatter: Table.api.formatter.status}, {field: 'createtime', title: __('Create time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime}, {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate} ] ] }); // 为表格绑定事件 Table.api.bindevent(table); // 批量发放按钮 $(document).on('click', '.btn-batchgrant', function(){ Fast.api.open('lottery/userchance/batchgrant', '批量发放抽奖机会', { area: ['800px', '600px'], callback: function(data) { table.bootstrapTable('refresh'); } }); }); // 统计按钮 $(document).on('click', '.btn-statistics', function(){ Fast.api.open('lottery/userchance/statistics', '抽奖机会统计', { area: ['1000px', '700px'] }); }); // 清理过期机会按钮 $(document).on('click', '.btn-clearexpired', function(){ Layer.confirm('确定要清理所有过期的抽奖机会吗?', {icon: 3, title: '提示'}, function(index){ Fast.api.ajax({ url: 'lottery/userchance/clearExpired', type: 'post' }, function(data, ret) { Layer.close(index); Layer.msg(ret.msg); table.bootstrapTable('refresh'); }); }); }); }, add: function () { Controller.api.bindevent(); }, edit: function () { Controller.api.bindevent(); }, batchGrant: function () { Controller.api.bindevent(); }, statistics: function () { Controller.api.bindevent(); // 加载统计数据 Controller.api.loadStatistics(); // 活动切换时重新加载统计 $(document).on('change', '#activity_id', function(){ Controller.api.loadStatistics(); }); }, api: { bindevent: function () { Form.api.bindevent($("form[role=form]")); // 来源类型变化时的处理 $(document).on('change', 'input[name="row[source_type]"]', function(){ var source_type = $(this).val(); $('.source-desc').text(''); switch(source_type) { case '1': $('.source-desc').text('用户购买商品后获得'); break; case '2': $('.source-desc').text('用户订单消费达到条件后获得'); break; case '3': $('.source-desc').text('用户充值达到条件后获得'); break; case '4': $('.source-desc').text('用户累计消费达到条件后获得'); break; case '5': $('.source-desc').text('管理员手动发放'); break; case '6': $('.source-desc').text('用户签到奖励获得'); break; case '7': $('.source-desc').text('用户邀请奖励获得'); break; case '8': $('.source-desc').text('其他活动奖励获得'); break; } }); // 过期时间控制 $(document).on('change', 'input[name="expire_enable"]', function(){ var expire_enable = $(this).val(); $('.expire-time-group').toggle(expire_enable == '1'); }); // 用户选择器 $(document).on('click', '.btn-choose-users', function(){ Fast.api.open('user/user/select', '选择用户', { area: ['80%', '80%'], callback: function(data) { if (data && data.length > 0) { var userIds = []; var userNames = []; $.each(data, function(i, user) { userIds.push(user.id); userNames.push(user.nickname); }); $('#user_ids').val(userIds.join(',')); $('#user_names').val(userNames.join(',')); } } }); }); }, loadStatistics: function() { var activity_id = $('#activity_id').val(); Fast.api.ajax({ url: 'lottery/userchance/statistics', type: 'get', data: {activity_id: activity_id} }, function(data, ret) { if (ret.code == 1 && ret.data) { $('#total_users').text(ret.data.total_users); $('#total_chances').text(ret.data.total_chances); $('#used_chances').text(ret.data.used_chances); $('#remain_chances').text(ret.data.remain_chances); // 更新来源统计表格 var tbody = $('#source_stats tbody'); tbody.empty(); if (ret.data.source_stats && ret.data.source_stats.length > 0) { $.each(ret.data.source_stats, function(i, item) { var sourceTypes = {"1":"购买商品","2":"订单消费","3":"充值","4":"累计消费","5":"管理员发放","6":"签到奖励","7":"邀请奖励","8":"活动奖励"}; var sourceText = sourceTypes[item.source_type] || '未知'; var remainChance = item.total_chance - item.used_chance; var row = '