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

简介: uni-app 142点赞朋友圈api开发(一)


/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;

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

目录
相关文章
|
2天前
|
移动开发 Android开发 数据安全/隐私保护
移动应用与系统的技术演进:从开发到操作系统的全景解析随着智能手机和平板电脑的普及,移动应用(App)已成为人们日常生活中不可或缺的一部分。无论是社交、娱乐、购物还是办公,移动应用都扮演着重要的角色。而支撑这些应用运行的,正是功能强大且复杂的移动操作系统。本文将深入探讨移动应用的开发过程及其背后的操作系统机制,揭示这一领域的技术演进。
本文旨在提供关于移动应用与系统技术的全面概述,涵盖移动应用的开发生命周期、主要移动操作系统的特点以及它们之间的竞争关系。我们将探讨如何高效地开发移动应用,并分析iOS和Android两大主流操作系统的技术优势与局限。同时,本文还将讨论跨平台解决方案的兴起及其对移动开发领域的影响。通过这篇技术性文章,读者将获得对移动应用开发及操作系统深层理解的钥匙。
|
5天前
|
JSON 中间件 API
开发REST API3-11
开发REST API3-11
|
6天前
|
XML 移动开发 前端开发
使用duxapp开发 React Native App 事半功倍
对于Taro的壳子,或者原生React Native,都会存在 `android` `ios`这两个文件夹,而在duxapp中,这些文件夹的内容是自动生成的,那么对于需要在这些文件夹中修改的配置内容,例如包名、版本号、新架构开关等,都通过配置文件的方式配置了,而不需要需修改具体的文件
|
11天前
|
监控 API 开发工具
探索 Postman:API 开发的瑞士军刀
在现代软件开发中,API 起着关键作用,连接前后端应用及微服务架构。Postman 是一款流行的一站式 API 开发工具,支持 REST、GraphQL 和 SOAP 等协议,具备构建、测试、调试 API 的强大功能,包括请求构建器、环境变量管理、测试脚本编写、文档生成及 Mock 服务器创建等。本文详细介绍 Postman 的核心功能与进阶技巧,助你提高 API 开发效率。
|
6天前
|
存储 开发工具 Android开发
使用.NET MAUI开发第一个安卓APP
【9月更文挑战第24天】使用.NET MAUI开发首个安卓APP需完成以下步骤:首先,安装Visual Studio 2022并勾选“.NET Multi-platform App UI development”工作负载;接着,安装Android SDK。然后,创建新项目时选择“.NET Multi-platform App (MAUI)”模板,并仅针对Android平台进行配置。了解项目结构,包括`.csproj`配置文件、`Properties`配置文件夹、平台特定代码及共享代码等。
|
10天前
|
前端开发 API 开发者
探索后端开发中的RESTful API设计原则
【9月更文挑战第21天】在数字化时代的浪潮中,后端开发扮演着至关重要的角色。本文将深入探讨RESTful API的设计原则,旨在为开发者提供一套清晰、高效的指导方针。我们将从资源的命名与表述开始,逐步引导您理解如何通过统一接口和状态码来构建可扩展且易于维护的API。文章不仅涵盖理论知识,还将通过实际代码示例,展示如何将这些原则应用于日常开发实践中。无论您是初学者还是经验丰富的开发者,这篇文章都将为您的后端开发之旅增添宝贵的知识财富。
|
11天前
|
JavaScript NoSQL 关系型数据库
深入浅出后端开发:从零搭建RESTful API
【9月更文挑战第20天】在数字时代的浪潮中,后端开发如同一座桥梁,连接用户界面与数据世界。本文将引领你踏上一段探索之旅,从零基础开始,一步步揭开后端开发的神秘面纱。我们将以构建一个RESTful API为例,深入理解后端逻辑的核心。通过简洁的代码示例和生动的比喻,本文旨在让初学者轻松入门,同时也为有一定基础的开发者提供新的视角和思考。准备好,让我们开始这段奇妙的旅程吧!
|
13天前
|
API 网络架构 开发者
探索后端开发:RESTful API设计的艺术
【9月更文挑战第18天】在数字化时代的浪潮中,后端开发如同搭建一座座坚固的桥梁,连接用户与数据的无限可能。本文将深入浅出地探讨RESTful API设计的精髓,从理论基础到实践应用,带领读者领略API设计的艺术。我们将以代码示例为灯塔,照亮理解之路,但
|
14天前
|
存储 JavaScript NoSQL
深入浅出后端开发:构建你的第一个RESTful API
【9月更文挑战第17天】在数字时代的浪潮中,后端开发是支撑起整个互联网的骨架。本文将引导读者了解后端开发的基本概念,并通过一个实际的代码示例,展示如何从零开始构建一个简单的RESTful API。我们将一起探索API设计的哲学、选择合适的后端语言和框架,以及实现数据存储和接口测试的过程。无论你是编程新手,还是希望扩展你的技术栈,这篇文章都将为你提供一次全面而深入的后端开发之旅。
33 0
|
2月前
|
机器人 API Python
智能对话机器人(通义版)会话接口API使用Quick Start
本文主要演示了如何使用python脚本快速调用智能对话机器人API接口,在参数获取的部分给出了具体的获取位置截图,这部分容易出错,第一次使用务必仔细参考接入参数获取的位置。
119 1