Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
new
- Loading branch information
Joey
committed
Apr 22, 2020
0 parents
commit 73d12b14eb9220015d7d8745d54031c746aba81c
Showing
46 changed files
with
2,756 additions
and
0 deletions.
There are no files selected for viewing
BIN
+8 KB
.DS_Store
Binary file not shown.
BIN
+6 KB
back/.DS_Store
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
node_modules/ | ||
npm-debug.log | ||
.vscode/ | ||
package-lock.json | ||
test.js | ||
mocks/ |
69
back/app.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const Koa = require("koa"); | ||
const static = require("koa-static"); | ||
const koaBody = require("koa-body"); | ||
const cors = require("koa2-cors"); | ||
|
||
const mongoose = require("mongoose"); | ||
const path = require("path"); | ||
const chalk = require("chalk"); | ||
|
||
const registerRouters = require("./controller/index"); | ||
const config = require("./config"); | ||
|
||
const logger = require("./middleware/logger"); | ||
const token = require("./middleware/token"); | ||
|
||
app = new Koa(); | ||
|
||
// CORS | ||
app.use(cors()); | ||
// HTTP请求日志 | ||
app.use(logger()); | ||
// token解析 | ||
app.use(token()); | ||
/** | ||
* mongondb | ||
*/ | ||
mongoose | ||
.connect(config.database.local, { | ||
useUnifiedTopology: true, | ||
useNewUrlParser: true, | ||
useFindAndModify: false | ||
}) | ||
.then( | ||
() => { | ||
console.log("[App] MongoDB connected success."); | ||
}, | ||
err => { | ||
console.log(err); | ||
console.log("[App] MongoDB connected fail."); | ||
} | ||
); | ||
|
||
|
||
// 静态文件 | ||
app.use(static(path.join(__dirname, "./public"))); | ||
// 解析POST请求体 | ||
app.use( | ||
koaBody({ | ||
multipart: true, | ||
formidable: { | ||
maxFileSize: 400 * 1024 * 1024 | ||
} | ||
}) | ||
); | ||
|
||
// 注册路由 | ||
app.use(registerRouters()); | ||
|
||
// 定义404路由 | ||
app.use(ctx => { | ||
ctx.body = "<h1>Hello World.</h1>"; | ||
}); | ||
|
||
// 启动 | ||
app.listen(config.port); | ||
|
||
console.log(); | ||
console.log(`${chalk.cyan("[App]: ")}service started at port: ${config.port}`); | ||
console.log(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module.exports = { | ||
port: 8001, | ||
jwtSecret: "0327", | ||
database: { | ||
local: "mongodb://localhost:27017/mydatabase" | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
const mongoose = require("mongoose"); | ||
const router = require("koa-router")(); | ||
const verify = require("../middleware/verify"); | ||
|
||
const Services = require("../services"); | ||
const CommentModel = require("../model/comment.model"); | ||
|
||
const { Comment } = Services; | ||
|
||
router.get("/api/comment", async (ctx, next) => { | ||
const { request, response } = ctx; | ||
const { | ||
user: { _id } | ||
} = request; | ||
const { goodsId, pageSize = 999, pageNum = 1 } = request.query; | ||
const condition = goodsId ? { goods: goodsId } : { user: _id }; | ||
|
||
const comments = await Comment.findComment(condition, { | ||
pageSize: +pageSize, | ||
pageNum: +pageNum | ||
}); | ||
const count = await Comment.findCommentCount(condition); | ||
|
||
ctx.body = { | ||
code: 1, | ||
data: comments, | ||
pageSize, | ||
pageNum, | ||
count, | ||
message: "success" | ||
}; | ||
}); | ||
|
||
router.put("/api/comment/:id", async (ctx, next) => { | ||
const { request, response, params } = ctx; | ||
const { id } = params; | ||
|
||
const update = request.body; | ||
const resp = await Comment.findAndUpdate({ _id: id }, update); | ||
|
||
if (resp) { | ||
ctx.body = { | ||
code: 1, | ||
data: resp, | ||
message: "success" | ||
}; | ||
} else { | ||
ctx.body = { | ||
code: 0, | ||
data: {}, | ||
message: "fail" | ||
}; | ||
} | ||
}); | ||
|
||
router.delete("/api/comment/:id", async (ctx, next) => { | ||
const { request, response, params } = ctx; | ||
const { id } = params; | ||
const resp = await Comment.deleteComment({ _id: id }); | ||
|
||
if (resp) { | ||
ctx.body = { | ||
code: 1, | ||
data: resp, | ||
message: "success" | ||
}; | ||
} else { | ||
ctx.body = { | ||
code: 0, | ||
data: {}, | ||
message: "fail" | ||
}; | ||
} | ||
}); | ||
|
||
router.post("/api/comment", verify(["admin", "user"]), async (ctx, next) => { | ||
const { request, response } = ctx; | ||
const { | ||
user: { _id } | ||
} = request; | ||
const { goodsId, content, reply } = request.body; | ||
|
||
const newComment = new CommentModel({ | ||
_id: mongoose.Types.ObjectId(), | ||
content, | ||
reply, | ||
user: _id, // link user | ||
goods: goodsId | ||
}); | ||
|
||
const resp = await newComment.save(); | ||
|
||
if (resp) { | ||
ctx.body = { | ||
code: 1, | ||
data: newComment, | ||
message: "success" | ||
}; | ||
} else { | ||
ctx.body = { | ||
code: 0, | ||
data: {}, | ||
message: "Error while saving Goods Detail" | ||
}; | ||
} | ||
}); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
const mongoose = require("mongoose"); | ||
const router = require("koa-router")(); | ||
const verify = require("../middleware/verify"); | ||
|
||
const Services = require("../services"); | ||
const FavoriteModel = require("../model/favorite.model"); | ||
|
||
const { Favorite } = Services; | ||
|
||
router.get("/api/favorite", verify(["admin", "user"]), async (ctx, next) => { | ||
const { request, response } = ctx; | ||
const { | ||
user: { _id } | ||
} = request; | ||
const { pageSize = 10, pageNum = 1 } = request.query; | ||
const condition = { user: _id }; | ||
|
||
const findFavorite = await Favorite.findFavorite(condition, { | ||
pageSize: +pageSize, | ||
pageNum: +pageNum | ||
}); | ||
const count = await Favorite.findFavoriteCount(condition); | ||
|
||
ctx.body = { | ||
code: 1, | ||
data: findFavorite, | ||
pageSize, | ||
pageNum, | ||
count, | ||
message: "success" | ||
}; | ||
}); | ||
|
||
router.delete( | ||
"/api/favorite/:id", | ||
verify(["admin", "user"]), | ||
async (ctx, next) => { | ||
const { request, response, params } = ctx; | ||
const { id } = params; | ||
const resp = await Favorite.deleteFavorite({ _id: id }); | ||
|
||
if (resp) { | ||
ctx.body = { | ||
code: 1, | ||
data: resp, | ||
message: "success" | ||
}; | ||
} else { | ||
ctx.body = { | ||
code: 0, | ||
data: {}, | ||
message: "fail" | ||
}; | ||
} | ||
} | ||
); | ||
|
||
router.put("/api/favorite", verify(["admin", "user"]), async (ctx, next) => { | ||
const { request, response } = ctx; | ||
const { | ||
user: { _id } | ||
} = request; | ||
const { goodsId } = request.body; | ||
const resp = await Favorite.findAndUpdate( | ||
{ user: _id, goods: goodsId }, | ||
request.body | ||
); | ||
|
||
if (resp) { | ||
ctx.body = { | ||
code: 1, | ||
data: resp, | ||
message: "success" | ||
}; | ||
} else { | ||
ctx.body = { | ||
code: 0, | ||
data: {}, | ||
message: "fail" | ||
}; | ||
} | ||
}); | ||
|
||
router.post("/api/favorite", verify(["admin", "user"]), async (ctx, next) => { | ||
const { request, response } = ctx; | ||
const { | ||
user: { _id } | ||
} = request; | ||
const { goodsId } = request.body; | ||
|
||
const hasFavorite = await Favorite.findOne({ | ||
goods: goodsId, // link goods | ||
user: _id // link user | ||
}); | ||
|
||
if (hasFavorite) { | ||
const resp = await Favorite.findAndUpdate( | ||
{ user: _id, goods: goodsId }, | ||
{ isLoved: true } | ||
); | ||
ctx.body = { | ||
code: 1, | ||
data: resp, | ||
message: "success" | ||
}; | ||
return; | ||
} | ||
|
||
const newFavorite = new FavoriteModel({ | ||
_id: mongoose.Types.ObjectId(), | ||
goods: goodsId, // link goods | ||
user: _id // link user | ||
}); | ||
const resp = await newFavorite.save(); | ||
if (resp) { | ||
ctx.body = { | ||
code: 1, | ||
data: newFavorite, | ||
message: "success" | ||
}; | ||
} else { | ||
ctx.body = { | ||
code: 0, | ||
data: {}, | ||
message: "Error while saving Favorite" | ||
}; | ||
} | ||
}); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const router = require("koa-router")(); | ||
|
||
const isArray = Array.isArray; | ||
|
||
function storage(file) { | ||
const reader = fs.createReadStream(file.path); | ||
const writer = fs.createWriteStream( | ||
path.join(__dirname, "../public/upload/", file.name) | ||
); | ||
reader.pipe(writer); | ||
return "/upload/" + file.name; | ||
} | ||
|
||
router.post("/api/file", async (ctx, next) => { | ||
try { | ||
let files = ctx.request.files.file; | ||
let path; | ||
|
||
if (isArray(files)) { | ||
path = []; | ||
for (const file of files) { | ||
path.push(storage(file)); | ||
} | ||
} else { | ||
path = storage(files); | ||
} | ||
|
||
ctx.body = { | ||
code: 1, | ||
data: path, | ||
message: "success" | ||
}; | ||
} catch (err) { | ||
console.error(err); | ||
ctx.body = { | ||
code: 0, | ||
data: {}, | ||
message: `error [${err}]` | ||
}; | ||
} | ||
}); | ||
|
||
module.exports = router; |
Oops, something went wrong.