iou.js
9.85 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
/**
* Created by Tommy Huang on 18/04/02.
*/
const config = require('config-lite')({
config_basedir: __dirname,
config_dir: 'config'
})
const moment = require('moment')
const uuid = require('uuid/v4')
const qr = require('qr-image')
const Helper = require('./helper')
const User = require('../models').User
const BorrowList = require('../models').BorrowList
const Redis = require('../models/redis')
exports.create = async function(req, res) {
try {
const idFromToken = req.user.userId
const id = req.body.userId
const amount = parseInt(req.body.amount) || 0
const role = req.body.role
const target = req.body.target
const start = req.body.start
const end = req.body.end
const rate = req.body.rate
const rateValue = parseFloat(req.body.rateValue)
const usage = req.body.usage
const explanation = req.body.explanation || ''
const imgs = req.files || []
if (!id || !role || !target || !start || !end || !rate || !rateValue || !usage ||!amount) throw new Error('参数错误')
if (idFromToken !== id) throw new Error('token check fail')
if (amount <= 0) throw new Error('非法的借款金额')
if (parseInt(rate.slice(0, rate.length - 1)) !== rateValue * 100 || rateValue < 0.01 || rateValue > 0.24) throw new Error('非法利率参数')
const startDate = moment(start)
const endDate = moment(end)
if (!(startDate.isBefore(endDate))) throw new Erro('还款日期最早为借款日期后一日')
const user = await User.findOne({where: {id: id}})
if (!user) throw new Erro('用户不存在')
let imgSrc = await Promise.all(imgs.map(async (item) => {
let fileName = `${uuid()}.${item.mimetype.split('/')[1]}`
await Helper.writeFile(`${config.img.iouImg}/${fileName}`, item.buffer)
return fileName
}))
imgSrc = imgSrc.join(',')
let newBorrowList = {
id: uuid(),
amount: amount,
startDate: startDate.format(),
endDate: endDate.format(),
rate: rateValue,
usage: usage,
explanation: explanation,
imgs: imgSrc,
targetName: target,
sponsorId: user.id
}
if (role === 'borrower') {
newBorrowList.borrowerId = user.id
} else {
newBorrowList.creditorId = user.id
}
let result = await BorrowList.create(newBorrowList)
res.json({
success: 1,
id: result.id
})
} catch (e) {
console.log(e)
res.json({
success: 0,
msg: `创建失败:${e.message}`
})
}
}
exports.get = async function (req, res) {
try {
const idFromToken = req.user.userId
const userId = req.query.userId
const id = req.query.id
if (!id || !userId) throw new Error('参数错误')
if (idFromToken !== userId) throw new Error('token check fail')
let info = await BorrowList.findOne({
where: {
id: id,
deleted: false
},
include: [{
model: User,
required: false,
as: 'Borrower',
attributes: ['id', 'name', 'username', 'headimgurl']
}, {
model: User,
required: false,
as: 'Creditor',
attributes: ['id', 'name', 'username', 'headimgurl']
}]
})
if (!info) throw new Error('借条不存在')
if (info.status === '未发起' && info.sponsorId !== userId) throw new Error('借条不存在')
res.json({
success: 1,
info: info
})
} catch (e) {
console.log(e)
res.json({
success: 0,
msg: `获取失败:${e.message}`
})
}
}
exports.submitIOU = async function(req, res) {
try {
const idFromToken = req.user.userId
const userId = req.body.userId
const id = req.body.id
const password = req.body.password
if (!id || !userId || !password ) throw new Error('参数错误')
if (idFromToken !== userId) throw new Error('token check fail')
let [user, list] = await Promise.all([
User.findOne({where: {id: userId}}),
BorrowList.findOne({where: {id: id, sponsorId: userId, status: '未发起'}})
])
if (!user) throw new Error('用户不存在')
if (!list) throw new Error('未找到对应的借条')
if (!Helper.passwdCheck(password, user.password)) throw new Error('交易密码不正确')
const shareUrl = Helper.buildShareUrl(list.id)
const qrImg = qr.imageSync(shareUrl, { type: 'svg' })
let update = {
qr: qrImg
}
if (user.id === list.creditorId) {
update.creditorAgree = true
update.status = '待确认'
}
if (user.id === list.borrowerId) {
update.borrowerAgree = true
update.status = '待确认'
}
let result = await list.update(update)
res.json({
success: 1,
})
} catch (e) {
console.log(e)
res.json({
success: 0,
msg: `借条提交失败:${e.message}`
})
}
}
exports.deleteIOU = async function(req, res) {
try {
const idFromToken = req.user.userId
const userId = req.body.userId
const id = req.body.id
const password = req.body.password
if (!id || !userId || !password ) throw new Error('参数错误')
if (idFromToken !== userId) throw new Error('token check fail')
let [user, list] = await Promise.all([
User.findOne({where: {id: userId}}),
BorrowList.findOne({where: {id: id, sponsorId: userId, status: '未发起'}})
])
if (!user) throw new Error('用户不存在')
if (!list) throw new Error('未找到对应的借条')
if (!Helper.passwdCheck(password, user.password)) throw new Error('交易密码不正确')
let result = await list.update({
deleted: true
})
res.json({
success: 1,
})
} catch (e) {
console.log(e)
res.json({
success: 0,
msg: `借条删除失败:${e.message}`
})
}
}
exports.comfirmIOU = async function(req, res) {
try {
const idFromToken = req.user.userId
const userId = req.body.userId
const id = req.body.id
const password = req.body.password
if (!id || !userId || !password ) throw new Error('参数错误')
if (idFromToken !== userId) throw new Error('token check fail')
let [user, list] = await Promise.all([
User.findOne({where: {id: userId}}),
BorrowList.findOne({where: {id: id, status: '待确认'}})
])
if (!user) throw new Error('用户不存在')
if (!list) throw new Error('未找到对应的借条')
if (!Helper.passwdCheck(password, user.password)) throw new Error('交易密码不正确')
if (user.name !== list.targetName) throw new Error('借条信息与您的身份信息不符,无法确认该借条')
let update = {}
if (list.creditorId && list.creditorAgree) {
update.borrowerId = user.id
update.borrowerAgree = true
update.status = '已生效'
}
if (list.borrowerId && list.borrowerAgree) {
update.creditorId = user.id
update.creditorAgree = true
update.status = '已生效'
}
let result = await list.update(update)
res.json({
success: 1,
})
} catch (e) {
console.log(e)
res.json({
success: 0,
msg: `借条确认失败:${e.message}`
})
}
}
exports.deleteIOU = async function(req, res) {
try {
const idFromToken = req.user.userId
const userId = req.body.userId
const id = req.body.id
const password = req.body.password
if (!id || !userId || !password ) throw new Error('参数错误')
if (idFromToken !== userId) throw new Error('token check fail')
let [user, list] = await Promise.all([
User.findOne({where: {id: userId}}),
BorrowList.findOne({where: {id: id, sponsorId: userId, status: '未发起'}})
])
if (!user) throw new Error('用户不存在')
if (!list) throw new Error('未找到对应的借条')
if (!Helper.passwdCheck(password, user.password)) throw new Error('交易密码不正确')
let result = await list.update({
deleted: true
})
res.json({
success: 1,
})
} catch (e) {
console.log(e)
res.json({
success: 0,
msg: `借条删除失败:${e.message}`
})
}
}
exports.rejectIOU = async function(req, res) {
try {
const idFromToken = req.user.userId
const userId = req.body.userId
const id = req.body.id
const password = req.body.password
if (!id || !userId || !password ) throw new Error('参数错误')
if (idFromToken !== userId) throw new Error('token check fail')
let [user, list] = await Promise.all([
User.findOne({where: {id: userId}}),
BorrowList.findOne({where: {id: id, sponsorId: userId, status: '待确认'}})
])
if (!user) throw new Error('用户不存在')
if (!list) throw new Error('未找到对应的借条')
if (!Helper.passwdCheck(password, user.password)) throw new Error('交易密码不正确')
if (user.name !== list.targetName) throw new Error('借条信息与您的身份信息不符,无法驳回该借条')
let result = await list.update({
status: '已驳回'
})
res.json({
success: 1,
})
} catch (e) {
console.log(e)
res.json({
success: 0,
msg: `借条删除失败:${e.message}`
})
}
}
exports.shareInfo = async function(req, res) {
try {
const idFromToken = req.user.userId
const userId = req.query.userId
const id = req.query.id
if (!id || !userId) throw new Error('未找到对应的借条')
if (idFromToken !== userId) throw new Error('token check fail')
let list = await BorrowList.findOne({
where: {
id: id,
sponsorId: userId,
status: '待确认'
}
})
if (!list) throw new Error('未找到对应的借条')
const shareUrl = Helper.buildShareUrl(list.id)
let qrImg = list.qr
if (!qrImg) {
qrImg = qr.imageSync(shareUrl, { type: 'svg' })
await list.update({qr: qrImg})
}
res.json({
success: 1,
img: qrImg,
url: shareUrl
})
} catch (e) {
console.log(e)
res.json({
success: 0,
msg: `${e.message}`
})
}
}