PluginConfigForm.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <template>
  2. <el-card shadow="never" class="settings-card">
  3. <template #header>
  4. <div class="card-header">
  5. <slot name="header">
  6. <span>插件配置</span>
  7. </slot>
  8. </div>
  9. </template>
  10. <formCreate
  11. :rule="configRule"
  12. :option="configOptions"
  13. v-if="configRule.length > 0"
  14. />
  15. <el-empty v-else description="暂无配置项" />
  16. </el-card>
  17. </template>
  18. <script setup>
  19. import { ref, watch } from 'vue'
  20. import { ElMessage } from 'element-plus'
  21. import { getPluginConfigApi, savePluginConfigApi } from '@/api/plugin'
  22. import {useUserStore} from "@/store/user.js";
  23. const userStore = useUserStore()
  24. const props = defineProps({
  25. pluginId: {
  26. type: String,
  27. required: true
  28. },
  29. setting: {
  30. type: String,
  31. default: null
  32. }
  33. })
  34. const configRule = ref([])
  35. const loading = ref(false)
  36. const configOptions = ref({
  37. form: {
  38. labelPosition: 'right',
  39. },
  40. submitBtn: {
  41. innerText: '保存配置',
  42. loading: loading
  43. },
  44. resetBtn: true,
  45. onSubmit: async (formData) => {
  46. try {
  47. loading.value = true
  48. await savePluginConfigApi(props.pluginId, formData)
  49. ElMessage.success('配置保存成功')
  50. } finally {
  51. loading.value = false
  52. }
  53. },
  54. beforeFetch: (options) => {
  55. options.headers = {
  56. Authorization: `Bearer ${userStore.token}`
  57. };
  58. }
  59. })
  60. const loadConfig = async () => {
  61. try {
  62. let { data } = await getPluginConfigApi(props.pluginId)
  63. if (!Array.isArray(data)) {
  64. console.warn('Expected configRule to be an array, but got:', typeof data)
  65. data = []
  66. }
  67. if (props.setting) {
  68. const findSetting = (items, target) => {
  69. for (const item of items) {
  70. if (item.field === target) return item
  71. if (item.children) {
  72. const found = findSetting(item.children, target)
  73. if (found) return found
  74. }
  75. }
  76. return null
  77. }
  78. const setting = findSetting(data, props.setting)
  79. configRule.value = setting ? [setting] : []
  80. } else {
  81. configRule.value = data
  82. }
  83. } catch (e) {
  84. console.error('加载配置失败:', e)
  85. ElMessage.error('配置加载失败')
  86. }
  87. }
  88. // 监听参数变化自动加载
  89. watch(() => props.pluginId, loadConfig, { immediate: true })
  90. watch(() => props.setting, loadConfig)
  91. </script>
  92. <style scoped>
  93. .settings-card {
  94. margin-bottom: 20px;
  95. }
  96. </style>