SQL-NodeJs-查询留言板数据—条件(全部/显示/隐藏)分页查询

NodeJs 3年前 (2020) bestcyt
33 0 0

nodejs分页查询留言信息
SQL-NodeJs-查询留言板数据---条件(全部/显示/隐藏)分页查询

// 查询留言板数据---条件(全部/显示/隐藏)分页查询
router.post('/getMessage', async (req, res, next) => {
	let {
		type,
		pageNo,
		pageSize
	} = req.body;
	pageNo = pageNo - 1;
	try {
		let totalAll, totalDisplay, totalHide, message, state = true;
		if (type == 'all') {
			totalAll = await querySql('select COUNT(*) from message');
			totalDisplay = await querySql('select COUNT(*) from message  where state = ?', [state]);
			if (state) {
				totalDisplay = totalDisplay[0]['COUNT(*)'];
				totalHide = totalAll[0]['COUNT(*)'] - totalDisplay;
			} else {
				totalHide = totalDisplay[0]['COUNT(*)'];
				totalDisplay = totalAll[0]['COUNT(*)'] - totalHide;
			}
			message = await querySql('select * from message order by id desc limit ?,?', [pageNo, pageSize]);
		} else {
			// 查询显示、隐藏
			state = type == 'display' ? true : false;
			totalAll = await querySql('select COUNT(*) from message');
			totalDisplay = await querySql('select COUNT(*) from message  where state = ?', [state]);
			if (state) {
				totalDisplay = totalDisplay[0]['COUNT(*)'];
				totalHide = totalAll[0]['COUNT(*)'] - totalDisplay;
			} else {
				totalHide = totalDisplay[0]['COUNT(*)'];
				totalDisplay = totalAll[0]['COUNT(*)'] - totalHide;
			}
			message = await querySql('select * from message where state = ? order by id desc limit ?,?', [state, pageNo, pageSize]);
		}
		res.send({
			code: 200,
			msg: '成功',
			pageNo: pageNo + 1,
			pageSize,
			totalAll: totalAll[0]['COUNT(*)'], //全部留言数量
			totalDisplay, //显示的留言-总数量
			totalHide, //隐藏的留言-总数量
			type, //全部、显示、隐藏
			data: message
		})
	} catch (e) {
		console.log(e)
		next(e)
	}
})

 

版权声明:bestcyt 发表于 2020年5月8日 pm10:48。
转载请注明:SQL-NodeJs-查询留言板数据—条件(全部/显示/隐藏)分页查询 | 爱前端导航

相关文章

联系站长