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

创建礼物


数据表迁移文件

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

module.exports = {
  up: (queryInterface, Sequelize) => {
    const { INTEGER, STRING, DATE, ENUM, TEXT } = Sequelize;
    return queryInterface.createTable("gift", {
      id: {
        type: INTEGER(20),
        primaryKey: true,
        autoIncrement: true,
      },
      name: {
        type: STRING(30),
        allowNull: false,
        defaultValue: "",
        comment: "礼物名称",
      },
      image: {
        type: STRING,
        allowNull: true,
        defaultValue: "",
        comment: "礼物图标",
      },
      coin: {
        type: INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: "金币",
      },
      created_time: DATE,
      updated_time: DATE,
    });
  },

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

模型:app/model/gift.js

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

  const Gift = app.model.define("gift", {
    id: {
      type: INTEGER(20),
      primaryKey: true,
      autoIncrement: true,
    },
    name: {
      type: STRING,
      allowNull: false,
      defaultValue: "",
      comment: "礼物名称",
    },
    image: {
      type: STRING,
      allowNull: true,
      defaultValue: "",
      comment: "礼物图标",
      get() {
        const ctx = app.createAnonymousContext();
        const { protocol, host } = ctx.request;
        return app.config.webUrl + this.getDataValue("image");
      },
    },
    coin: {
      type: INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: "金币",
    },
    created_time: DATE,
    updated_time: DATE,
  });

  return Gift;
};

配置:config/config.default.js

js
config.webUrl = "http://127.0.0.1:7001/";

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

js
	async create() {
        const { ctx, app } = this;
        await ctx.renderTemplate({
            title: "创建礼物",
            tempType: "form",
            form: {
                // 提交地址
                action: "/admin/gift",
                fields:[{
					label: "礼物名称",
					type: "text",
					name: "name",
					placeholder: "礼物名称",
				}, {
					label: "礼物图标",
					type: "file",
					name: "image",
				},
				{
					label: "金币",
					type: "number",
					name: "coin",
					default: 0
				}]
            },
            // 新增成功跳转路径
            successUrl:"/admin/gift"
        })
    }

    async save() {
        const { ctx, app } = this;

        // 参数验证
        ctx.validate({
            name: {
                type: 'string',
                required: true,
                desc: '礼物名称'
            },
            image:{
                type:"string",
            },
            coin:{
                type:"int",
            }
        });
        let { name, image,coin } = ctx.request.body;

        // 创建礼物
        let gift = await app.model.Gift.create({
            name,
            image,
            coin
        });
        if (!gift) {
            ctx.throw(400, '创建礼物失败');
        }
        ctx.apiSuccess(gift);
    }

路由:app/router.js

js
router.get("/admin/gift/create", controller.admin.gift.create);
router.post("/admin/gift", controller.admin.gift.save);