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

创建分享


创建数据迁移表

shell
npx sequelize migration:generate --name=share

1.执行完命令后,会在database / migrations / 目录下生成数据表迁移文件,然后定义

js
"use strict";

module.exports = {
  up: (queryInterface, Sequelize) => {
    const { INTEGER, STRING, DATE, ENUM, TEXT } = Sequelize;
    return queryInterface.createTable("share", {
      id: {
        type: INTEGER(20),
        primaryKey: true,
        autoIncrement: true,
      },
      sharedurl: {
        type: STRING,
        allowNull: true,
        defaultValue: "",
        comment: "分享链接",
      },
      file_id: {
        type: INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: "文件id",
        references: {
          model: "file",
          key: "id",
        },
        onDelete: "cascade",
        onUpdate: "restrict", // 更新时操作
      },
      iscancel: {
        type: INTEGER(1),
        allowNull: false,
        defaultValue: 0,
        comment: "是否取消分享",
      },
      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("share");
  },
};
  • 执行 migrate 进行数据库变更
shell
npx sequelize db:migrate

模型创建

js
// app/model/share.js
const crypto = require("crypto");
module.exports = (app) => {
  const { STRING, INTEGER, DATE, ENUM, TEXT } = app.Sequelize;

  const Share = app.model.define("share", {
    id: {
      type: INTEGER(20),
      primaryKey: true,
      autoIncrement: true,
    },
    sharedurl: {
      type: STRING,
      allowNull: true,
      defaultValue: "",
      comment: "分享链接",
    },
    file_id: {
      type: INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: "文件id",
      references: {
        model: "file",
        key: "id",
      },
      onDelete: "cascade",
      onUpdate: "restrict", // 更新时操作
    },
    iscancel: {
      type: INTEGER(1),
      allowNull: false,
      defaultValue: 0,
      comment: "是否取消分享",
    },
    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,
  });

  Share.associate = function (models) {
    // 关联文件
    Share.belongsTo(app.model.File);
  };

  return Share;
};

控制器:app/controller/share.js

js
	// 创建分享
    async create(){
        const { ctx,app,service } = this;
        let user_id = ctx.authUser.id

        ctx.validate({
            file_id:{
                type:"int",
                required:true,
                desc:"文件ID"
            }
        })

        let { file_id } = ctx.request.body;
        // 文件/文件夹是否存在
        let f = await app.model.File.findOne({
            where:{
                id:file_id,
                user_id
            }
        })
        if (!f) {
            return this.ctx.throw(404, '文件不存在');
        }
        // 唯一ID
        let sharedurl = ctx.genID(15)
        // 创建分享
        let s = await app.model.Share.create({
            sharedurl,
            file_id,
            iscancel:0,
            user_id
        })

        let url = 'http://127.0.0.1:7001/share/'+sharedurl
        ctx.apiSuccess('分享链接:'+url)
    }

路由:app/router.js

js
router.post("/share/create", controller.share.create);