// 定价管理模块
class PricingManager {
constructor(options = {}) {
this.apiBaseUrl = options.apiBaseUrl || 'http://localhost:3000';
this.grid = options.grid || null;
this.resources = [];
this.init();
}
init() {
this.bindEvents();
// 默认进入页面时加载一次
this.loadResources();
}
bindEvents() {
// 刷新按钮
const refreshBtn = document.getElementById('refreshPricingBtn');
if (refreshBtn) {
refreshBtn.addEventListener('click', () => {
this.loadResources();
});
}
}
// 加载资源
async loadResources() {
if (!this.grid) return;
this.grid.innerHTML = '
加载中...
';
try {
const response = await fetch(`${this.apiBaseUrl}/api/store/resources`);
if (!response.ok) {
throw new Error(`加载资源列表失败: ${response.status} ${response.statusText}`);
}
const result = await response.json();
if (result.success && result.resources) {
this.resources = result.resources;
console.log('[PricingManager] 资源数:', this.resources.length, this.resources);
this.renderResources();
} else {
this.grid.innerHTML = '暂无资源数据
';
}
} catch (error) {
console.error('[PricingManager] 加载资源失败:', error);
const errorMessage = error.message || '未知错误';
this.grid.innerHTML = `加载失败: ${errorMessage}
`;
}
}
// 渲染资源
renderResources() {
if (!this.grid) return;
// 使用 grid 布局,紧凑显示
this.grid.style.display = 'grid';
this.grid.style.gridTemplateColumns = 'repeat(auto-fill, minmax(240px, 1fr))';
this.grid.style.gap = '16px';
this.grid.style.padding = '20px';
this.grid.style.background = '#f9fafb';
if (this.resources.length === 0) {
this.grid.innerHTML = '暂无资源数据
';
return;
}
console.log('[PricingManager] 渲染资源:', this.resources.length);
this.grid.innerHTML = this.resources.map(resource => `
${resource.previewUrl ?
`

` :
'
无预览
'
}
${resource.categoryDir || '-'}
`).join('');
}
// 确认价格修改
async confirmPrice(resourcePath, inputEl) {
const price = inputEl.value.trim();
const priceNum = parseFloat(price);
if (isNaN(priceNum) || priceNum < 0) {
this.showError('价格必须是大于等于0的数字');
return;
}
try {
const response = await fetch(`${this.apiBaseUrl}/api/admin/store/update-price`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
resourcePath: resourcePath,
price: priceNum
})
});
const result = await response.json();
if (result.success) {
// 更新本地数据和原始值
const resource = this.resources.find(r => r.path === resourcePath);
if (resource) {
resource.points = priceNum;
}
inputEl.dataset.original = priceNum;
inputEl.value = priceNum;
// 隐藏确认按钮
const confirmBtn = inputEl.parentElement.querySelector('.btn-confirm');
if (confirmBtn) {
confirmBtn.style.display = 'none';
}
this.showSuccess('价格更新成功');
} else {
this.showError('价格更新失败: ' + (result.message || '未知错误'));
}
} catch (error) {
console.error('[PricingManager] 更新价格失败:', error);
this.showError('价格更新失败,请稍后重试');
}
}
// 更新价格(保留兼容)
async updatePrice(resourcePath, price) {
// 现在由 confirmPrice 处理
}
// 显示错误/成功消息
showError(message) {
if (window.showCustomAlert) {
window.showCustomAlert(message);
} else {
console.error('[PricingManager] 错误:', message);
}
}
showSuccess(message) {
if (window.showCustomAlert) {
window.showCustomAlert(message, 'success');
} else {
console.log('[PricingManager] 成功:', message);
}
}
}
// 导出
if (typeof module !== 'undefined' && module.exports) {
module.exports = PricingManager;
} else {
window.PricingManager = PricingManager;
}