diff --git a/public/js/admin.js b/public/js/admin.js
index 6b1dc4a..0d48293 100644
--- a/public/js/admin.js
+++ b/public/js/admin.js
@@ -607,6 +607,10 @@ async function createCoupon() {
}
try {
+ console.log('Creating coupon with data:', {
+ code, name, discount_type: discountType, discount_value: discountValue, is_reusable: isReusable, max_uses: maxUses
+ });
+
const response = await fetch('/api/admin/coupons', {
method: 'POST',
headers: {
@@ -622,7 +626,20 @@ async function createCoupon() {
})
});
- const result = await response.json();
+ console.log('Response status:', response.status);
+ console.log('Response headers:', response.headers);
+
+ const responseText = await response.text();
+ console.log('Response text:', responseText);
+
+ let result;
+ try {
+ result = JSON.parse(responseText);
+ } catch (parseError) {
+ console.error('Failed to parse JSON:', parseError);
+ console.error('Response was:', responseText.substring(0, 200));
+ throw new Error('服务器返回了无效的响应格式');
+ }
if (response.ok) {
alert('优惠码创建成功!');
diff --git a/public/js/main.js b/public/js/main.js
index a4a2d8c..3efc725 100644
--- a/public/js/main.js
+++ b/public/js/main.js
@@ -205,19 +205,33 @@ function showShippingInfoModal() {
const quantity = parseInt(quantityInput.value) || 1;
const unitPrice = currentProduct.price;
const originalTotal = unitPrice * quantity;
- let finalTotal = originalTotal;
- let discountText = '';
- let hasDiscount = false;
- // 计算折扣
+ // 计算数量折扣
+ let quantityDiscount = 0;
+ let quantityDiscountText = '';
+ let afterQuantityDiscount = originalTotal;
+
if (quantity >= 5) {
- finalTotal = originalTotal * 0.9;
- discountText = '9折优惠 (-10%)';
- hasDiscount = true;
+ quantityDiscount = originalTotal * 0.1;
+ quantityDiscountText = '9折优惠 (-10%)';
+ afterQuantityDiscount = originalTotal * 0.9;
} else if (quantity >= 2) {
- finalTotal = originalTotal * 0.95;
- discountText = '9.5折优惠 (-5%)';
- hasDiscount = true;
+ quantityDiscount = originalTotal * 0.05;
+ quantityDiscountText = '9.5折优惠 (-5%)';
+ afterQuantityDiscount = originalTotal * 0.95;
+ }
+
+ // 计算优惠码折扣
+ let couponDiscount = 0;
+ let finalTotal = afterQuantityDiscount;
+
+ if (appliedCoupon) {
+ if (appliedCoupon.discount_type === 'percentage') {
+ couponDiscount = afterQuantityDiscount * (appliedCoupon.discount_value / 100);
+ } else if (appliedCoupon.discount_type === 'fixed') {
+ couponDiscount = Math.min(appliedCoupon.discount_value, afterQuantityDiscount);
+ }
+ finalTotal = afterQuantityDiscount - couponDiscount;
}
// 更新订单汇总信息
@@ -230,9 +244,25 @@ function showShippingInfoModal() {
const discountRow = document.querySelector('.discount-row');
const discountTextElement = document.getElementById('summary-discount-text');
- if (hasDiscount) {
- const discountAmount = originalTotal - finalTotal;
- discountTextElement.textContent = `-$${discountAmount.toFixed(2)} USDT (${discountText})`;
+ if (quantityDiscount > 0 || couponDiscount > 0) {
+ let totalDiscountAmount = quantityDiscount + couponDiscount;
+ let discountDescription = '';
+
+ if (quantityDiscount > 0 && couponDiscount > 0) {
+ // 同时有数量折扣和优惠码折扣
+ discountDescription = `数量${quantityDiscountText} + 优惠码"${appliedCoupon.name}"`;
+ } else if (quantityDiscount > 0) {
+ // 只有数量折扣
+ discountDescription = quantityDiscountText;
+ } else if (couponDiscount > 0) {
+ // 只有优惠码折扣
+ const couponDiscountText = appliedCoupon.discount_type === 'percentage'
+ ? `${appliedCoupon.discount_value}%折扣`
+ : `$${appliedCoupon.discount_value}减免`;
+ discountDescription = `优惠码"${appliedCoupon.name}" (${couponDiscountText})`;
+ }
+
+ discountTextElement.textContent = `-$${totalDiscountAmount.toFixed(2)} USDT (${discountDescription})`;
discountRow.classList.remove('no-discount');
} else {
discountTextElement.textContent = `$0.00 USDT (无优惠)`;
@@ -368,12 +398,23 @@ async function handleOrderSubmit(e) {
// 计算最终价格(包含折扣)
const unitPrice = currentProduct.price;
const originalTotal = unitPrice * quantity;
- let finalTotal = originalTotal;
+ // 数量折扣计算
+ let afterQuantityDiscount = originalTotal;
if (quantity >= 5) {
- finalTotal = originalTotal * 0.9; // 9折
+ afterQuantityDiscount = originalTotal * 0.9; // 9折
} else if (quantity >= 2) {
- finalTotal = originalTotal * 0.95; // 9.5折
+ afterQuantityDiscount = originalTotal * 0.95; // 9.5折
+ }
+
+ // 优惠码折扣计算
+ let finalTotal = afterQuantityDiscount;
+ if (appliedCoupon) {
+ if (appliedCoupon.discount_type === 'percentage') {
+ finalTotal = afterQuantityDiscount * (1 - appliedCoupon.discount_value / 100);
+ } else if (appliedCoupon.discount_type === 'fixed') {
+ finalTotal = Math.max(0, afterQuantityDiscount - appliedCoupon.discount_value);
+ }
}
const orderData = {
@@ -713,12 +754,23 @@ async function handleShippingOrderSubmit(e) {
// 计算最终价格(包含折扣)
const unitPrice = currentProduct.price;
const originalTotal = unitPrice * quantity;
- let finalTotal = originalTotal;
+ // 数量折扣计算
+ let afterQuantityDiscount = originalTotal;
if (quantity >= 5) {
- finalTotal = originalTotal * 0.9; // 9折
+ afterQuantityDiscount = originalTotal * 0.9; // 9折
} else if (quantity >= 2) {
- finalTotal = originalTotal * 0.95; // 9.5折
+ afterQuantityDiscount = originalTotal * 0.95; // 9.5折
+ }
+
+ // 优惠码折扣计算
+ let finalTotal = afterQuantityDiscount;
+ if (appliedCoupon) {
+ if (appliedCoupon.discount_type === 'percentage') {
+ finalTotal = afterQuantityDiscount * (1 - appliedCoupon.discount_value / 100);
+ } else if (appliedCoupon.discount_type === 'fixed') {
+ finalTotal = Math.max(0, afterQuantityDiscount - appliedCoupon.discount_value);
+ }
}
const orderData = {
diff --git a/server.js b/server.js
index 22d5870..76cd01c 100644
--- a/server.js
+++ b/server.js
@@ -42,6 +42,31 @@ db.serialize(() => {
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
)`);
+ // 检查并添加缺失的列
+ db.all("PRAGMA table_info(orders)", [], (err, columns) => {
+ if (err) {
+ console.error('Error checking table structure:', err);
+ return;
+ }
+
+ const columnNames = columns.map(col => col.name);
+
+ // 添加缺失的优惠码相关列
+ if (!columnNames.includes('coupon_code')) {
+ db.run("ALTER TABLE orders ADD COLUMN coupon_code TEXT", (err) => {
+ if (err) console.error('Error adding coupon_code column:', err);
+ else console.log('Added coupon_code column to orders table');
+ });
+ }
+
+ if (!columnNames.includes('coupon_discount')) {
+ db.run("ALTER TABLE orders ADD COLUMN coupon_discount REAL DEFAULT 0", (err) => {
+ if (err) console.error('Error adding coupon_discount column:', err);
+ else console.log('Added coupon_discount column to orders table');
+ });
+ }
+ });
+
// 创建优惠码表
db.run(`CREATE TABLE IF NOT EXISTS coupons (
id INTEGER PRIMARY KEY AUTOINCREMENT,