From e532e48e478e2b57a4cedb708dca1e8388aa0ecf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E6=B5=B7=E6=B4=8B?= Date: Sun, 24 May 2026 17:48:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=9F=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/domain.js | 22 ++++++++++++++++++++++ routes/upload.js | 3 ++- scripts/fix-image-urls.js | 13 +++++++++---- 3 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 config/domain.js diff --git a/config/domain.js b/config/domain.js new file mode 100644 index 0000000..14e14cb --- /dev/null +++ b/config/domain.js @@ -0,0 +1,22 @@ +// 服务端域名配置常量 - 备案完成后只需修改这里 +const DOMAIN_CONFIG = { + // 临时域名(备案期间使用) + BASE_URL: 'http://110.42.255.239:3006', + + // 正式域名(备案完成后启用,取消注释并注释上面一行) + // BASE_URL: 'https://donghy.top', + + // API 路径前缀 + API_PATH: '/api', + + // 图片路径前缀 + IMG_PATH: '/img' +} + +// 完整的图片基础地址 +const IMG_BASE_URL = `${DOMAIN_CONFIG.BASE_URL}${DOMAIN_CONFIG.IMG_PATH}` + +module.exports = { + DOMAIN_CONFIG, + IMG_BASE_URL +} diff --git a/routes/upload.js b/routes/upload.js index b81ee66..1d3eedc 100644 --- a/routes/upload.js +++ b/routes/upload.js @@ -2,6 +2,7 @@ const Router = require('koa-router') const multer = require('@koa/multer') const path = require('path') const fs = require('fs') +const { IMG_BASE_URL } = require('../config/domain') const router = new Router() @@ -36,7 +37,7 @@ router.post('/', upload.single('file'), async (ctx) => { return } - const fileUrl = `https://donghy.top/img/${ctx.file.filename}` + const fileUrl = `${IMG_BASE_URL}/${ctx.file.filename}` ctx.body = { code: 200, message: '上传成功', diff --git a/scripts/fix-image-urls.js b/scripts/fix-image-urls.js index a384992..355e59f 100644 --- a/scripts/fix-image-urls.js +++ b/scripts/fix-image-urls.js @@ -1,11 +1,16 @@ const mysql = require('mysql2/promise'); const config = require('../config/database'); +const { DOMAIN_CONFIG } = require('../config/domain'); async function fixImageUrls() { try { const connection = await mysql.createConnection(config); console.log('✅ 数据库连接成功\n'); + // 获取当前配置的域名 + const targetDomain = DOMAIN_CONFIG.BASE_URL; + console.log(`🎯 目标域名: ${targetDomain}\n`); + // 查找包含旧地址的记录 console.log('🔍 检查数据库中的图片URL...'); const [goods] = await connection.execute('SELECT id, images FROM goods WHERE images LIKE "%localhost%"'); @@ -18,8 +23,8 @@ async function fixImageUrls() { const oldImages = item.images; // 替换所有 localhost:3000 和 localhost:3006 let newImages = oldImages - .replace(/http:\/\/localhost:3000/g, 'https://donghy.top') - .replace(/http:\/\/localhost:3006/g, 'https://donghy.top'); + .replace(/http:\/\/localhost:3000/g, targetDomain) + .replace(/http:\/\/localhost:3006/g, targetDomain); await connection.execute( 'UPDATE goods SET images = ? WHERE id = ?', @@ -41,8 +46,8 @@ async function fixImageUrls() { if (item.images) { const oldImages = item.images; let newImages = oldImages - .replace(/http:\/\/localhost:3000/g, 'https://donghy.top') - .replace(/http:\/\/localhost:3006/g, 'https://donghy.top'); + .replace(/http:\/\/localhost:3000/g, targetDomain) + .replace(/http:\/\/localhost:3006/g, targetDomain); await connection.execute( 'UPDATE points_goods SET images = ? WHERE id = ?',