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

文件列表


控制器:app/controller/file.js

js
	async list() {
        const { ctx, app } = this;
        const user_id = ctx.authUser.id;
        ctx.validate({
            file_id: {
                required: true,
                type: "int",
                defValue: 0,
                desc: "目录id"
            },
            orderby: {
                required: false,
                type: "string",
                defValue: 'name',
                range: {
                    in: ['name', 'created_time']
                },
                desc: "排序"
            },
            type:{
                required: false,
                type: "string",
                desc: "类型"
            }
        });

        const { file_id, orderby,type } = ctx.query;
        
        let where = {
            user_id,
            file_id
        }

        if(type && type !== 'all'){
            const Op = app.Sequelize.Op;
            where.ext = {
                [Op.like]: type + '%',  
            }
        }
        
        let rows = await app.model.File.findAll({
            where,
            order: [
                ['isdir', 'desc'],
                [orderby, 'desc']
            ]
        });

        ctx.apiSuccess({
            rows
        });
    }

路由:app/router.js

js
router.get("/file", controller.file.list);