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

直播间礼物情况


数据表迁移文件

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

module.exports = {
  up: (queryInterface, Sequelize) => {
    const { INTEGER, STRING, DATE, ENUM, TEXT } = Sequelize;
    return queryInterface.createTable("live_gift", {
      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", // 更新时操作
      },
      gift_id: {
        type: INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: "礼物id",
        references: {
          model: "gift",
          key: "id",
        },
        onDelete: "cascade",
        onUpdate: "restrict", // 更新时操作
      },
      created_time: DATE,
      updated_time: DATE,
    });
  },

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

模型:app/model/live_gift.js

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

  const LiveGift = app.model.define("live_gift", {
    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", // 更新时操作
    },
    gift_id: {
      type: INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: "礼物id",
      references: {
        model: "gift",
        key: "id",
      },
      onDelete: "cascade",
      onUpdate: "restrict", // 更新时操作
    },
    created_time: DATE,
    updated_time: DATE,
  });

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

  return LiveGift;
};

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

js
// 礼物记录
    async gift(){
        const { ctx, app } = this;
        const id = ctx.params.id

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

        ctx.apiSuccess({
            ths:[{
                title: '礼物名称',
                key: 'gift_name',
            },{
                title: '礼物图标',
                key: 'gift_image',
                type:"image"
            },{
                title: '礼物金币',
                key: 'gift_coin',
            },{
                title: '赠送者',
                key: 'username',
            },{
                title: '赠送时间',
                key: 'created_time',
            }],
            data:res.map(item=>{
                return {
                    created_time:app.formatTime(item.created_time),
                    gift_name:item.gift.name,
                    gift_coin:item.gift.coin,
                    gift_image:item.gift.image,
                    username:item.user.username,
                    avatar:item.user.avatar,
                }
            })
        })
    }

路由:app/router.js

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