GetGoods.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. const API = require("../../lib/API");
  2. const db = require("../../plugin/DataBase/db");
  3. const { BaseStdResponse } = require("../../BaseStdResponse");
  4. // 获取产品
  5. class GetProduct extends API {
  6. constructor() {
  7. super();
  8. this.setPath('/Goods')
  9. this.setMethod('GET')
  10. }
  11. async onRequest(req, res) {
  12. let { id } = req.query
  13. if ([id].some(value => value === '' || value === null || value === undefined))
  14. return res.json({
  15. ...BaseStdResponse.MISSING_PARAMETER
  16. })
  17. let sql = `
  18. SELECT
  19. id,
  20. name,
  21. content,
  22. price,
  23. num,
  24. \`limit\`,
  25. icon,
  26. isHot,
  27. description,
  28. category,
  29. features,
  30. lepao_count,
  31. ic_count,
  32. vip,
  33. vip_validity_type,
  34. vip_valid_days,
  35. vip_fixed_expire_time,
  36. allow_refund
  37. FROM
  38. goods
  39. WHERE id = ? AND state = 1
  40. `
  41. let rows = await db.query(sql, [id])
  42. if (!rows || rows.length === 0)
  43. return res.json({
  44. ...BaseStdResponse.MISSING_FILE,
  45. msg: '获取商品详情失败!'
  46. })
  47. res.json({
  48. ...BaseStdResponse.OK,
  49. data: rows[0]
  50. })
  51. sql = 'UPDATE goods SET views = views + 1 WHERE id = ?'
  52. await db.query(sql, [id])
  53. }
  54. }
  55. module.exports.GetProduct = GetProduct;