Skip to content

Commit 14a6b1a

Browse files
author
zhongchubing
committed
refactor: 接口增加基础的controller
1 parent b1bff4b commit 14a6b1a

File tree

13 files changed

+187
-140
lines changed

13 files changed

+187
-140
lines changed

geekape-nav-main/components/AppSearch.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ export default {
2626
methods: {
2727
async querySearchAsync(query, cb) {
2828
if (query !== '') {
29-
const res = await axios.get(API_NAV + `?keyword=${query}`)
30-
if (Array.isArray(res.data)) {
31-
res.data = res.data.map(item=> (item.value = item.name, item))
32-
cb(res.data)
29+
const { data } = await axios.get(API_NAV + `?keyword=${query}`)
30+
if (Array.isArray(data.data)) {
31+
const finalData = data.data.map(item=> (item.value = item.name, item))
32+
cb(finalData)
3333
}
3434
} else {
3535
cb([])

geekape-nav-server/app/controller/category.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
import Controller from './common';
1+
import Controller from '../core/base_controller';
22

33
export default class CategoryController extends Controller {
4+
tableName(): string {
5+
return 'Category'
6+
}
7+
48
async list() {
59
const { ctx } = this
610
const { showInMenu = true } = ctx.query
@@ -17,17 +21,13 @@ export default class CategoryController extends Controller {
1721
this.error(error.message)
1822
}
1923
}
24+
2025
async add() {
21-
const { ctx } = this
22-
let data = await ctx.service.common.add(ctx.request.body, 'Category');
23-
this.success(data)
26+
await super.add()
2427
}
2528

2629
async edit() {
27-
const { ctx } = this
28-
const { id } = ctx.request.body;
29-
let data = await ctx.service.common.update(ctx.request.body, 'Category');
30-
this.success(data)
30+
await super.update()
3131
}
3232

3333
async del() {

geekape-nav-server/app/controller/common.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 41 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Controller from './common';
1+
import Controller from '../core/base_controller';
22
const request = require('request');
33
const cheerio = require('cheerio');
44

@@ -9,63 +9,45 @@ enum NAV_STATUS {
99
}
1010

1111
export default class NavController extends Controller {
12+
tableName(): string {
13+
return 'Nav'
14+
}
15+
1216
async list() {
1317
const { ctx } = this
1418
const { model } = ctx
15-
try {
16-
let { pageSize = 10, pageNumber = 1, status = 0, categoryId, name } = ctx.query
17-
pageSize = Number(pageSize)
18-
pageNumber = Number(pageNumber)
19-
const skipNumber = pageSize * pageNumber - pageSize
19+
let { status = 0, categoryId, name } = ctx.query
2020

21-
let findParam: any = {
22-
status,
23-
}
24-
if (!status) {
25-
findParam = {
26-
$or: [
27-
{ status: { $exists: false } },
28-
{ status: 0 },
29-
]
30-
}
21+
let findParam: any = {
22+
status,
23+
}
24+
if (!status) {
25+
findParam = {
26+
$or: [
27+
{ status: { $exists: false } },
28+
{ status: 0 },
29+
]
3130
}
32-
if (categoryId) {
33-
findParam.categoryId = {
34-
$eq: categoryId
35-
}
31+
}
32+
if (categoryId) {
33+
findParam.categoryId = {
34+
$eq: categoryId
3635
}
37-
if (name) {
38-
let reg = new RegExp(name,'i');
39-
findParam.name = {
40-
$regex: reg
41-
}
36+
}
37+
if (name) {
38+
let reg = new RegExp(name,'i');
39+
findParam.name = {
40+
$regex: reg
4241
}
43-
44-
const [resData, total] = await Promise.all([
45-
model.Nav.find(findParam).skip(skipNumber).limit(pageSize).sort({_id: -1}),
46-
model.Nav.find(findParam).count(),
47-
])
48-
this.success({
49-
data: resData,
50-
total,
51-
pageNumber: Math.ceil(total / pageSize),
52-
})
53-
} catch (error) {
54-
this.error(error.message)
5542
}
43+
44+
await super.getList(findParam);
5645
}
5746

5847
async add() {
59-
const { ctx } = this
60-
const { request: req, model } = ctx
61-
try {
62-
req.body.status = NAV_STATUS.wait
63-
req.body.createTime = new Date()
64-
const res = await model.Nav.create(req.body);
65-
this.success(res)
66-
} catch (error) {
67-
this.error(error.message)
68-
}
48+
this.ctx.request.body.status = NAV_STATUS.wait
49+
this.ctx.request.body.createTime = new Date()
50+
await super.add()
6951
}
7052

7153
async reptile() {
@@ -94,23 +76,17 @@ export default class NavController extends Controller {
9476
}
9577

9678
async del() {
97-
const { id } = this.ctx.request.body
98-
const res = await this.ctx.service.common.remove(id, 'Nav');
99-
this.success(res)
79+
await super.remove();
10080
}
10181

10282
async edit() {
103-
const { body } = this.ctx.request
104-
body.updateTime = new Date()
105-
const res = this.ctx.service.common.update(body, 'Nav')
106-
this.success(res);
83+
this.ctx.request.body.updateTime = new Date()
84+
await super.update();
10785
}
10886

10987
async audit() {
110-
const { body } = this.ctx.request
111-
body.auditTime = new Date()
112-
const res = this.ctx.service.common.update(body, 'Nav')
113-
this.success(res);
88+
this.ctx.request.body.auditTime = new Date()
89+
await super.update();
11490
}
11591

11692
/**
@@ -150,21 +126,21 @@ export default class NavController extends Controller {
150126

151127
async get() {
152128
const { ctx } = this
153-
const { id, keyword, limit = 10 } = ctx.query
129+
const { id, keyword } = ctx.query
154130

155131
let res
156132

157133
if (id) {
158-
res = await ctx.service.common.get(ctx.query.id, 'Nav');
134+
res = await super.get();
159135
} else if(keyword) {
160-
res = await ctx.service.nav.find(keyword, limit);
136+
let reg = new RegExp(keyword,'i');
137+
await super.getList({
138+
name: { $regex: reg },
139+
}, (table)=> table.limit(10));
161140
}
162-
this.success(res)
163141
}
164142

165143
async random() {
166-
const { ctx } = this
167-
const res = await ctx.service.common.getRandomData(10, 'Nav');
168-
this.success(res)
144+
await super.getRandomList();
169145
}
170146
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Controller from '../core/base_controller';
2+
3+
export default class CategoryController extends Controller {
4+
tableName(): string {
5+
return 'Tag';
6+
}
7+
}

geekape-nav-server/app/controller/user.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Controller from './common'
1+
import Controller from '../core/base_controller'
22

33
export default class UserController extends Controller {
44
public async login() {
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import { Controller } from 'egg';
2+
3+
export default class CommonController extends Controller {
4+
tableName() {
5+
return ''
6+
}
7+
success(data) {
8+
this.ctx.body = {
9+
code: 1,
10+
msg: 'ok',
11+
data,
12+
};
13+
}
14+
error(msg) {
15+
this.ctx.body = {
16+
code: 0,
17+
msg,
18+
data: null,
19+
};
20+
}
21+
22+
//添加
23+
async add() {
24+
const { request } = this.ctx
25+
const tableName = this.tableName()
26+
try {
27+
const res = await this.ctx.model[tableName].create(request.body);
28+
this.success(res)
29+
} catch (e) {
30+
this.error(e.message)
31+
}
32+
}
33+
34+
//删除
35+
async remove() {
36+
const { request } = this.ctx
37+
const tableName = this.tableName()
38+
try {
39+
const id = request.body.id
40+
const res = await this.ctx.model[tableName].remove({ _id: id })
41+
this.success(res)
42+
} catch (e) {
43+
this.error(e.message)
44+
}
45+
}
46+
47+
//更新
48+
async update() {
49+
const { request } = this.ctx
50+
const tableName = this.tableName()
51+
try {
52+
const id = request.body.id
53+
delete request.body.id
54+
const res = await this.ctx.model[tableName].update({ _id: id }, request.body)
55+
this.success(res)
56+
} catch (e) {
57+
this.error(e.message)
58+
}
59+
}
60+
61+
//查找一个
62+
async get() {
63+
const { request, query } = this.ctx
64+
const tableName = this.tableName()
65+
try {
66+
const id = query.id
67+
const res = await this.ctx.model[tableName].findOne({ _id: id })
68+
this.success(res)
69+
} catch (e) {
70+
this.error(e.message)
71+
}
72+
}
73+
74+
//查找多个
75+
async getList(findObj = {}, otherCMD = (_table: any) => _table) {
76+
const { request, query } = this.ctx
77+
const tableName = this.tableName()
78+
try {
79+
let { pageSize = 10, pageNumber = 1 } = query
80+
pageSize = Number(pageSize)
81+
pageNumber = Number(pageNumber)
82+
const skipNumber = pageSize * pageNumber - pageSize
83+
const table = this.ctx.model[tableName]
84+
85+
const [data, total] = await Promise.all([
86+
otherCMD(table.find(findObj).skip(skipNumber).limit(pageSize).sort({_id: -1})),
87+
table.find(findObj).count()
88+
])
89+
90+
this.success({
91+
data,
92+
total,
93+
pageNumber: Math.ceil(total / pageSize),
94+
})
95+
} catch (e) {
96+
this.error(e.message)
97+
}
98+
}
99+
100+
//查找随机数量列表
101+
async getRandomList(randomNumber: number = 10) {
102+
const { request, query } = this.ctx
103+
const tableName = this.tableName()
104+
try {
105+
const res = await this.ctx.model[tableName].aggregate( [ { $sample: { size: randomNumber } } ] )
106+
this.success(res)
107+
} catch (e) {
108+
this.error(e.message)
109+
}
110+
}
111+
}

geekape-nav-server/app/model/tag.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = app => {
2+
const mongoose = app.mongoose;
3+
const Schema = mongoose.Schema;
4+
5+
const TagSchema = new Schema({
6+
name: String,
7+
category: String,
8+
}, { collection: 'tag' });
9+
return mongoose.model('Tag', TagSchema);
10+
};

geekape-nav-server/app/service/Common.ts

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)