AddGoods.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. const API = require("../../../lib/API");
  2. const db = require("../../../plugin/DataBase/db");
  3. const AccessControl = require("../../../lib/AccessControl");
  4. const { BaseStdResponse } = require("../../../BaseStdResponse");
  5. // 添加/编辑商品
  6. class AddProduct extends API {
  7. constructor() {
  8. super()
  9. this.setPath('/Admin/Goods')
  10. this.setMethod('POST')
  11. }
  12. async onRequest(req, res) {
  13. let {
  14. uuid,
  15. session,
  16. id,
  17. name,
  18. state,
  19. content,
  20. price,
  21. num,
  22. lepao_count,
  23. ic_count,
  24. icon,
  25. description,
  26. features,
  27. vip = 0,
  28. vip_validity_type = 'none',
  29. vip_valid_days = 0,
  30. vip_fixed_expire_time = 0,
  31. allow_refund = 1
  32. } = req.body
  33. const goodsIcon = (icon && String(icon).trim()) ? String(icon).trim().slice(0, 16) : '🏃'
  34. const goodsDesc = description != null ? String(description).trim().slice(0, 200) : ''
  35. let goodsFeatures = '[]'
  36. if (features != null && features !== '') {
  37. const raw = typeof features === 'string' ? features : JSON.stringify(features)
  38. try {
  39. const arr = JSON.parse(raw)
  40. goodsFeatures = JSON.stringify(Array.isArray(arr) ? arr.map(s => String(s).trim()).filter(Boolean).slice(0, 6) : [])
  41. } catch {
  42. goodsFeatures = '[]'
  43. }
  44. }
  45. const safeVip = Number(vip) === 1 ? 1 : 0
  46. const safeVipType = ['none', 'days', 'fixed'].includes(String(vip_validity_type)) ? String(vip_validity_type) : 'none'
  47. const safeVipDays = Math.max(0, Number(vip_valid_days || 0))
  48. const safeVipFixedExpire = Math.max(0, Number(vip_fixed_expire_time || 0))
  49. const safeAllowRefund = Number(allow_refund) === 0 ? 0 : 1
  50. if ([uuid, session, name, state, content, price, num, lepao_count, ic_count].some(value => value === '' || value === null || value === undefined))
  51. return res.json({
  52. ...BaseStdResponse.MISSING_PARAMETER
  53. })
  54. // 检查 session
  55. if (!await AccessControl.checkSession(uuid, session))
  56. return res.status(401).json({
  57. ...BaseStdResponse.ACCESS_DENIED
  58. })
  59. // 检查权限
  60. let permission = await AccessControl.getPermission(uuid)
  61. if (!permission.includes("admin") && !permission.includes("product"))
  62. return res.json({
  63. ...BaseStdResponse.PERMISSION_DENIED
  64. })
  65. let sql, r
  66. const time = new Date().getTime()
  67. if (!id) {
  68. sql = 'INSERT INTO goods (name, create_user, create_time, update_time, state, content, price, lepao_count, ic_count, num, icon, description, features, vip, vip_validity_type, vip_valid_days, vip_fixed_expire_time, allow_refund) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'
  69. r = await db.query(sql, [name, uuid, time, time, state, content, price, lepao_count, ic_count, num, goodsIcon, goodsDesc, goodsFeatures, safeVip, safeVipType, safeVipDays, safeVipFixedExpire, safeAllowRefund])
  70. } else {
  71. sql = 'UPDATE goods SET name = ?, update_user = ?, update_time = ?, state = ?, content = ?, price = ?, lepao_count = ?, ic_count = ?, num = ?, icon = ?, description = ?, features = ?, vip = ?, vip_validity_type = ?, vip_valid_days = ?, vip_fixed_expire_time = ?, allow_refund = ? WHERE id = ?'
  72. r = await db.query(sql, [name, uuid, time, state, content, price, lepao_count, ic_count, num, goodsIcon, goodsDesc, goodsFeatures, safeVip, safeVipType, safeVipDays, safeVipFixedExpire, safeAllowRefund, id])
  73. }
  74. try {
  75. if (r && r.affectedRows > 0) {
  76. res.json({
  77. ...BaseStdResponse.OK
  78. })
  79. } else {
  80. res.json({ ...BaseStdResponse.ERR, endpoint: 7894378, msg: '编辑商品失败!数据库错误!请检查参数是否正确' })
  81. }
  82. } catch (err) {
  83. this.logger.error(`编辑商品失败!${err.stack}`)
  84. res.json({
  85. ...BaseStdResponse.ERR,
  86. msg: "编辑商品失败!",
  87. });
  88. }
  89. }
  90. }
  91. module.exports.AddProduct = AddProduct;