-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
72 changed files
with
7,120 additions
and
10,469 deletions.
There are no files selected for viewing
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,34 @@ | ||
'use strict'; | ||
const egg = require('egg'); | ||
module.exports = class AdminController extends egg.Controller { | ||
async login(ctx) { | ||
await ctx.renderClient('admin/login/login.js', {}); | ||
} | ||
async home(ctx) { | ||
const url = ctx.url.replace(/\/admin/, ''); | ||
const { mode } = ctx.query; | ||
if (mode === 'csr') { | ||
await ctx.renderClient('admin.js', { ctx, url, title: 'easy-admin' }); | ||
} else { | ||
await ctx.render('admin.js', { ctx, url, title: 'easy-admin' }); | ||
} | ||
} | ||
|
||
async list(ctx) { | ||
this.ctx.body = ctx.service.article.getArtilceList(ctx.request.body); | ||
} | ||
|
||
async add(ctx) { | ||
ctx.body = this.service.article.saveArticle(ctx.request.body); | ||
} | ||
|
||
async del(ctx) { | ||
const { id } = ctx.params; | ||
ctx.body = this.service.article.deleteArticle(id); | ||
} | ||
|
||
async detail(ctx) { | ||
const id = ctx.params.id; | ||
ctx.body = this.service.article.getArticle(Number(id)); | ||
} | ||
}; |
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,8 @@ | ||
'use strict'; | ||
module.exports = app => { | ||
return class CategoryController extends app.Controller { | ||
async index(ctx) { | ||
await ctx.render('category.js', { message: 'Egg React Server Side Render: Category' }); | ||
} | ||
}; | ||
}; |
This file was deleted.
Oops, something went wrong.
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,21 @@ | ||
'usestrict'; | ||
const egg = require('egg'); | ||
module.exports = class IndexController extends egg.Controller { | ||
|
||
async ssr(ctx) { | ||
await ctx.render('home.js', { url: ctx.url }); | ||
} | ||
|
||
async csr(ctx) { | ||
const result = this.service.article.getArtilceList(); | ||
await ctx.renderClient('home.js', result); | ||
} | ||
|
||
async list() { | ||
this.ctx.body = this.service.article.getArtilceList(); | ||
} | ||
|
||
async detail() { | ||
const id = this.ctx.query.id; | ||
} | ||
}; |
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,11 @@ | ||
'use strict'; | ||
const Factory = require('../lib/db/factory'); | ||
const DBSymbol = Symbol('Application#db'); | ||
module.exports = { | ||
get db() { | ||
if (!this[DBSymbol]) { | ||
this[DBSymbol] = Factory(); | ||
} | ||
return this[DBSymbol]; | ||
}, | ||
}; |
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 @@ | ||
'use strict'; | ||
module.exports = { | ||
get db() { | ||
return this.app.db; | ||
} | ||
}; |
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,25 @@ | ||
'use strict'; | ||
const shortid = require('shortid'); | ||
module.exports = class DB { | ||
constructor(name = 'blog.json') { | ||
this.name = name; | ||
} | ||
getUniqueId() { | ||
return shortid.generate(); | ||
} | ||
get(collectionName) { | ||
return null; | ||
} | ||
add(collectionName, json) { | ||
return null; | ||
} | ||
update(collectionName, where, json) { | ||
return null; | ||
} | ||
delete(collectionName, field) { | ||
return null; | ||
} | ||
getPager(collectionName, query) { | ||
return null; | ||
} | ||
}; |
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,31 @@ | ||
'use strict'; | ||
module.exports = class Collection { | ||
constructor(db, name) { | ||
this.db = db; | ||
this.name = name; | ||
} | ||
get() { | ||
return this.db.get(this.name); | ||
} | ||
getByWhere(json) { | ||
return this.db.get(this.name).find(json); | ||
} | ||
add(json) { | ||
return this.db.add(this.name, json); | ||
} | ||
update(where, json) { | ||
return this.db.update(this.name, where, json); | ||
} | ||
delete(field) { | ||
return this.db.delete(this.name, field); | ||
} | ||
getPager(query) { | ||
return this.db.getPager(this.name, query); | ||
} | ||
getOrderAscByField(field) { | ||
return this.get().orderBy(field, 'asc').value(); | ||
} | ||
getOrderDescByField(field) { | ||
return this.get().orderBy(field, 'desc').value(); | ||
} | ||
}; |
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,14 @@ | ||
'use strict'; | ||
const File = require('./file'); | ||
const MySQL = require('./mysql'); | ||
const MongoDB = require('./mongo'); | ||
module.exports = type => { | ||
switch (type) { | ||
case 'mysql': | ||
return new MySQL(); | ||
case 'mongo': | ||
return new MongoDB(); | ||
default: | ||
return new File(); | ||
} | ||
}; |
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,60 @@ | ||
'use strict'; | ||
const lowdb = require('lowdb'); | ||
const lodashid = require('lodash-id'); | ||
const FileSync = require('lowdb/adapters/FileSync'); | ||
const Base = require('./base'); | ||
module.exports = class FileDB extends Base { | ||
constructor(name) { | ||
super(name); | ||
const file = new FileSync(this.name); | ||
this.instance = lowdb(file); | ||
this.instance._.mixin(lodashid); | ||
this.create(); | ||
} | ||
create() { | ||
this.instance.defaults({ | ||
article: [], | ||
user: {} | ||
}).write(); | ||
} | ||
get(collectionName) { | ||
return this.instance.get(collectionName); | ||
} | ||
add(collectionName, json) { | ||
return this.get(collectionName) | ||
.push(json) | ||
.write(); | ||
} | ||
update(collectionName, where, json) { | ||
return this.get(collectionName).find(where).assign(json).write(); | ||
} | ||
delete(collectionName, field) { | ||
return this.get(collectionName).remove(field).write(); | ||
} | ||
getPager(collectionName, json) { | ||
const { | ||
where, | ||
like, | ||
pageIndex, | ||
pageSize, | ||
orderByField, | ||
orderBy | ||
} = json; | ||
const start = (pageIndex - 1) * pageSize; | ||
const end = pageIndex * pageSize; | ||
const result = this.get(collectionName) | ||
.filter(where) | ||
.filter(item => { | ||
return Object.keys(like).reduce((isLike, key) => { | ||
return isLike && item[key] && item[key].indexOf(like[key]) > -1; | ||
}, true); | ||
}) | ||
.orderBy(orderByField, orderBy); | ||
const total = result.size().value(); | ||
const list = result.slice(start, end).value(); | ||
return { | ||
total, | ||
list | ||
}; | ||
} | ||
}; |
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,3 @@ | ||
'use strict'; | ||
const Base = require('./base'); | ||
module.exports = class MongoDB extends Base {}; |
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,3 @@ | ||
'use strict'; | ||
const Base = require('./base'); | ||
module.exports = class MySQLDB extends Base {}; |
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,44 @@ | ||
'use strict'; | ||
module.exports = class Query { | ||
constructor() { | ||
this.orderByField = 'createTime'; | ||
this.orderBy = 'desc'; | ||
this._pageIndex = 1; | ||
this._pageSize = 10; | ||
this.where = new Proxy({}, { | ||
set(target, key, value, proxy) { | ||
if (value === undefined) { | ||
return true; | ||
} | ||
return Reflect.set(target, key, value, proxy); | ||
} | ||
}); | ||
|
||
this.like = new Proxy({}, { | ||
set(target, key, value, proxy) { | ||
if (value === undefined) { | ||
return true; | ||
} | ||
return Reflect.set(target, key, value, proxy); | ||
} | ||
}); | ||
} | ||
|
||
get pageIndex() { | ||
return this._pageIndex; | ||
} | ||
set pageIndex(value) { | ||
if (value !== undefined) { | ||
this._pageIndex = value; | ||
} | ||
} | ||
|
||
get pageSize() { | ||
return this._pageSize; | ||
} | ||
set pageSize(value) { | ||
if (value !== undefined) { | ||
this._pageSize = value; | ||
} | ||
} | ||
}; |
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 = () => { | ||
return async function locale(ctx, next) { | ||
ctx.locals.locale = ctx.query.locale || 'cn'; | ||
ctx.locals.origin = ctx.request.origin; | ||
await next(); | ||
}; | ||
}; |
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,13 @@ | ||
'use strict'; | ||
|
||
module.exports = class Model { | ||
constructor() { | ||
super(); | ||
this.id = void 0; | ||
this.title = undefined; | ||
this.summary = undefined; | ||
this.tag = undefined; | ||
this.hits = 0; | ||
this.createTime = Date.now(); | ||
} | ||
}; |
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,2 @@ | ||
'use strict'; | ||
module.exports = class ArticleDetail {}; |
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,9 @@ | ||
'use strict'; | ||
module.exports = class User { | ||
constructor() { | ||
this.id = null; | ||
this.name = null; | ||
this.password = null; | ||
this.roleId = null; | ||
} | ||
}; |
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 @@ | ||
'use strict'; | ||
const Pagination = require('./pagination'); | ||
module.exports = class Model { | ||
constructor() { | ||
this.pagination = new Pagination(); | ||
} | ||
}; |
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 |
---|---|---|
@@ -1,8 +1,14 @@ | ||
|
||
module.exports = app => { | ||
app.get('/', app.controller.home.index); | ||
app.get('/about', app.controller.home.index); | ||
app.get('/js', app.controller.home.js); | ||
app.get('/jsx', app.controller.home.jsx); | ||
app.get('/api/article/list', app.controller.home.pager); | ||
const { router, controller } = app; | ||
router.get('/', controller.home.index.ssr); | ||
router.get('/csr', controller.home.index.csr); | ||
router.get('/api/list', controller.home.index.list); | ||
router.get('/category', controller.category.category.index); | ||
router.get('/login', controller.admin.admin.login); | ||
router.post('/admin/api/article/list', controller.admin.admin.list); | ||
router.post('/admin/api/article/add', controller.admin.admin.add); | ||
router.get('/admin/api/article/del/:id', controller.admin.admin.del); | ||
router.get('/admin/api/article/:id', controller.admin.admin.detail); | ||
router.get('/admin(/.+)?', controller.admin.admin.home); | ||
}; |
Oops, something went wrong.