|
|
@@ -0,0 +1,307 @@
|
|
|
+/**
|
|
|
+ * 测试页面的JavaScript文件
|
|
|
+ * 处理数据请求和用户交互
|
|
|
+ */
|
|
|
+
|
|
|
+// 全局变量
|
|
|
+let testData = [];
|
|
|
+let statsData = {};
|
|
|
+
|
|
|
+/**
|
|
|
+ * 初始化测试页面
|
|
|
+ */
|
|
|
+function initTestPage() {
|
|
|
+ console.log('初始化测试页面...');
|
|
|
+
|
|
|
+ // 加载数据
|
|
|
+ loadTestData();
|
|
|
+ loadStatsData();
|
|
|
+
|
|
|
+ // 绑定事件
|
|
|
+ bindEvents();
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 绑定页面事件
|
|
|
+ */
|
|
|
+function bindEvents() {
|
|
|
+ // 可以在这里添加更多的事件绑定
|
|
|
+ console.log('事件绑定完成');
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 加载测试数据
|
|
|
+ */
|
|
|
+function loadTestData() {
|
|
|
+ showLoading(true);
|
|
|
+
|
|
|
+ frappe.call({
|
|
|
+ method: 'uperp.uperp.api.get_test_data',
|
|
|
+ callback: function(response) {
|
|
|
+ hideLoading();
|
|
|
+
|
|
|
+ if (response.message && response.message.success) {
|
|
|
+ testData = response.message.data;
|
|
|
+ renderTestDataTable();
|
|
|
+ showMessage('数据加载成功', 'success');
|
|
|
+ } else {
|
|
|
+ const errorMsg = response.message ? response.message.message : '数据加载失败';
|
|
|
+ showMessage(errorMsg, 'error');
|
|
|
+ console.error('加载测试数据失败:', response);
|
|
|
+ }
|
|
|
+ },
|
|
|
+ error: function(error) {
|
|
|
+ hideLoading();
|
|
|
+ showMessage('网络请求失败', 'error');
|
|
|
+ console.error('API请求错误:', error);
|
|
|
+ }
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 加载统计数据
|
|
|
+ */
|
|
|
+function loadStatsData() {
|
|
|
+ frappe.call({
|
|
|
+ method: 'uperp.uperp.api.get_dashboard_stats',
|
|
|
+ callback: function(response) {
|
|
|
+ if (response.message && response.message.success) {
|
|
|
+ statsData = response.message.stats;
|
|
|
+ renderStatsCards();
|
|
|
+ } else {
|
|
|
+ console.error('加载统计数据失败:', response);
|
|
|
+ }
|
|
|
+ },
|
|
|
+ error: function(error) {
|
|
|
+ console.error('统计数据API请求错误:', error);
|
|
|
+ }
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 渲染数据表格
|
|
|
+ */
|
|
|
+function renderTestDataTable() {
|
|
|
+ const tbody = document.getElementById('test-data-tbody');
|
|
|
+ if (!tbody) return;
|
|
|
+
|
|
|
+ // 清空现有内容
|
|
|
+ tbody.innerHTML = '';
|
|
|
+
|
|
|
+ if (!testData || testData.length === 0) {
|
|
|
+ tbody.innerHTML = '<tr><td colspan="7" class="text-center">暂无数据</td></tr>';
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 生成表格行
|
|
|
+ testData.forEach(function(item) {
|
|
|
+ const row = document.createElement('tr');
|
|
|
+ row.innerHTML = `
|
|
|
+ <td>${item.id}</td>
|
|
|
+ <td>${item.name}</td>
|
|
|
+ <td>${item.description}</td>
|
|
|
+ <td>
|
|
|
+ <span class="badge ${getStatusBadgeClass(item.status)}">
|
|
|
+ ${item.status}
|
|
|
+ </span>
|
|
|
+ </td>
|
|
|
+ <td>${item.created_date}</td>
|
|
|
+ <td>${item.value}</td>
|
|
|
+ <td>
|
|
|
+ <button class="btn btn-sm btn-primary" onclick="viewDetails(${item.id})">
|
|
|
+ <i class="fa fa-eye"></i> 查看
|
|
|
+ </button>
|
|
|
+ <button class="btn btn-sm btn-warning" onclick="editItem(${item.id})">
|
|
|
+ <i class="fa fa-edit"></i> 编辑
|
|
|
+ </button>
|
|
|
+ </td>
|
|
|
+ `;
|
|
|
+ tbody.appendChild(row);
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 渲染统计卡片
|
|
|
+ */
|
|
|
+function renderStatsCards() {
|
|
|
+ // 更新统计数字
|
|
|
+ updateElementText('total-records', statsData.total_records || 0);
|
|
|
+ updateElementText('active-records', statsData.active_records || 0);
|
|
|
+ updateElementText('pending-records', statsData.pending_records || 0);
|
|
|
+ updateElementText('total-value', statsData.total_value || 0);
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 获取状态徽章样式类
|
|
|
+ */
|
|
|
+function getStatusBadgeClass(status) {
|
|
|
+ switch (status) {
|
|
|
+ case '激活':
|
|
|
+ return 'badge-success';
|
|
|
+ case '待审核':
|
|
|
+ return 'badge-warning';
|
|
|
+ case '停用':
|
|
|
+ return 'badge-danger';
|
|
|
+ default:
|
|
|
+ return 'badge-secondary';
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 更新元素文本内容
|
|
|
+ */
|
|
|
+function updateElementText(elementId, text) {
|
|
|
+ const element = document.getElementById(elementId);
|
|
|
+ if (element) {
|
|
|
+ element.textContent = text;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 显示/隐藏加载指示器
|
|
|
+ */
|
|
|
+function showLoading(show = true) {
|
|
|
+ const indicator = document.getElementById('loading-indicator');
|
|
|
+ if (indicator) {
|
|
|
+ indicator.style.display = show ? 'block' : 'none';
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function hideLoading() {
|
|
|
+ showLoading(false);
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 显示消息
|
|
|
+ */
|
|
|
+function showMessage(message, type = 'info') {
|
|
|
+ const messageArea = document.getElementById('message-area');
|
|
|
+ if (!messageArea) return;
|
|
|
+
|
|
|
+ const alertClass = `alert-${type === 'error' ? 'danger' : type}`;
|
|
|
+ messageArea.innerHTML = `
|
|
|
+ <div class="alert ${alertClass} alert-dismissible fade show" role="alert">
|
|
|
+ ${message}
|
|
|
+ <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
|
|
|
+ </div>
|
|
|
+ `;
|
|
|
+ messageArea.style.display = 'block';
|
|
|
+
|
|
|
+ // 自动隐藏消息(3秒后)
|
|
|
+ setTimeout(function() {
|
|
|
+ const alert = messageArea.querySelector('.alert');
|
|
|
+ if (alert) {
|
|
|
+ alert.remove();
|
|
|
+ }
|
|
|
+ if (messageArea.children.length === 0) {
|
|
|
+ messageArea.style.display = 'none';
|
|
|
+ }
|
|
|
+ }, 3000);
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 测试API连接
|
|
|
+ */
|
|
|
+function testApiConnection() {
|
|
|
+ frappe.call({
|
|
|
+ method: 'uperp.uperp.api.test_api_connection',
|
|
|
+ callback: function(response) {
|
|
|
+ if (response.message && response.message.success) {
|
|
|
+ showMessage(`API连接正常! 当前用户: ${response.message.user}`, 'success');
|
|
|
+ } else {
|
|
|
+ showMessage('API连接测试失败', 'error');
|
|
|
+ }
|
|
|
+ },
|
|
|
+ error: function(error) {
|
|
|
+ showMessage('API连接测试失败', 'error');
|
|
|
+ console.error('API连接测试错误:', error);
|
|
|
+ }
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 导出数据
|
|
|
+ */
|
|
|
+function exportData() {
|
|
|
+ if (!testData || testData.length === 0) {
|
|
|
+ showMessage('没有可导出的数据', 'warning');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建CSV内容
|
|
|
+ let csvContent = "ID,名称,描述,状态,创建日期,数值\n";
|
|
|
+ testData.forEach(function(item) {
|
|
|
+ csvContent += `${item.id},"${item.name}","${item.description}","${item.status}","${item.created_date}",${item.value}\n`;
|
|
|
+ });
|
|
|
+
|
|
|
+ // 创建下载链接
|
|
|
+ const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
|
|
|
+ const link = document.createElement('a');
|
|
|
+ const url = URL.createObjectURL(blob);
|
|
|
+ link.setAttribute('href', url);
|
|
|
+ link.setAttribute('download', 'test-data.csv');
|
|
|
+ link.style.visibility = 'hidden';
|
|
|
+ document.body.appendChild(link);
|
|
|
+ link.click();
|
|
|
+ document.body.removeChild(link);
|
|
|
+
|
|
|
+ showMessage('数据导出成功', 'success');
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 查看详情
|
|
|
+ */
|
|
|
+function viewDetails(id) {
|
|
|
+ const item = testData.find(data => data.id === id);
|
|
|
+ if (item) {
|
|
|
+ const detailsHtml = `
|
|
|
+ <div class="modal fade" id="detailsModal" tabindex="-1">
|
|
|
+ <div class="modal-dialog">
|
|
|
+ <div class="modal-content">
|
|
|
+ <div class="modal-header">
|
|
|
+ <h5 class="modal-title">详情信息</h5>
|
|
|
+ <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
|
|
+ </div>
|
|
|
+ <div class="modal-body">
|
|
|
+ <table class="table">
|
|
|
+ <tr><th>ID:</th><td>${item.id}</td></tr>
|
|
|
+ <tr><th>名称:</th><td>${item.name}</td></tr>
|
|
|
+ <tr><th>描述:</th><td>${item.description}</td></tr>
|
|
|
+ <tr><th>状态:</th><td>${item.status}</td></tr>
|
|
|
+ <tr><th>创建日期:</th><td>${item.created_date}</td></tr>
|
|
|
+ <tr><th>数值:</th><td>${item.value}</td></tr>
|
|
|
+ </table>
|
|
|
+ </div>
|
|
|
+ <div class="modal-footer">
|
|
|
+ <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ `;
|
|
|
+
|
|
|
+ // 移除旧的模态框
|
|
|
+ const oldModal = document.getElementById('detailsModal');
|
|
|
+ if (oldModal) {
|
|
|
+ oldModal.remove();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 添加新的模态框
|
|
|
+ document.body.insertAdjacentHTML('beforeend', detailsHtml);
|
|
|
+
|
|
|
+ // 显示模态框
|
|
|
+ const modal = new bootstrap.Modal(document.getElementById('detailsModal'));
|
|
|
+ modal.show();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 编辑项目
|
|
|
+ */
|
|
|
+function editItem(id) {
|
|
|
+ const item = testData.find(data => data.id === id);
|
|
|
+ if (item) {
|
|
|
+ // 这里可以实现编辑功能
|
|
|
+ showMessage(`编辑功能待实现 - ID: ${id}`, 'info');
|
|
|
+ }
|
|
|
+}
|