Browse Source

修复客服列表页面网络请求失败问题

pull/1/head
SwTt29 2 months ago
parent
commit
77e4750737
  1. 24
      pages/customer-service/index.js
  2. 11
      utils/api.js

24
pages/customer-service/index.js

@ -22,29 +22,15 @@ Page({
// 获取当前用户类型参数
const userType = this.data.userType || 'seller';
const res = await new Promise((resolve, reject) => {
wx.request({
url: `http://localhost:3003/api/managers?type=${userType}`,
method: 'GET',
timeout: 15000,
header: {
'content-type': 'application/json'
},
success: resolve,
fail: (error) => {
console.error('网络请求失败:', error);
reject(error);
}
});
});
// 使用api.js中的请求工具,自动获取正确的服务器地址
const apiResponse = await api.request(`/api/managers?type=${userType}`, 'GET');
console.log('API响应状态码:', res?.statusCode);
console.log('API响应数据:', res?.data ? JSON.stringify(res.data) : 'undefined');
console.log('API响应数据:', apiResponse ? JSON.stringify(apiResponse) : 'undefined');
// 更宽松的响应检查,确保能处理各种有效的响应格式
if (res && res.statusCode === 200 && res.data) {
if (apiResponse) {
// 无论success字段是否存在,只要有data字段就尝试处理
const dataSource = res.data.data || res.data;
const dataSource = apiResponse.data || apiResponse;
if (Array.isArray(dataSource)) {
const processedData = dataSource.map(item => {
// 解析information字段中的信息

11
utils/api.js

@ -109,9 +109,8 @@ function setDeviceType(deviceType) {
}
}
// 导出函数供外部使用
// 将函数定义提升并直接导出,确保在小程序环境中正确识别
module.exports = {
// 定义基础API导出 - 先不导出request,因为它在后面定义
const baseApi = {
setTestMode: setTestMode,
isTestMode: isTestMode,
setDeviceType: setDeviceType
@ -312,8 +311,12 @@ function request(url, method, data) {
});
}
// 导出统一的API对象
// 导出统一的API对象 - 合并基础API和其他功能
module.exports = {
// 包含基础API功能
...baseApi,
// 添加request函数
request: request,
// 添加商品到购物车 - 增强版本,即使本地找不到商品也尝试直接请求服务器
addToCart: function (goodsItem) {
return new Promise((resolve, reject) => {

Loading…
Cancel
Save