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

学习分享2年前 (2023)更新 bestcyt
308 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)
	}
})

 

© 版权声明

相关文章