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

作品列表


控制器:app/controller/video.js

js
// 当前用户的视频列表
   async index() {
       let { ctx, app } = this;
       let currentUser = ctx.authUser;
       ctx.validate({
           page: {
               required: true,
               desc: "页码",
               type: "int"
           },
           user_id: {
               required: true,
               desc: "用户id",
               type: "int"
           }
       });
       let user_id = ctx.query.user_id;
       let rows = await ctx.page(app.model.Video, {
           user_id
       });
       ctx.apiSuccess(rows);
   }

扩展:app/extend/context.js

js
	// 分页
    async page(model, where, options = {}) {
        let page = this.params.page ? parseInt(this.params.page) : 1;
        let limit = this.query.limit ? parseInt(this.query.limit) : 10;
        let offset = (page - 1) * limit;

        if (!options.order) {
            options.order = [
                ['id', 'DESC']
            ];
        }

        return await model.findAll({
            where,
            offset,
            limit,
            ...options
        });
    }

路由:app/router.js

js
router.get("/video_list/:page", controller.video.index);