login-require
v1.0.0
Published
```javascript //登录 router.post("/api/login", (req, res) => { let { name, password } = req.body;
Downloads
1
Readme
###登录页面
//登录
router.post("/api/login", (req, res) => {
let {
name,
password
} = req.body;
let data = JSON.parse(fs.readFileSync('mock/user.json'));
let flag = data.find(item => {
return item.name === name && item.password === password;
})
if (flag) {
res.send({ code: 1 })
} else {
res.send({ code: 0 })
}
})
//注册
router.post("/api/register", (req, res) => {
let {
name,
password
} = req.body;
let data = JSON.parse(fs.readFileSync('mock/user.json'));
let flag = data.find(item => {
return item.name !== name;
})
if (flag) {
req.body.id = Math.random().toString().slice(2)
data.push(req.body);
fs.writeFileSync('mock/user.json', JSON.stringify(data))
res.send({
code: 1
})
} else {
res.send({ code: 0 })
}
})
//新增
router.post('/api/add', (req, res) => {
let {
name,
address,
tit
} = req.body;
req.body.id = Math.random().toString().slice(2);
let data = JSON.parse(fs.readFileSync('mock/data.json'));
let flag = data.find(item => {
return item.name !== name;
});
if (flag) {
data.push(req.body);
res.send({
data: fs.writeFileSync('mock/data.json', JSON.stringify(data)),
code: 1
})
}
})
//编辑
router.post('/api/submit', (req, res) => {
let {
id
} = req.body
let data = JSON.parse(fs.readFileSync('mock/data.json'));
let flag = data.find(item => {
return item.id === id;
})
Object.assign(flag, req.body);
fs.writeFileSync('mock/data.json', JSON.stringify(data));
res.send({
code: 1
})
})
//删除
router.post("/api/delete", (req, res) => {
let {
id
} = req.body;
let data = JSON.parse(fs.readFileSync('mock/data.json'));
let flag = data.filter(item => {
return item.id !== id
})
data = fs.writeFileSync('mock/data.json', JSON.stringify(flag))
res.send({
code: 1,
})
})
//渲染
function rander() {
axios.post('/api/rander', {
index,
pagesize
}).then(res => {
indexMax = res.data.maxIndex;
plate.innerHTML = res.data.data.map(item => {
return ` <li>
<p>${item.name}</p>
<p>${item.address}</p>
<p>${item.tit}</p>
<p>
<span class="change" id=${item.id}>编辑</span>
<span class="add" id=${item.id}>添加</span>
<span class="del" id=${item.id}>删除</span>
</p>
</li>`
}).join('');
//页码渲染
let html = '';
for (let i = 1; i <= res.data.maxIndex; i++) {
html += `<li>${i}</li>`
};
page.innerHTML = html;
page.children[index].classList.add('active')
})
};