123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- 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 '<span class="label label-' + color + '">' + remain + '</span>';
- }},
- {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 '<span class="text-success">永不过期</span>';
- }
- var now = Math.floor(Date.now() / 1000);
- var color = value > now ? 'info' : 'danger';
- return '<span class="text-' + color + '">' + Table.api.formatter.datetime(value) + '</span>';
- }},
- {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 = '<tr>' +
- '<td>' + sourceText + '</td>' +
- '<td>' + item.count + '</td>' +
- '<td>' + item.total_chance + '</td>' +
- '<td>' + item.used_chance + '</td>' +
- '<td>' + remainChance + '</td>' +
- '</tr>';
- tbody.append(row);
- });
- } else {
- tbody.append('<tr><td colspan="5" class="text-center text-muted">暂无数据</td></tr>');
- }
- }
- });
- }
- }
- };
- return Controller;
- });
|