guoziyun před 1 měsícem
rodič
revize
5d30abaaf7
1 změnil soubory, kde provedl 54 přidání a 39 odebrání
  1. 54 39
      routes/v2/video-coloring-page.js

+ 54 - 39
routes/v2/video-coloring-page.js

@@ -1,29 +1,37 @@
-var express = require('express');
+var express = require("express");
 var router = express.Router();
-const models = require('../../models');
-const common = require('./common');
-const utils = require('../../libs/utils');
-const redis = require('../../libs/redis');
-const config = require('../../config/app');
+const models = require("../../models");
+const common = require("./common");
+const utils = require("../../libs/utils");
+const redis = require("../../libs/redis");
+const config = require("../../config/app");
 
 const CACHE_PREFIX = "art_v2";
 // const CACHE_EXPIRES = 60; // 60s刷新一次
 const CACHE_EXPIRES = 600;
 
-const artSelect = 'name title desc seoTitle seoDescription width height date publishTime tags lastMod mystery hasSpecial useSpecialThumb publishVersion totalStartCount totalDoneCount completionRate';
+const artSelect =
+  "name title desc seoTitle seoDescription width height date publishTime tags lastMod mystery hasSpecial useSpecialThumb publishVersion totalStartCount totalDoneCount completionRate";
 
 // 获得所有video coloring pages
