GetGoodsList.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. const API = require("../../lib/API");
  2. const db = require("../../plugin/DataBase/db");
  3. const { BaseStdResponse } = require("../../BaseStdResponse");
  4. // 获取产品列表
  5. class GetGoodsList extends API {
  6. constructor() {
  7. super();
  8. this.setPath('/Goods/List')
  9. this.setMethod('get')
  10. }
  11. async onRequest(req, res) {
  12. let { keyword } = req.query
  13. let sql = `
  14. SELECT
  15. id,
  16. name,
  17. price,
  18. num,
  19. \`limit\`,
  20. icon,
  21. isHot,
  22. description,
  23. category,
  24. features
  25. FROM
  26. goods
  27. WHERE state = 1
  28. `
  29. let params = []
  30. if (keyword) {
  31. sql += ` AND name LIKE ?`
  32. params.push(`%${keyword}%`)
  33. }
  34. sql += `
  35. ORDER BY
  36. id DESC
  37. `
  38. let rows = await db.query(sql, params)
  39. if (!rows)
  40. return res.json({
  41. ...BaseStdResponse.ERR,
  42. msg: '获取商品失败!'
  43. })
  44. res.json({
  45. ...BaseStdResponse.OK,
  46. data: rows
  47. })
  48. }
  49. }
  50. module.exports.GetGoodsList = GetGoodsList;