Sửa lỗi hiển thị thumbnail của facebook
This commit is contained in:
@@ -23,6 +23,7 @@ const assetRoutes = require('./assetRoutes');
|
|||||||
// Ở đây tôi gắn các route còn lại trực tiếp để không làm gián đoạn hệ thống
|
// Ở đây tôi gắn các route còn lại trực tiếp để không làm gián đoạn hệ thống
|
||||||
router.use('/admin', adminRoutes);
|
router.use('/admin', adminRoutes);
|
||||||
router.use('/auth', authRoutes); // Tích hợp API Đăng ký/Đăng nhập
|
router.use('/auth', authRoutes); // Tích hợp API Đăng ký/Đăng nhập
|
||||||
|
router.get('/share/:id', sceneRoutes.shareScene); // Route hỗ trợ Open Graph
|
||||||
router.use('/tours', tourRoutes); // Thêm các route cho Tour
|
router.use('/tours', tourRoutes); // Thêm các route cho Tour
|
||||||
router.use('/scenes', sceneRoutes);
|
router.use('/scenes', sceneRoutes);
|
||||||
router.use('/users', userRoutes);
|
router.use('/users', userRoutes);
|
||||||
|
|||||||
@@ -168,9 +168,11 @@ router.get('/', optionalAuth, async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// @route GET /api/share/:id
|
/**
|
||||||
// @desc Trang trung gian hỗ trợ Open Graph (Facebook, Zalo,...)
|
* @route GET /api/share/:id
|
||||||
router.get('/share/:id', async (req, res) => {
|
* @desc Trang trung gian hỗ trợ Open Graph (Facebook, Zalo,...)
|
||||||
|
*/
|
||||||
|
const shareScene = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const scene = await Scene.findById(req.params.id).populate('tourId');
|
const scene = await Scene.findById(req.params.id).populate('tourId');
|
||||||
if (!scene) return res.status(404).send('Không tìm thấy cảnh 3D');
|
if (!scene) return res.status(404).send('Không tìm thấy cảnh 3D');
|
||||||
@@ -179,19 +181,21 @@ router.get('/share/:id', async (req, res) => {
|
|||||||
const title = tour ? tour.name : (scene.name || 'Virtual Tour 3D');
|
const title = tour ? tour.name : (scene.name || 'Virtual Tour 3D');
|
||||||
const description = tour ? tour.description : (scene.description || 'Khám phá không gian 360 độ chân thực');
|
const description = tour ? tour.description : (scene.description || 'Khám phá không gian 360 độ chân thực');
|
||||||
|
|
||||||
// Xác định token (ưu tiên query token, sau đó là token của tour/scene nếu có)
|
// Lấy token chia sẻ (nếu có)
|
||||||
const token = req.query.token || scene.shareToken || (tour && tour.shareToken) || '';
|
const token = req.query.token || scene.shareToken || (tour && tour.shareToken) || '';
|
||||||
|
|
||||||
// Xử lý Protocol/Host để tạo URL tuyệt đối
|
// Xác định host của hệ thống
|
||||||
const protocol = req.headers['x-forwarded-proto'] || req.protocol;
|
const protocol = req.headers['x-forwarded-proto'] || req.protocol;
|
||||||
const host = req.get('host');
|
const host = process.env.SYSTEM_HOST || `${protocol}://${req.get('host')}`;
|
||||||
const baseUrl = `${protocol}://${host}`;
|
|
||||||
|
|
||||||
// URL ảnh thumbnail gọi sang Asset API với cờ watermark
|
// URL ảnh thumbnail gọi sang Asset API với cờ watermark (đã được xử lý trong assetRoutes.js)
|
||||||
const imageUrl = `${baseUrl}/api/assets/view/${scene.assetId}?watermark=true${token ? '&token=' + token : ''}`;
|
const imageUrl = `${host}/api/assets/view/${scene.assetId}?watermark=true${token ? '&token=' + token : ''}`;
|
||||||
|
|
||||||
// URL thực tế của ứng dụng để redirect người dùng
|
// URL thực tế của ứng dụng để redirect người dùng
|
||||||
const appUrl = `${baseUrl}/?sceneId=${scene._id}${token ? '&token=' + token : ''}`;
|
const appUrl = `${host}/?sceneId=${scene._id}${token ? '&token=' + token : ''}`;
|
||||||
|
|
||||||
|
// URL Canonical của chính trang chia sẻ này (Dùng cho og:url)
|
||||||
|
const shareUrl = `${host}/api/share/${scene._id}${token ? '?token=' + token : ''}`;
|
||||||
|
|
||||||
res.send(`
|
res.send(`
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
@@ -200,13 +204,17 @@ router.get('/share/:id', async (req, res) => {
|
|||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>${title}</title>
|
<title>${title}</title>
|
||||||
|
<link rel="canonical" href="${appUrl}" />
|
||||||
|
|
||||||
<!-- Open Graph / Facebook -->
|
<!-- Open Graph / Facebook -->
|
||||||
|
<meta property="og:site_name" content="3D Tours - Virtual Tour 360">
|
||||||
<meta property="og:type" content="website">
|
<meta property="og:type" content="website">
|
||||||
<meta property="og:url" content="${appUrl}">
|
<meta property="og:url" content="${shareUrl}">
|
||||||
<meta property="og:title" content="${title}">
|
<meta property="og:title" content="${title}">
|
||||||
<meta property="og:description" content="${description}">
|
<meta property="og:description" content="${description}">
|
||||||
<meta property="og:image" content="${imageUrl}">
|
<meta property="og:image" content="${imageUrl}">
|
||||||
|
<meta property="og:image:secure_url" content="${imageUrl}">
|
||||||
|
<meta property="og:image:type" content="image/jpeg">
|
||||||
<meta property="og:image:width" content="1200">
|
<meta property="og:image:width" content="1200">
|
||||||
<meta property="og:image:height" content="630">
|
<meta property="og:image:height" content="630">
|
||||||
|
|
||||||
@@ -230,7 +238,10 @@ router.get('/share/:id', async (req, res) => {
|
|||||||
console.error("[Share Error]", error);
|
console.error("[Share Error]", error);
|
||||||
res.status(500).send('Lỗi máy chủ');
|
res.status(500).send('Lỗi máy chủ');
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
|
||||||
|
// Đăng ký route phụ trợ trong router này
|
||||||
|
router.get('/share/:id', shareScene);
|
||||||
|
|
||||||
// @route GET /api/scenes/:id
|
// @route GET /api/scenes/:id
|
||||||
router.get('/:id', optionalAuth, async (req, res) => {
|
router.get('/:id', optionalAuth, async (req, res) => {
|
||||||
@@ -458,4 +469,5 @@ router.delete('/:id', protect, async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
router.shareScene = shareScene; // Xuất hàm để apiRoutes sử dụng
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
Reference in New Issue
Block a user