Skip to content
关注公众号,获取新课通知

直播间观看情况


数据表迁移文件

shell
npx sequelize migration:generate --name=live_user
js
"use strict";

module.exports = {
  up: (queryInterface, Sequelize) => {
    const { INTEGER, STRING, DATE, ENUM, TEXT } = Sequelize;
    return queryInterface.createTable("live_user", {
      id: {
        type: INTEGER(20),
        primaryKey: true,
        autoIncrement: true,
      },
      live_id: {
        type: INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: "直播间id",
        references: {
          model: "live",
          key: "id",
        },
        onDelete: "cascade",
        onUpdate: "restrict", // 更新时操作
      },
      user_id: {
        type: INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: "用户id",
        references: {
          model: "user",
          key: "id",
        },
        onDelete: "cascade",
        onUpdate: "restrict", // 更新时操作
      },
      created_time: DATE,
      updated_time: DATE,
    });
  },

  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable("live_user");
  },
};

模型:app/model/live_user.js

js
module.exports = (app) => {
  const { STRING, INTEGER, DATE, ENUM, TEXT } = app.Sequelize;

  const LiveUser = app.model.define("live_user", {
    id: {
      type: INTEGER(20),
      primaryKey: true,
      autoIncrement: true,
    },
    live_id: {
      type: INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: "直播间id",
      references: {
        model: "live",
        key: "id",
      },
      onDelete: "cascade",
      onUpdate: "restrict", // 更新时操作
    },
    user_id: {
      type: INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: "用户id",
      references: {
        model: "user",
        key: "id",
      },
      onDelete: "cascade",
      onUpdate: "restrict", // 更新时操作
    },
    created_time: DATE,
    updated_time: DATE,
  });

  // 关联关系
  LiveUser.associate = function (models) {
    // 关联用户
    LiveUser.belongsTo(app.model.User);
    // 关联直播间
    LiveUser.belongsTo(app.model.Live);
  };

  return LiveUser;
};

控制器:app/controller/admin/live.js

js
	// 观看记录
    async look(){
        const { ctx, app } = this;
        const id = ctx.params.id

        let res = await app.model.LiveUser.findAll({
            where:{
                live_id:id
            },
            include:[{
                model:app.model.User,
                attributes:['id','username','avatar']
            }]
        })

        ctx.apiSuccess({
            ths:[{
                title: '用户名',
                key: 'username',
            },{
                title: '观看时间',
                key: 'created_time',
            }],
            data:res.map(item=>{
                return {
                    id:item.id,
                    username:item.user.username,
                    avatar:item.user.avatar,
                    created_time:app.formatTime(item.created_time)
                }
            })
        })
    }

路由:app/router.js

js
router.get("/admin/live/look/:id", controller.admin.live.look);