productService.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // src/lib/services/productService.ts
  2. import api from '@/lib/api';
  3. export interface ProductListParams {
  4. page?: number;
  5. size?: number;
  6. page_size?: number;
  7. keyword?: string;
  8. is_active?: boolean;
  9. category?: string;
  10. [key: string]: any;
  11. }
  12. export const productService = {
  13. /**
  14. * 获取已启用的产品列表 (客户端前台)
  15. */
  16. async listEnabled(params?: ProductListParams) {
  17. const res = await api.get('/api/vas/product/list-enable', { params });
  18. return res.data;
  19. },
  20. /**
  21. * 分页查询产品列表 (后台)
  22. */
  23. async listAll(params?: ProductListParams) {
  24. const res = await api.get('/api/vas/product/list', { params });
  25. return res.data;
  26. },
  27. /**
  28. * 获取产品详情
  29. */
  30. async getDetail(productId: number) {
  31. const res = await api.get('/api/vas/product/detail', {
  32. params: { product_id: productId },
  33. });
  34. return res.data;
  35. },
  36. /**
  37. * 新建产品
  38. */
  39. async create(payload: Record<string, any>) {
  40. const res = await api.post('/api/vas/product/create', payload);
  41. return res.data;
  42. },
  43. /**
  44. * 更新产品
  45. */
  46. async update(productId: number, payload: Record<string, any>) {
  47. const res = await api.post('/api/vas/product/update', payload, {
  48. params: { product_id: productId },
  49. });
  50. return res.data;
  51. },
  52. /**
  53. * 删除产品
  54. */
  55. async delete(productId: number) {
  56. const res = await api.delete('/api/vas/product/delete', {
  57. params: { product_id: productId },
  58. });
  59. return res.data;
  60. },
  61. /**
  62. * 查询所有表单 Schema 列表
  63. */
  64. async listSchemas(params?: { page?: number; size?: number; page_size?: number; keyword?: string }) {
  65. const res = await api.get('/api/vas/schema/list', { params });
  66. return res.data;
  67. },
  68. /**
  69. * 获取指定 Schema 详情
  70. */
  71. async getSchemaDetail(schemaId: number) {
  72. const res = await api.get('/api/vas/schema/detail', {
  73. params: { schema_id: schemaId },
  74. });
  75. return res.data;
  76. },
  77. /**
  78. * 新增动态表单 Schema
  79. */
  80. async createSchema(payload: Record<string, any>) {
  81. const res = await api.post('/api/vas/schema/create', payload);
  82. return res.data;
  83. },
  84. /**
  85. * 更新动态表单 Schema
  86. */
  87. async updateSchema(schemaId: number, payload: Record<string, any>) {
  88. const res = await api.post('/api/vas/schema/update', payload, {
  89. params: { schema_id: schemaId },
  90. });
  91. return res.data;
  92. },
  93. /**
  94. * 删除表单 Schema
  95. */
  96. async deleteSchema(schemaId: number) {
  97. const res = await api.delete('/api/vas/schema/delete', {
  98. params: { id: schemaId, schema_id: schemaId },
  99. });
  100. return res.data;
  101. },
  102. /**
  103. * 获取产品的路由配置列表
  104. */
  105. async listRoutings(productId: number) {
  106. const res = await api.get('/api/vas/product_routing/list', {
  107. params: { product_id: productId },
  108. });
  109. if (res.data && Array.isArray(res.data.data)) {
  110. // 按照 priority 降序,其次 id 降序排列
  111. res.data.data.sort((a: any, b: any) => {
  112. const pDiff = (b.priority ?? 0) - (a.priority ?? 0);
  113. if (pDiff !== 0) return pDiff;
  114. return (b.id ?? 0) - (a.id ?? 0);
  115. });
  116. }
  117. return res.data;
  118. },
  119. /**
  120. * 创建产品路由配置
  121. */
  122. async createRouting(payload: Record<string, any>) {
  123. const normalizedPayload = {
  124. product_id: payload.product_id,
  125. routing_key: payload.routing_key,
  126. script_version: payload.script_version || 'latest',
  127. priority: Number(payload.priority) || 10,
  128. config: payload.config && typeof payload.config === 'object' ? payload.config : {},
  129. };
  130. const res = await api.post('/api/vas/product_routing/create', normalizedPayload);
  131. return res.data;
  132. },
  133. /**
  134. * 更新产品路由配置 (若后端无独立 update 端点则优雅回退至先删后建)
  135. */
  136. async updateRouting(id: number, payload: Record<string, any>) {
  137. try {
  138. const res = await api.post('/api/vas/product_routing/update', payload, {
  139. params: { id },
  140. });
  141. return res.data;
  142. } catch (err: any) {
  143. // 若后端 404/405 无 update 接口,采用删除 + 重新创建策略
  144. if (err?.response?.status === 404 || err?.response?.status === 405) {
  145. await api.delete('/api/vas/product_routing/delete', {
  146. params: { id },
  147. });
  148. const normalizedPayload = {
  149. product_id: payload.product_id,
  150. routing_key: payload.routing_key,
  151. script_version: payload.script_version || 'latest',
  152. priority: Number(payload.priority) || 10,
  153. config: payload.config && typeof payload.config === 'object' ? payload.config : {},
  154. };
  155. const createRes = await api.post('/api/vas/product_routing/create', normalizedPayload);
  156. return createRes.data;
  157. }
  158. throw err;
  159. }
  160. },
  161. /**
  162. * 删除产品路由配置
  163. */
  164. async deleteRouting(id: number) {
  165. const res = await api.delete('/api/vas/product_routing/delete', {
  166. params: { id },
  167. });
  168. return res.data;
  169. },
  170. };