-router.get('/', (req, res, next) => {
+router.get("/", (req, res, next) => {
   (async function () {
     let uri = req.originalUrl.substring(1);
     let cacheKey = `${CACHE_PREFIX}_video-coloring-pages`;
     let htmlData = await redis.getAsync(cacheKey);
     htmlData = null;
     if (!htmlData) {
-      let videos = await models.ArtVideoStory
-        .find({ enabled: true, seoTitle: { $exists: true } })
-        .populate({ path: 'contents', model: 'Art', select: 'name title width height date publishTime tags lastMod publishVersion' })
-        .sort({ order: 'asc' })
+      let videos = await models.ArtVideoStory.find({
+        enabled: true,
+        seoTitle: { $exists: true },
+      })
+        .populate({
+          path: "contents",
+          model: "Art",
+          select:
+            "name title width height date publishTime tags lastMod publishVersion",
+        })
+        .sort({ order: "asc" })
         .lean()
         .exec();
 
@@ -34,7 +42,8 @@ router.get('/', (req, res, next) => {
         if (doc.seoTitle) {
           try {
             let titleJson = JSON.parse(doc.seoTitle);
-            doc.seoTitle = titleJson && titleJson['en'] ? titleJson['en'] : doc.name;
+            doc.seoTitle =
+              titleJson && titleJson["en"] ? titleJson["en"] : doc.name;
           } catch (e) {
             console.error(e.message);
           }
@@ -44,7 +53,8 @@ router.get('/', (req, res, next) => {
         if (doc.seoDescription) {
           try {
             let descJson = JSON.parse(doc.seoDescription);
-            doc.seoDescription = descJson && descJson['en'] ? descJson['en'] : doc.seoTitle;
+            doc.seoDescription =
+              descJson && descJson["en"] ? descJson["en"] : doc.seoTitle;
           } catch (e) {
             console.error(e.message);
           }
@@ -54,10 +64,8 @@ router.get('/', (req, res, next) => {
 
         common.organizeData(doc.contents);
 
-        delete doc.seoDescription;
-
         let jsonStr = JSON.stringify(doc, (key, value) => {
-          if (key == 'seoTitle' || key == 'seoDescription') {
+          if (key == "seoTitle" || key == "seoDescription") {
             value = value.replace(/"/g, '\\\"');
           }
           return value;
@@ -65,8 +73,11 @@ router.get('/', (req, res, next) => {
         doc.jsonStr = jsonStr;
       }
 
-      pageId = 'Video Coloring Pages';
-      const comments = await models.Comment.find({ approved: true, page: pageId }).sort({ createdAt: -1 });
+      pageId = "Video Coloring Pages";
+      const comments = await models.Comment.find({
+        approved: true,
+        page: pageId,
+      }).sort({ createdAt: -1 });
       let data = {
         uri: req.originalUrl,
         data: videos,
@@ -75,7 +86,7 @@ router.get('/', (req, res, next) => {
         dateFormat: common.dateFormat,
       };
       // 渲染EJS模板到内存中
-      res.render('v2/video-coloring-pages', data, async (err, html) => {
+      res.render("v2/video-coloring-pages", data, async (err, html) => {
         if (err) {
           // 如果渲染出错,调用next()传递错误
           return next(err);
@@ -84,7 +95,7 @@ router.get('/', (req, res, next) => {
         // 渲染成功,存redis, 发送数据到客户端
         htmlData = html;
         try {
-          await redis.set(cacheKey, htmlData, 'EX', CACHE_EXPIRES);
+          await redis.set(cacheKey, htmlData, "EX", CACHE_EXPIRES);
         } catch (e) {
           console.error(e);
         }
@@ -93,17 +104,14 @@ router.get('/', (req, res, next) => {
       });
     } else {
       // 缓存命中, 直接发送缓存数据
-      res.set({ 'X-From-Cache': 'true' });
+      res.set({ "X-From-Cache": "true" });
       res.send(htmlData);
     }
-
   })().catch(next);
-
 });
 
-
 // video coloring page详情页路由
-router.get('/:id', function (req, res, next) {
+router.get("/:id", function (req, res, next) {
   (async function () {
     let id = req.params.id;
     utils.validators.validateId(id);
@@ -113,13 +121,17 @@ router.get('/:id', function (req, res, next) {
     htmlData = null;
     if (!htmlData) {
       // 专辑
-      let doc = await models.ArtVideoStory
-        .findById(id)
-        .populate({ path: 'contents', model: 'Art', select: artSelect, populate: { path: 'user', select: 'username name' } })
+      let doc = await models.ArtVideoStory.findById(id)
+        .populate({
+          path: "contents",
+          model: "Art",
+          select: artSelect,
+          populate: { path: "user", select: "username name" },
+        })
         .lean()
         .exec();
 
-      if (!doc) throw createError(404, 'Page Not Found!');
+      if (!doc) throw createError(404, "Page Not Found!");
 
       let host = config.cdnHost ?? config.resHost;
 
@@ -129,7 +141,8 @@ router.get('/:id', function (req, res, next) {
       if (doc.seoTitle) {
         try {
           let titleJson = JSON.parse(doc.seoTitle);
-          doc.seoTitle = titleJson && titleJson['en'] ? titleJson['en'] : doc.name;
+          doc.seoTitle =
+            titleJson && titleJson["en"] ? titleJson["en"] : doc.name;
         } catch (e) {
           console.error(e.message);
         }
@@ -139,7 +152,8 @@ router.get('/:id', function (req, res, next) {
       if (doc.seoDescription) {
         try {
           let descJson = JSON.parse(doc.seoDescription);
-          doc.seoDescription = descJson && descJson['en'] ? descJson['en'] : doc.seoTitle;
+          doc.seoDescription =
+            descJson && descJson["en"] ? descJson["en"] : doc.seoTitle;
         } catch (e) {
           console.error(e.message);
         }
@@ -149,7 +163,10 @@ router.get('/:id', function (req, res, next) {
 
       common.organizeData(doc.contents);
 
-      const comments = await models.Comment.find({ approved: true, page: id }).sort({ createdAt: -1 });
+      const comments = await models.Comment.find({
+        approved: true,
+        page: id,
+      }).sort({ createdAt: -1 });
 
       // deeplink 相关
       let applink = `https://art.pcoloring.com${req.originalUrl}`;
@@ -166,7 +183,7 @@ router.get('/:id', function (req, res, next) {
         downlink,
       };
       // 渲染EJS模板到内存中
-      res.render('v2/video-coloring-page', data, async (err, html) => {
+      res.render("v2/video-coloring-page", data, async (err, html) => {
         if (err) {
           // 如果渲染出错,调用next()传递错误
           return next(err);
@@ -175,7 +192,7 @@ router.get('/:id', function (req, res, next) {
         // 渲染成功,存redis, 发送数据到客户端
         htmlData = html;
         try {
-          await redis.set(cacheKey, htmlData, 'EX', CACHE_EXPIRES);
+          await redis.set(cacheKey, htmlData, "EX", CACHE_EXPIRES);
         } catch (e) {
           console.error(e);
         }
@@ -184,12 +201,10 @@ router.get('/:id', function (req, res, next) {
       });
     } else {
       // 缓存命中, 直接发送缓存数据
-      res.set({ 'X-From-Cache': 'true' });
+      res.set({ "X-From-Cache": "true" });
       res.send(htmlData);
     }
-
   })().catch(next);
-
 });
 
-module.exports = router;
+module.exports = router;