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

简介: 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);
    }

下图是我测试的截图

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

目录
相关文章
|
1月前
|
API 数据安全/隐私保护 iOS开发
利用uni-app 开发的iOS app 发布到App Store全流程
利用uni-app 开发的iOS app 发布到App Store全流程
88 3
|
1月前
|
Android开发 开发者 UED
个人开发 App 成功上架手机应用市场的关键步骤
个人开发 App 成功上架手机应用市场的关键步骤
|
1月前
|
开发工具 数据安全/隐私保护 Android开发
【教程】APP 开发后如何上架?
【教程】APP 开发后如何上架?
|
3天前
|
人工智能 机器人 API
【Python+微信】【企业微信开发入坑指北】3. 如何利用企业微信API给微信群推送消息
【Python+微信】【企业微信开发入坑指北】3. 如何利用企业微信API给微信群推送消息
7 0
|
3天前
|
缓存 人工智能 API
【Python+微信】【企业微信开发入坑指北】2. 如何利用企业微信API主动给用户发应用消息
【Python+微信】【企业微信开发入坑指北】2. 如何利用企业微信API主动给用户发应用消息
8 0
|
15天前
|
小程序 前端开发 API
小程序全栈开发中的RESTful API设计
【4月更文挑战第12天】本文探讨了小程序全栈开发中的RESTful API设计,旨在帮助开发者理解和掌握相关技术。RESTful API基于REST架构风格,利用HTTP协议进行数据交互,遵循URI、客户端-服务器架构、无状态通信、标准HTTP方法和资源表述等原则。在小程序开发中,通过资源建模、设计API接口、定义资源表述及实现接口,实现前后端高效分离,提升开发效率和代码质量。小程序前端利用微信API与后端交互,确保数据流通。掌握这些实践将优化小程序全栈开发。
|
24天前
|
前端开发 Java API
构建RESTful API:Java中的RESTful服务开发
【4月更文挑战第3天】本文介绍了在Java环境中构建RESTful API的重要性及方法。遵循REST原则,利用HTTP方法处理资源,实现CRUD操作。在Java中,常用框架如Spring MVC简化了RESTful服务开发,包括定义资源、设计表示层、实现CRUD、考虑安全性、文档和测试。通过Spring MVC示例展示了创建RESTful服务的步骤,强调了其在现代Web服务开发中的关键角色,有助于提升互操作性和用户体验。
构建RESTful API:Java中的RESTful服务开发
|
29天前
|
机器学习/深度学习 前端开发 API
实现以图搜货功能,淘宝API开发实战分享
实现以图搜货功能,淘宝API开发实战分享
24 0
|
1月前
|
Java Android开发 开发者
【Uniapp开发】APP的真机调试指南,从开发到上架全过程
【Uniapp开发】APP的真机调试指南,从开发到上架全过程
36 3
游戏直播APP平台开发多少钱成本:定制与成品源码差距这么大
开发一款游戏直播APP平台所需的费用是多少?对于计划投身这一领域的投资者来说,首要关心的问题之一就是。本文将探讨两种主要的开发模式——定制开发与成品源码二次开发的成本差异及其优劣势。