Skip to content

Commit

Permalink
people follow finish
Browse files Browse the repository at this point in the history
  • Loading branch information
tuwq committed May 20, 2018
1 parent 20af105 commit a79e7e4
Show file tree
Hide file tree
Showing 20 changed files with 277 additions and 172 deletions.
116 changes: 70 additions & 46 deletions server/router/controller/follow.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var Answer = mongoose.model('Answer')
const checkUtil = require('../../common/checkUtil.js')
const tokenUtil = require('../../common/token.js')

const self = this

exports.followTarget = function (req,res) {
// 检查登录
Expand Down Expand Up @@ -119,77 +120,99 @@ exports.getUserBind = function(target_id,me_id,callback) {
}

exports.userFans = function(req,res) {
let fields = req.body
User.findById(fields.detail_id)
.select('avatar username')
.populate('fans')
.exec((err,user)=> {
// 所有粉丝对象
let fans = user.fans
// 寻找每个粉丝的回答数和关注列表人数
getFansData(fans,(infos)=> {
return res.json(util.Result({users: fans,infos: infos}))
// 检查登录
var token = req.headers.token
tokenUtil.verifyToken(token)
.then((_id)=> {
let fields = req.body
User.findById(fields.detail_id)
.select('avatar username')
.populate('fans')
.exec((err,user)=> {
// 所有粉丝对象
let fans = user.fans
// 寻找每个粉丝的回答数和关注列表人数
getFansData(fans,_id,(infos)=> {
return res.json(util.Result({users: fans,infos: infos}))
})
})
}).catch((err)=> {
return res.json(util.Result(1))
})
}

function getFansData(fans,callback) {
function getFansData(fans,_id,callback) {
var infos = [];
(function iterator(i){
if ( i == fans.length ) {
return callback(infos)
}
Question.count({user_id: fans[i]._id})
.exec((err,questionSum)=> {
infos.push(new FansInfo({
questionSum: questionSum,
followSum: fans[i].followers.length
}))
iterator( i+1 )
// 当前用户是否关注过该粉丝列表中的用户
self.getUserBind(fans[i]._id,_id,(flowerStatus)=> {
Answer.count({user_id: fans[i]._id})
.exec((err,answerSum)=> {
infos.push(new FansInfo({
answerSum: answerSum,
followSum: fans[i].followers.length,
flowerStatus
}))
iterator( i+1 )
})
})
})(0)
}

function FansInfo({questionSum,followSum}) {
this.questionSum = questionSum
function FansInfo({answerSum,followSum,flowerStatus}) {
this.answerSum = answerSum
this.followSum = followSum
this.flowerStatus = flowerStatus
}

exports.userFollow = function (req,res) {
let fields = req.body
User.findById(fields.detail_id)
.populate({
path: 'followers',
select: 'avatar username info'
})
.exec((err,user)=> {
// 获得用户关注列表信息
let followers = user.followers
getFollowsData(followers,(infos)=> {
return res.json(util.Result({users: followers,infos}))
var token = req.headers.token
tokenUtil.verifyToken(token)
.then((_id)=> {
let fields = req.body
User.findById(fields.detail_id)
.populate({
path: 'followers',
select: '_id avatar username info'
})
.exec((err,user)=> {
// 获得用户关注列表信息
let followers = user.followers
getFollowsData(followers,_id,(infos)=> {
return res.json(util.Result({users: followers,infos}))
})
})
}).catch((err)=> {
return res.json(util.Result(1))
})
}

function getFollowsData (followers,callback) {
function getFollowsData (followers,_id,callback) {
let infos = [];
(function iterator(i){
if (i==followers.length) {
return callback(infos)
}
// 查询回答数,提问数,被回答数
Answer.count({user_id: followers[i]._id},(err,answerSum)=> {
Question.find({user_id: followers[i]._id})
.select('_id')
.exec((err,questions)=> {
// 遍历问题,寻找每个问题下有多少个回答
getAnsweredSum(questions,(answeredSum)=> {
infos.push(new FollowerInfo({
answerSum,
questionSum: questions.length,
answeredSum
}))
iterator( i+1 )
// 当前用户是否关注过该关注列表中的用户
self.getUserBind(followers[i]._id,_id,(flowerStatus)=> {
// 查询回答数,提问数,被回答数
Answer.count({user_id: followers[i]._id},(err,answerSum)=> {
Question.find({user_id: followers[i]._id})
.select('_id')
.exec((err,questions)=> {
// 遍历问题,寻找每个问题下有多少个回答
getAnsweredSum(questions,(answeredSum)=> {
infos.push(new FollowerInfo({
answerSum,
questionSum: questions.length,
answeredSum,
flowerStatus
}))
iterator( i+1 )
})
})
})
})
Expand All @@ -209,8 +232,9 @@ function getAnsweredSum(questions,callback) {
})(0)
}

function FollowerInfo({answerSum,questionSum,answeredSum}) {
function FollowerInfo({answerSum,questionSum,answeredSum,flowerStatus}) {
this.answerSum = answerSum
this.questionSum = questionSum
this.answeredSum = answeredSum
this.flowerStatus = flowerStatus
}
23 changes: 10 additions & 13 deletions src/pages/Detail/components/attention.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@
关注{{item.info.gender==0?'他':item.info.gender==1?'他':'她'}}
</button>
<button class="follwButton cancel" ref="cancel" v-show="item.followStatus==1"
@click.stop.prevent="followPeople(item._id,0,index)">
{{followText}}
@click.stop.prevent="followPeople(item._id,0,index)"
@mouseenter="enter($event)" @mouseleave="leave($event)">
已关注
</button>
</div>
</div>
Expand Down Expand Up @@ -68,11 +69,16 @@ import axios from 'axios'
base: '../../../../static/avatar/60/',
loading: true,
users: [],
sum: 0,
followText: '已关注'
sum: 0
}
},
methods: {
enter(e) {
e.target.innerText = '取消关注'
},
leave(e) {
e.target.innerText = '已关注'
},
clsModal() {
this.setAttentionQuestionModal(false)
},
Expand Down Expand Up @@ -113,15 +119,6 @@ import axios from 'axios'
this.sum = res.data.result.sum
this.users = this.mergeData(res.data.result.users,res.data.result.infos)
this.loading = false
this.$nextTick(()=> {
$(this.$refs.cancel).each((index,item)=> {
$(item).hover(()=> {
$(item).text('取消关注')
},()=> {
$(item).text('已关注')
})
})
})
})
})
},
Expand Down
24 changes: 8 additions & 16 deletions src/pages/Detail/components/scroll-header.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
@click.stop.prevent="attention(question._id,1)">关注问题</button>
<button class="attention cancel" v-show="attentionStatus==1" type="button"
@click.stop.prevent="attention(question._id,0)"
style="color: #fff;background-color: #8590a6;">{{attentionText}}</button>
style="color: #fff;background-color: #8590a6;"
@mouseenter="enter($event)" @mouseleave="leave($event)">已关注</button>
<button class="invite" type="button"><svg viewBox="0 0 12 12" width="14" height="16">
<title></title><g><path d="M.423 10.32L0 12l1.667-.474 1.55-.44-2.4-2.33-.394 1.564zM10.153.233c-.327-.318-.85-.31-1.17.018l-.793.817 2.49 2.414.792-.814c.318-.328.312-.852-.017-1.17l-1.3-1.263zM3.84 10.536L1.35 8.122l6.265-6.46 2.49 2.414-6.265 6.46z"></path></g>
</svg>写回答</button>
Expand All @@ -30,18 +31,19 @@ import axios from 'axios'
default: 0
}
},
data() {
return {
attentionText: '已关注'
}
},
computed: {
...mapGetters([
'question',
'user'
])
},
methods: {
enter(e) {
e.target.innerText = '取消关注'
},
leave(e) {
e.target.innerText = '已关注'
},
attention(question_id,status) {
axios.post('/attention/question/add',{
user_id: this.user._id,
Expand All @@ -50,17 +52,7 @@ import axios from 'axios'
}).then((res)=> {
this.$emit('changeAttention',res.data.status==1?1:0)
})
},
initClick() {
$('.cancel').hover(() => {
this.attentionText = '取消关注'
},() => {
this.attentionText = '已关注'
})
}
},
mounted() {
this.initClick()
}
}
</script>
Expand Down
Loading

0 comments on commit a79e7e4

Please sign in to comment.