uni-app 146朋友圈列表api开发

Posted 2019ab

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了uni-app 146朋友圈列表api开发相关的知识,希望对你有一定的参考价值。

router.js

  // 朋友圈列表
  router.get('/moment_timeline/:page',controller.moment.timeline);

app/controller/moment.js

// 朋友圈列表
    async timeline() 
        const  ctx, app  = this;
        let current_user_id = ctx.authUser.id;
        
        let page = ctx.params.page ? parseInt(ctx.params.page) : 1;
        let limit = ctx.query.limit ? parseInt(ctx.query.limit) : 10;
        let offset = (page - 1) * limit;

        let rows = await app.model.MomentTimeline.findAll(
            where: 
                user_id: current_user_id
            ,
            include: [
                model: app.model.Moment,
                include: [
                    model: app.model.User,
                    attributes: ['id', 'nickname', 'username', 'avatar']
                , 
                    model: app.model.MomentComment,
                    attributes: 
                        exclude: ['created_at', 'updated_at']
                    ,
                    include: [
                        model: app.model.User,
                        as: "momentCommentUser",
                        attributes: ['id', 'nickname', 'username']
                    , 
                        model: app.model.User,
                        as: "momentCommentReply",
                        attributes: ['id', 'nickname', 'username']
                    ]
                , 
                    model: app.model.MomentLike,
                    attributes: ['user_id', 'moment_id'],
                    include: [
                        model: app.model.User,
                        attributes: ['id', 'nickname', 'username']
                    ]
                ]
            ],
            offset,
            limit,
            order: [
                ['id', 'DESC']
            ]
        );

        let friends = await app.model.Friend.findAll(
            where: 
                user_id: current_user_id,
                lookhim: 1
            ,
            attributes: ['friend_id']
        );

        let bfriends = await app.model.Friend.findAll(
            where: 
                friend_id: current_user_id,
                lookme: 1
            ,
            attributes: ['user_id']
        );

        friends = friends.map(item => item.friend_id);

        bfriends = bfriends.map(item => item.user_id);

        friends = friends.filter(item => bfriends.includes(item));


        let res = [];
        rows.forEach(item => 
            if (friends.includes(item.moment.user_id) || item.moment.user_id === current_user_id) 
                let comments = [];
                item.moment.moment_comments.forEach(v => 
                    if (friends.includes(v.momentCommentUser.id) || v.momentCommentUser.id === current_user_id) 
                        comments.push(
                            content: v.content,
                            user: 
                                id: v.momentCommentUser.id,
                                name: v.momentCommentUser.nickname || v.momentCommentUser.username
                            ,
                            reply: v.momentCommentReply ? 
                                id: v.momentCommentReply.id,
                                name: v.momentCommentReply.nickname || v.momentCommentReply.username
                             : null
                        );
                    
                );

                let likes = [];
                item.moment.moment_likes.forEach(v => 
                    if (friends.includes(v.user.id) || v.user.id === current_user_id) 
                        likes.push(
                            id: v.user.id,
                            name: v.user.nickname || v.user.username
                        );
                    
                );

                res.push(
                    id: item.id,
                    user_id: item.moment.user_id,
                    user_name: item.moment.user.nickname || item.moment.user.username,
                    avatar: item.moment.user.avatar,
                    moment_id: item.moment_id,
                    content: item.moment.content,
                    image: item.moment.image ? item.moment.image.split(',') : [],
                    video: item.moment.video ? JSON.parse(item.moment.video) : null,
                    location: item.moment.location,
                    own: item.own,
                    created_at: item.created_at,
                    comments,
                    likes
                );
            
        );

        ctx.apiSuccess(res);
    

下图是我测试的截图

感谢大家观看,我们下次见

以上是关于uni-app 146朋友圈列表api开发的主要内容,如果未能解决你的问题,请参考以下文章

uni-app 145评论朋友圈api开发

uni-app 144评论朋友圈api开发

uni-app 139发布朋友圈api开发

uni-app 140发布朋友圈api开发

uni-app 142点赞朋友圈api开发

uni-app 143点赞朋友圈api开发