uni-app 147我的朋友圈列表api开发

简介: uni-app 147我的朋友圈列表api开发


router.js

// 某个人的朋友圈列表
  router.get('/moment/:page',controller.moment.list);

app/controller/moment.js

// 某个用户的朋友圈列表
    async list() {
        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 user_id = ctx.query.user_id ? parseInt(ctx.query.user_id) : 0;
        // ctx.validate({
        //     user_id: {
        //         type: "int",
        //         required: false,
        //         defValue: current_user_id,
        //         desc: "用户id"
        //     }
        // });
        let lookIds = [];
        if (!user_id) {
            // 本人
            user_id = current_user_id;
            lookIds = false;
        } else {
            // 验证我是否具备权限
            let f = await app.model.User.findOne({
                where: {
                    id: user_id,
                    status: 1
                },
                attributes: ['id', 'nickname', 'username', 'avatar'],
                include: [{
                    model: app.model.Friend,
                    as: "bfriends",
                    where: {
                        user_id: current_user_id
                    },
                    attributes: ['lookhim', 'isblack']
                }, {
                    model: app.model.Friend,
                    as: "friends",
                    where: {
                        friend_id: current_user_id
                    },
                    attributes: ['lookme', 'isblack']
                }]
            });
            // 用户是否存在
            if (!f) {
                return ctx.apiFail('用户不存在或已被禁用');
            }
            // 是否是好友关系
            if (!f.bfriends.length || !f.friends.length) {
                return ctx.apiSuccess([]);
            }
            // 不可见
            if (f.bfriends[0].isblack || f.friends[0].isblack || !f.bfriends[0].lookhim || !f.friends[0].lookme) {
                return ctx.apiSuccess([]);
            }
            // 获取当前用户所有好友(查找共同好友)
            let friends = await app.model.Friend.findAll({
                where: {
                    user_id: current_user_id,
                    isblack: 0
                },
                attributes: ['friend_id']
            });
            lookIds = friends.map(item => item.friend_id);
        }
        let rows = await app.model.Moment.findAll({
            where: {
                user_id
            },
            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 res = [];
        rows.forEach(item => {
            let comments = [];
            item.moment_comments.forEach(v => {
                if (!lookIds || lookIds.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_likes.forEach(v => {
                if (!lookIds || lookIds.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({
                user_id: item.user_id,
                user_name: item.user.nickname || item.user.username,
                avatar: item.user.avatar,
                moment_id: item.id,
                content: item.content,
                image: item.image ? item.image.split(',') : [],
                video: item.video ? JSON.parse(item.video) : null,
                location: item.location,
                own: 1,
                created_at: item.created_at,
                comments,
                likes
            });
        });
        ctx.apiSuccess(res);
    }

下图是我测试的截图

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

目录
相关文章
|
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月前
|
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 开发后如何上架?
|
1月前
|
API
uni-app 146朋友圈列表api开发
uni-app 146朋友圈列表api开发
18 0