Skip to content

Commit 1c5c32a

Browse files
committed
feat(tinshu): 添加后台接口
1 parent 3096e47 commit 1c5c32a

File tree

6 files changed

+59
-45
lines changed

6 files changed

+59
-45
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,17 @@ import { mock } from "@qy-mock/core";
383383
mock("@cname"); //"杨平"
384384
```
385385

386+
#### `mockArray`
387+
388+
描述: 根据规则`mock`出一个对应长度的数组
389+
390+
示例:
391+
392+
```typescript
393+
import { mockArray } from "@qy-mock/core";
394+
mockArray("@cname", 3); //["杨平","千云","天衡"]
395+
```
396+
386397
#### `编写一个Mock模块`
387398

388399
框架内部帮你写好了五个请求,分别是`Get`,`Post`,`Delete`,`Put`,`Options`,使用方式一样的。接下来编写一个示例

packages/tingshu/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import { createApp, inject, provide, emit, on, Get, Post } from "@qy-mock/core";
22
const { app, mockApp } = createApp("apiV3/audio2");
33
import { AdminRouter } from "./src/router";
4-
mockApp.registerRouter([AdminRouter]).mount("40003");
4+
mockApp
5+
.registerRouter([AdminRouter])
6+
.setBaseResponse({
7+
customTransformer: (data) => Object.assign(data, { resultCode: 1 }),
8+
})
9+
.mount("40003");

packages/tingshu/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"@qy-mock/core": "^0.0.0",
1717
"@qy-mock/shared": "^1.0.0",
1818
"glob": "^7.1.7",
19-
"mockjs": "^1.1.0",
2019
"ts-node": "^10.2.0",
2120
"typescript": "^4.3.5"
2221
}

packages/tingshu/src/modules/commentAdmin.ts

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Mock from "mockjs";
1+
import { mock, mockArray } from "@qy-mock/core";
22
/**
33
* 删除评论
44
* @param {*} reqData
@@ -13,34 +13,32 @@ export function delComment(reqData) {
1313
* @returns
1414
*/
1515
export function getAlbumCommentList(reqData) {
16-
var data = [];
1716
const pageindex = reqData.pageindex || 10;
18-
for (var i = 0; i <= pageindex; i++) {
19-
const demoResponse = {
20-
avatar: Mock.mock("@image('100x100')"),
21-
comment: Mock.mock("@cparagraph()"),
22-
id: Mock.mock("@increment()"),
23-
nickName: Mock.mock("@cname"),
24-
};
25-
const demo = {
26-
avatar: Mock.mock("@image('50x50')"),
27-
comment: Mock.mock("@cparagraph()"),
28-
comment_id: Mock.mock("@increment()"),
29-
create_time: Math.floor(
30-
new Date(Mock.mock("@datetime")).getTime() / 1000
31-
),
32-
nickName: Mock.mock("@cname"),
33-
uid: Mock.mock("@increment()"),
34-
response: new Array(4).fill(demoResponse),
35-
};
36-
data.push(demo);
37-
}
3817
// 总数
3918
const count = 56;
4019
const totalpage = Math.floor(count / pageindex) + 1;
20+
const data = mockArray(
21+
mock({
22+
avatar: "@image('50x50')",
23+
comment: "@cparagraph()",
24+
comment_id: "@increment()",
25+
create_time: Math.floor(new Date(mock("@datetime()")).getTime() / 1000),
26+
nickName: "@cname()",
27+
uid: "@increment()",
28+
response: mockArray(
29+
{
30+
avatar: "@image('100x100')",
31+
comment: "@cparagraph()",
32+
id: "@increment()",
33+
nickName: "@cname",
34+
},
35+
mock("@integer(1,6)")
36+
),
37+
}),
38+
pageindex
39+
);
4140
return {
42-
resultCode: 1,
43-
data: data,
41+
data,
4442
count,
4543
totalpage,
4644
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { mock } from "@qy-mock/core";
2+
3+
export function getNotify() {
4+
return {
5+
data: mock({
6+
id: "@id",
7+
"type|1-3": 1,
8+
content: "@cparagraph()",
9+
publish_time: "@date('yyyy-MM-dd')",
10+
}),
11+
};
12+
}

packages/tingshu/src/modules/notifyAdmin.ts

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,17 @@
1-
import Mock from "mockjs";
1+
import { mock, mockArray } from "@qy-mock/core";
22
export function addNotify() {
33
return true;
44
}
55
export function notifyList() {
6-
const data = [
6+
let data = mockArray(
77
{
8-
publish_time: "2020-07-11",
9-
content: Mock.mock("@cparagraph()"),
10-
rule_type: 1,
11-
id: 1,
8+
publish_time: "@date('yyyy-MM-dd')",
9+
content: mock("@cparagraph()"),
10+
"rule_type|1-3": 1,
11+
id: "@id",
1212
},
13-
{
14-
publish_time: "2020-07-16",
15-
content: Mock.mock("@cparagraph()"),
16-
rule_type: 2,
17-
id: 2,
18-
},
19-
{
20-
publish_time: "2020-04-08",
21-
content: Mock.mock("@cparagraph()"),
22-
rule_type: 3,
23-
id: 3,
24-
},
25-
];
13+
3
14+
);
2615
return {
2716
data,
2817
};

0 commit comments

Comments
 (0)