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

直播间列表


数据表迁移文件

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

module.exports = {
  up: (queryInterface, Sequelize) => {
    const { INTEGER, STRING, DATE, ENUM, TEXT } = Sequelize;
    return queryInterface.createTable("live", {
      id: {
        type: INTEGER(20),
        primaryKey: true,
        autoIncrement: true,
      },
      title: {
        type: STRING(100),
        allowNull: false,
        defaultValue: "",
        comment: "直播间标题",
      },
      cover: {
        type: STRING,
        allowNull: true,
        defaultValue: "",
        comment: "直播间封面",
      },
      user_id: {
        type: INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: "用户id",
        references: {
          model: "user",
          key: "id",
        },
        onDelete: "cascade",
        onUpdate: "restrict", // 更新时操作
      },
      look_count: {
        type: INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: "总观看人数",
      },
      coin: {
        type: INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: "总金币",
      },
      key: {
        type: STRING,
        allowNull: false,
        defaultValue: "",
        comment: "唯一标识",
      },
      status: {
        type: INTEGER(1),
        allowNull: false,
        defaultValue: 0,
        comment: "直播间状态 0未开播 1直播中 2暂停直播 3直播结束",
      },
      created_time: DATE,
      updated_time: DATE,
    });
  },

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

模型:app/model/live.js

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

  const Live = app.model.define("live", {
    id: {
      type: INTEGER(20),
      primaryKey: true,
      autoIncrement: true,
    },
    title: {
      type: STRING(100),
      allowNull: false,
      defaultValue: "",
      comment: "直播间标题",
    },
    cover: {
      type: STRING,
      allowNull: true,
      defaultValue: "",
      comment: "直播间封面",
    },
    user_id: {
      type: INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: "用户id",
      references: {
        model: "user",
        key: "id",
      },
      onDelete: "cascade",
      onUpdate: "restrict", // 更新时操作
    },
    look_count: {
      type: INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: "总观看人数",
    },
    coin: {
      type: INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: "总金币",
    },
    key: {
      type: STRING,
      allowNull: false,
      defaultValue: "",
      comment: "唯一标识",
    },
    status: {
      type: INTEGER(1),
      allowNull: false,
      defaultValue: 0,
      comment: "直播间状态 0未开播 1直播中 2暂停直播 3直播结束",
    },
    created_time: {
      type: DATE,
      get() {
        return app.formatTime(this.getDataValue("created_time"));
      },
    },
    updated_time: DATE,
  });

  // 关联关系
  Live.associate = function (models) {
    // 关联主播
    Live.belongsTo(app.model.User);
  };

  return Live;
};

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

js
	async index() {
        const { ctx, app } = this;
        let tabs = [{
            name:"全部",
            url:"/admin/live",
            active:false
        },{
            name:"直播中",
            url:"?status=1",
            status:1,
            active:false
        },{
            name:"未开播",
            url:"?status=0",
            status:0,
            active:false
        },{
            name:"直播结束",
            url:"?status=3",
            status:3,
            active:false
        }];
        
        let where = (!ctx.query.status && ctx.query.status != 0) ? {} : { status:ctx.query.status }

        let data = await ctx.page('Live',where,{
            include:[{
                model:app.model.User,
                attributes:['id','username']
            }]
        })

        tabs = tabs.map(item=>{
            if((!ctx.query.status && ctx.query.status != 0 && item.url === '/admin/live') || item.status == ctx.query.status){
                item.active = true
            }
            return item
        })

        data = JSON.parse(JSON.stringify(data))

        await ctx.renderTemplate({
            title: "直播间管理",
            tempType: "table",
            table: {
                tabs,
                // 表头
                columns: [{
                    title: '直播间',
                    fixed: 'left',
                    render(item) {
                        return `
                        <h2 class="table-avatar">
                            <a class="avatar mr-2"><img class="rounded" src="${item.cover}"></a>
                            <a>${item.title} <span>创建人:ceshi</span></a>	
                        </h2>
                        `
                    },
                },{
                    title: '观看人数',
                    key: 'look_count',
                    width: 180,
                    fixed: 'center'
                },{
                    title: '金币数',
                    key: 'coin',
                    width: 180,
                    fixed: 'center'
                },{
                    title: '创建时间',
                    key: 'created_time',
                    width: 180,
                    fixed: 'center'
                },{
                    title: "操作",
                    width: 200,
                    fixed: 'center',
                    render(item) {
                        let close = ''
                        if(item.status !== 3){
                            close = `<a @click="modal('/admin/live/close/${item.id}','是否要关闭该直播间?')" class="btn btn-sm bg-warning text-white">关闭直播</a>`
                        }
                        return `
                        <div class="actions btn-group btn-group-sm">
                            <a @click="openInfo('/admin/live/look/${item.id}','观看记录')" class="btn btn-sm bg-primary text-white">观看记录</a>						<a @click="openInfo('/admin/live/gift/${item.id}','礼物记录')" class="btn btn-sm bg-purple text-white">礼物记录</a>					
                            <a @click="openInfo('/admin/live/comment/${item.id}','弹幕记录')" class="btn btn-sm bg-success text-white">弹幕记录</a>			
                            ${close}		
                            <a @click="del('/admin/live/delete/${item.id}')" class="btn btn-sm bg-danger text-white">删除</a>	
                        </div>
                        `
                    },
                }],
                data
            }
        })
    }

路由:app/router.js

js
router.get("/admin/live", controller.admin.live.index);