Просмотр исходного кода

管理页面费率汇率修改

Taio_O 3 недель назад
Родитель
Сommit
8aabc2f537
3 измененных файлов с 201 добавлено и 33 удалено
  1. 44 4
      admin/controllers/groupController.js
  2. 107 21
      admin/models/Group.js
  3. 50 8
      admin/views/groups.html

+ 44 - 4
admin/controllers/groupController.js

@@ -33,17 +33,40 @@ const getGroupById = async (req, res) => {
 // @access  Private/Admin
 const createGroup = async (req, res) => {
     try {
-        const { groupId, groupName } = req.body;
+        const { 
+            groupId, 
+            groupName,
+            group_type = 'group',
+            creator_id,
+            admin_id,
+            in_fee_rate = 0,
+            in_exchange_rate = 1,
+            out_fee_rate = 0,
+            out_exchange_rate = 1
+        } = req.body;
+        
         const groupIdExists = await Group.findByGroupId(groupId);
         
         if (groupIdExists) {
             return res.status(400).json({ message: '群组ID已存在' });
         }
 
-        const id = await Group.create({ groupId, groupName });
+        const id = await Group.create({ 
+            group_id: groupId, 
+            group_name: groupName,
+            group_type,
+            creator_id,
+            admin_id,
+            in_fee_rate,
+            in_exchange_rate,
+            out_fee_rate,
+            out_exchange_rate
+        });
+        
         const group = await Group.findById(id);
         res.status(201).json(group);
     } catch (error) {
+        console.error('创建群组失败:', error);
         res.status(500).json({ message: '服务器错误' });
     }
 };
@@ -53,17 +76,34 @@ const createGroup = async (req, res) => {
 // @access  Private/Admin
 const updateGroup = async (req, res) => {
     try {
-        const { groupName, isActive, feeRate } = req.body;
+        const { 
+            group_name, 
+            is_active, 
+            in_fee_rate, 
+            in_exchange_rate, 
+            out_fee_rate, 
+            out_exchange_rate 
+        } = req.body;
+        
         const group = await Group.findById(req.params.id);
         
         if (!group) {
             return res.status(404).json({ message: '群组不存在' });
         }
 
-        await Group.update(req.params.id, { groupName, isActive, feeRate });
+        await Group.update(req.params.id, { 
+            group_name, 
+            is_active, 
+            in_fee_rate, 
+            in_exchange_rate, 
+            out_fee_rate, 
+            out_exchange_rate 
+        });
+        
         const updatedGroup = await Group.findById(req.params.id);
         res.json(updatedGroup);
     } catch (error) {
+        console.error('更新群组失败:', error);
         res.status(500).json({ message: '服务器错误' });
     }
 };

+ 107 - 21
admin/models/Group.js

@@ -25,19 +25,27 @@ createGroupTable();
 const Group = {
     // 获取所有群组
     findAll: async () => {
-        const [rows] = await pool.query(
-            'SELECT * FROM groups ORDER BY created_at DESC'
-        );
-        return rows;
+        try {
+            const [rows] = await pool.query('SELECT * FROM groups ORDER BY created_at DESC');
+            return rows;
+        } catch (error) {
+            console.error('查询群组列表失败:', error);
+            throw error;
+        }
     },
 
     // 根据ID查找群组
     findById: async (id) => {
-        const [rows] = await pool.query(
-            'SELECT * FROM groups WHERE id = ?',
-            [id]
-        );
-        return rows[0];
+        try {
+            const [rows] = await pool.query(
+                'SELECT * FROM groups WHERE id = ?',
+                [id]
+            );
+            return rows[0];
+        } catch (error) {
+            console.error('查询群组失败:', error);
+            throw error;
+        }
     },
 
     // 根据群组ID查找群组
@@ -50,25 +58,103 @@ const Group = {
     },
 
     // 创建群组
-    create: async (groupData) => {
-        const [result] = await pool.query(
-            'INSERT INTO groups (group_id, group_name, creator_id) VALUES (?, ?, ?)',
-            [groupData.groupId, groupData.groupName, groupData.creatorId]
-        );
-        return result.insertId;
+    create: async ({ 
+        group_id, 
+        group_name, 
+        group_type = 'group',
+        creator_id,
+        admin_id,
+        in_fee_rate = 0,
+        in_exchange_rate = 1,
+        out_fee_rate = 0,
+        out_exchange_rate = 1
+    }) => {
+        try {
+            const [result] = await pool.query(
+                `INSERT INTO groups (
+                    group_id, 
+                    group_name, 
+                    group_type,
+                    creator_id,
+                    admin_id,
+                    in_fee_rate,
+                    in_exchange_rate,
+                    out_fee_rate,
+                    out_exchange_rate
+                ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
+                [
+                    group_id,
+                    group_name,
+                    group_type,
+                    creator_id,
+                    admin_id,
+                    in_fee_rate,
+                    in_exchange_rate,
+                    out_fee_rate,
+                    out_exchange_rate
+                ]
+            );
+            return result.insertId;
+        } catch (error) {
+            console.error('创建群组失败:', error);
+            throw error;
+        }
     },
 
     // 更新群组
-    update: async (id, groupData) => {
-        await pool.query(
-            'UPDATE groups SET group_name = ?, is_active = ?, fee_rate = ? WHERE id = ?',
-            [groupData.groupName, groupData.isActive, groupData.feeRate, id]
-        );
+    update: async (id, { 
+        group_name, 
+        is_active, 
+        in_fee_rate, 
+        in_exchange_rate, 
+        out_fee_rate, 
+        out_exchange_rate 
+    }) => {
+        try {
+            const [result] = await pool.query(
+                `UPDATE groups 
+                SET group_name = ?,
+                    is_active = ?,
+                    in_fee_rate = ?,
+                    in_exchange_rate = ?,
+                    out_fee_rate = ?,
+                    out_exchange_rate = ?,
+                    updated_at = CURRENT_TIMESTAMP
+                WHERE id = ?`,
+                [
+                    group_name,
+                    is_active,
+                    in_fee_rate,
+                    in_exchange_rate,
+                    out_fee_rate,
+                    out_exchange_rate,
+                    id
+                ]
+            );
+
+            if (result.affectedRows === 0) {
+                throw new Error('群组不存在');
+            }
+
+            return result;
+        } catch (error) {
+            console.error('更新群组失败:', error);
+            throw error;
+        }
     },
 
     // 删除群组
     delete: async (id) => {
-        await pool.query('DELETE FROM groups WHERE id = ?', [id]);
+        try {
+            const [result] = await pool.query('DELETE FROM groups WHERE id = ?', [id]);
+            if (result.affectedRows === 0) {
+                throw new Error('群组不存在');
+            }
+            return result;
+        } catch (error) {
+            console.error('删除群组失败:', error);
+            throw error;
+        }
     }
 };
 

+ 50 - 8
admin/views/groups.html

@@ -116,7 +116,10 @@
                                         <th>群组ID</th>
                                         <th>群组名称</th>
                                         <th>状态</th>
-                                        <th>费率(%)</th>
+                                        <th>入款费率(%)</th>
+                                        <th>入款汇率</th>
+                                        <th>出款费率(%)</th>
+                                        <th>出款汇率</th>
                                         <th>创建时间</th>
                                         <th>操作</th>
                                     </tr>
@@ -183,8 +186,20 @@
                             </div>
                         </div>
                         <div class="mb-3">
-                            <label for="editFeeRate" class="form-label">费率 (%)</label>
-                            <input type="number" class="form-control" id="editFeeRate" step="0.1" min="0" max="100" required>
+                            <label for="editInFeeRate" class="form-label">入款费率 (%)</label>
+                            <input type="number" class="form-control" id="editInFeeRate" step="0.01" min="0" max="100" required>
+                        </div>
+                        <div class="mb-3">
+                            <label for="editInExchangeRate" class="form-label">入款汇率</label>
+                            <input type="number" class="form-control" id="editInExchangeRate" step="0.0001" min="0" required>
+                        </div>
+                        <div class="mb-3">
+                            <label for="editOutFeeRate" class="form-label">出款费率 (%)</label>
+                            <input type="number" class="form-control" id="editOutFeeRate" step="0.01" min="0" max="100" required>
+                        </div>
+                        <div class="mb-3">
+                            <label for="editOutExchangeRate" class="form-label">出款汇率</label>
+                            <input type="number" class="form-control" id="editOutExchangeRate" step="0.0001" min="0" required>
                         </div>
                     </form>
                 </div>
@@ -276,7 +291,10 @@
                             ${group.is_active ? '启用' : '禁用'}
                         </span>
                     </td>
-                    <td>${parseFloat(group.fee_rate || 0).toFixed(1)}%</td>
+                    <td>${parseFloat(group.in_fee_rate || 0).toFixed(2)}%</td>
+                    <td>${parseFloat(group.in_exchange_rate || 1).toFixed(4)}</td>
+                    <td>${parseFloat(group.out_fee_rate || 0).toFixed(2)}%</td>
+                    <td>${parseFloat(group.out_exchange_rate || 1).toFixed(4)}</td>
                     <td>${new Date(group.created_at).toLocaleString()}</td>
                     <td>
                         <button class="btn btn-sm btn-primary btn-action" onclick="editGroup('${group.id}')">
@@ -340,7 +358,10 @@
                     document.getElementById('editGroupId').value = group.id;
                     document.getElementById('editGroupName').value = group.group_name;
                     document.getElementById('editGroupStatus').checked = group.is_active;
-                    document.getElementById('editFeeRate').value = group.fee_rate || 0;
+                    document.getElementById('editInFeeRate').value = group.in_fee_rate || 0;
+                    document.getElementById('editInExchangeRate').value = group.in_exchange_rate || 1;
+                    document.getElementById('editOutFeeRate').value = group.out_fee_rate || 0;
+                    document.getElementById('editOutExchangeRate').value = group.out_exchange_rate || 1;
 
                     const modal = new bootstrap.Modal(document.getElementById('editGroupModal'));
                     modal.show();
@@ -367,9 +388,13 @@
             const groupId = document.getElementById('editGroupId').value;
             const groupName = document.getElementById('editGroupName').value;
             const isActive = document.getElementById('editGroupStatus').checked;
-            const feeRate = parseFloat(document.getElementById('editFeeRate').value) || 0;
+            const inFeeRate = parseFloat(document.getElementById('editInFeeRate').value) || 0;
+            const inExchangeRate = parseFloat(document.getElementById('editInExchangeRate').value) || 1;
+            const outFeeRate = parseFloat(document.getElementById('editOutFeeRate').value) || 0;
+            const outExchangeRate = parseFloat(document.getElementById('editOutExchangeRate').value) || 1;
 
-            if (isNaN(feeRate) || feeRate < 0 || feeRate > 100) {
+            if (isNaN(inFeeRate) || inFeeRate < 0 || inFeeRate > 100 ||
+                isNaN(outFeeRate) || outFeeRate < 0 || outFeeRate > 100) {
                 Swal.fire({
                     icon: 'error',
                     title: '输入错误',
@@ -378,6 +403,16 @@
                 return;
             }
 
+            if (isNaN(inExchangeRate) || inExchangeRate <= 0 ||
+                isNaN(outExchangeRate) || outExchangeRate <= 0) {
+                Swal.fire({
+                    icon: 'error',
+                    title: '输入错误',
+                    text: '汇率必须大于0'
+                });
+                return;
+            }
+
             try {
                 const token = localStorage.getItem('token');
                 const response = await fetch(`/api/groups/${groupId}`, {
@@ -386,7 +421,14 @@
                         'Authorization': `Bearer ${token}`,
                         'Content-Type': 'application/json'
                     },
-                    body: JSON.stringify({ groupName, isActive, feeRate })
+                    body: JSON.stringify({ 
+                        group_name: groupName,
+                        is_active: isActive,
+                        in_fee_rate: inFeeRate,
+                        in_exchange_rate: inExchangeRate,
+                        out_fee_rate: outFeeRate,
+                        out_exchange_rate: outExchangeRate
+                    })
                 });
 
                 if (response.ok) {