uni-app 95获取群聊相关信息(一)

142 篇文章 4 订阅
139 篇文章 1 订阅

/app/model/group_user.js

// app/model/group_user.js
'use strict';
const crypto = require('crypto');
module.exports = app => {
    const { STRING, INTEGER, DATE, ENUM, TEXT } = app.Sequelize;
    // 配置(重要:一定要配置详细,一定要!!!)
    const GroupUser = app.model.define('group_user', {
        id: {
            type: INTEGER(20).UNSIGNED,
            primaryKey: true,
            autoIncrement: true
        },
        user_id: {
            type: INTEGER(20).UNSIGNED,
            allowNull: false,
            comment: '用户id',
            //  定义外键(重要)
            references: {
                model: 'user', // 对应表名称(数据表名称)
                key: 'id' // 对应表的主键
            },
            onUpdate: 'restrict', // 更新时操作
            onDelete: 'cascade'  // 删除时操作
        },
        group_id: {
            type: INTEGER(20).UNSIGNED,
            allowNull: false,
            comment: '群组id',
            //  定义外键(重要)
            references: {
                model: 'group', // 对应表名称(数据表名称)
                key: 'id' // 对应表的主键
            },
            onUpdate: 'restrict', // 更新时操作
            onDelete: 'cascade'  // 删除时操作
        },
        nickname: {
            type: STRING(30),
            allowNull: false,
            defaultValue: '',
            comment: '在群里的昵称',
        },
        created_at: DATE,
        updated_at: DATE
    });
    // 定义关联关系
    GroupUser.associate = function(model){
        GroupUser.belongsTo(app.model.User,{
            foreignKey:'user_id'
        });
    };
    return GroupUser;
};

app/controller/group.js

// 查看群资料
    async info() {
        const { ctx, app } = this;
        let current_user_id = ctx.authUser.id;
        // 验证参数
        ctx.validate({
            id: {
                required: true,
                type: 'int',
                desc: "群组id"
            }
        });
        let { id } = ctx.params;
        // 群组是否存在
        let group = await app.model.Group.findOne({
            where: {
                status: 1,
                id
            },
            include: [{
                model: app.model.GroupUser,
                attributes: ['user_id', 'nickname'],
                include: [{
                    model: app.model.User,
                    attributes: ['id', 'nickname', 'avatar', 'username']
                }]
            }]
        });

        if (!group) {
            return ctx.apiFail('该群聊不存在或者已被封禁');
        }

        // 当前用户是否是该群成员
        let index = group.group_users.findIndex(item => item.user_id === current_user_id);
        if (index === -1) {
            return ctx.apiFail('你不是该群成员,没有权限');
        }

        ctx.apiSuccess(group);
    }

router.js

 // 获取群资料
 router.get('/group_info/:id',controller.group.info);

下图是我测试的结果

在这里插入图片描述

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

2019ab

你的鼓励就是我的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值