fix: 修复api.request的POST/PUT数据序列化问题,统一处理
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
@@ -19,11 +19,17 @@ const request = (url, method, query = {}, data = {}) => {
|
|||||||
requestData = 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) => {
|
return new Promise((resolve, reject) => {
|
||||||
uni.request({
|
uni.request({
|
||||||
url: BASE_URL + url,
|
url: BASE_URL + url,
|
||||||
method: method,
|
method: method,
|
||||||
data: requestData,
|
data: requestBody,
|
||||||
header: {
|
header: {
|
||||||
'Content-Type': useFormData ? 'application/x-www-form-urlencoded' : 'application/json',
|
'Content-Type': useFormData ? 'application/x-www-form-urlencoded' : 'application/json',
|
||||||
'Authorization': token ? `Bearer ${token}` : '',
|
'Authorization': token ? `Bearer ${token}` : '',
|
||||||
@@ -55,4 +61,4 @@ const request = (url, method, query = {}, data = {}) => {
|
|||||||
|
|
||||||
export default {
|
export default {
|
||||||
request
|
request
|
||||||
}
|
}
|
||||||
@@ -15,50 +15,14 @@ export default {
|
|||||||
* 新增分类
|
* 新增分类
|
||||||
*/
|
*/
|
||||||
createCategory(data) {
|
createCategory(data) {
|
||||||
const token = uni.getStorageSync('token')
|
return api.request('/products/categories', 'POST', {}, data)
|
||||||
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
|
|
||||||
})
|
|
||||||
})
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 修改分类
|
* 修改分类
|
||||||
*/
|
*/
|
||||||
updateCategory(id, data) {
|
updateCategory(id, data) {
|
||||||
const token = uni.getStorageSync('token')
|
return api.request(`/products/categories/${id}`, 'PUT', {}, data)
|
||||||
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
|
|
||||||
})
|
|
||||||
})
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -134,34 +98,7 @@ export default {
|
|||||||
* 保存种类的属性定义
|
* 保存种类的属性定义
|
||||||
*/
|
*/
|
||||||
saveCategoryAttributes(categoryId, attrs) {
|
saveCategoryAttributes(categoryId, attrs) {
|
||||||
const token = uni.getStorageSync('token')
|
return api.request(`/products/categories/${categoryId}/attributes`, 'POST', {}, { attributes: attrs })
|
||||||
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
|
|
||||||
})
|
|
||||||
})
|
|
||||||
},
|
},
|
||||||
|
|
||||||
// ============ 商品属性 ============
|
// ============ 商品属性 ============
|
||||||
|
|||||||
@@ -211,21 +211,16 @@ export default {
|
|||||||
this.showAttrDialog = false
|
this.showAttrDialog = false
|
||||||
},
|
},
|
||||||
async saveAttributes() {
|
async saveAttributes() {
|
||||||
console.log('this.attributes:', JSON.stringify(this.attributes))
|
|
||||||
const validAttrs = this.attributes.filter(a => a.name.trim())
|
const validAttrs = this.attributes.filter(a => a.name.trim())
|
||||||
console.log('validAttrs:', JSON.stringify(validAttrs))
|
|
||||||
if (validAttrs.length === 0) {
|
if (validAttrs.length === 0) {
|
||||||
uni.showToast({ title: '请至少添加一个属性', icon: 'none' })
|
uni.showToast({ title: '请至少添加一个属性', icon: 'none' })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const payload = { attributes: JSON.parse(JSON.stringify(validAttrs)) }
|
await productApi.saveCategoryAttributes(this.currentCategory.categoryId, validAttrs)
|
||||||
console.log('payload:', JSON.stringify(payload))
|
|
||||||
await api.request(`/products/categories/${this.currentCategory.categoryId}/attributes`, 'POST', {}, payload)
|
|
||||||
uni.showToast({ title: '保存成功', icon: 'success' })
|
uni.showToast({ title: '保存成功', icon: 'success' })
|
||||||
this.closeAttrDialog()
|
this.closeAttrDialog()
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('保存失败:', e)
|
|
||||||
uni.showToast({ title: '保存失败', icon: 'none' })
|
uni.showToast({ title: '保存失败', icon: 'none' })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user