manghe/public/static/admin/reward-component.js
2025-04-02 14:56:13 +00:00

390 lines
14 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 奖励信息组件
* 用于创建和管理奖励信息表单元素
*/
/**
* 初始化奖励信息区域
* @param {string} containerId - 容器ID
* @param {Array|string} existingRewards - 已存在的奖励数据数组或reward_id
*/
function initRewardInfo(containerId, existingRewards = null) {
var $ = layui.$;
var form = layui.form;
// 奖励类型选项
var rewardTypes = [
{ value: '1', text: '钻石' },
{ value: '2', text: 'UU币' },
{ value: '3', text: '达达卷' },
{ value: '4', text: '优惠券' }
];
// 存储优惠券数据
var couponsData = [];
// 创建奖励信息卡片
var cardHtml = '<div class="layui-card">' +
'<div class="layui-card-header">' +
'<button type="button" class="layui-btn layui-btn-sm layui-btn-normal" id="addReward">' +
'<i class="layui-icon">&#xe654;</i>添加奖励' +
'</button>' +
'</div>' +
'<div class="layui-card-body">' +
'<div id="rewardContainer">' +
'<!-- 动态添加的奖励项将显示在这里 -->' +
'</div>' +
'</div>' +
'</div>';
if ($('input[name="reward_id"]').length == 0) {
cardHtml += '<input type="hidden" name="reward_id" value="">';
}
// 渲染奖励信息卡片
$('#' + containerId).html(cardHtml);
// 请求优惠券数据
function fetchCoupons(callback) {
var load = layer.load(2);
$.ajax({
url: '/admin/get_coupons',
type: 'GET',
dataType: 'json',
success: function (res) {
layer.close(load);
if (res.code == 0) {
couponsData = res.data;
if (typeof callback === 'function') {
callback(couponsData);
}
} else {
layer.msg(res.msg || '获取优惠券失败', { icon: 2, anim: 6, time: 1500 });
}
},
error: function () {
layer.close(load);
layer.msg('网络错误,请稍后重试', { icon: 2, anim: 6, time: 1500 });
}
});
}
// 生成唯一ID
function generateUniqueId() {
return 'reward_' + new Date().getTime() + '_' + Math.floor(Math.random() * 1000);
}
// 添加奖励项
function addRewardItem(rewardData = null) {
var uniqueId = generateUniqueId();
var html = '<div class="layui-form-item reward-item" id="' + uniqueId + '">' +
'<div class="layui-inline">' +
'<select class="reward-type-select" lay-filter="rewardTypeFilter">' +
'<option value="">请选择奖励类型</option>';
// 添加奖励类型选项
for (var i = 0; i < rewardTypes.length; i++) {
var selected = rewardData && rewardData.reward_type == rewardTypes[i].value ? 'selected' : '';
html += '<option value="' + rewardTypes[i].value + '" ' + selected + '>' + rewardTypes[i].text + '</option>';
}
html += '</select>' +
'</div>';
// 根据奖励类型决定显示哪个输入区域
var rewardValueDisplay = (!rewardData || rewardData.reward_type != '4') ? '' : 'style="display:none;"';
var couponDisplay = (!rewardData || rewardData.reward_type != '4') ? 'style="display:none;"' : '';
var rewardValue = rewardData ? rewardData.reward_value : '';
html += '<div class="layui-inline reward-value-container" ' + rewardValueDisplay + '>' +
'<input type="text" placeholder="请输入奖励数值" class="layui-input reward-value-input" value="' + rewardValue + '">' +
'</div>' +
'<div class="layui-inline coupon-container" ' + couponDisplay + '>' +
'<select class="coupon-select">' +
'<option value="">请选择优惠券</option>' +
'<option value="loading" disabled>加载中...</option>' +
'</select>' +
'</div>' +
'<div class="layui-inline">' +
'<button type="button" class="layui-btn layui-btn-danger layui-btn-sm remove-reward" data-id="' + uniqueId + '">' +
'<i class="layui-icon">&#xe640;</i>删除' +
'</button>' +
'</div>' +
'</div>';
$('#rewardContainer').append(html);
form.render('select'); // 重新渲染表单
// 绑定删除按钮事件
$('.remove-reward[data-id="' + uniqueId + '"]').click(function () {
var id = $(this).data('id');
$('#' + id).remove();
});
// 如果是优惠券类型,需要加载优惠券数据
if (rewardData && rewardData.reward_type == '4') {
var $item = $('#' + uniqueId);
var $couponSelect = $item.find('.coupon-select');
// 获取优惠券数据并选中指定的优惠券
if (couponsData.length > 0) {
fillCouponOptions($couponSelect, couponsData, rewardData.reward_extend);
} else {
fetchCoupons(function (coupons) {
fillCouponOptions($couponSelect, coupons, rewardData.reward_extend);
});
}
}
return uniqueId;
}
// 添加奖励按钮点击事件
$('#addReward').click(function () {
addRewardItem();
});
// 监听奖励类型选择
form.on('select(rewardTypeFilter)', function (data) {
var $item = $(data.elem).closest('.reward-item');
var value = data.value;
if (value == '4') { // 选择了优惠券
$item.find('.reward-value-container').hide();
$item.find('.coupon-container').show();
$item.find('.reward-value-input').val(''); // 清空奖励数值
// 获取当前选择框的优惠券选择框
var $couponSelect = $item.find('.coupon-select');
// 如果优惠券数据已加载,直接填充
if (couponsData.length > 0) {
fillCouponOptions($couponSelect, couponsData);
} else {
// 否则请求加载
fetchCoupons(function (coupons) {
fillCouponOptions($couponSelect, coupons);
});
}
} else {
$item.find('.reward-value-container').show();
$item.find('.coupon-container').hide();
$item.find('.coupon-select').val(''); // 清空优惠券选择
form.render('select');
}
});
// 填充优惠券选项
function fillCouponOptions($select, coupons, selectedCouponId = null) {
// 清空现有选项,保留第一个"请选择"
$select.find('option:not(:first)').remove();
// 添加优惠券选项
for (var i = 0; i < coupons.length; i++) {
var coupon = coupons[i];
var selected = selectedCouponId && coupon.id == selectedCouponId ? 'selected' : '';
$select.append('<option value="' + coupon.id + '" ' + selected + '>' +
coupon.title + ' (满' + coupon.man_price + '减' + coupon.price + ')</option>');
}
// 重新渲染表单
form.render('select');
}
// 处理初始化奖励数据
if (existingRewards) {
// 如果传入的是字符串reward_id则通过接口获取奖励数据
if (typeof existingRewards === 'string' && existingRewards.trim() !== '') {
var rewardId = existingRewards;
// 通过API获取奖励数据
var loadingIndex = layer.load(1, { shade: [0.1, '#fff'] });
$.ajax({
url: '/admin/get_rewards_by_id',
type: 'GET',
data: { reward_id: rewardId },
dataType: 'json',
success: function (res) {
layer.close(loadingIndex);
if (res.status === 1 && res.data && res.data.length > 0) {
// 先获取优惠券数据,再初始化奖励表单
if ($('input[name="reward_id"]').length > 0) {
$('input[name="reward_id"]').val(rewardId);
}
fetchCoupons(function () {
// 遍历奖励数据,添加奖励项
for (var i = 0; i < res.data.length; i++) {
var rewardItem = {
reward_type: res.data[i].reward_type.toString(),
reward_value: res.data[i].reward_value || '',
reward_extend: res.data[i].reward_extend || ''
};
addRewardItem(rewardItem);
}
});
} else {
layer.msg('没有找到奖励数据或获取失败', { icon: 2, time: 2000 });
}
},
error: function () {
layer.close(loadingIndex);
layer.msg('网络错误,无法获取奖励数据', { icon: 2, time: 2000 });
}
});
}
// 如果传入的是数组,直接使用
else if (Array.isArray(existingRewards) && existingRewards.length > 0) {
// 先获取优惠券数据,再初始化奖励表单
fetchCoupons(function () {
for (var i = 0; i < existingRewards.length; i++) {
addRewardItem(existingRewards[i]);
}
});
}
} else {
// 预加载优惠券数据,以便后续使用
fetchCoupons();
}
}
/**
* 验证奖励信息
* @returns {boolean} - 验证结果
*/
function validateRewardInfo() {
var $ = layui.$;
var hasRewardType = false;
$('.reward-type-select').each(function () {
if ($(this).val()) {
hasRewardType = true;
var $item = $(this).closest('.reward-item');
var rewardType = $(this).val();
// 如果不是优惠券类型,检查奖励数值
if (rewardType != '4' && !$item.find('.reward-value-input').val()) {
layer.msg('请输入奖励数值', { icon: 2, anim: 6, time: 1500 });
hasRewardType = false;
return false;
}
// 如果是优惠券类型,检查是否选择了优惠券
if (rewardType == '4' && !$item.find('.coupon-select').val()) {
layer.msg('请选择优惠券', { icon: 2, anim: 6, time: 1500 });
hasRewardType = false;
return false;
}
}
});
if (!hasRewardType) {
layer.msg('请至少添加一项奖励信息', { icon: 2, anim: 6, time: 1500 });
return false;
}
// 验证通过后,处理奖励数据
processRewardData();
return true;
}
/**
* 处理奖励数据将其转换为JSON格式并添加到隐藏字段
*/
function processRewardData() {
var $ = layui.$;
var rewardData = [];
// 收集所有奖励项的数据
$('.reward-item').each(function () {
var $item = $(this);
var rewardType = $item.find('.reward-type-select').val();
if (rewardType) {
var rewardObj = {
reward_type: rewardType,
reward_value: '',
coupon_id: ''
};
// 根据奖励类型设置相应的值
if (rewardType == '4') { // 优惠券类型
rewardObj.coupon_id = $item.find('.coupon-select').val() || '';
} else { // 其他类型
rewardObj.reward_value = $item.find('.reward-value-input').val() || '';
}
rewardData.push(rewardObj);
}
});
// 将奖励数据转换为JSON字符串
var rewardJson = JSON.stringify(rewardData);
// 检查表单中是否已存在reward隐藏字段
if ($('input[name="reward"]').length > 0) {
$('input[name="reward"]').val(rewardJson);
} else {
// 创建隐藏字段并添加到表单中
$('form').append('<input type="hidden" name="reward" value=\'' + rewardJson + '\'>');
}
// 清除可能残留的旧字段(兼容性考虑)
$('input[name="reward_type[]"], input[name="reward_value[]"], input[name="coupon_id[]"]').remove();
}
/**
* 处理奖励数据将其转换为JSON格式并添加到隐藏字段
*/
async function processRewardIdData(pre, reward_id = '') {
var $ = layui.$;
var rewardData = [];
// 收集所有奖励项的数据
$('.reward-item').each(function () {
var $item = $(this);
var rewardType = $item.find('.reward-type-select').val();
if (rewardType) {
var rewardObj = {
reward_type: rewardType,
reward_value: '',
coupon_id: ''
};
// 根据奖励类型设置相应的值
if (rewardType == '4') { // 优惠券类型
rewardObj.coupon_id = $item.find('.coupon-select').val() || '';
} else { // 其他类型
rewardObj.reward_value = $item.find('.reward-value-input').val() || '';
}
rewardData.push(rewardObj);
}
});
if (reward_id || reward_id == '') {
if ($('input[name="reward_id"]').length > 0) {
reward_id = $('input[name="reward_id"]').val();
}
}
// 将奖励数据转换为JSON字符串
var rewardJson = JSON.stringify(rewardData);
// reward_add_json
var url = "/admin/reward_add_json.html";
var $ = layui.$;
var load = layer.load(2);
let l = await $.post(url, { reward: rewardJson, reward_id: reward_id, reward_id_pre: pre });
layer.close(load);
console.log(l);
if (l && l.status === 1) {
reward_id = l.data.reward_id;
}
// 检查表单中是否已存在reward隐藏字段
if ($('input[name="reward_id"]').length > 0) {
$('input[name="reward_id"]').val(reward_id);
} else {
// 创建隐藏字段并添加到表单中
$('form').append('<input type="hidden" name="reward_id" value=\'' + reward_id + '\'>');
}
// 清除可能残留的旧字段(兼容性考虑)
$('input[name="reward_type[]"], input[name="reward_value[]"], input[name="coupon_id[]"]').remove();
}