123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505 |
- define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
- var Controller = {
- index: function () {
- // 初始化表格参数配置
- Table.api.init({
- extend: {
- index_url: 'shop/coupon/index' + location.search,
- add_url: 'shop/coupon/add',
- edit_url: 'shop/coupon/edit',
- del_url: 'shop/coupon/del',
- multi_url: 'shop/coupon/multi',
- import_url: 'shop/coupon/import',
- table: 'shop_coupon',
- }
- });
- var table = $("#table");
- // 初始化表格
- table.bootstrapTable({
- url: $.fn.bootstrapTable.defaults.extend.index_url,
- pk: 'id',
- sortName: 'id',
- fixedColumns: true,
- fixedRightNumber: 1,
- columns: [
- [{
- checkbox: true
- },
- {
- field: 'id',
- title: __('Id')
- },
- {
- field: 'name',
- title: __('Name'),
- operate: 'LIKE'
- },
- {
- field: 'condition_name',
- title: __('Condition'),
- operate: false
- },
- {
- field: 'result',
- title: __('Result'),
- visible: false,
- searchList: {
- "0": __('Result 0'),
- "1": __('Result 1')
- },
- formatter: Table.api.formatter.normal
- },
- {
- field: 'result_data',
- title: __('Result'),
- operate: false,
- formatter: function (value, row) {
- if (row.result == 0) {
- return '订单满' + row.result_data.money + '元,打' + row.result_data.number + '折';
- } else {
- return '订单满' + row.result_data.money + '元,减' + row.result_data.number + '元';
- }
- }
- },
- // {
- // field: 'result_data',
- // title: __('Result_data'),
- // operate: 'LIKE'
- // },
- {
- field: 'give_num',
- title: __('Give_num'),
- operate: 'BETWEEN'
- },
- {
- field: 'allow_num',
- title: __('Allow_num'),
- operate: 'BETWEEN'
- },
- {
- field: 'received_num',
- title: __('Received_num'),
- operate: 'BETWEEN'
- },
- {
- field: 'is_private',
- title: __('Is_private'),
- searchList: {
- "yes": __('Is_private yes'),
- "no": __('Is_private no')
- },
- formatter: Table.api.formatter.normal
- },
- {
- field: 'is_open',
- title: __('Is_open'),
- searchList: {
- "0": __('Is_open 0'),
- "1": __('Is_open 1')
- },
- formatter: Table.api.formatter.normal
- },
- {
- field: 'url', title: __('Url'), operate: false, formatter: function (value, row, index) {
- return '<a href="' + row.url + '" target="_blank" class="btn btn-default btn-xs"><i class="fa fa-link"></i></a>';
- }
- },
- {
- field: 'status', title: __('Status'), operate: false, formatter: function (value, row, index) {
- return row.expired ? "<span class='text-danger'>已过期</span>" : "<span class='text-success'>正常</span>";
- }
- },
- {
- field: 'receive_times',
- title: __('Receive_times'),
- operate: 'RANGE',
- addclass: 'datetimerange',
- autocomplete: false
- },
- {
- field: 'use_times',
- title: __('Use_times'),
- operate: 'RANGE',
- addclass: 'datetimerange',
- autocomplete: false,
- formatter: function (value, row) {
- if (row.mode == 'fixation') {
- return value + '天';
- } else {
- return value;
- }
- }
- },
- {
- field: 'createtime',
- title: __('Createtime'),
- operate: 'RANGE',
- addclass: 'datetimerange',
- autocomplete: false,
- visible: false,
- formatter: Table.api.formatter.datetime
- },
- {
- field: 'updatetime',
- title: __('Updatetime'),
- operate: 'RANGE',
- addclass: 'datetimerange',
- autocomplete: false,
- visible: false,
- operate: 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);
- },
- select: function () {
- // 从URL参数中获取选择模式,默认为单选(优惠券通常单选)
- var urlParams = new URLSearchParams(window.location.search);
- var selectMode = urlParams.get('selectMode') || 'single'; // 'single' 或 'multiple'
- var maxSelect = parseInt(urlParams.get('maxSelect')) || 0; // 最大选择数量,0表示无限制
-
- // 初始化表格参数配置
- Table.api.init({
- extend: {
- index_url: 'shop/coupon/index' + location.search,
- table: 'shop_coupon',
- }
- });
- Table.config.dragsortfield = '';
- var table = $("#table");
- // 根据选择模式配置表格列
- var columns = [
- {field: 'id', title: 'ID', width: 60},
- {
- field: 'name',
- title: '优惠券信息',
- operate: 'LIKE',
- formatter: function (value, row, index) {
- var html = '<div>';
- html += '<div style="font-weight: bold; margin-bottom: 4px;">' + value + '</div>';
-
- // 优惠券条件和结果描述
- if (row.result_data) {
- html += '<div style="font-size: 12px; color: #666; margin-bottom: 2px;">';
- if (row.result == 0) {
- html += '满' + row.result_data.money + '元,打' + row.result_data.number + '折';
- } else {
- html += '满' + row.result_data.money + '元,减' + row.result_data.number + '元';
- }
- html += '</div>';
- }
-
- // 使用时间和状态
- html += '<div style="font-size: 11px; color: #999;">';
- if (row.receive_times) {
- html += '领取: ' + row.receive_times;
- }
- if (row.use_times) {
- if (row.receive_times) html += ' | ';
- html += '使用: ' + row.use_times;
- if (row.mode == 'fixation') {
- html += '天';
- }
- }
- html += '</div>';
-
- return html;
- }
- },
- {
- field: 'condition_name',
- title: '使用条件',
- width: 120,
- operate: false,
- formatter: function (value, row, index) {
- return '<span style="font-size: 12px;">' + (value || '-') + '</span>';
- }
- },
- {
- field: 'give_num',
- title: '发放/已领',
- width: 100,
- operate: false,
- formatter: function (value, row, index) {
- return '<div style="text-align: center; font-size: 12px;">' +
- '<div>发放: ' + (row.give_num || 0) + '</div>' +
- '<div style="color: #666;">已领: ' + (row.received_num || 0) + '</div>' +
- '</div>';
- }
- },
- {
- field: 'status',
- title: '状态',
- width: 80,
- operate: false,
- formatter: function (value, row, index) {
- if (row.expired) {
- return '<span class="label label-danger">已过期</span>';
- } else if (row.is_open == 1) {
- return '<span class="label label-success">正常</span>';
- } else {
- return '<span class="label label-warning">停用</span>';
- }
- }
- },
-
- // 操作列 - 添加选择按钮
- {
- field: 'operate',
- title: selectMode === 'single' ? '选择' : '操作',
- width: selectMode === 'single' ? 80 : 100,
- operate: false,
- formatter: function (value, row, index) {
- // 检查优惠券是否可选(未过期且开启)
- var disabled = row.expired || row.is_open != 1;
- var disabledClass = disabled ? ' disabled' : '';
- var disabledAttr = disabled ? ' disabled="disabled"' : '';
-
- var html = '';
- if (selectMode === 'single') {
- // 单选模式:蓝色选择按钮
- html = '<button class="btn btn-xs btn-primary btn-select-single' + disabledClass + '" data-id="' + row.id + '" data-index="' + index + '" title="' + (disabled ? '优惠券不可用' : '点击选择该优惠券') + '"' + disabledAttr + '>' +
- '<i class="fa fa-check"></i> 选择</button>';
- } else {
- // 多选模式:绿色选择按钮
- html = '<button class="btn btn-xs btn-success btn-select-multi' + disabledClass + '" data-id="' + row.id + '" data-index="' + index + '" data-selected="false" title="' + (disabled ? '优惠券不可用' : '点击添加到选择列表') + '"' + disabledAttr + '>' +
- '<i class="fa fa-plus"></i> 选择</button>';
- }
- return html;
- }
- }
- ];
- // 初始化表格
- table.bootstrapTable({
- url: $.fn.bootstrapTable.defaults.extend.index_url,
- pk: 'id',
- sortName: 'id',
- clickToSelect: false, // 禁用点击行选择
- columns: [columns]
- });
-
- // 存储选中的优惠券(用于按钮模式)
- var selectedCoupons = [];
-
- // 单选按钮点击事件
- $(document).on('click', '.btn-select-single:not(.disabled)', function () {
- var $btn = $(this);
- var id = $btn.data('id');
- var index = $btn.data('index');
- var rowData = table.bootstrapTable('getData')[index];
-
- // 直接选择优惠券并关闭弹窗
- Fast.api.close([rowData]);
- });
-
- // 多选按钮点击事件
- $(document).on('click', '.btn-select-multi:not(.disabled)', function () {
- var $btn = $(this);
- var id = $btn.data('id');
- var index = $btn.data('index');
- var isSelected = $btn.data('selected');
- var rowData = table.bootstrapTable('getData')[index];
-
- if (!isSelected) {
- // 检查最大选择限制
- if (maxSelect > 0 && selectedCoupons.length >= maxSelect) {
- Toastr.warning('最多只能选择 ' + maxSelect + ' 个优惠券');
- return;
- }
-
- // 添加到选中列表
- selectedCoupons.push(rowData);
- $btn.removeClass('btn-success').addClass('btn-danger')
- .html('<i class="fa fa-minus"></i> 取消选择')
- .attr('title', '点击从选择列表中移除')
- .data('selected', true);
- } else {
- // 从选中列表移除
- selectedCoupons = selectedCoupons.filter(function(item) {
- return item.id != id;
- });
- $btn.removeClass('btn-danger').addClass('btn-success')
- .html('<i class="fa fa-plus"></i> 选择')
- .attr('title', '点击添加到选择列表')
- .data('selected', false);
- }
-
- // 更新选中数量显示
- updateSelectedCount();
- });
-
- // 更新选中数量显示
- function updateSelectedCount() {
- var count = selectedCoupons.length;
- var countText = count > 0 ? '已选择 ' + count + ' 个优惠券' : '请选择优惠券';
-
- // 更新确认按钮文本
- $('.btn-coupon-select').text(countText === '请选择优惠券' ? '确认选择' : '确认选择 (' + count + ')');
-
- // 如果达到最大选择数量,禁用其他选择按钮
- if (maxSelect > 0 && count >= maxSelect) {
- $('.btn-select-multi[data-selected="false"]:not(.disabled)').prop('disabled', true).addClass('disabled');
- } else {
- $('.btn-select-multi[data-selected="false"]:not(.disabled)').prop('disabled', false).removeClass('disabled');
- }
- }
-
- // 确认按钮绑定事件(仅多选模式使用)
- $(document).on('click', '.btn-coupon-select', function () {
- var selectedRows = [];
-
- if (selectMode === 'single') {
- // 单选模式:不应该走到这里,因为单选时直接选择
- Layer.alert('单选模式请直接点击优惠券的选择按钮');
- return;
- } else {
- // 多选模式:使用按钮选择的优惠券
- selectedRows = selectedCoupons;
- }
-
- if (selectedRows.length === 0) {
- Layer.alert('请选择优惠券');
- return;
- }
-
- // 调用父窗口回调函数
- Fast.api.close(selectedRows);
- });
-
- // 在页面标题中显示选择模式
- var modeText = selectMode === 'single' ? '单选' : '多选';
- if (selectMode === 'multiple' && maxSelect > 0) {
- modeText += '(最多' + maxSelect + '个)';
- }
- $('.panel-heading, .layui-layer-title').each(function() {
- var currentTitle = $(this).text();
- if (currentTitle.indexOf('选择优惠券') !== -1 && currentTitle.indexOf(modeText) === -1) {
- $(this).text(currentTitle + ' - ' + modeText);
- } else if (currentTitle.indexOf('选择') !== -1 && currentTitle.indexOf('优惠券') === -1 && currentTitle.indexOf(modeText) === -1) {
- $(this).text(currentTitle.replace('选择', '选择优惠券') + ' - ' + modeText);
- }
- });
-
- // 显示选择模式提示信息
- $('#selection-info').show();
- $('#selection-mode-text').text(modeText);
-
- if (selectMode === 'single') {
- $('#selection-tip').html('<strong>单击任意优惠券的"选择"按钮即可完成选择</strong>');
- // 单选模式隐藏确认按钮
- $('#confirm-select-btn').hide();
- } else {
- var tipText = '点击优惠券的"选择"按钮添加到选择列表,绿色表示可选择,红色表示已选择';
- if (maxSelect > 0) {
- tipText += ',最多可选择' + maxSelect + '个优惠券';
- }
- $('#selection-tip').text(tipText);
- // 多选模式显示确认按钮
- $('#confirm-select-btn').show();
- }
-
- // 表格刷新后恢复选择状态
- table.on('refresh.bs.table load-success.bs.table', function () {
- if (selectMode === 'multiple') {
- // 恢复多选按钮状态
- setTimeout(function() {
- $('.btn-select-multi').each(function() {
- var $btn = $(this);
- var id = $btn.data('id');
- var isSelected = selectedCoupons.some(function(item) {
- return item.id == id;
- });
-
- if (isSelected && !$btn.hasClass('disabled')) {
- $btn.removeClass('btn-success').addClass('btn-danger')
- .html('<i class="fa fa-minus"></i> 取消选择')
- .attr('title', '点击从选择列表中移除')
- .data('selected', true);
- } else if (!$btn.hasClass('disabled')) {
- $btn.removeClass('btn-danger').addClass('btn-success')
- .html('<i class="fa fa-plus"></i> 选择')
- .attr('title', '点击添加到选择列表')
- .data('selected', false);
- }
- });
-
- // 更新选中数量显示
- updateSelectedCount();
- }, 100);
- }
- });
-
- // 为表格绑定事件
- Table.api.bindevent(table);
- },
- result: function () {
- //优惠模式切换
- $('#c-result').on('change', function () {
- if ($(this).val() == 0) {
- $('#text-a').html('打');
- $('#text-b').html('折');
- } else {
- $('#text-a').html('减');
- $('#text-b').html('元');
- }
- })
- //去掉日期的选项
- $('#c-receive_times').data('ranges', 1);
- $('#c-use_times').data('ranges', 1);
- let receive_times1 = '';
- let receive_times2 = '';
- let use_times1 = '';
- let use_times2 = '';
- //有效期模式切换
- $('.modes').click(function () {
- var value = $(this).val() //获取选中的radio的值
- if (value != 'fixation') {//固定日期
- receive_times2 = $('#c-receive_times').val();
- use_times2 = $('#c-use_times').val();
- $('#modetpl').html(Template('datestpl', {receive_times: receive_times1, use_times: use_times1}));
- //去掉日期的选项
- $('#c-receive_times').data('ranges', 1);
- $('#c-use_times').data('ranges', 1);
- Form.events.daterangepicker($("form[role=form]"));
- } else {//天数
- receive_times1 = $('#c-receive_times').val();
- use_times1 = $('#c-use_times').val();
- $('#modetpl').html(Template('fixationtpl', {receive_times: receive_times2, use_times: use_times2}));
- }
- });
- },
- add: function () {
- this.result();
- Controller.api.bindevent();
- },
- edit: function () {
- this.result();
- Controller.api.bindevent();
- },
- api: {
- bindevent: function () {
- Form.api.bindevent($("form[role=form]"));
- }
- }
- };
- return Controller;
- });
|