丑团git
前端 expo https://gitee.com/honbingitee/ugly-tuan-expo 后端 egg.js https://gitee.com/honbingitee/ugly-tuan-egg
/* eslint-disable indent */
'use strict';
const Controller = require('egg').Controller;
const mongoose = require("mongoose");
class CommentController extends Controller {
/**
* 创建评论
* @param {string} content
* @param {number} type
* @param {ObjectId} sender
*/
async createComment() {
const { ctx } = this;
console.log("body:", ctx.request.body);
const { body } = ctx.request;
const { content, type = 0, sender: senderIdStr = "61793fcd45c2dbfa30e709c1" } = body;
if (!content || !senderIdStr)
ctx.body = { code: -1, message: "检查参数" };
else {
const senderId = mongoose.Types.ObjectId(senderIdStr);
const comment = { content, type, sender: senderId, thumbsUp: 0 };
const sender = await ctx.model.User.find({ _id: senderId });
if (!sender.length) ctx.body = { code: -2, message: "查无此人" };
else {
console.log("sender:", sender);
console.log("comment:", comment);
const item = await ctx.model.Comment.create(comment);
console.log("result:", item);
if (item) {
const payload = { ...comment, sender, _id: item._id };
console.log("payload:", payload);
ctx.body = { code: 0, payload };
} else ctx.body = { code: -3, message: '创建失败' };
}
}
}
async queryComment() {
const { ctx } = this;
let { limit } = ctx.query;
limit = Number(limit);
// const comments = await ctx.model.Comment.find().limit(isNaN(limit) ? 20 : limit);
const comments = await ctx.model.Comment.aggregate([
{
$lookup: {
from: 'user',
localField: 'sender',
foreignField: '_id',
as: 'sender'
}
},
{ $limit: isNaN(limit) ? 20 : limit },
{ $sort: { _id: -1 } }
]);
console.log("comments:", comments);
ctx.body = { comments };
}
async thumbsUp() {
const { ctx } = this;
const { _id } = ctx.request.body;
const item = await ctx.model.Comment.update({ _id }, {
$inc: { thumbsUp: 1 }
});
console.log(item);
if (item.ok) {
ctx.body = { code: 0, message: '点赞成功' }
} else ctx.body = { code: -1, message: '这都能失败' }
}
}
module.exports = CommentController;
module.exports = app => {
const mongoose = app.mongoose;
const Schema = mongoose.Schema;
const CommentSchema = new Schema({
content: { type: String },
type: { type: Number },
sender: { type: mongoose.Types.ObjectId },
thumbsUp: { type: Number }
});
return mongoose.model('Comment', CommentSchema, 'comment');
}
'use strict';
/**
* @param {Egg.Application} app - egg application
*/
module.exports = app => {
const { router, controller } = app;
router.get('/', controller.home.index);
router.post('/createComment', controller.comment.createComment);
router.get('/queryComment', controller.comment.queryComment);
router.post('/thumbsUp', controller.comment.thumbsUp);
};
/* eslint valid-jsdoc: "off" */
'use strict';
const options = {
host: "159.75.22.82",
port: "27017",
db_name: "uglyTuan",
user: "hongbin",
pass: "Hongbin",
};
const url = `mongodb://${options.user}:${options.pass}@${options.host}:${options.port}/${options.db_name}`;
/**
* @param {Egg.EggAppInfo} appInfo app info
*/
module.exports = appInfo => {
/**
* built-in config
* @type {Egg.EggAppConfig}
**/
const config = exports = {};
// use for cookie sign key, should change to your own and keep security
config.keys = appInfo.name + '_1635295023359_9413';
// add your middleware config here
config.middleware = [];
// add your user config here
const userConfig = {
// myAppName: 'egg',
};
config.security = {
csrf: {
enable: false,
},
};
config.cluster = {
https: {
key: '../../Nginx_SSL/2_hongbin.xyz.key',
cert: '../../Nginx_SSL/1_hongbin.xyz_bundle.crt'
}
};
return {
...config,
...userConfig,
mongoose: {
url,
options: {
useUnifiedTopology: true
}
}
};
};
/** @type Egg.EggPlugin */
module.exports = {
// had enabled by egg
// static: {
// enable: true,
// }
mongoose: {
enable: true,
package: 'egg-mongoose',
}
};
package.json
{
"name": "uglyTuan",
"version": "1.0.0",
"description": "",
"private": true,
"egg": {
"declarations": true
},
"dependencies": {
"egg": "^2.15.1",
"egg-mongoose": "^3.3.1",
"egg-scripts": "^2.11.0"
},
"devDependencies": {
"autod": "^3.0.1",
"autod-egg": "^1.1.0",
"egg-bin": "^4.11.0",
"egg-ci": "^1.11.0",
"egg-mock": "^3.21.0",
"eslint": "^5.13.0",
"eslint-config-egg": "^7.1.0"
},
"engines": {
"node": ">=10.0.0"
},
"scripts": {
"start": "egg-scripts start --daemon --title=egg-server-uglyTuan",
"stop": "egg-scripts stop --title=egg-server-uglyTuan",
"dev": "egg-bin dev",
"debug": "egg-bin debug",
"test": "npm run lint -- --fix && npm run test-local",
"test-local": "egg-bin test",
"cov": "egg-bin cov",
"lint": "eslint .",
"ci": "npm run lint && npm run cov",
"autod": "autod"
},
"ci": {
"version": "10"
},
"repository": {
"type": "git",
"url": ""
},
"author": "hongbin",
"license": "MIT"
}