routes.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import express from 'express';
  2. const router = express.Router();
  3. // GET /api/order — 查询订单列表
  4. router.get('/', async (req, res) => {
  5. try {
  6. const Parse = (globalThis as any).Parse;
  7. const q = new Parse.Query('Order');
  8. q.notEqualTo('isDeleted', true);
  9. q.descending('createdAt');
  10. q.limit(100);
  11. const { status, search } = req.query;
  12. if (status && status !== 'all') {
  13. q.equalTo('status', status);
  14. }
  15. if (search) {
  16. q.contains('customerName', search as string);
  17. }
  18. const list = await q.find({ useMasterKey: true });
  19. const data = list.map((o: any) => o.toJSON());
  20. res.json({ success: true, data });
  21. } catch (e: any) {
  22. res.status(500).json({ success: false, error: e.message });
  23. }
  24. });
  25. // GET /api/order/:id — 查询单个订单
  26. router.get('/:id', async (req, res) => {
  27. try {
  28. const Parse = (globalThis as any).Parse;
  29. const q = new Parse.Query('Order');
  30. q.equalTo('objectId', req.params.id);
  31. const order = await q.first({ useMasterKey: true });
  32. if (!order) {
  33. return res.status(404).json({ success: false, error: '订单不存在' });
  34. }
  35. res.json({ success: true, data: order.toJSON() });
  36. } catch (e: any) {
  37. res.status(500).json({ success: false, error: e.message });
  38. }
  39. });
  40. // POST /api/order — 创建/更新订单
  41. router.post('/', async (req, res) => {
  42. try {
  43. const Parse = (globalThis as any).Parse;
  44. const { objectId, ...fields } = req.body;
  45. let obj: any;
  46. if (objectId) {
  47. // 更新已有订单
  48. const q = new Parse.Query('Order');
  49. obj = await q.get(objectId, { useMasterKey: true });
  50. } else {
  51. // 创建新订单
  52. const Order = Parse.Object.extend('Order');
  53. obj = new Order();
  54. }
  55. // 设置字段
  56. Object.entries(fields).forEach(([key, value]) => {
  57. obj.set(key, value);
  58. });
  59. if (!objectId) {
  60. obj.set('isDeleted', false);
  61. }
  62. const saved = await obj.save(null, { useMasterKey: true });
  63. res.json({ success: true, data: saved.toJSON() });
  64. } catch (e: any) {
  65. res.status(500).json({ success: false, error: e.message });
  66. }
  67. });
  68. export default router;