lrf402788946 4 tahun lalu
induk
melakukan
518f1b037f

+ 51 - 0
app/controller/.reply.js

@@ -0,0 +1,51 @@
+module.exports = {
+  create: {
+    requestBody: [
+      "name",
+      "icon",
+      "openid",
+      "content",
+      "reply_time",
+      "article_id",
+    ],
+  },
+  destroy: {
+    params: ["!id"],
+    service: "delete",
+  },
+  update: {
+    params: ["!id"],
+    requestBody: [
+      "name",
+      "icon",
+      "openid",
+      "content",
+      "reply_time",
+      "article_id",
+    ],
+  },
+  show: {
+    parameters: {
+      params: ["!id"],
+    },
+    service: "fetch",
+  },
+  index: {
+    parameters: {
+      query: {
+        "create_time@start": "create_time@start",
+        "create_time@end": "create_time@end",
+      },
+      // options: {
+      //   "meta.state": 0 // 默认条件
+      // },
+    },
+    service: "query",
+    options: {
+      query: ["skip", "limit"],
+      sort: ["meta.createdAt"],
+      desc: true,
+      count: true,
+    },
+  },
+};

+ 40 - 0
app/controller/.thumbs.js

@@ -0,0 +1,40 @@
+module.exports = {
+  create: {
+    requestBody: ["openid", "type", "article_id"],
+  },
+  destroy: {
+    params: ["!id"],
+    service: "delete",
+  },
+  update: {
+    params: ["!id"],
+    requestBody: ["openid", "type", "article_id"],
+  },
+  show: {
+    parameters: {
+      params: ["!id"],
+    },
+    service: "fetch",
+  },
+  index: {
+    parameters: {
+      query: {
+        openid: "openid",
+        type: "type",
+        article_id: "article_id",
+        "create_time@start": "create_time@start",
+        "create_time@end": "create_time@end",
+      },
+      // options: {
+      //   "meta.state": 0 // 默认条件
+      // },
+    },
+    service: "query",
+    options: {
+      query: ["skip", "limit"],
+      sort: ["meta.createdAt"],
+      desc: true,
+      count: true,
+    },
+  },
+};

+ 13 - 0
app/controller/reply.js

@@ -0,0 +1,13 @@
+'use strict';
+const meta = require('./.reply.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose/lib/controller');
+
+// 回复
+class ReplyController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.reply;
+  }
+}
+module.exports = CrudController(ReplyController, meta);

+ 13 - 0
app/controller/thumbs.js

@@ -0,0 +1,13 @@
+'use strict';
+const meta = require('./.thumbs.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose/lib/controller');
+
+// 点赞
+class ThumbsController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.thumbs;
+  }
+}
+module.exports = CrudController(ThumbsController, meta);

+ 24 - 0
app/middleware/fetchThumbs.js

@@ -0,0 +1,24 @@
+'use strict';
+const _ = require('lodash');
+const querystring = require('querystring');
+const { ObjectId } = require('mongoose').Types;
+module.exports = options => {
+  return async function fetchThumbs(ctx, next) {
+    const url = ctx.request.url.split('?')[1];
+    const query = JSON.parse(JSON.stringify(querystring.parse(url)));
+    const { openid } = query;
+    await next();
+    if (openid) {
+      let data = _.get(ctx.response, 'body.data');
+      if (data) {
+        data = JSON.parse(JSON.stringify(data));
+        const res = await ctx.service.thumbs.getSum({ ids: [ data._id ], openid });
+        const r = res.find(f => ObjectId(f._id).equals(data._id));
+        if (r && r.sum > 0) data.thumbs = true;
+        else data.thumbs = false;
+        ctx.response.body.data = data;
+      }
+    }
+
+  };
+};

+ 7 - 3
app/middleware/read.js

