Connect front web app to workbench APIs

This commit is contained in:
王鹏
2026-05-22 16:43:55 +08:00
parent d39eeca8a9
commit 625489e77c
6 changed files with 82 additions and 5 deletions

View File

@@ -27,6 +27,7 @@
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { SwitchButton } from '@element-plus/icons-vue'
import { logout } from '@/api/auth'
import { getToken, getUser, removeToken } from '@/utils/auth'
const router = useRouter()
@@ -41,9 +42,13 @@ const user = computed(() => {
})
const displayName = computed(() => user.value?.nickName || user.value?.userName || '已登录')
function handleLogout() {
removeToken()
router.push('/')
async function handleLogout() {
try {
await logout()
} finally {
removeToken()
router.push('/')
}
}
</script>

View File

@@ -132,6 +132,64 @@ watch(
}
)
watch(
tables,
(nextTables) => {
nextTables.forEach(normalizeTable)
},
{ deep: true, immediate: true }
)
function toPascalCase(value) {
return String(value || '')
.split(/[^A-Za-z0-9]+|_/)
.filter(Boolean)
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
.join('')
}
function toCamelCase(value) {
const pascal = toPascalCase(value)
return pascal ? pascal.charAt(0).toLowerCase() + pascal.slice(1) : ''
}
function javaTypeOf(columnType) {
const normalized = String(columnType || '').toLowerCase()
if (normalized.startsWith('bigint')) return 'Long'
if (normalized.startsWith('int')) return 'Integer'
if (normalized.startsWith('decimal')) return 'BigDecimal'
if (normalized === 'datetime' || normalized === 'date') return 'Date'
return 'String'
}
function normalizeTable(table) {
if (!table) return
table.className = toPascalCase(table.tableName)
table.moduleName = table.moduleName || 'system'
table.businessName = toCamelCase(table.tableName)
table.functionName = table.tableComment || table.tableName
table.tplCategory = table.tplCategory || 'crud'
table.tplWebType = table.tplWebType || 'element-plus'
const columns = Array.isArray(table.columns) ? table.columns : []
columns.forEach((column) => normalizeColumn(column))
}
function normalizeColumn(column) {
if (!column) return
column.javaField = toCamelCase(column.columnName)
column.javaType = javaTypeOf(column.columnType)
column.isPk = column.isPk === '1' ? '1' : '0'
column.isIncrement = column.isIncrement === '1' ? '1' : '0'
column.isRequired = column.isRequired === '1' ? '1' : '0'
column.isInsert = column.isInsert || '1'
column.isEdit = column.isEdit || '1'
column.isList = column.isList || '1'
column.isQuery = column.isQuery || '0'
column.queryType = column.queryType || 'EQ'
column.htmlType = column.htmlType || 'input'
}
function uid(prefix) {
return `${prefix}_${Date.now()}_${Math.random().toString(16).slice(2)}`
}

View File

@@ -146,7 +146,7 @@ async function handleGenerateDatabase() {
}
generating.value = true
errorMessage.value = ''
errorMessage.value = ''
try {
const id = await ensureProject()
const result = await generateDatabase(id, {

View File

@@ -67,6 +67,14 @@ function normalizeNodes(nodes, prefix = '') {
})
}
function pickCodeContent(data) {
if (typeof data === 'string') return data
if (!data || typeof data !== 'object') return ''
if (typeof data.content === 'string') return data.content
const firstValue = Object.values(data).find((value) => typeof value === 'string')
return firstValue || ''
}
async function loadType(type) {
const result = await getProjectStructure(projectId.value, type)
const data = unwrap(result)
@@ -96,7 +104,7 @@ async function handleFileSelect(file) {
category: file.category || file.path || file.label
})
const data = unwrap(result)
code.value = typeof data === 'string' ? data : data.content || ''
code.value = pickCodeContent(data)
} catch (error) {
ElMessage.error(error.message || '加载文件内容失败')
} finally {