| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- // src/lib/services/productService.ts
- import api from '@/lib/api';
- export interface ProductListParams {
- page?: number;
- size?: number;
- page_size?: number;
- keyword?: string;
- is_active?: boolean;
- category?: string;
- [key: string]: any;
- }
- export const productService = {
- /**
- * 获取已启用的产品列表 (客户端前台)
- */
- async listEnabled(params?: ProductListParams) {
- const res = await api.get('/api/vas/product/list-enable', { params });
- return res.data;
- },
- /**
- * 分页查询产品列表 (后台)
- */
- async listAll(params?: ProductListParams) {
- const res = await api.get('/api/vas/product/list', { params });
- return res.data;
- },
- /**
- * 获取产品详情
- */
- async getDetail(productId: number) {
- const res = await api.get('/api/vas/product/detail', {
- params: { product_id: productId },
- });
- return res.data;
- },
- /**
- * 新建产品
- */
- async create(payload: Record<string, any>) {
- const res = await api.post('/api/vas/product/create', payload);
- return res.data;
- },
- /**
- * 更新产品
- */
- async update(productId: number, payload: Record<string, any>) {
- const res = await api.post('/api/vas/product/update', payload, {
- params: { product_id: productId },
- });
- return res.data;
- },
- /**
- * 删除产品
- */
- async delete(productId: number) {
- const res = await api.delete('/api/vas/product/delete', {
- params: { product_id: productId },
- });
- return res.data;
- },
- /**
- * 查询所有表单 Schema 列表
- */
- async listSchemas(params?: { page?: number; size?: number; page_size?: number; keyword?: string }) {
- const res = await api.get('/api/vas/schema/list', { params });
- return res.data;
- },
- /**
- * 获取指定 Schema 详情
- */
- async getSchemaDetail(schemaId: number) {
- const res = await api.get('/api/vas/schema/detail', {
- params: { schema_id: schemaId },
- });
- return res.data;
- },
- /**
- * 新增动态表单 Schema
- */
- async createSchema(payload: Record<string, any>) {
- const res = await api.post('/api/vas/schema/create', payload);
- return res.data;
- },
- /**
- * 更新动态表单 Schema
- */
- async updateSchema(schemaId: number, payload: Record<string, any>) {
- const res = await api.post('/api/vas/schema/update', payload, {
- params: { schema_id: schemaId },
- });
- return res.data;
- },
- /**
- * 删除表单 Schema
- */
- async deleteSchema(schemaId: number) {
- const res = await api.delete('/api/vas/schema/delete', {
- params: { id: schemaId, schema_id: schemaId },
- });
- return res.data;
- },
- /**
- * 获取产品的路由配置列表
- */
- async listRoutings(productId: number) {
- const res = await api.get('/api/vas/product_routing/list', {
- params: { product_id: productId },
- });
- if (res.data && Array.isArray(res.data.data)) {
- // 按照 priority 降序,其次 id 降序排列
- res.data.data.sort((a: any, b: any) => {
- const pDiff = (b.priority ?? 0) - (a.priority ?? 0);
- if (pDiff !== 0) return pDiff;
- return (b.id ?? 0) - (a.id ?? 0);
- });
- }
- return res.data;
- },
- /**
- * 创建产品路由配置
- */
- async createRouting(payload: Record<string, any>) {
- const normalizedPayload = {
- product_id: payload.product_id,
- routing_key: payload.routing_key,
- script_version: payload.script_version || 'latest',
- priority: Number(payload.priority) || 10,
- config: payload.config && typeof payload.config === 'object' ? payload.config : {},
- };
- const res = await api.post('/api/vas/product_routing/create', normalizedPayload);
- return res.data;
- },
- /**
- * 更新产品路由配置 (若后端无独立 update 端点则优雅回退至先删后建)
- */
- async updateRouting(id: number, payload: Record<string, any>) {
- try {
- const res = await api.post('/api/vas/product_routing/update', payload, {
- params: { id },
- });
- return res.data;
- } catch (err: any) {
- // 若后端 404/405 无 update 接口,采用删除 + 重新创建策略
- if (err?.response?.status === 404 || err?.response?.status === 405) {
- await api.delete('/api/vas/product_routing/delete', {
- params: { id },
- });
- const normalizedPayload = {
- product_id: payload.product_id,
- routing_key: payload.routing_key,
- script_version: payload.script_version || 'latest',
- priority: Number(payload.priority) || 10,
- config: payload.config && typeof payload.config === 'object' ? payload.config : {},
- };
- const createRes = await api.post('/api/vas/product_routing/create', normalizedPayload);
- return createRes.data;
- }
- throw err;
- }
- },
- /**
- * 删除产品路由配置
- */
- async deleteRouting(id: number) {
- const res = await api.delete('/api/vas/product_routing/delete', {
- params: { id },
- });
- return res.data;
- },
- };
|