/app/controller/moment.js
'use strict';
const Controller = require('egg').Controller;
class MomentController extends Controller {
// 发布朋友圈
async create() {
const { ctx, app } = this;
let current_user_id = ctx.authUser.id;
// 参数验证
ctx.validate({
content: {
type: 'string',
required: false,
desc: '内容'
},
image: {
type: 'string',
required: false,
desc: '图片'
},
video: {
type: 'string',
required: false,
desc: '视频'
},
type: {
type: 'string',
required: true,
range: {
in: ['content', 'image', 'video']
},
desc: '朋友圈类型'
},
location: {
type: 'string',
required: false,
desc: '位置'
},
remind: {
type: 'string',
required: false,
defValue: "",
desc: '提醒谁看'
},
see: {
type: 'string',
required: false,
defValue: "all",
desc: '谁可以看'
}
});
let { content, image, video, type, location, remind, see } = ctx.request.body;
if (!ctx.request.body[type]) {
return ctx.apiFail(`${type} 不能为空`);
}
let moment = await app.model.Moment.create({
content, image, video, location, remind, see,
user_id: current_user_id
});
if (!moment) {
return ctx.apiFail('发布失败');
}
// 推送到好友的时间轴
this.toTimeline(moment);
ctx.apiSuccess('ok');
}
// 推送到好友的时间轴
async toTimeline(moment) {
const { ctx, app } = this;
let current_user_id = ctx.authUser.id;
// 获取当前用户所有好友
let friends = await app.model.Friend.findAll({
where: {
user_id: current_user_id,
isblack: 0
},
attributes: ['friend_id']
});
// 谁可以看
/**
all 全部人可看
only:1,2,3 指定人可见
except:1,2,3 谁不可看
none 仅自己可见
*/
let sees = moment.see.split(':');
let o = {
only: [],
except: []
}
let oType = sees[0];
if ((sees[0] === 'only' || sees[0] === 'except') && sees[1]) {
o[sees[0]] = (sees[1].split(',')).map(v => parseInt(v));
}
let addData = friends.filter(item => {
return oType === 'all' || (oType === 'only' && o.only.includes(item.friend_id)) || (oType === 'except' && !o.except.includes(item.friend_id));
});
addData = addData.map(item => {
return {
user_id: item.friend_id,
moment_id: moment.id,
own: 0
}
});
addData.push({
user_id: current_user_id,
moment_id: moment.id,
own: 1
});
// 推送到时间轴当中
await app.model.MomentTimeline.bulkCreate(addData);
// 消息推送
let message = {
avatar: ctx.authUser.avatar,
user_id: current_user_id,
type: "new"
}
addData.forEach(item => {
ctx.sendAndSaveMessage(item.user_id, message, 'moment');
});
// 提醒用户
if (moment.remind) {
let arr = moment.remind.split(',');
arr.forEach(user_id => {
ctx.sendAndSaveMessage(user_id, {
avatar: ctx.authUser.avatar,
user_id: current_user_id,
type: "remind"
}, 'moment');
});
}
}
// 点赞
async like() {
const { ctx, app } = this;
let current_user_id = ctx.authUser.id;
ctx.validate({
id: {
type: "int",
required: true,
desc: "朋友圈id"
}
});
let { id } = ctx.request.body;
let MomentTimeline = await app.model.MomentTimeline.findOne({
where: {
user_id: current_user_id,
moment_id: id
},
include: [{
model: app.model.Moment,
attributes: ['user_id'],
include: [{
model: app.model.MomentLike,
attributes: ['user_id'],
}]
}]
});
if (!MomentTimeline) {
return ctx.apiFail('朋友圈消息不存在');
}
let like = await app.model.MomentLike.findOne({
where: {
user_id: current_user_id,
moment_id: id
}
});
let message = {
avatar: ctx.authUser.avatar,
user_id: current_user_id,
type: "like"
}
if (like) {
await like.destroy();
ctx.apiSuccess(MomentTimeline.moment.moment_likes);
} else {
await app.model.MomentLike.create({
user_id: current_user_id,
moment_id: id
});
ctx.apiSuccess(MomentTimeline.moment.moment_likes);
}
// 通知作者
if (MomentTimeline.moment.user_id && MomentTimeline.moment.user_id !== current_user_id) {
ctx.sendAndSaveMessage(MomentTimeline.moment.user_id, message, 'moment');
}
// 通知相关人
MomentTimeline.moment.moment_likes.forEach(item => {
if (item.user_id !== current_user_id) {
ctx.sendAndSaveMessage(item.user_id, message, 'moment');
}
});
}
}
module.exports = MomentController;
感谢大家观看,我们下次见