曹睿
2025-04-25 ec1bef3a37e8dcdf22f1bf52e7c272a18306f4b9
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<template>
  <view class="work">
    <!-- 筛选 -->
    <wd-drop-menu close-on-click-modal class="mb-20rpx mr-20rpx ml-20rpx">
      <wd-drop-menu-item ref="dropMenu" title="筛选" icon="filter" icon-size="18px">
        <view>
          <wd-input
            v-model="queryParams.keywords"
            label="关键字"
            type="text"
            clearable
            placeholder="请输入关键字"
          />
          <view class="flex flex-row items-center mb-20rpx">
            <wd-button class="mt-20rpx mb-20rpx" size="medium" @click="handleQuery()">
              查询
            </wd-button>
            <wd-button size="medium" type="info" @click="handleReset">重置</wd-button>
          </view>
        </view>
      </wd-drop-menu-item>
    </wd-drop-menu>
 
    <view class="data-container">
      <!-- 列表内容 -->
      <view v-for="(item, index) in pageData" :key="index" class="mb-20rpx">
        <wd-card>
          <template #title>
            <view class="flex items-center justify-between">
              <view class="flex-1 text-truncate">{{ item.configName }}</view>
            </view>
          </template>
 
          <wd-cell-group>
            <wd-cell title="配置键" :value="item.configKey" />
            <wd-cell title="配置值" :value="item.configValue" />
            <wd-cell title="备注" :value="item.remark" />
          </wd-cell-group>
 
          <template #footer>
            <wd-button size="small" plain type="primary" @click="handleAction(item)">
              操作
            </wd-button>
          </template>
        </wd-card>
      </view>
    </view>
 
    <!-- 加载更多 -->
    <wd-loadmore :state="state" @reload="handleQuery" />
 
    <!-- 底部按钮 -->
    <view class="fixed bottom-0 w-full flex justify-around items-center p-20rpx bg-#fff">
      <wd-button size="medium" type="primary" @click="handleOpenDialog">添加</wd-button>
      <wd-button size="medium" type="success" @click="refreshCache">刷新缓存</wd-button>
    </view>
 
    <wd-popup v-model="showEditPopup" position="bottom">
      <view class="p-20rpx">
        <wd-form ref="formRef" :model="form">
          <wd-input
            v-model="form.configName"
            label="配置名称"
            type="text"
            placeholder="请输入配置名称"
            :rules="[{ required: true, message: '请填写配置名称' }]"
          />
          <wd-input
            v-model="form.configKey"
            label="配置键名"
            type="text"
            placeholder="请输入配置键名"
            :rules="[{ required: true, message: '请填写配置键' }]"
          />
          <wd-input
            v-model="form.configValue"
            label="配置键值"
            type="text"
            placeholder="请输入配置键值"
            :rules="[{ required: true, message: '请填写配置值' }]"
          />
          <wd-textarea
            v-model="form.remark"
            prop="remark"
            label="配置描述"
            label-align="right"
            clearable
            :maxlength="100"
            show-word-limit
            label-width="100px"
            placeholder="请输入配置描述"
          />
        </wd-form>
        <view class="flex justify-around mt-20rpx">
          <wd-button @click="showEditPopup = false">取消</wd-button>
          <wd-button type="primary" native-type="submit" :loading="loading" @click="submitForm">
            确定
          </wd-button>
        </view>
      </view>
    </wd-popup>
  </view>
</template>
 
<script lang="ts" setup>
import ConfigAPI, { ConfigPageVO, ConfigForm, ConfigPageQuery } from "@/api/system/config";
import { DropMenuItemExpose } from "wot-design-uni/components/wd-drop-menu-item/types";
import { LoadMoreState } from "wot-design-uni/components/wd-loadmore/types";
import { FormInstance } from "wot-design-uni/components/wd-form/types";
import { debounce } from "@/utils";
const state = ref<LoadMoreState>("loading"); // 加载状态 loading, finished:, error
const total = ref(0);
const queryParams = reactive<ConfigPageQuery>({
  pageNum: 1,
  pageSize: 10,
  keywords: "",
});
// 系统配置表格数据
const pageData = ref<ConfigPageVO[]>([]);
const formRef = ref<FormInstance>();
 
const loading = ref(false);
 
/**
 * 搜索栏
 */
