friend.js
3.34 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
/**
* Created by Tommy Huang on 18/04/10.
*/
const config = require('config-lite')({
config_basedir: __dirname,
config_dir: 'config'
})
const Helper = require('./helper')
const _ = require('lodash')
const Op = require('sequelize').Op
const User = require('../models').User
const BorrowList = require('../models').BorrowList
const Friendship = require('../models').Friendship
exports.getList = async function (req, res) {
try {
const idFromToken = req.user.userId
const userId = req.query.userId
const search = req.query.search || ''
if (!userId) throw new Error('参数错误')
if (idFromToken !== userId) throw new Error('token check fail')
let friendships = await Friendship.findAll({where: {userId: userId}})
let friendIds = _.map(friendships, 'friend_id')
let list = await User.findAll({
where: {
id: friendIds,
name: {
[Op.like]: `%${search}%`
}
},
attributes: ['id', 'name', 'headimgurl'],
})
const count = list.length
res.json({
success: 1,
count: count,
list: list
})
} catch (e) {
console.log(e)
res.json({
success: 0,
msg: `获取好友失败:${e.message}`
})
}
}
exports.getFriendInfo = async function (req, res) {
try {
const idFromToken = req.user.userId
const userId = req.query.userId
const id = req.query.id
if (!userId) throw new Error('参数错误')
if (idFromToken !== userId) throw new Error('token check fail')
const [friendship, friend] = await Promise.all([
Friendship.findOne({where: {userId: userId, friendId: id}}),
User.findOne({
where: {id: id},
attributes: ['id', 'name', 'headimgurl', 'phone'],
})
])
if (!friendship || !friend) throw new Error('好友不存在')
const [borrow, credit] = await Promise.all([
BorrowList.findAll({where:{
borrowerId: friend.id,
deleted: false,
status: ['已生效']
}}),
BorrowList.findAll({where:{
creditorId: friend.id,
deleted: false,
status: ['已生效']
}}),
])
const borrowInfo = buildBorrowRecordStatistics(borrow, 'borrow')
const creditInfo = buildBorrowRecordStatistics(credit, 'credit')
res.json({
success: 1,
user: friend,
borrowInfo: borrowInfo,
creditInfo: creditInfo
})
} catch (e) {
console.log(e)
res.json({
success: 0,
msg: `获取好友信息失败:${e.message}`
})
}
}
/**
*
* @param {Array} list 历史借条列表
* @param {String} type 借款:'borrow', 出借: ’credit‘
*/
function buildBorrowRecordStatistics(list, type) {
let totalAmount = 0
let times = list.length
let peoples = 0
let inOneDay = 0
let effectiveAmount = 0
let targetIds = []
list.forEach((item) => {
totalAmount += item.amount
if (item.status === '已生效') {effectiveAmount += item.amount}
if (item.startDate === item.endDate) {inOneDay++}
let targetId = type === 'borrow' ? item.creditorId : item.borrowerId
targetIds.push(targetId)
})
targetIds = new Set(targetIds)
peoples = targetIds.size
const inOneDayRate = times === 0 ? 0 : (Math.floor(inOneDay/times * 10000) / 100)
return {
totalAmount: totalAmount,
times: times,
effectiveAmount: effectiveAmount,
inOneDayRate: inOneDayRate,
peoples: peoples
}
}