_ware.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import { MockRequest, MockStatusError } from '@delon/mock';
  2. import { deepCopy } from '@delon/util';
  3. import { genMp } from './utils';
  4. interface UserPro {
  5. cid: number;
  6. cname: string;
  7. id: number;
  8. name: string;
  9. mp: string;
  10. stock: number;
  11. outer_id: string;
  12. market_price: number;
  13. price: number;
  14. sale_num: number;
  15. status: string;
  16. modified: Date;
  17. [key: string]: any;
  18. }
  19. const DATA: UserPro[] = [];
  20. for (let i = 1; i <= 20; i += 1) {
  21. const name = ['HUAWEI Mate 20 Pro', '小米MAX3', 'IPhone X', 'Gree 8,000 BTU Portable Air Conditioner'][
  22. Math.floor(Math.random() * 10) % 4
  23. ];
  24. DATA.push({
  25. cid: i * 1000,
  26. cname: '',
  27. id: i + 10000,
  28. name,
  29. mp: genMp(),
  30. stock: Math.floor(Math.random() * 1000) % 1000,
  31. outer_id: `S50-${Math.floor(Math.random() * 100) % 100}`,
  32. market_price: Math.floor(Math.random() * 1000) % 1000,
  33. price: Math.floor(Math.random() * 1000) % 1000,
  34. sale_num: Math.floor(Math.random() * 200) % 200,
  35. modified: new Date(),
  36. status: ['CUSTORMER_DOWN', 'ON_SALE', 'AUDIT_AWAIT', 'DELETED'][Math.floor(Math.random() * 10) % 4],
  37. brand: 1,
  38. place: 1,
  39. });
  40. }
  41. function get(params: any): any {
  42. let ret = deepCopy(DATA);
  43. if (params.q) {
  44. ret = ret.filter((data: any) => data.name.indexOf(params.q) > -1);
  45. }
  46. if (params.email) {
  47. ret = ret.filter((data: any) => data.email.indexOf(params.email) > -1);
  48. }
  49. return ret;
  50. }
  51. function getIdx(id: number): number {
  52. id = +id;
  53. const idx = DATA.findIndex((w) => w.id === id);
  54. if (idx === -1) {
  55. throw new MockStatusError(404);
  56. }
  57. return idx;
  58. }
  59. export const WARES = {
  60. '/ware': (req: MockRequest) => {
  61. const pi = +(req.queryString.pi || 1);
  62. const ps = +(req.queryString.ps || 10);
  63. const data = get(req.queryString);
  64. return {
  65. total: data.length,
  66. list: data.slice((pi - 1) * ps, pi * ps),
  67. };
  68. },
  69. 'POST /ware': (req: MockRequest) => {
  70. const id = req.body.id || 0;
  71. if (id > 0) {
  72. const idx = getIdx(id);
  73. DATA[idx] = { ...DATA[idx], ...req.body };
  74. return { msg: 'ok', item: DATA[idx] };
  75. }
  76. const item = { ...req.body, id: DATA.sort((a, b) => b.id - a.id)[0].id + 1 };
  77. DATA.push(item);
  78. return { msg: 'ok', item };
  79. },
  80. '/ware/:id': (req: MockRequest) => {
  81. const idx = getIdx(req.params.id || 0);
  82. const item = {
  83. id: 0,
  84. brand: 1,
  85. is_7return: true,
  86. prop: {
  87. 1: '是',
  88. 2: '24天',
  89. 3: '0.5克',
  90. },
  91. place: 1,
  92. weight: 10,
  93. skus: [
  94. {
  95. id: 10001,
  96. attributes: '1:10',
  97. names: [`红色`, `S`],
  98. price: 1000,
  99. stock: 10,
  100. },
  101. {
  102. id: 10002,
  103. attributes: '1:11',
  104. names: [`红色`, `M`],
  105. price: 1000,
  106. stock: 11,
  107. },
  108. {
  109. id: 10003,
  110. attributes: '3:10',
  111. names: [`蓝色1`, `S`],
  112. price: 1000,
  113. stock: 12,
  114. },
  115. {
  116. id: 10004,
  117. attributes: '3:11',
  118. names: [`蓝色1`, `M`],
  119. price: 1000,
  120. stock: 13,
  121. },
  122. ],
  123. imgs: {
  124. 0: ['https://randomuser.me/api/portraits/lego/0.jpg'],
  125. 1: ['https://randomuser.me/api/portraits/lego/1.jpg'],
  126. 3: ['https://randomuser.me/api/portraits/lego/3.jpg'],
  127. },
  128. desc: `<p>Test</p>`,
  129. ...(DATA[idx] as any),
  130. };
  131. return item;
  132. },
  133. 'DELETE /ware/:id': (req: MockRequest) => {
  134. const idx = getIdx(req.params.id || 0);
  135. DATA.splice(idx, 1);
  136. return { msg: 'ok' };
  137. },
  138. 'POST /ware/status': (req: MockRequest) => {
  139. const idx = getIdx(req.body.id || 0);
  140. const item = DATA[idx];
  141. item.status = req.body.status;
  142. return { msg: 'ok', item };
  143. },
  144. '/ware/cat': [
  145. { id: 1, name: '颜色', value: '红色', color: '#f5222d', type: 'color' },
  146. { id: 2, name: '颜色', value: '绿色', color: '#a0d911', type: 'color' },
  147. { id: 3, name: '颜色', value: '蓝色', color: '#1890ff', type: 'color' },
  148. { id: 4, name: '颜色', value: '洋红', color: '#eb2f96', type: 'color' },
  149. { id: 10, name: '尺寸', value: 'S', type: 'size' },
  150. { id: 11, name: '尺寸', value: 'M', type: 'size' },
  151. { id: 12, name: '尺寸', value: 'L', type: 'size' },
  152. { id: 13, name: '尺寸', value: 'XL', type: 'size' },
  153. { id: 14, name: '尺寸', value: 'XXL', type: 'size' },
  154. { id: 15, name: '尺寸', value: 'XXXL', type: 'size' },
  155. ],
  156. };