upload.js
3.8 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
const router = require('koa-router')()
const fs = require('fs');
var status = require('../util/resTemplate')
var mu = require('../util/multerUtil')
const os = require('os');
const path = require('path');
const multer = require('koa-multer');
const moment = require('moment')
var body = require('koa-better-body')
var formParse = require('co-busboy')
const uuid = require('../util/UuidUtil');
const attachmentModel =require( '../model/attachmentModel')
const applacation = require('../applacation');
const tencentRecordInfoModel =require( '../model/tencentRecordInfoModel')
router.prefix('/upload');
var uploaddemo=mu.single('file');
router.post('/upLoadDoc',uploaddemo, async (ctx, next) => {
try {
const businessId = ctx.query.businessId;
const category = ctx.query.category;
const siteId = ctx.query.siteId;
let w = await fs.rename(applacation.uploade+ctx.req.file.filename,applacation.uploade+ctx.req.file.originalname)
let path = 'http://localhost:10000/'+ctx.req.file.originalname
attachmentData = {
id:uuid.db32(),
businessId:businessId,
category:category,
path:path,
siteId:siteId
}
let oldAttachment = await attachmentModel.findOne({where:{businessId:businessId,category:1}})
if(category==2){
let oldAttachment2 = await attachmentModel.findAll({where:{category:2,siteId:siteId}})
if(oldAttachment2.length>5){
throw new Error('最多上传5张')
}else {
let oldAttachment2one = await attachmentModel.findOne({where:{businessId:businessId,category:2,siteId:siteId}})
if(oldAttachment2one){
let oldAttachment = await attachmentModel.update({path:path},{where:{id:oldAttachment2one.id}})
}else{
let attachment = await attachmentModel.create(attachmentData)
}
}
}else if(oldAttachment){
let oldAttachment = await attachmentModel.update({path:path},{where:{businessId:businessId,category:1}})
}else{
let attachment = await attachmentModel.create(attachmentData)
}
ctx.response.status= 200;
ctx.body = {
code:200,
fileUrl:path
}
} catch (error) {
console.log(error)
ctx.response.status= 400;
ctx.body = {code:400,msg:error}
}
})
router.post('/getImgBySiteId',async(ctx, next)=>{
try{
let img = await attachmentModel.findAll({
where:{
siteId:ctx.request.body.siteId,
category:2
}
});
status.successTemp(ctx,200,img);
}catch(e){
console.log(e)
status.catchError(ctx,400,e.message);
}
})
router.post('/getImgBySiteId',async(ctx, next)=>{
try{
let img = await attachmentModel.findAll({
where:{
siteId:ctx.request.body.siteId,
category:2
}
});
status.successTemp(ctx,200,img);
}catch(e){
console.log(e)
status.catchError(ctx,400,e.message);
}
})
router.post('/tgpCallback', async (ctx, next) => {
//let s = await uploaddemo(ctx.req,ctx.res,next)
//let w = await fs.rename('/Users/fzy/project/koa2_Sequelize_project/uploads/'+'19543f72d9682d0bedf67fc98da4597b','/Users/fzy/project/koa2_Sequelize_project/uploads/'+'www.jpg')
try {
//let f = await fs.appendFileSync(path.join(__dirname, 'tgp.txt'),JSON.stringify(ctx.request.body)+'/n')
var body = ctx.request.body
let id = uuid.db32()
let stream = body.stream_id.split('_');
let info = {
id:id,
channelId:stream[0]+'_'+stream[1],
recordInfo:JSON.stringify(body),
createTime:JSON.stringify( new Date())
}
let tencentRecordInfo = await tencentRecordInfoModel.create(info)
ctx.response.status= 200;
ctx.body = { "code":0 }
} catch (error) {
console.log(error)
ctx.response.status= 400;
ctx.body = {}
}
})
module.exports = router