1.首先是控制层(controller层)
1)前提是我已经连接了数据库并且可以对 用户数据库表实现增删改查(这里可以是你数据库里任意一个表格,看你想对哪个数据进行分页)
这篇文章就是写如何对图中的这个表格’kun’,如何实现分页的。
todo.js(实现对用户的增删改查)
const Controller = require('egg').Controller;
const { Sequelize } = require('sequelize');
const Crypto = require('crypto'); //这个是密码加密解密需要的 这篇文章先不深入
class TodoController extends Controller {
async first(){
await this.ctx.render('firstpage.tpl',{});
}
async list() {
let todoes = await this.ctx.service.todo.getTodoes();
await this.ctx.render('userpage.tpl', {todoes});
}
async add() {
await this.ctx.render('todo/add.tpl', {});
let username=this.ctx.request.body.username;
let salt=Crypto.randomBytes(8).toString('hex');
let password = Crypto.pbkdf2Sync(this.ctx.request.body.password,salt,10000,64,'sha512' ).toString('hex');
let user=await this.ctx.service.todo.saveTodo({
'username':username,
'password':password,
'salt':salt,
create_time: new Date()
});
this.ctx.redirect('/getusercount');
}
async edit() {
let id = this.ctx.params.id;
let todo = await this.ctx.service.todo.getTodo(id);
if (this.ctx.request.method === 'POST') {
todo.username = this.ctx.request.body.username;
todo.password = this.ctx.request.body.password;
console.log( todo.username);
await this.ctx.service.todo.updateTodo(todo);
this.ctx.redirect('/getusercount');
}
await this.ctx.render('todo/add.tpl', {todo});
}
async delete() {
let id = this.ctx.request.query.id;
await this.ctx.service.todo.deleteTodo(id);
this.ctx.redirect('/getuercount');
}
}
module.exports = TodoController; //注意这里的Todo 每个js文件都要和文件名保持一致哦~ 比如我的js是user.js 那这里就应该是UserController
todo是这个js的参数,这个参数和js文件名无关,我就是随意起的。不要太钻牛角尖哦~
userpage,js
const Controller =require('egg').Controller;
class UserpageController extends Controller