75 lines
1.8 KiB
JavaScript
75 lines
1.8 KiB
JavaScript
const fs = require('fs')
|
|
const path = require('path')
|
|
|
|
module.exports = [
|
|
{
|
|
url: '/api/save-layout',
|
|
type: 'post',
|
|
response(config) {
|
|
try {
|
|
const layoutData = config.body
|
|
const layoutPath = path.join(process.cwd(), 'public', 'layout.json')
|
|
|
|
// 确保public目录存在
|
|
const publicDir = path.dirname(layoutPath)
|
|
if (!fs.existsSync(publicDir)) {
|
|
fs.mkdirSync(publicDir, { recursive: true })
|
|
}
|
|
|
|
// 写入布局数据到文件
|
|
fs.writeFileSync(layoutPath, JSON.stringify(layoutData, null, 2), 'utf8')
|
|
|
|
return {
|
|
code: 200,
|
|
msg: '布局保存成功',
|
|
data: {
|
|
success: true,
|
|
path: layoutPath
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('保存布局文件失败:', error)
|
|
return {
|
|
code: 500,
|
|
msg: '保存布局失败',
|
|
data: {
|
|
success: false,
|
|
error: error.message
|
|
}
|
|
}
|
|
}
|
|
},
|
|
},
|
|
{
|
|
url: '/public/layout.json',
|
|
type: 'get',
|
|
response(config) {
|
|
try {
|
|
const layoutPath = path.join(process.cwd(), 'public', 'layout.json')
|
|
|
|
if (fs.existsSync(layoutPath)) {
|
|
const layoutData = fs.readFileSync(layoutPath, 'utf8')
|
|
return {
|
|
code: 200,
|
|
msg: '布局文件读取成功',
|
|
data: JSON.parse(layoutData)
|
|
}
|
|
} else {
|
|
return {
|
|
code: 404,
|
|
msg: '布局文件不存在',
|
|
data: null
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('读取布局文件失败:', error)
|
|
return {
|
|
code: 500,
|
|
msg: '读取布局失败',
|
|
data: null
|
|
}
|
|
}
|
|
},
|
|
},
|
|
]
|