This commit is contained in:
董海洋
2026-05-24 17:48:20 +08:00
parent e41eb624b9
commit e532e48e47
3 changed files with 33 additions and 5 deletions
+22
View File
@@ -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
}
+2 -1
View File
@@ -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: '上传成功',
+9 -4
View File
@@ -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 = ?',