Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by max on 2017/6/5.
- */
- const Koa = require('koa');
- const Router = require('koa-router');
- const bodyParser = require('koa-bodyparser');
- const uuidV4 = require('uuid/v4');
- const mongodb = require('mongodb').MongoClient;
- const app = new Koa();
- const router = Router();
- let db = null;
- app.use(bodyParser({
- 'formLimit': '50mb',
- 'jsonLimit': '50mb',
- 'textLimit': '50mb'
- }));
- app.use(async (ctx, next) => {
- if (db == null) {
- db = await mongodb.connect('mongodb://localhost:27017/Library', {
- autoReconnect: true,
- poolSize: 10,
- });
- }
- ctx.db = db;
- await next();
- });
- // 設定根路徑的處理函數
- router.get('/', async function(ctx) {
- ctx.body = 'Hello World';
- });
- // login
- router.use('/api/login', async function(ctx, next) {
- ctx.set('Access-Control-Allow-Origin', '*');
- ctx.set('Access-Control-Allow-Headers', 'Content-Type');
- await next();
- });
- router.options('/api/login', async function(ctx) {
- ctx.body = { code: 200, msg: '登入成功' };
- });
- router.post('/api/login', async function(ctx) {
- const username = await ctx.request.body.username;
- const password = await ctx.request.body.password;
- var user = {
- id: 1,
- username: 'admin',
- password: '123456',
- avatar: 'https://raw.githubusercontent.com/taylorchen709/markdown-images/master/vueadmin/user.png',
- name: '张某某'
- };
- user = JSON.parse(JSON.stringify(user));
- console.log(`username: ${username}, password: ${password}`);
- ctx.body = { code: 200, msg: '登入成功', user };
- });
- // martial classic classification
- router.use('/api/martial/classic/classifications', async function(ctx, next) {
- ctx.set('Access-Control-Allow-Origin', '*');
- ctx.set('Access-Control-Allow-Headers', 'Content-Type');
- await next()
- });
- router.options('/api/martial/classic/classifications', async function(ctx) {
- ctx.body = { code: 200, msg: '資料更新成功' };
- });
- router.post('/api/martial/classic/classifications', async function(ctx) {
- const firstTitle = await ctx.request.body.firstTitle;
- const secondTitle = await ctx.request.body.secondTitle;
- const imageGroup = await ctx.request.body.imageGroup;
- const collection = ctx.db.collection('Martial');
- let ts = new Date().getTime();
- try {
- let data = await collection.updateOne(
- {
- pageName: "classic"
- },
- {
- $set: {
- pageName: 'classic',
- 'pageData.firstTitle': firstTitle,
- 'pageData.secondTitle': secondTitle,
- 'pageData.imageGroup': imageGroup,
- 'pageData.lastTime': ts
- }
- },
- {
- upsert: true
- }
- );
- console.log('Classifications successfully update');
- } catch (e) {
- console.log(e);
- console.log('Classifications failed to update');
- }
- ctx.body = { code: 200, msg: '資料更新成功' };
- });
- router.get('/api/martial/classic/classifications', async function (ctx) {
- const data = await getClassic(ctx.db);
- ctx.body = { code: 200, msg: '資料讀取成功', data: data };
- });
- async function getClassic (db) {
- const collection = db.collection('Martial');
- try {
- let data = await collection.find({ "pageName": "classic" }).toArray();
- console.log('Classifications successfully respond');
- return Promise.resolve(data);
- } catch (err) {
- console.log('Classifications failed to respond');
- return Promise.reject(err);
- }
- }
- // get classification name
- router.use('/api/classifications/name', async function(ctx, next) {
- ctx.set('Access-Control-Allow-Origin', '*');
- ctx.set('Access-Control-Allow-Headers', 'Content-Type');
- await next()
- });
- router.get('/api/classifications/name', async function (ctx) {
- let data = [];
- let classicObject = await getClassificationsName( ctx.db, 'classic' );
- let classicResult = await formatClassificationsObject( classicObject );
- if ( classicResult != null ) data.push(classicResult);
- ctx.body = { code: 200, msg: '資料讀取成功', data: data };
- });
- async function getClassificationsName ( db, pageName ) {
- const collection = db.collection('Martial');
- try {
- let data = await collection.find({ "pageName": pageName }, { "pageData.imageGroup.src": 0 }).toArray();
- console.log('Classifications name successfully respond');
- return Promise.resolve(data);
- } catch (err) {
- console.log('Classifications name failed to respond');
- return Promise.reject(err);
- }
- }
- async function formatClassificationsObject ( object ) {
- let result;
- let classificationsList = [];
- try {
- let pageName = object[0].pageName;
- object[0].pageData.imageGroup.forEach(function (item) {
- classificationsList.push({
- index: item.index,
- classification: item.firstTitle
- });
- });
- result = {
- 'pageName': pageName,
- 'classifications': classificationsList
- };
- return Promise.resolve(result);
- } catch (err) {
- return null;
- }
- }
- // martial classic books
- router.use('/api/martial/classic/books', async function(ctx, next) {
- ctx.set('Access-Control-Allow-Origin', '*');
- ctx.set('Access-Control-Allow-Headers', 'Content-Type');
- await next()
- });
- router.options('/api/martial/classic/books', async function(ctx) {
- ctx.body = { code: 200, msg: '書本新增成功' };
- });
- router.post('/api/martial/classic/books', async function(ctx) {
- const pageName = await ctx.request.body.pageName;
- const classificationIndex = await ctx.request.body.classificationIndex;
- const booksList = await ctx.request.body.imageGroup;
- const collection = ctx.db.collection('Martial');
- try {
- let data = await collection.update(
- {
- pageName: pageName
- },
- {
- $set: {
- [`pageData.imageGroup.${classificationIndex}.booksList`]: booksList
- }
- },
- {
- upsert: true
- }
- );
- console.log('BookList successfully update');
- } catch (e) {
- console.log(e);
- console.log('BookList failed to update');
- }
- ctx.body = { code: 200, msg: '文章新增成功' };
- });
- router.use('/api/martial/classic/books', async function(ctx, next) {
- ctx.set('Access-Control-Allow-Origin', '*');
- ctx.set('Access-Control-Allow-Headers', 'Content-Type');
- await next()
- });
- router.get('/api/martial/classic/books', async function (ctx) {
- const classificationIndex = ctx.request.query.classificationIndex;
- let booksObject = await getClassificationsBooks( ctx.db, classificationIndex );
- let booksList = await formatClassificationBooksObject( classificationIndex, booksObject );
- ctx.body = { code: 200, msg: '資料讀取成功', data: booksList };
- });
- async function getClassificationsBooks ( db, index ) {
- const collection = db.collection('Martial');
- try {
- let data = await collection.aggregate([
- { "$match" : { "pageName" : "classic" } },
- { "$unwind" : "$pageData.imageGroup" },
- { "$match" : { "pageData.imageGroup.index" : parseInt(index) } },
- { "$project" : { "pageData.imageGroup" : 1 } }
- ]).toArray();
- console.log('Classifications books successfully respond');
- return Promise.resolve(data);
- } catch (err) {
- console.log('Classifications books failed to respond');
- return Promise.reject(err);
- }
- }
- async function formatClassificationBooksObject ( index, object ) {
- try {
- let result = {
- 'classificationIndex': parseInt(index),
- 'booksList': object[0].pageData.imageGroup.booksList
- };
- return Promise.resolve(result);
- } catch (err) {
- return null;
- }
- }
- app.use(router.routes());
- app.listen(3001, function() {
- console.log('Listening on port 3001');
- });
Add Comment
Please, Sign In to add comment