【Node.js】项目之动态路由和分页操作

详情评论

通过goods_id 去拿此商品 的所有评论信息,根据uid去拿到用户的账号和头像,然后传页码和一页的品论条数

// comment part
// we can get all the evaluation information by goods_id
// we can get username and avatar from uid
// params: page and comment length
router.get('/comment/:id', async (req, res, next) => {
    // receive dynamic router
    // id represent goods_id which is necessary
    let id = req.params.id
    let { page = 1, length = 5 } = req.query
    // console.log(req.query)
    let start = (page - 1) * length;
    let sql = `SELECT eval_id, goods_id,
               ge.uid, style_name_id,
               style_value_id, eval_text,
               eval_star, create_time,
               username,
               CONCAT("${url}", head_photo_url) AS head_photo_url
               FROM goods_eval AS ge
               JOIN member
               ON ge.uid = member.uid
               WHERE ge.goods_id = ${id}
               LIMIT ${start}, ${length}`
    let [err, result] = await db.query(sql)
    if (!err && result.length > 0) {
        // calculate the count of comment of one product
        let sql1 = `SELECT count(*) AS count FROM goods_eval WHERE goods_id = ${id}`
        let [err1, result1] = await db.query(sql1)
        // add result to the data
        if (!err1) {
            let count = result1[0].count
            page = Number(page)
            let totalPage = Math.ceil(count / length)
            let data = {
                count,
                totalPage,
                page,
                result
            }
            res.send(getMsg('Comment success', 200, data))
        } else {
            next('Comment failure')
        }
    } else {
        next('Comment failure')
    }
})

返回:

{
"msg": "Comment success",
"status": 200,
"data": {
"count": 9,
"totalPage": 2,
"page": 2,
"result": [
{
"eval_id": "9182e28c-32aa-11e9-a7e9-e0accb719100",
"goods_id": "1302045135030100001",
"uid": "9deb3b30-3101-11e9-8bcc-e0accb719100",
"style_name_id": "24976",
"style_value_id": "82559",
"eval_text": "我妈妈买的,他说很好用\n",
"eval_star": 1,
"create_time": "1550404377",
"username": "lqiyoccdc",
"head_photo_url": "http://localhost:3000/image_source/head_photo/girl_head_03.png"
},
{
"eval_id": "91838cd1-32aa-11e9-8716-e0accb719100",
"goods_id": "1302045135030100001",
"uid": "9dec7c5e-3101-11e9-8c71-e0accb719100",
"style_name_id": "24976",
"style_value_id": "82559",
"eval_text": "好的卖家。谢谢喽。我的同事们都很喜欢呢。下次再来哦\n",
"eval_star": 5,
"create_time": "1550404377",
"username": "qyzkcmmgbq",
"head_photo_url": "http://localhost:3000/image_source/head_photo/girl_head_03.png"
},
{
"eval_id": "91845200-32aa-11e9-8cfc-e0accb719100",
"goods_id": "1302045135030100001",
"uid": "9dedcf78-3101-11e9-ba02-e0accb719100",
"style_name_id": "24976",
"style_value_id": "82559",
"eval_text": "哈哈,东西收到!\n",
"eval_star": 1,
"create_time": "1550404377",
"username": "oqutozkpgrlowi",
"head_photo_url": "http://localhost:3000/image_source/head_photo/girl_head_03.png"
},
{
"eval_id": "918512ab-32aa-11e9-a855-e0accb719100",
"goods_id": "1302045135030100001",
"uid": "9def66cf-3101-11e9-9471-e0accb719100",
"style_name_id": "24976",
"style_value_id": "82559",
"eval_text": "老客户有优惠吗?我要办vip会员卡,嘿嘿!\n",
"eval_star": 5,
"create_time": "1550404377",
"username": "hchxhfpe",
"head_photo_url": "http://localhost:3000/image_source/head_photo/girl_head_03.png"
}
]
}
}

