From 281b471e4357d047d7d30cc7f09e69a887214a62 Mon Sep 17 00:00:00 2001 From: zhangjing1 Date: Tue, 17 Mar 2026 19:40:53 +0800 Subject: [PATCH] fix: shipping template form encoding and sale category customize_page_id - request: add qs serialization when Content-Type is application/x-www-form-urlencoded so backend receives form body instead of JSON - request: append country_code to string data (e.g. qs string) instead of assigning property to avoid breaking form body - shipping: create/update template APIs send data via qs.stringify(params) with form-urlencoded header for correct backend parsing - saleCategory: use numeric 0 for customize_page_id default and in submit, avoid empty string for consistency Made-with: Cursor --- src/api/request/index.js | 9 +++++++++ src/api/request/request.js | 9 +++++++-- src/api/shipping.js | 12 ++++++++++-- src/view/goods/saleCategory.vue | 10 +++++----- 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/api/request/index.js b/src/api/request/index.js index 2fceabd..b2fcda4 100755 --- a/src/api/request/index.js +++ b/src/api/request/index.js @@ -2,6 +2,7 @@ * Copyright © ShopeX (http://www.shopex.cn). All rights reserved. * See LICENSE file for license details. */ +import qs from 'qs' import Vue from 'vue' import store from '@/store' import router from '@/router' @@ -93,6 +94,14 @@ function createRequestClient() { config.data = formData // FormData 会自动设置 Content-Type,不需要手动设置 delete config.headers['Content-Type'] + } else if ( + config.headers && + config.headers['Content-Type'] === 'application/x-www-form-urlencoded' && + typeof config.data === 'object' && + config.data !== null + ) { + // 声明为表单时若 data 仍是对象,则序列化为 form-urlencoded,避免后端解析失败 + config.data = qs.stringify(config.data) } } diff --git a/src/api/request/request.js b/src/api/request/request.js index 9d6b6c1..563cecb 100755 --- a/src/api/request/request.js +++ b/src/api/request/request.js @@ -60,11 +60,16 @@ class RequestClient { ar: 'ar-SA' } if (lang && !skipCountryCode) { + const countryCode = langMap[lang] if (config.data) { - config.data.country_code = langMap[lang] + if (typeof config.data === 'string') { + config.data = config.data + (config.data ? '&' : '') + 'country_code=' + encodeURIComponent(countryCode) + } else { + config.data.country_code = countryCode + } } else { config.data = { - country_code: langMap[lang] + country_code: countryCode } } } diff --git a/src/api/shipping.js b/src/api/shipping.js index 8682d0c..40ead6a 100755 --- a/src/api/shipping.js +++ b/src/api/shipping.js @@ -2,6 +2,7 @@ * Copyright © ShopeX (http://www.shopex.cn). All rights reserved. * See LICENSE file for license details. */ +import qs from 'qs' import { fetch } from './request' export function getShippingTemplatesList(query) { @@ -19,6 +20,7 @@ export function getShippingTemplatesInfo(id) { }) } +/** 以表单格式提交,避免 Content-Type 为 form-urlencoded 但 body 为 JSON 导致后端解析失败 */ export function createShippingTemplates(params) { return fetch({ url: '/shipping/templates/create', @@ -26,15 +28,21 @@ export function createShippingTemplates(params) { headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, - params: params + params, + data: qs.stringify(params) }) } +/** 编辑同样以表单格式提交 */ export function updateShippingTemplates(id, params) { return fetch({ url: '/shipping/templates/update/' + id, method: 'put', - params: params + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + }, + params, + data: qs.stringify(params) }) } diff --git a/src/view/goods/saleCategory.vue b/src/view/goods/saleCategory.vue index 3700e85..e2d52ec 100755 --- a/src/view/goods/saleCategory.vue +++ b/src/view/goods/saleCategory.vue @@ -146,7 +146,7 @@ export default { parent_id: 0, parent_name: '', image_url: '', - customize_page_id: '' + customize_page_id: 0 }, categoryFormList: [ { @@ -226,7 +226,7 @@ export default { parent_id: 0, parent_name: '', image_url: '', - customize_page_id: '' + customize_page_id: 0 } this.categoryDialog = true }, @@ -238,7 +238,7 @@ export default { sort, parent_id, image_url, - customize_page_id: customize_page_id == 0 ? '' : customize_page_id + customize_page_id } this.categoryDialog = true }, @@ -253,7 +253,7 @@ export default { parent_id: category_id, parent_name: category_name, image_url: '', - customize_page_id: '' + customize_page_id: 0 } this.categoryDialog = true }, @@ -297,7 +297,7 @@ export default { resolve(list) }, async onCategoryFormSubmit() { - const { category_name, sort, image_url, customize_page_id, parent_id, category_id } = + const { category_name, sort, image_url, customize_page_id=0, parent_id, category_id } = this.categoryForm if (category_id) { await this.$api.goods.editCategory({