q-koa 11.3.2 → 11.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -21,614 +21,27 @@ exports.initModel = async (ctx) => {
|
|
|
21
21
|
: {
|
|
22
22
|
alter: true,
|
|
23
23
|
}
|
|
24
|
+
|
|
25
|
+
const appConfig = getConfig(app)
|
|
26
|
+
if (Array.isArray(model)) {
|
|
27
|
+
const { is_dev } = await appConfig.getObject('base')
|
|
28
|
+
if (!is_dev && option === 'force')
|
|
29
|
+
throw new Error('正式环境不允许array model force')
|
|
30
|
+
await Promise.all(model.map((m) => app.model[m].sync(config)))
|
|
31
|
+
return ctx.SUCCESS('ok')
|
|
32
|
+
}
|
|
24
33
|
if (model === 'all') {
|
|
25
34
|
const modelList = Object.keys(app.model)
|
|
26
35
|
await Promise.all(modelList.map((m) => app.model[m].sync(config)))
|
|
27
36
|
return ctx.SUCCESS('ok')
|
|
28
37
|
}
|
|
29
38
|
if (app.model[model]) {
|
|
30
|
-
if (Object.keys(app.model[model]).length === 0)
|
|
39
|
+
if (Object.keys(app.model[model]).length === 0) {
|
|
31
40
|
return ctx.ERROR('请检查,model为空')
|
|
41
|
+
}
|
|
32
42
|
const result = await app.model[model].sync(config)
|
|
33
43
|
return ctx.SUCCESS(result)
|
|
34
44
|
} else {
|
|
35
45
|
return ctx.ERROR(`app.model没有${model}`)
|
|
36
46
|
}
|
|
37
|
-
return ctx.SUCCESS(null)
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
exports.customRouter = async (ctx) => {
|
|
41
|
-
// const {
|
|
42
|
-
// app,
|
|
43
|
-
// appName,
|
|
44
|
-
// } = getAppByCtx(ctx);
|
|
45
|
-
|
|
46
|
-
// const list = [{
|
|
47
|
-
// path: 'jiangong_statis',
|
|
48
|
-
// name: '数据面板'
|
|
49
|
-
// }]
|
|
50
|
-
const list = []
|
|
51
|
-
ctx.SUCCESS(list)
|
|
52
|
-
return list
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
exports.showTables = async (ctx) => {
|
|
56
|
-
const { app, appName } = getAppByCtx(ctx)
|
|
57
|
-
|
|
58
|
-
if (app.cache && app.cache.get('showTables')) {
|
|
59
|
-
ctx.SUCCESS(app.cache.get('showTables'))
|
|
60
|
-
return app.cache.get('showTables')
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const pluginDir = path.resolve(
|
|
64
|
-
__dirname,
|
|
65
|
-
`${process.cwd()}/app/${appName}/plugins`
|
|
66
|
-
)
|
|
67
|
-
let aliasModelList = []
|
|
68
|
-
const allList = await fsPromise.readdir(pluginDir)
|
|
69
|
-
allList.forEach((folder) => {
|
|
70
|
-
const isFolder = fs.lstatSync(path.resolve(pluginDir, folder)).isDirectory()
|
|
71
|
-
const hasConfig = fs.existsSync(
|
|
72
|
-
path.resolve(
|
|
73
|
-
__dirname,
|
|
74
|
-
`${process.cwd()}/app/${appName}/plugins/${folder}/config.js`
|
|
75
|
-
)
|
|
76
|
-
)
|
|
77
|
-
let hasModel = false
|
|
78
|
-
if (hasConfig) {
|
|
79
|
-
const folderConfig = require(`${process.cwd()}/app/${appName}/plugins/${folder}/config.js`)
|
|
80
|
-
if (folderConfig.model) {
|
|
81
|
-
hasModel = true
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (
|
|
86
|
-
isFolder &&
|
|
87
|
-
hasConfig &&
|
|
88
|
-
hasModel &&
|
|
89
|
-
!Object.keys(app.model).includes(folder)
|
|
90
|
-
) {
|
|
91
|
-
aliasModelList = [...aliasModelList, folder]
|
|
92
|
-
}
|
|
93
|
-
})
|
|
94
|
-
const modelList = [...Object.keys(app.model), ...aliasModelList]
|
|
95
|
-
const models = modelList.map((_model) => {
|
|
96
|
-
const defaultExcludes = [
|
|
97
|
-
'id',
|
|
98
|
-
'created_at',
|
|
99
|
-
'updated_at',
|
|
100
|
-
'deleted_at',
|
|
101
|
-
'createdid',
|
|
102
|
-
]
|
|
103
|
-
const mock = !lodash.isEmpty(
|
|
104
|
-
app.mock && app.mock[_model] && app.mock[_model]()
|
|
105
|
-
)
|
|
106
|
-
const defaultConfigPath = path.resolve(__dirname, `../${_model}/config.js`)
|
|
107
|
-
const defaultConfigExist = fs.existsSync(defaultConfigPath)
|
|
108
|
-
const target =
|
|
109
|
-
app.config[_model] &&
|
|
110
|
-
Object.keys(app.config[_model]).length === 0 &&
|
|
111
|
-
defaultConfigExist
|
|
112
|
-
? require(defaultConfigPath)
|
|
113
|
-
: app.config[_model]
|
|
114
|
-
// const target = Object.keys(app.config[model])
|
|
115
|
-
|
|
116
|
-
if (!target) throw new Error(`检查是否有${_model}/config.js`)
|
|
117
|
-
const model = target.model ? target.model : _model
|
|
118
|
-
return {
|
|
119
|
-
modelName: _model,
|
|
120
|
-
model,
|
|
121
|
-
name: target ? target.name : '',
|
|
122
|
-
// 属于哪个分类
|
|
123
|
-
belongs: lodash.get(target, 'belongs', 'page'),
|
|
124
|
-
// list切换tab显示 ex lanuage
|
|
125
|
-
limit: lodash.get(target, 'limit', 20),
|
|
126
|
-
excludeAuth: lodash.get(target, 'excludeAuth', []),
|
|
127
|
-
// 可排序字段
|
|
128
|
-
order: lodash.get(target, 'order', []),
|
|
129
|
-
// 默认排序
|
|
130
|
-
defaultOrder: lodash.get(target, 'defaultOrder', []),
|
|
131
|
-
// 下拉筛选
|
|
132
|
-
select: lodash.get(target, 'select', []),
|
|
133
|
-
// 是否可以选择批量删除
|
|
134
|
-
multiple: lodash.get(target, 'multiple', true),
|
|
135
|
-
// 是否后台拆分sql
|
|
136
|
-
is_split: lodash.get(target, 'is_split', false),
|
|
137
|
-
// 是否后台拆分count
|
|
138
|
-
is_split_count: lodash.get(target, 'is_split_count', false),
|
|
139
|
-
// 是否后台显示虚拟字段
|
|
140
|
-
show_virtual: lodash.get(target, 'show_virtual', false),
|
|
141
|
-
// 忽略字段
|
|
142
|
-
excludes:
|
|
143
|
-
_model === 'setting'
|
|
144
|
-
? lodash.get(target, 'excludes', [])
|
|
145
|
-
: lodash.difference(
|
|
146
|
-
lodash.uniq([
|
|
147
|
-
...defaultExcludes,
|
|
148
|
-
...lodash.get(target, 'excludes', []),
|
|
149
|
-
]),
|
|
150
|
-
lodash.get(target, 'order', [])
|
|
151
|
-
),
|
|
152
|
-
// reference admin components
|
|
153
|
-
reference: lodash.get(target, 'reference', []),
|
|
154
|
-
// reference admin components
|
|
155
|
-
personal: lodash.get(target, 'personal', []),
|
|
156
|
-
// sortOrder router
|
|
157
|
-
sortOrder: lodash.get(target, 'sortOrder', 1),
|
|
158
|
-
|
|
159
|
-
attributes: lodash.mapValues(
|
|
160
|
-
app.attributes[model],
|
|
161
|
-
(item) => item.comment
|
|
162
|
-
),
|
|
163
|
-
|
|
164
|
-
// include for admin
|
|
165
|
-
include: lodash.get(target, 'include', ''),
|
|
166
|
-
// only include exsit, and for custom like user_distribute has two user model
|
|
167
|
-
relate: lodash.get(target, 'relate', ''),
|
|
168
|
-
// can be mock boolean
|
|
169
|
-
mock,
|
|
170
|
-
// reference model query
|
|
171
|
-
referenceSelect: lodash.get(target, 'referenceSelect', []),
|
|
172
|
-
// 是否可以选择批量删除
|
|
173
|
-
availableSort: lodash.get(target, 'availableSort', false),
|
|
174
|
-
// 用于后台管理不要查询太多数据,常用于表多的model
|
|
175
|
-
autoData: lodash.get(target, 'autoData', null),
|
|
176
|
-
// 有些model需要前置查询其它model数据
|
|
177
|
-
initList: lodash.get(target, 'initList', []),
|
|
178
|
-
// 有些model的table需要前置查询模型
|
|
179
|
-
initTableList: lodash.get(target, 'initTableList', []),
|
|
180
|
-
// 局部更新列表
|
|
181
|
-
editInline: lodash.get(target, 'editInline', {}),
|
|
182
|
-
// 有些model需要前置查询其它model数据
|
|
183
|
-
comment: lodash.get(target, 'comment', {}),
|
|
184
|
-
// 默认查询,覆盖之前
|
|
185
|
-
modelQuery: lodash.get(target, 'modelQuery', {}),
|
|
186
|
-
// 插入字段,覆盖之前
|
|
187
|
-
modelUpsert: lodash.get(target, 'modelUpsert', {}),
|
|
188
|
-
// 编辑软删除
|
|
189
|
-
modelDelete: lodash.get(target, 'modelDelete', {}),
|
|
190
|
-
// 删除时检查是不是含有
|
|
191
|
-
deleteCheckList: lodash.get(target, 'deleteCheckList', []),
|
|
192
|
-
// 批量更新字段
|
|
193
|
-
bulkCreateList: lodash.get(target, 'bulkCreateList', []),
|
|
194
|
-
|
|
195
|
-
fn: app.controller[_model] ? Object.keys(app.controller[_model]) : [],
|
|
196
|
-
}
|
|
197
|
-
})
|
|
198
|
-
|
|
199
|
-
ctx.SUCCESS(models)
|
|
200
|
-
app.cache && app.cache.set('showTables', models)
|
|
201
|
-
return models
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
exports.getTable = async (ctx) => {
|
|
205
|
-
const { app } = getAppByCtx(ctx)
|
|
206
|
-
|
|
207
|
-
const {
|
|
208
|
-
model: _model,
|
|
209
|
-
show_virtual = false,
|
|
210
|
-
is_cache = true,
|
|
211
|
-
} = ctx.request.body
|
|
212
|
-
if (_model === 'page') return ctx.SUCCESS({})
|
|
213
|
-
if (is_cache && app.cache && app.cache.get(`${_model}-table`)) {
|
|
214
|
-
return ctx.SUCCESS(app.cache.get(`${_model}-table`))
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
const model = app.config[_model].model || _model
|
|
218
|
-
|
|
219
|
-
const _result = await app.sequelize.getQueryInterface().describeTable(model)
|
|
220
|
-
|
|
221
|
-
const modelAttributes = lodash.cloneDeep(app.attributes[model])
|
|
222
|
-
const result = lodash.pick(_result, [
|
|
223
|
-
...Object.keys(modelAttributes),
|
|
224
|
-
'id',
|
|
225
|
-
'createdid',
|
|
226
|
-
'updated_at',
|
|
227
|
-
'created_at',
|
|
228
|
-
])
|
|
229
|
-
|
|
230
|
-
const obj = {}
|
|
231
|
-
if (show_virtual) {
|
|
232
|
-
Array.from(
|
|
233
|
-
new Set([...Object.keys(result), ...Object.keys(modelAttributes)])
|
|
234
|
-
).forEach((attr) => {
|
|
235
|
-
obj[attr] = lodash.merge(
|
|
236
|
-
modelAttributes[attr],
|
|
237
|
-
lodash.omitBy(result[attr], lodash.isNull),
|
|
238
|
-
app.config[_model].comment && app.config[_model].comment
|
|
239
|
-
? app.config[_model].comment[attr]
|
|
240
|
-
: {}
|
|
241
|
-
)
|
|
242
|
-
})
|
|
243
|
-
} else {
|
|
244
|
-
Object.keys(result).forEach((attr) => {
|
|
245
|
-
obj[attr] = lodash.merge(
|
|
246
|
-
modelAttributes[attr],
|
|
247
|
-
lodash.omitBy(result[attr], lodash.isNull),
|
|
248
|
-
app.config[_model].comment && app.config[_model].comment
|
|
249
|
-
? app.config[_model].comment[attr]
|
|
250
|
-
: {}
|
|
251
|
-
)
|
|
252
|
-
})
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
app.cache && app.cache.set(`${_model}-table`, obj)
|
|
256
|
-
return ctx.SUCCESS(obj)
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
exports.dropModel = async (ctx) => {
|
|
260
|
-
const { app } = getAppByCtx(ctx)
|
|
261
|
-
|
|
262
|
-
const { model } = ctx.request.body
|
|
263
|
-
const result = await app.model[model].drop()
|
|
264
|
-
return ctx.SUCCESS(result)
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
exports.getImage = async (ctx) => {
|
|
268
|
-
const { app } = getAppByCtx(ctx)
|
|
269
|
-
|
|
270
|
-
const appConfig = getConfig(app)
|
|
271
|
-
|
|
272
|
-
const { site_host } = await appConfig.getObject('base')
|
|
273
|
-
// const { dir, upload } = app.config.static;
|
|
274
|
-
const targetDir = `${path.resolve(
|
|
275
|
-
__dirname,
|
|
276
|
-
`${process.cwd()}/public/upload`
|
|
277
|
-
)}`
|
|
278
|
-
const files = await fsPromise.readdir(targetDir)
|
|
279
|
-
const images = files.filter((file) =>
|
|
280
|
-
/\w(\.gif|\.jpeg|\.png|\.jpg|\.bmp)/i.test(file)
|
|
281
|
-
)
|
|
282
|
-
const result = []
|
|
283
|
-
for (let i = 0; i < images.length; i++) {
|
|
284
|
-
const image = `${path.resolve(
|
|
285
|
-
__dirname,
|
|
286
|
-
`${process.cwd()}/public/upload/${images[i]}`
|
|
287
|
-
)}`
|
|
288
|
-
|
|
289
|
-
result.push({
|
|
290
|
-
img: `http://${site_host || 'api.kuashou.com'}/upload/${images[i]}`,
|
|
291
|
-
name: images[i],
|
|
292
|
-
...fs.statSync(image),
|
|
293
|
-
})
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
ctx.SUCCESS(result)
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
exports.mock = async (ctx) => {
|
|
300
|
-
const { app } = getAppByCtx(ctx)
|
|
301
|
-
|
|
302
|
-
const { model } = ctx.request.body
|
|
303
|
-
const result = app.mock && app.mock[model] && app.mock[model]()
|
|
304
|
-
return ctx.SUCCESS(!lodash.isEmpty(result))
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
exports.table = async (ctx) => {
|
|
308
|
-
const { app } = getAppByCtx(ctx)
|
|
309
|
-
const { model } = ctx.request.body
|
|
310
|
-
if (!model) {
|
|
311
|
-
const result = Object.keys(app.model).map((m) => ({
|
|
312
|
-
[m]: lodash.mapValues(app.attributes[m], (item) => item.comment),
|
|
313
|
-
}))
|
|
314
|
-
ctx.SUCCESS(result)
|
|
315
|
-
} else {
|
|
316
|
-
const result = app.attributes[model]
|
|
317
|
-
ctx.SUCCESS(result)
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
exports.showAllSchemas = async (ctx) => {
|
|
322
|
-
const { app } = getAppByCtx(ctx)
|
|
323
|
-
|
|
324
|
-
const { model } = ctx.request.body
|
|
325
|
-
const result = await app.sequelize.getQueryInterface().describeTable(model)
|
|
326
|
-
return ctx.SUCCESS(result)
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
exports.initDataWithCache = exports.initData = async (ctx) => {
|
|
330
|
-
const { app } = getAppByCtx(ctx)
|
|
331
|
-
const appConfig = getConfig(app)
|
|
332
|
-
|
|
333
|
-
const { is_cache } = await appConfig.getObject('base')
|
|
334
|
-
const { includes, excludes = [] } = ctx.request.body
|
|
335
|
-
|
|
336
|
-
const cacheTarget = JSON.stringify(ctx.request.body)
|
|
337
|
-
|
|
338
|
-
let result = null
|
|
339
|
-
if (is_cache && cache.get(cacheTarget)) {
|
|
340
|
-
result = cache.get(cacheTarget)
|
|
341
|
-
} else {
|
|
342
|
-
result = await app.service.system.initData({
|
|
343
|
-
app,
|
|
344
|
-
includes,
|
|
345
|
-
excludes,
|
|
346
|
-
ctx,
|
|
347
|
-
})
|
|
348
|
-
if (is_cache && cacheTarget.includes('setting/findAll')) {
|
|
349
|
-
cache.set(cacheTarget, result)
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
return ctx.SUCCESS(result)
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
exports.recentlyError = async (ctx) => {
|
|
356
|
-
// const {
|
|
357
|
-
// app,
|
|
358
|
-
// appName,
|
|
359
|
-
// } = getAppByCtx(ctx);
|
|
360
|
-
|
|
361
|
-
if (ctx.ws) {
|
|
362
|
-
const ws = await ctx.ws()
|
|
363
|
-
const send = (data) => ws.send(JSON.stringify(data))
|
|
364
|
-
ctx.app.on('error', async (err) => {
|
|
365
|
-
console.log(err)
|
|
366
|
-
try {
|
|
367
|
-
send({
|
|
368
|
-
type: 'error',
|
|
369
|
-
payload: err ? err.errorData : {},
|
|
370
|
-
})
|
|
371
|
-
} catch (e) {
|
|
372
|
-
console.log(e)
|
|
373
|
-
}
|
|
374
|
-
})
|
|
375
|
-
} else {
|
|
376
|
-
throw new Error('这是一个websocket url')
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
exports.sandbox = async (ctx) => {
|
|
381
|
-
const code = `
|
|
382
|
-
module.exports = async (callback)=>{
|
|
383
|
-
callback({
|
|
384
|
-
name:1
|
|
385
|
-
})
|
|
386
|
-
}
|
|
387
|
-
`
|
|
388
|
-
// const { code } = ctx.request.body;
|
|
389
|
-
const vm = new VM({
|
|
390
|
-
sandbox: {
|
|
391
|
-
axios,
|
|
392
|
-
cheerio,
|
|
393
|
-
module,
|
|
394
|
-
setTimeout,
|
|
395
|
-
sleep: (time) =>
|
|
396
|
-
new Promise((resolve) => setTimeout(resolve, time * 1000)),
|
|
397
|
-
},
|
|
398
|
-
})
|
|
399
|
-
vm.run(code)(ctx.SUCCESS)
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
exports.onlineChat = async (ctx) => {
|
|
403
|
-
if (ctx.ws) {
|
|
404
|
-
const ws = await ctx.ws()
|
|
405
|
-
const send = (data) => ws.send(JSON.stringify(data))
|
|
406
|
-
ws.on('message', async (code) => {
|
|
407
|
-
const vm = new VM({
|
|
408
|
-
sandbox: {
|
|
409
|
-
send: (data) => ws.send(JSON.stringify(data)),
|
|
410
|
-
axios,
|
|
411
|
-
cheerio,
|
|
412
|
-
},
|
|
413
|
-
})
|
|
414
|
-
vm.run(code)
|
|
415
|
-
const unhandledRejections = {}
|
|
416
|
-
|
|
417
|
-
process.on('unhandledRejection', (reason) => {
|
|
418
|
-
if (unhandledRejections[reason]) {
|
|
419
|
-
console.log('====?', reason)
|
|
420
|
-
} else {
|
|
421
|
-
send({
|
|
422
|
-
type: 'error',
|
|
423
|
-
payload: `${reason}`,
|
|
424
|
-
})
|
|
425
|
-
unhandledRejections[reason] = true
|
|
426
|
-
}
|
|
427
|
-
})
|
|
428
|
-
})
|
|
429
|
-
} else {
|
|
430
|
-
throw new Error('这是一个websocket url')
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
exports.chat = async (ctx) => {
|
|
435
|
-
const { app } = getAppByCtx(ctx)
|
|
436
|
-
if (ctx.ws) {
|
|
437
|
-
const ws = await ctx.ws()
|
|
438
|
-
const send = (data) => ws.send(JSON.stringify(data))
|
|
439
|
-
ws.on('message', async (payload) => {
|
|
440
|
-
const message = JSON.parse(payload)
|
|
441
|
-
const result = await app.model.ask.create({
|
|
442
|
-
user_id: message.payload.user_id,
|
|
443
|
-
content: message.payload.content,
|
|
444
|
-
is_ask: Boolean(message.payload.user_id),
|
|
445
|
-
})
|
|
446
|
-
|
|
447
|
-
send({
|
|
448
|
-
type: 'message',
|
|
449
|
-
payload: result,
|
|
450
|
-
})
|
|
451
|
-
})
|
|
452
|
-
} else {
|
|
453
|
-
throw new Error('这是一个websocket url')
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
exports.task = async (ctx) => {
|
|
458
|
-
const { app } = getAppByCtx(ctx)
|
|
459
|
-
|
|
460
|
-
const { task } = ctx.request.body
|
|
461
|
-
|
|
462
|
-
if (app.task && app.task[task]) {
|
|
463
|
-
await app.task[task](app)
|
|
464
|
-
}
|
|
465
|
-
ctx.SUCCESS(task)
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
exports.dashboard = async (ctx) => {
|
|
469
|
-
// const {
|
|
470
|
-
// app,
|
|
471
|
-
// } = getAppByCtx(ctx);
|
|
472
|
-
|
|
473
|
-
const result = [
|
|
474
|
-
// {
|
|
475
|
-
// name: '用户',
|
|
476
|
-
// number: 5,
|
|
477
|
-
// route: 'page/user'
|
|
478
|
-
// }
|
|
479
|
-
]
|
|
480
|
-
ctx.SUCCESS(result)
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
exports.showAllModel = async (ctx) => {
|
|
484
|
-
const { app, appName } = getAppByCtx(ctx)
|
|
485
|
-
const { is_count = false } = ctx.request.body
|
|
486
|
-
const result = await app.sequelize.showAllSchemas()
|
|
487
|
-
const list = result.map(
|
|
488
|
-
(item) => item[`Tables_in_${app.sequelize.config.database}`]
|
|
489
|
-
)
|
|
490
|
-
let tableList = []
|
|
491
|
-
for (let i = 0; i < list.length; i++) {
|
|
492
|
-
if (!app.model[list[i]]) continue
|
|
493
|
-
const defaultConfigPath = path.resolve(__dirname, `../${list[i]}/config.js`)
|
|
494
|
-
const defaultConfigExist = fs.existsSync(defaultConfigPath)
|
|
495
|
-
const target =
|
|
496
|
-
app.config[list[i]] &&
|
|
497
|
-
Object.keys(app.config[list[i]]).length === 0 &&
|
|
498
|
-
defaultConfigExist
|
|
499
|
-
? require(defaultConfigPath)
|
|
500
|
-
: app.config[list[i]]
|
|
501
|
-
let count = 0
|
|
502
|
-
if (is_count) {
|
|
503
|
-
count = await app.model[list[i]].count()
|
|
504
|
-
}
|
|
505
|
-
tableList = [
|
|
506
|
-
...tableList,
|
|
507
|
-
{
|
|
508
|
-
count,
|
|
509
|
-
name: target.name,
|
|
510
|
-
model: list[i],
|
|
511
|
-
count,
|
|
512
|
-
},
|
|
513
|
-
]
|
|
514
|
-
}
|
|
515
|
-
return ctx.SUCCESS(
|
|
516
|
-
tableList.map((item) => lodash.omit(item, is_count ? [] : ['count']))
|
|
517
|
-
)
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
exports.drop = async (ctx) => {
|
|
521
|
-
const { app, appName } = getAppByCtx(ctx)
|
|
522
|
-
|
|
523
|
-
const result = await app.sequelize.showAllSchemas()
|
|
524
|
-
const list = result.map(
|
|
525
|
-
(item) => item[`Tables_in_${app.sequelize.config.database}`]
|
|
526
|
-
)
|
|
527
|
-
const filterList = list.filter((item) => !app.model[item])
|
|
528
|
-
await Promise.all(
|
|
529
|
-
filterList.map((folder) => {
|
|
530
|
-
const model = app.sequelize.define(
|
|
531
|
-
folder,
|
|
532
|
-
{
|
|
533
|
-
id: {
|
|
534
|
-
type: Sequelize.INTEGER(11),
|
|
535
|
-
allowNull: false,
|
|
536
|
-
primaryKey: true,
|
|
537
|
-
autoIncrement: true,
|
|
538
|
-
},
|
|
539
|
-
},
|
|
540
|
-
{
|
|
541
|
-
freezeTableName: true,
|
|
542
|
-
comment: folder,
|
|
543
|
-
createdAt: 'created_at',
|
|
544
|
-
updatedAt: 'updated_at',
|
|
545
|
-
name: {
|
|
546
|
-
singular: folder,
|
|
547
|
-
plural: `${folder}s`,
|
|
548
|
-
},
|
|
549
|
-
}
|
|
550
|
-
)
|
|
551
|
-
return model.drop()
|
|
552
|
-
})
|
|
553
|
-
)
|
|
554
|
-
|
|
555
|
-
ctx.SUCCESS(`删除${filterList.length}个无用表`)
|
|
556
|
-
}
|
|
557
|
-
|
|
558
|
-
exports.mockPay = async (ctx) => {
|
|
559
|
-
const { app } = getAppByCtx(ctx)
|
|
560
|
-
const { order_id, order_price, prefix, ...rest } = ctx.request.body
|
|
561
|
-
const model = prefix ? `${prefix}_order` : 'order'
|
|
562
|
-
console.log(model, 'model')
|
|
563
|
-
if (!(app.service[model] && app.service[model].notify)) {
|
|
564
|
-
throw new Error(`没配置${model}支付回调notify`)
|
|
565
|
-
}
|
|
566
|
-
await app.service[model].notify({
|
|
567
|
-
app,
|
|
568
|
-
order_id,
|
|
569
|
-
order: model,
|
|
570
|
-
order_price,
|
|
571
|
-
...rest,
|
|
572
|
-
})
|
|
573
|
-
ctx.SUCCESS('ok')
|
|
574
|
-
}
|
|
575
|
-
|
|
576
|
-
exports.copyOther = async (ctx) => {
|
|
577
|
-
const { app, appName } = getAppByCtx(ctx)
|
|
578
|
-
|
|
579
|
-
const { model, source } = ctx.request.body
|
|
580
|
-
if (!model) return ctx.ERROR('model?')
|
|
581
|
-
if (!source) return ctx.ERROR('source? https://www.kuashou.com/kuashou')
|
|
582
|
-
let list = []
|
|
583
|
-
if (Array.isArray(model)) {
|
|
584
|
-
list = model
|
|
585
|
-
} else {
|
|
586
|
-
if (model !== 'all') {
|
|
587
|
-
list = [model]
|
|
588
|
-
} else {
|
|
589
|
-
const result = await app.sequelize.showAllSchemas()
|
|
590
|
-
list = result.map(
|
|
591
|
-
(item) => item[`Tables_in_${app.sequelize.config.database}`]
|
|
592
|
-
)
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
|
|
596
|
-
const prefix = `${source}`
|
|
597
|
-
let successList = []
|
|
598
|
-
let failList = []
|
|
599
|
-
for (let i = 0; i < list.length; i++) {
|
|
600
|
-
const url = `${prefix}/${list[i]}/findAll`
|
|
601
|
-
|
|
602
|
-
const result = await axios
|
|
603
|
-
.post(
|
|
604
|
-
url,
|
|
605
|
-
{},
|
|
606
|
-
{
|
|
607
|
-
headers: {
|
|
608
|
-
'Client-Type': 0,
|
|
609
|
-
},
|
|
610
|
-
}
|
|
611
|
-
)
|
|
612
|
-
.then((res) => res.data)
|
|
613
|
-
if (result.code === 200) {
|
|
614
|
-
await app.model[list[i]].sync({
|
|
615
|
-
force: true,
|
|
616
|
-
})
|
|
617
|
-
try {
|
|
618
|
-
await app.model[list[i]].bulkCreate(
|
|
619
|
-
result.data.map((i) => lodash.omitBy(i, lodash.isNull)),
|
|
620
|
-
{}
|
|
621
|
-
)
|
|
622
|
-
successList = [...successList, list[i]]
|
|
623
|
-
} catch (e) {
|
|
624
|
-
console.log(e.message)
|
|
625
|
-
failList = [...failList, list[i]]
|
|
626
|
-
}
|
|
627
|
-
} else {
|
|
628
|
-
console.log(url, result.data)
|
|
629
|
-
failList = [...failList, list[i]]
|
|
630
|
-
continue
|
|
631
|
-
}
|
|
632
|
-
}
|
|
633
|
-
ctx.SUCCESS({ successList, failList })
|
|
634
47
|
}
|
|
@@ -3,9 +3,12 @@ const { Validator, getAppByCtx } = require('q-koa')
|
|
|
3
3
|
exports.initModel = async (ctx) => {
|
|
4
4
|
const params = {
|
|
5
5
|
model: {
|
|
6
|
-
type: 'string',
|
|
7
6
|
required: true,
|
|
8
7
|
message: '请输入model',
|
|
8
|
+
validator: (rule, value) =>
|
|
9
|
+
typeof value === 'string' || Array.isArray(value)
|
|
10
|
+
? Promise.resolve()
|
|
11
|
+
: Promise.reject(new Error('string | array')),
|
|
9
12
|
},
|
|
10
13
|
}
|
|
11
14
|
await new Validator(params).validate(ctx.request.body)
|
|
@@ -22,7 +25,7 @@ exports.showAllSchemas = async (ctx) => {
|
|
|
22
25
|
validator: (rule, value) =>
|
|
23
26
|
app[appName].model && app[appName].model[value]
|
|
24
27
|
? Promise.resolve()
|
|
25
|
-
: Promise.reject('没有这个模型'),
|
|
28
|
+
: Promise.reject(new Error('没有这个模型')),
|
|
26
29
|
},
|
|
27
30
|
}
|
|
28
31
|
await new Validator(params).validate(ctx.request.body)
|
|
@@ -39,7 +42,7 @@ exports.dropModel = async (ctx) => {
|
|
|
39
42
|
validator: (rule, value) =>
|
|
40
43
|
app[appName].model && app[appName].model[value]
|
|
41
44
|
? Promise.resolve()
|
|
42
|
-
: Promise.reject('没有这个模型'),
|
|
45
|
+
: Promise.reject(new Error('没有这个模型')),
|
|
43
46
|
},
|
|
44
47
|
}
|
|
45
48
|
await new Validator(params).validate(ctx.request.body)
|