File size: 4,808 Bytes
d0dd276
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<script setup>
import { useDashboardStore } from '../../../stores/dashboard'
import { ref, reactive, watch } from 'vue'

const dashboardStore = useDashboardStore()

// Initialize localConfig with default structure
const localConfig = reactive({
  maxRequestsPerMinute: 0,
  maxRequestsPerDayPerIp: 0
})

const populatedFromStore = ref(false);

// Watch for store changes to populate localConfig ONCE when config is loaded
watch(
  () => ({
    storeMaxRequestsPerMinute: dashboardStore.config.maxRequestsPerMinute,
    storeMaxRequestsPerDayPerIp: dashboardStore.config.maxRequestsPerDayPerIp,
    configIsActuallyLoaded: dashboardStore.isConfigLoaded,
  }),
  (newValues) => {
    if (newValues.configIsActuallyLoaded && !populatedFromStore.value) {
      localConfig.maxRequestsPerMinute = newValues.storeMaxRequestsPerMinute;
      localConfig.maxRequestsPerDayPerIp = newValues.storeMaxRequestsPerDayPerIp;
      populatedFromStore.value = true;
    }
  },
  { deep: true, immediate: true }
)

// 保存组件配置
async function saveComponentConfigs(passwordFromParent) {
  if (!passwordFromParent) {
    return { success: false, message: '基本配置: 密码未提供' }
  }

  let allSucceeded = true;
  let individualMessages = [];

  // 逐个保存配置项
  const configKeys = Object.keys(localConfig)
  for (const key of configKeys) {
    // 如果配置有变化才更新
    if (localConfig[key] !== dashboardStore.config[key]) {
      try {
        await dashboardStore.updateConfig(key, localConfig[key], passwordFromParent)
        // 更新store中的值 - 仅在API调用成功后
        dashboardStore.config[key] = localConfig[key]
        individualMessages.push(`${key} 保存成功`);
      } catch (error) {
        allSucceeded = false;
        individualMessages.push(`${key} 保存失败: ${error.message || '未知错误'}`);
      }
    }
  }

  if (allSucceeded && individualMessages.length === 0) {
    // 如果没有任何更改,也算成功,但提示用户
     return { success: true, message: '基本配置: 无更改需要保存' };
  }
  
  return {
    success: allSucceeded,
    message: `基本配置: ${individualMessages.join('; ')}`
  };
}

defineExpose({
  saveComponentConfigs,
  localConfig
})
</script>

<template>
  <div class="basic-config">
    <h3 class="section-title">基本配置</h3>
    
    <div class="config-form">
      <!-- 数值配置项 -->
      <div class="config-row">
        <div class="config-group">
          <label class="config-label">每分钟请求限制</label>
          <input 
            type="number" 
            class="config-input" 
            v-model.number="localConfig.maxRequestsPerMinute" 
            min="0"
          >
        </div>
        
        <div class="config-group">
          <label class="config-label">每IP每日请求限制</label>
          <input 
            type="number" 
            class="config-input" 
            v-model.number="localConfig.maxRequestsPerDayPerIp" 
            min="0"
          >
        </div>
      </div>
      
      <!-- 移除独立的保存区域 -->
      <!-- 消息提示由父组件处理 -->
    </div>
  </div>
</template>

<style scoped>
.section-title {
  color: var(--color-heading);
  border-bottom: 1px solid var(--color-border);
  padding-bottom: 10px;
  margin-bottom: 20px;
  transition: all 0.3s ease;
  position: relative;
  font-weight: 600;
}

.section-title::after {
  content: '';
  position: absolute;
  bottom: -1px;
  left: 0;
  width: 50px;
  height: 2px;
  background: var(--gradient-primary);
}

.basic-config {
  margin-bottom: 25px;
}

.config-form {
  background-color: var(--stats-item-bg);
  border-radius: var(--radius-lg);
  padding: 20px;
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--card-border);
}

.config-row {
  display: flex;
  gap: 15px;
  margin-bottom: 15px;
  flex-wrap: wrap;
}

.config-group {
  flex: 1;
  min-width: 120px;
}

.config-label {
  display: block;
  font-size: 14px;
  margin-bottom: 5px;
  color: var(--color-text);
  font-weight: 500;
}

.config-input {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background-color: var(--color-background);
  color: var(--color-text);
  font-size: 14px;
  transition: all 0.3s ease;
}

.config-input:focus {
  outline: none;
  border-color: var(--button-primary);
  box-shadow: 0 0 0 2px rgba(79, 70, 229, 0.2);
}

/* 移动端优化 */
@media (max-width: 768px) {
  .config-row {
    gap: 10px;
  }
  
  .config-group {
    min-width: 100px;
  }
}

/* 小屏幕手机进一步优化 */
@media (max-width: 480px) {
  .config-row {
    flex-direction: column;
    gap: 10px;
  }
  
  .config-group {
    width: 100%;
  }
  
  .config-form {
    padding: 15px;
  }
}
</style>