-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdepartment.js
98 lines (89 loc) · 2.14 KB
/
department.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import axios from "axios";
import prompts from "prompts";
import JSONbig from "json-bigint";
import api from "./api";
import config from "./config";
import store from "./store";
import Menu from "./menu";
const { domain, defaultPassword } = config;
const CREATE_DEPARTMENT_FORM = () => [
{
type: "text",
name: "name",
message: "What is the name of it?"
}
];
class Department {
async list() {
const res = await api({
method: "get",
url: `department/list?access_token=${store.accessToken}&id=1`,
transformResponse: [data => JSONbig.parse(data)]
});
return res.data.department;
}
async create() {
const response = await prompts(CREATE_DEPARTMENT_FORM());
const params = {
parentid: 1,
...response
};
console.log("creating department...");
const res = await api({
method: "post",
url: `department/create?access_token=${store.accessToken}`,
data: params
});
// Done
if (res.data.errcode === 0) {
console.log("Successfully created");
} else {
console.log(res.data);
}
// 显示主菜单
const menu = new Menu();
menu.start();
}
async delete() {
const department = await this.list();
const questions = department.map(item => {
const id = item.id;
return {
title: `${item.name}-${id}`,
value: id
};
});
const form = await prompts([
{
type: "autocomplete",
name: "item",
message: "Select a department",
choices: questions,
limit: 100
},
{
type: "confirm",
name: "confirm",
message: "Are you sure to delete?"
}
]);
if (form.item && form.confirm) {
// excute deletion
const res = await api({
method: "get",
url: `department/delete?access_token=${store.accessToken}&id=${
form.item
}`
});
if (res.data.errcode === 0) {
console.log("Successfully deleted");
} else {
console.log(res.data.errmsg);
}
// 显示主菜单
const menu = new Menu();
menu.start();
}
}
}
export default Department;