-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
363 lines (300 loc) · 10.2 KB
/
Copy pathindex.js
File metadata and controls
363 lines (300 loc) · 10.2 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#!/usr/bin/env node
const inquirer = require('inquirer');
const { mkdirp } = require('mkdirp');
const fs = require('fs');
const path = require('path');
async function generateFiles() {
// 提示用户输入表结构
const { tableStructure } = await inquirer.prompt([
{
type: 'editor',
name: 'tableStructure',
message: '请输入表结构:',
},
]);
// 解析表名
const tableNameMatch = tableStructure.match(/CREATE TABLE `([^`]+)`/);
if (!tableNameMatch) {
console.error('无法解析表名,请检查输入的表结构。');
return;
}
const tableName = tableNameMatch[1];
if (tableName.indexOf('_') === -1) {
console.error('表名必须包含下划线,请检查输入的表结构');
return;
}
const tableNameParts = tableName.match(/^([^_]+)_(.*)$/);
const folderName = tableNameParts[1];
const fileName = tableNameParts[2];
const { camelCase: tableNameCase, pascalCase: tableNameGigCase } = convertToCamelCase(tableName);
const { camelCase: fileNameCamel, pascalCase: fileNameBigCamel } = convertToCamelCase(fileName);
// 解析字段
const fieldMatches = tableStructure.match(/`([^`]+)`/g);
const fields = fieldMatches.map(match => match.replace(/`/g, ''));
// 创建文件夹
const controllersPath = path.join(process.cwd(), 'controllers', folderName);
const servicesPath = path.join(process.cwd(), 'services', folderName);
const modelsPath = path.join(process.cwd(), 'models');
await mkdirp(controllersPath);
await mkdirp(servicesPath);
await mkdirp(modelsPath);
// 生成 models 文件
const modelFilePath = path.join(modelsPath, tableNameCase + '.js');
if (!fs.existsSync(modelFilePath)) {
const modelContent = generateModelContent(tableName, tableNameGigCase);
fs.writeFileSync(modelFilePath, modelContent);
}
// 生成 controllers 文件
const controllerFilePath = path.join(controllersPath, fileNameCamel + '.js');
if (!fs.existsSync(controllerFilePath)) {
const controllerContent = generateControllerContent(folderName, fileNameCamel, fileNameBigCamel);
fs.writeFileSync(controllerFilePath, controllerContent);
}
// 生成 services 文件
const serviceFilePath = path.join(servicesPath, fileNameCamel + '.js');
if (!fs.existsSync(serviceFilePath)) {
const serviceContent = generateServiceContent(tableNameCase, fileNameBigCamel, fields);
fs.writeFileSync(serviceFilePath, serviceContent);
}
console.log('文件生成成功!');
}
function convertToCamelCase(str = '') {
const parts = str.split('_');
return {
camelCase: parts.map((p, i) => i ? p[0].toUpperCase() + p.slice(1) : p).join(''),
pascalCase: parts.map(p => p[0].toUpperCase() + p.slice(1)).join('')
};
};
function generateModelContent(tableName, tableNameGigCase) {
return `
module.exports = class ${tableNameGigCase} {
constructor() {
this.table = '${tableName}';
this.db = koacrab.mysql;
}
async list(info = {}) {
let { field = [], where = {}, order = 'add_time desc, id desc', limit = [0, 10], pageNum = '', pageSize = '' } = info;
let { error, result } = await this.db.table(this.table)
.field(field)
.where(where)
.order(order)
.limit(limit)
.select()
.execSql();
return result;
}
async find(info = {}) {
let { field = [], where = {}, order = '' } = info;
let { error, result } = await this.db.table(this.table)
.field(field)
.where(where)
.order(order)
.find()
.execSql();
return result;
}
async count(info = {}) {
let { where = {} } = info;
let { result } = await this.db.table(this.table)
.where(where)
.count()
.find()
.execSql();
return result;
}
async insert(insertData) {
let { result } = await this.db.table(this.table)
.data(insertData)
.insert()
.execSql();
return result.insertId || ''
}
async delete(info = {}) {
let { where = {} } = info;
await this.db.table(this.table)
.where(where)
.delete()
.execSql()
}
async update(info = {}) {
let { result } = await this.db.table(this.table)
.data(info.data)
.where(info.where)
.update()
.execSql()
return result;
}
async query(info = '') {
let { result } = await this.db.query(info).execSql();
return result;
}
}
`;
}
function generateControllerContent(folderName, fileNameCamel, fileNameBigCamel) {
return `
module.exports = class ${fileNameBigCamel} {
constructor() {
}
async list() {
let fields = this.request.fields;
let services = this.services['${folderName}/${fileNameCamel}'];
let result = await services.list(fields);
this.renderText(result);
}
async info() {
let query = this.request.query;
let services = this.services['${folderName}/${fileNameCamel}'];
let result = await services.info(query);
this.renderText(result);
}
async del() {
let query = this.request.query;
let services = this.services['${folderName}/${fileNameCamel}'];
let result = await services.del(query);
this.renderText(result);
}
async add() {
let fields = this.request.fields;
let services = this.services['${folderName}/${fileNameCamel}'];
let result = await services.add(fields);
this.renderText(result);
}
}
`;
}
function generateServiceContent(tableNameCase, fileNameBigCamel, fields) {
const filteredFields = fields.filter(field =>!['id', 'delete_time', 'update_time'].includes(field)); // 过滤掉id、delete_time、update_time字段
return `
const tools = require('@tools/tools.js');
module.exports = class ${fileNameBigCamel} {
constructor() {}
// 列表
async list(info = {}) {
let reason = '', result = [], error = 0;
let lists = await koacrab.models.${tableNameCase}.list(info);
let total = await koacrab.models.${tableNameCase}.count(info);
return {
error: error,
reason: reason,
result: {
lists,
total
}
}
}
// 添加
async add(info = {}) {
let error = 0;
let reason = '', result = {};
let time = new Date().getTime();
let { id = '', token = '' } = info;
id = tools.encryptAndDecrypt(1, 3, id);
let userInfo = await koacrab.models.user.find({
field: ['id', 'nickname', 'alias_name', 'status', 'o.openid'],
where: { token: info.token, service_id: info.serviceId || 0 }
});
if (!userInfo.id) {
error = 100;
reason = '请先登录!';
} else {
if (id) {
let updateData = {
update_time: time
};
// 根据表结构字段来更新数据
${filteredFields.map(field => `if (info.hasOwnProperty('${field}')) {
updateData['${field}'] = info['${field}'] || '';
}`).join('\n')}
await koacrab.models.${tableNameCase}.update({
data: updateData,
where: { id }
});
} else {
let insertData = {
// user_id: userInfo.id,
add_time: time,
};
// 根据表结构字段来插入数据
${filteredFields.map(field => `if (info.hasOwnProperty('${field}')) {
insertData['${field}'] = info['${field}'] || '';
}`).join('\n')}
await koacrab.models.${tableNameCase}.insert(insertData);
reason = '添加成功!';
}
}
return {
error: error,
reason: reason,
result: result || {}
}
}
// 详情
async info(info = {}) {
let reason = '', error = 0, result = {};
let { id = '', token = '' } = info;
id = tools.encryptAndDecrypt(1, 3, id);
if (id) {
let c = await koacrab.models.${tableNameCase}.find({
where: { id }
});
if (detailInfo['id']) {
result = detailInfo;
} else {
error = 1;
reason = '未找到信息!';
}
} else {
error = 1;
reason = '无效的参数!';
}
return {
error: error,
reason: reason,
result: result || {}
}
}
// 删除
async del(info = {}) {
let reason = '', error = 0;
let result = {};
let { id = '', token = '' } = info;
id = tools.encryptAndDecrypt(1, 3, id);
let userInfo = await koacrab.models.user.find({
field: ['id'],
where: { token: token }
});
if (!userInfo.id) {
error = 1;
reason = '您无权限删除!'
} else {
let activityInfo = await koacrab.models.${tableNameCase}.find({
where: { id }
});
if (activityInfo['user_id'] !== userInfo['id']) {
return {
error: 1,
reason: '您不是创建者,无权限删除!',
result: ''
}
}
let time = new Date().getTime();
await koacrab.models.${tableNameCase}.update({
data: { delete_time: time },
where: { id }
});
error = 0;
reason = '删除成功!';
}
return {
error: error,
reason: reason,
result: ''
}
}
}
`;
}
generateFiles().catch(error => {
console.error('发生错误:', error);
});