From 10269adfefae35064e83d794e43b56904ae8b328 Mon Sep 17 00:00:00 2001 From: Agent Date: Mon, 30 Mar 2026 00:22:59 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8Dapi.request=E7=9A=84PO?= =?UTF-8?q?ST/PUT=E6=95=B0=E6=8D=AE=E5=BA=8F=E5=88=97=E5=8C=96=E9=97=AE?= =?UTF-8?q?=E9=A2=98=EF=BC=8C=E7=BB=9F=E4=B8=80=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/index.js | 10 ++++-- src/api/product.js | 69 ++---------------------------------- src/pages/category/index.vue | 7 +--- 3 files changed, 12 insertions(+), 74 deletions(-) diff --git a/src/api/index.js b/src/api/index.js index 02eafab..829d9d5 100644 --- a/src/api/index.js +++ b/src/api/index.js @@ -19,11 +19,17 @@ const request = (url, method, query = {}, data = {}) => { requestData = data } + // POST/PUT 请求需要 JSON 序列化 + let requestBody = requestData + if (method !== 'GET' && Object.keys(requestData).length > 0) { + requestBody = JSON.stringify(requestData) + } + return new Promise((resolve, reject) => { uni.request({ url: BASE_URL + url, method: method, - data: requestData, + data: requestBody, header: { 'Content-Type': useFormData ? 'application/x-www-form-urlencoded' : 'application/json', 'Authorization': token ? `Bearer ${token}` : '', @@ -55,4 +61,4 @@ const request = (url, method, query = {}, data = {}) => { export default { request -} +} \ No newline at end of file diff --git a/src/api/product.js b/src/api/product.js index 7bb6269..e3e5ed1 100644 --- a/src/api/product.js +++ b/src/api/product.js @@ -15,50 +15,14 @@ export default { * 新增分类 */ createCategory(data) { - const token = uni.getStorageSync('token') - const role = uni.getStorageSync('role') || '' - return new Promise((resolve, reject) => { - uni.request({ - url: `${import.meta.env.VITE_API_BASE_URL}/products/categories`, - method: 'POST', - data: JSON.stringify(data), - header: { - 'Content-Type': 'application/json', - 'Authorization': token ? `Bearer ${token}` : '', - 'X-User-Role': role - }, - success: (res) => { - if (res.data.code === 0) resolve(res.data.data) - else reject(res.data) - }, - fail: reject - }) - }) + return api.request('/products/categories', 'POST', {}, data) }, /** * 修改分类 */ updateCategory(id, data) { - const token = uni.getStorageSync('token') - const role = uni.getStorageSync('role') || '' - return new Promise((resolve, reject) => { - uni.request({ - url: `${import.meta.env.VITE_API_BASE_URL}/products/categories/${id}`, - method: 'PUT', - data: JSON.stringify(data), - header: { - 'Content-Type': 'application/json', - 'Authorization': token ? `Bearer ${token}` : '', - 'X-User-Role': role - }, - success: (res) => { - if (res.data.code === 0) resolve(res.data.data) - else reject(res.data) - }, - fail: reject - }) - }) + return api.request(`/products/categories/${id}`, 'PUT', {}, data) }, /** @@ -134,34 +98,7 @@ export default { * 保存种类的属性定义 */ saveCategoryAttributes(categoryId, attrs) { - const token = uni.getStorageSync('token') - const role = uni.getStorageSync('role') || '' - return new Promise((resolve, reject) => { - // 将数据转为 form 数据格式 - let formData = new Object() - attrs.forEach((attr, index) => { - formData[`attributes[${index}].name`] = attr.name || '' - formData[`attributes[${index}].attrType`] = attr.attrType || 'number' - formData[`attributes[${index}].unit`] = attr.unit || '' - }) - uni.request({ - url: `${import.meta.env.VITE_API_BASE_URL}/products/categories/${categoryId}/attributes`, - method: 'POST', - data: formData, - header: { - 'Authorization': token ? `Bearer ${token}` : '', - 'X-User-Role': role - }, - success: (res) => { - if (res.data.code === 0) { - resolve(res.data.data) - } else { - reject(res.data) - } - }, - fail: reject - }) - }) + return api.request(`/products/categories/${categoryId}/attributes`, 'POST', {}, { attributes: attrs }) }, // ============ 商品属性 ============ diff --git a/src/pages/category/index.vue b/src/pages/category/index.vue index ea8f4e6..bae3729 100644 --- a/src/pages/category/index.vue +++ b/src/pages/category/index.vue @@ -211,21 +211,16 @@ export default { this.showAttrDialog = false }, async saveAttributes() { - console.log('this.attributes:', JSON.stringify(this.attributes)) const validAttrs = this.attributes.filter(a => a.name.trim()) - console.log('validAttrs:', JSON.stringify(validAttrs)) if (validAttrs.length === 0) { uni.showToast({ title: '请至少添加一个属性', icon: 'none' }) return } try { - const payload = { attributes: JSON.parse(JSON.stringify(validAttrs)) } - console.log('payload:', JSON.stringify(payload)) - await api.request(`/products/categories/${this.currentCategory.categoryId}/attributes`, 'POST', {}, payload) + await productApi.saveCategoryAttributes(this.currentCategory.categoryId, validAttrs) uni.showToast({ title: '保存成功', icon: 'success' }) this.closeAttrDialog() } catch (e) { - console.error('保存失败:', e) uni.showToast({ title: '保存失败', icon: 'none' }) } }