const dropMenu = ref<DropMenuItemExpose>();
 
/**
 * 搜索
 */
function handleQuery() {
  pageData.value = [];
  dropMenu.value?.close();
  queryParams.pageNum = 1;
  loadmore();
}
 
const showEditPopup = ref(false);
const form = ref<ConfigForm>({});
 
// 修改编辑函数
function handleEdit(id: number) {
  ConfigAPI.getFormData(id).then((data) => {
    Object.assign(form.value, data);
    // 显示弹窗
    showEditPopup.value = true;
  });
}
 
// 提交表单
async function submitForm() {
  loading.value = true;
  try {
    if (formRef.value) {
      formRef.value!.validate().then(async ({ valid }) => {
        if (valid) {
          if (form.value.id) {
            await ConfigAPI.update(form.value.id, form.value);
            uni.showToast({ title: "更新成功", icon: "success" });
          } else {
            await ConfigAPI.add(form.value);
            uni.showToast({ title: "添加成功", icon: "success" });
          }
          showEditPopup.value = false;
          handleQuery(); // 刷新列表
        } else {
          uni.showToast({ title: "请检查表单", icon: "error" });
          loading.value = false;
        }
      });
    }
  } catch {
    uni.showToast({ title: "操作失败", icon: "error" });
    loading.value = false;
  } finally {
    loading.value = false;
  }
}
 
/**
 * 重置搜索条件
 */
function handleReset() {
  queryParams.pageNum = 1;
  queryParams.keywords = "";
  pageData.value = [];
  dropMenu.value?.close();
  handleQuery();
}
 
/**
 * 触底事件
 */
onReachBottom(() => {
  queryParams.pageNum++;
  handleQuery();
});
 
/**
 * 加载更多
 */
function loadmore() {
  state.value = "loading";
  ConfigAPI.getPage(queryParams)
    .then((data) => {
      if (queryParams.pageNum === 1) {
        pageData.value = data.list;
      } else {
        pageData.value.push(...data.list);
      }
      total.value = data.total;
    })
    .finally(() => {
      state.value = "finished";
    });
}
 
/**
 * 添加
 */
function handleOpenDialog() {
  form.value.id = undefined;
  form.value.configName = "";
  form.value.configKey = "";
  form.value.configValue = "";
  form.value.remark = "";
  showEditPopup.value = true;
}
 
/**
 * 刷新缓存
 */
const refreshCache = debounce(() => {
  ConfigAPI.refreshCache().then(() => {
    uni.showToast({
      title: "刷新缓存成功",
      icon: "success",
      duration: 1000, // 显示时间,单位为毫秒,设置为 0 则不会自动消失
    });
  });
}, 1000);
 
/**
 * 删除
 */
function handleDelete(item: ConfigPageVO) {
  uni.showModal({
    title: "提示",
    content: "确定要删除该配置吗?",
    success: async (res) => {
      if (res.confirm) {
        if (item.id) {
          ConfigAPI.deleteById(item.id).then(() => {
            uni.showToast({ title: "删除成功", icon: "success" });
          });
        }
      }
    },
  });
}
 
/**
 * 操作
 */
function handleAction(item: ConfigPageVO) {
  const actions = ["编辑", "删除"];
  uni.showActionSheet({
    itemList: actions,
    success: ({ tapIndex }) => {
      switch (actions[tapIndex]) {
        case "编辑":
          handleEdit(item.id || 0);
          break;
        case "删除":
          handleDelete(item);
          break;
      }
    },
  });
}
 
onMounted(() => {
  handleQuery();
});
 
/**
 * 页面返回回来也要重新加载数据
 */
onLoad(() => {
  queryParams.pageNum = 1;
  handleQuery();
});
</script>
<style lang="scss" scoped>
.data-container {
  :deep(.wd-cell__wrapper) {
    padding: 4rpx 0;
  }
 
  :deep(.wd-cell) {
    padding-right: 10rpx;
    background: #f8f8f8;
  }
 
  :deep(.wd-fab__trigger) {
    width: 80rpx !important;
    height: 80rpx !important;
  }
 
  :deep(.wd-cell__right) {
    flex: 2;
  }
 
  .filter-container {
    padding: 10rpx;
    background: #fff;
  }
 
  .data-container {
    margin-top: 20rpx;
  }
}
</style>