商品列表,考虑分页和三种情况(综合排序1、新品2和价格3排序)

  • third_id作为get的参数必须传递
  • 结果返回总个数,总页数,当前页和结果
{
    "msg": "success",
    "status": 200,
    "data": {
            count:count,
            pages:20,
            page:page
            result:[
                {},
                {},
                {}
            ]
     }
}
// goodlist, considering parameters in url
router.get('/goodslist', async (req, res, next) => {
    // receive parameters and id(third_id) is necessary
    let { id: third_id, page = 1, length = 5, orderby = 1 } = req.query
    let start = (page - 1) * length
    let orderStr = '', sort = '', situation = '';
    // judge situation by orderby parameters
    switch (orderby) {
        case "1":
            orderStr = `ORDER BY rand()`
            break;
        case "2":
            situation = `AND new_status = 1`
            orderStr = `ORDER BY rand()`
            break;
        case "3":
            orderStr = `ORDER BY goods_price`
            sort = `ASC`
            break;
        default:
            orderStr = `ORDER BY rand()`
            break;
    }
    let sql = `SELECT id, goods_id, 
               third_id, goods_name, 
               CONCAT("${url}", image_url) AS image_url, 
               goods_introduce,goods_manufacturer, 
               goods_price, assem_price, new_status
               FROM goods_list WHERE third_id = ${third_id} 
               ${situation} ${orderStr} ${sort}
               LIMIT ${start}, ${length}`
    let [err, result] = await db.query(sql)
    // calculate the count of product and total page, add to the result
    // query for the count of one product
    if (!err && result.length > 0) {
        let sql1 = `SELECT COUNT(*) AS count FROM goods_list WHERE third_id = ${third_id} ${situation}`
        let [err1, result1] = await db.query(sql1)
        // add result to the data
        if (!err1) {
            let count = result1[0].count
            page = Number(page)
            let totalPage = Math.ceil(count / length)
            let data = {
                count,
                totalPage,
                page,
                result
            }
            res.send(getMsg('Goods list success', 200, data))
        } else {
            next('Goods list failure')
        }
    } else {
        next('Goods list failure')
    }
})

返回:

{
"msg": "Goods list success",
"status": 200,
"data": {
"count": 9,
"totalPage": 2,
"page": 1,
"result": [
{
"id": 18,
"goods_id": "1302745008010000001",
"third_id": "684",
"goods_name": "即食麻辣海鲜鱿鱼头4盒装无防腐剂开盒即食",
"image_url": "http://localhost:3000/image_source/goods_loop_img/rBACW1xJVBKAFF6nAACdn6IMB2o952.jpg",
"goods_introduce": "阿根廷的海钓鱿鱼,鱿鱼中的精品,川味,简单即食,可四餐,每餐32元",
"goods_manufacturer": "JUSCO制造商直供",
"goods_price": 129,
"assem_price": 0,
"new_status": "1"
},
{
"id": 14,
"goods_id": "1302745004010000001",
"third_id": "684",
"goods_name": "即食麻辣海鲜鲍鱼大虾鱿鱼圈3盒组合装",
"image_url": "http://localhost:3000/image_source/goods_loop_img/rBACW1wjQ8iASXBKAAFS2ivO95A624.jpg",
"goods_introduce": "无任何防腐剂,高营养,口口生香。优惠组合装,单盒最低26元起",
"goods_manufacturer": "JUSCO制造商直供",
"goods_price": 139,
"assem_price": 86.88,
"new_status": "1"
},
{
"id": 16,
"goods_id": "1302745001010000001",
"third_id": "684",
"goods_name": "即食麻辣海鲜三条鱿鱼",
"image_url": "http://localhost:3000/image_source/goods_loop_img/rBACVFwcSQCAC1QdAADqElh2fqY251.jpg",
"goods_introduce": "阿根廷的海钓鱿鱼,鱿鱼中的精品,川味,简单即食,可做下酒菜海鲜面火锅",
"goods_manufacturer": "JUSCO制造商直供",
"goods_price": 89,
"assem_price": 0,
"new_status": "1"
},
{
"id": 25,
"goods_id": "1302745009010000001",
"third_id": "684",
"goods_name": "即食麻辣海鲜鱿鱼圈4盒装无防腐剂开盒即食",
"image_url": "http://localhost:3000/image_source/goods_loop_img/rBACYVxJV_qANuXCAAEISTGFio8435.jpg",
"goods_introduce": "液氮秒冻,极速锁鲜,无防腐剂,川味即食。可吃四餐,平均每餐32元",
"goods_manufacturer": "JUSCO制造商直供",
"goods_price": 129,
"assem_price": 80.63,
"new_status": "1"
},
{
"id": 22,
"goods_id": "1302705003010000001",
"third_id": "684",
"goods_name": "川味口水鸡 300g【5盒】",
"image_url": "http://localhost:3000/image_source/goods_loop_img/rBACYVw1rNyAKqPMAAGN9NqDAFs771.jpg",
"goods_introduce": "集麻辣鲜香于一身,口水滴哒,味压江南十二州。整盒鸡肉,平均每餐约23元",
"goods_manufacturer": "味美思制造商直供",
"goods_price": 119,
"assem_price": 74.38,
"new_status": "1"
}
]
}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值