@@ -3,10 +3,14 @@ const _ = require('lodash');
 const { ObjectId } = require('mongoose').Types;
 module.exports = options => {
   return async function read(ctx, next) {
-    // 获取url
-    const uri = ctx.request.url;
+    // 将uri转换成url使用url模块处理
+    let url = ctx.request.url;
+    if (!url.includes('http')) {
+      url = `http://localhost${url}`;
+    }
+    const obj = new URL(url);
     // 以 / 将字符串分段成为数组
-    const arr = uri.split('/');
+    const arr = obj.pathname.split('/');
     // fetch的格式是最后的'/'后的内容为ObjectId
     const tail = _.last(arr);
     // 使用mongoose的 ObjectId类 去校验是否为ObjectId,不是ObjectId的情况,则不能直接使用plus,plus中使用的是findById

+ 28 - 0
app/middleware/thumbs.js

@@ -0,0 +1,28 @@
+'use strict';
+const _ = require('lodash');
+const querystring = require('querystring');
+const { ObjectId } = require('mongoose').Types;
+module.exports = options => {
+  return async function thumbs(ctx, next) {
+    const url = ctx.request.url.split('?')[1];
+    const query = JSON.parse(JSON.stringify(querystring.parse(url)));
+    const { openid } = query;
+    await next();
+    let data = _.get(ctx.response, 'body.data');
+    if (_.isArray(data) && data.length > 0) {
+      data = JSON.parse(JSON.stringify(data));
+      // 整合数据
+      const ids = data.map(i => i._id);
+      const res = await ctx.service.thumbs.getSum({ ids, openid });
+      data = data.map(i => {
+        const r = res.find(f => ObjectId(f._id).equals(i._id));
+        if (r) i.thumbs = r.sum;
+        else i.thumbs = 0;
+        return i;
+      });
+      // 更新返回数据
+      ctx.response.body.data = data;
+    }
+
+  };
+};

+ 28 - 0
app/model/reply.js

@@ -0,0 +1,28 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const moment = require('moment');
+const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
+const { ObjectId } = require('mongoose').Types;
+// 回复表
+const reply = {
+  name: { type: String },
+  icon: { type: String },
+  openid: { type: String },
+  content: { type: String },
+  reply_time: { type: String },
+  article_id: { type: ObjectId },
+  remark: { type: String, maxLength: 200 },
+  create_time: { type: String, default: moment(new Date()).format('YYYY-MM-DD HH:mm:ss') },
+};
+const schema = new Schema(reply, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.index({ name: 1 });
+schema.index({ openid: 1 });
+schema.index({ reply_time: 1 });
+schema.index({ article_id: 1 });
+schema.index({ 'meta.createdAt': 1 });
+schema.plugin(metaPlugin);
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('Reply', schema, 'reply');
+};

+ 24 - 0
app/model/thumbs.js

@@ -0,0 +1,24 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const moment = require('moment');
+const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
+const { ObjectId } = require('mongoose').Types;
+// 点赞表
+const thumbs = {
+  openid: { type: String }, // openid
+  type: { type: String }, // 0=>文字/辟谣;1=>话题;2=>服务
+  article_id: { type: ObjectId }, // 数据id
+  remark: { type: String, maxLength: 200 },
+  create_time: { type: String, default: moment(new Date()).format('YYYY-MM-DD HH:mm:ss') },
+};
+const schema = new Schema(thumbs, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.index({ openid: 1 });
+schema.index({ type: 1 });
+schema.index({ article_id: 1 });
+schema.index({ 'meta.createdAt': 1 });
+schema.plugin(metaPlugin);
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('Thumbs', schema, 'thumbs');
+};

+ 2 - 0
app/router.js

@@ -16,4 +16,6 @@ module.exports = app => {
   require('./router/refute')(app); // 文章/辟谣
   require('./router/topic')(app); // 社区话题
   require('./router/service')(app); // 咨询服务
+  require('./router/thumbs')(app); // 点赞
+  require('./router/reply')(app); // 回复
 };

+ 5 - 1
app/router/refute.js

@@ -6,7 +6,11 @@ module.exports = app => {
   const profix = '/api/article/';
   const target = 'refute';
   const readMiddleware = app.middleware.read(target);
-  router.get(target, `${profix}${target}/:id`, readMiddleware, controller[target].show);
+  const queryMiddleware = app.middleware.thumbs();
+  const fetchThumbs = app.middleware.fetchThumbs();
+
+  router.get(target, `${profix}${target}/:id`, readMiddleware, fetchThumbs, controller[target].show);
+  router.get(target, `${profix}${target}`, queryMiddleware, controller[target].index);
   router.resources(target, `${profix}${target}`, controller[target]); // index、create、show、destroy
   router.post(target, `${profix}${target}/update/:id`, controller[target].update);
 };

+ 11 - 0
app/router/reply.js

@@ -0,0 +1,11 @@
+'use strict';
+
+
+module.exports = app => {
+  const { router, controller } = app;
+  const profix = '/api/article/';
+  const target = 'reply';
+
+  router.resources(target, `${profix}${target}`, controller[target]); // index、create、show、destroy
+  router.post(target, `${profix}${target}/update/:id`, controller[target].update);
+};

+ 4 - 1
app/router/service.js

@@ -6,7 +6,10 @@ module.exports = app => {
   const profix = '/api/article/';
   const target = 'service';
   const readMiddleware = app.middleware.read(target);
-  router.get(target, `${profix}${target}/:id`, readMiddleware, controller[target].show);
+  const queryMiddleware = app.middleware.thumbs();
+  const fetchThumbs = app.middleware.fetchThumbs();
+  router.get(target, `${profix}${target}/:id`, readMiddleware, fetchThumbs, controller[target].show);
+  router.get(target, `${profix}${target}`, queryMiddleware, controller[target].index);
   router.resources(target, `${profix}${target}`, controller[target]); // index、create、show、destroy
   router.post(target, `${profix}${target}/update/:id`, controller[target].update);
 };

+ 10 - 0
app/router/thumbs.js

@@ -0,0 +1,10 @@
+'use strict';
+
+
+module.exports = app => {
+  const { router, controller } = app;
+  const profix = '/api/article/';
+  const target = 'thumbs';
+  router.resources(target, `${profix}${target}`, controller[target]); // index、create、show、destroy
+  router.post(target, `${profix}${target}/update/:id`, controller[target].update);
+};

+ 4 - 1
app/router/topic.js

@@ -6,7 +6,10 @@ module.exports = app => {
   const profix = '/api/article/';
   const target = 'topic';
   const readMiddleware = app.middleware.read(target);
-  router.get(target, `${profix}${target}/:id`, readMiddleware, controller[target].show);
+  const queryMiddleware = app.middleware.thumbs();
+  const fetchThumbs = app.middleware.fetchThumbs();
+  router.get(target, `${profix}${target}/:id`, readMiddleware, fetchThumbs, controller[target].show);
+  router.get(target, `${profix}${target}`, queryMiddleware, controller[target].index);
   router.resources(target, `${profix}${target}`, controller[target]); // index、create、show、destroy
   router.post(target, `${profix}${target}/update/:id`, controller[target].update);
 };

+ 15 - 0
app/service/reply.js

@@ -0,0 +1,15 @@
+'use strict';
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+const _ = require('lodash');
+const assert = require('assert');
+
+// 回复
+class ReplyService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'reply');
+    this.model = this.ctx.model.Reply;
+  }
+}
+
+module.exports = ReplyService;

+ 42 - 0
app/service/thumbs.js

@@ -0,0 +1,42 @@
+'use strict';
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+const _ = require('lodash');
+const assert = require('assert');
+const { ObjectId } = require('mongoose').Types;
+
+// 点赞
+class ThumbsService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'thumbs');
+    this.model = this.ctx.model.Thumbs;
+  }
+  async create(body) {
+    // 有就取消点赞,没有就点赞
+    const { openid, article_id } = body;
+    const count = await this.model.count({ openid, article_id });
+    let res = false;
+    if (count > 0) await this.model.deleteOne({ openid, article_id });
+    else { await this.model.create(body); res = true; }
+    return res;
+  }
+
+  /**
+   * 查询某人对某些数据是否点赞
+   * @param {Object} { ids, openid }
+   */
+  async getSum({ ids, openid }) {
+    const match = { article_id: { $in: ids.map(i => ObjectId(i)) } };
+    if (openid) match.openid = openid;
+    const res = await this.model.aggregate([
+      { $match: match },
+      { $group: {
+        _id: '$article_id',
+        sum: { $sum: 1 },
+      } },
+    ]);
+    return res;
+  }
+}
+
+module.exports = ThumbsService;