Browse Source

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

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

28
pages/customer-service/index.js

@ -22,29 +22,15 @@ Page({
// 获取当前用户类型参数 // 获取当前用户类型参数
const userType = this.data.userType || 'seller'; const userType = this.data.userType || 'seller';
const res = await new Promise((resolve, reject) => { // 使用api.js中的请求工具,自动获取正确的服务器地址
wx.request({ const apiResponse = await api.request(`/api/managers?type=${userType}`, 'GET');
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);
}
});
});
console.log('API响应状态码:', res?.statusCode); console.log('API响应数据:', apiResponse ? JSON.stringify(apiResponse) : 'undefined');
console.log('API响应数据:', res?.data ? JSON.stringify(res.data) : 'undefined');
// 更宽松的响应检查,确保能处理各种有效的响应格式 // 更宽松的响应检查,确保能处理各种有效的响应格式
if (res && res.statusCode === 200 && res.data) { if (apiResponse) {
// 无论success字段是否存在,只要有data字段就尝试处理 // 无论success字段是否存在,只要有data字段就尝试处理
const dataSource = res.data.data || res.data; const dataSource = apiResponse.data || apiResponse;
if (Array.isArray(dataSource)) { if (Array.isArray(dataSource)) {
const processedData = dataSource.map(item => { const processedData = dataSource.map(item => {
// 解析information字段中的信息 // 解析information字段中的信息
@ -262,7 +248,7 @@ Page({
/** /**
* 检查当前用户身份 * 检查当前用户身份
*/ */
checkUserType: function() { checkUserType: function () {
const app = getApp(); const app = getApp();
const userInfo = app.globalData.userInfo || {}; const userInfo = app.globalData.userInfo || {};
const isManager = userInfo.userType === 'manager' || userInfo.type === 'manager'; const isManager = userInfo.userType === 'manager' || userInfo.type === 'manager';
@ -284,7 +270,7 @@ Page({
this.loadCustomerServices(); this.loadCustomerServices();
}, },
onUnload: function() { onUnload: function () {
// 停止定期刷新 // 停止定期刷新
this.stopPeriodicRefresh(); this.stopPeriodicRefresh();
}, },

21
utils/api.js

@ -109,9 +109,8 @@ function setDeviceType(deviceType) {
} }
} }
// 导出函数供外部使用 // 定义基础API导出 - 先不导出request,因为它在后面定义
// 将函数定义提升并直接导出,确保在小程序环境中正确识别 const baseApi = {
module.exports = {
setTestMode: setTestMode, setTestMode: setTestMode,
isTestMode: isTestMode, isTestMode: isTestMode,
setDeviceType: setDeviceType setDeviceType: setDeviceType
@ -312,8 +311,12 @@ function request(url, method, data) {
}); });
} }
// 导出统一的API对象 // 导出统一的API对象 - 合并基础API和其他功能
module.exports = { module.exports = {
// 包含基础API功能
...baseApi,
// 添加request函数
request: request,
// 添加商品到购物车 - 增强版本,即使本地找不到商品也尝试直接请求服务器 // 添加商品到购物车 - 增强版本,即使本地找不到商品也尝试直接请求服务器
addToCart: function (goodsItem) { addToCart: function (goodsItem) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -1512,7 +1515,7 @@ module.exports = {
}, },
// 获取personnel表数据 // 获取personnel表数据
getPersonnelData: function() { getPersonnelData: function () {
console.log('获取personnel表数据...'); console.log('获取personnel表数据...');
return new Promise((resolve) => { return new Promise((resolve) => {
request('/api/managers', 'GET', {}) // 使用现有的managers接口查询采购员数据 request('/api/managers', 'GET', {}) // 使用现有的managers接口查询采购员数据
@ -1531,7 +1534,7 @@ module.exports = {
}, },
// 根据手机号获取客服的managerId - 优化版 // 根据手机号获取客服的managerId - 优化版
getManagerIdByPhone: function(phoneNumber) { getManagerIdByPhone: function (phoneNumber) {
console.log('根据手机号获取managerId:', phoneNumber); console.log('根据手机号获取managerId:', phoneNumber);
return new Promise(async (resolve) => { return new Promise(async (resolve) => {
// 严格验证手机号参数 // 严格验证手机号参数
@ -1587,7 +1590,7 @@ module.exports = {
}, },
// 统一的用户类型设置函数 // 统一的用户类型设置函数
setUserType: function(userId, phoneNumber, currentType = '') { setUserType: function (userId, phoneNumber, currentType = '') {
console.log('API.setUserType - userId:', userId, 'phoneNumber:', phoneNumber, 'currentType:', currentType); console.log('API.setUserType - userId:', userId, 'phoneNumber:', phoneNumber, 'currentType:', currentType);
return new Promise(async (resolve) => { return new Promise(async (resolve) => {
try { try {
@ -2930,7 +2933,7 @@ module.exports = {
}, },
// 更新用户类型 - 恢复原有的类型转换功能 // 更新用户类型 - 恢复原有的类型转换功能
updateUserType: async function(typeToAdd) { updateUserType: async function (typeToAdd) {
try { try {
const userId = wx.getStorageSync('userId'); const userId = wx.getStorageSync('userId');
const openid = wx.getStorageSync('openid'); const openid = wx.getStorageSync('openid');
@ -3024,7 +3027,7 @@ module.exports = {
// 如果有userInfo,合并其属性但不覆盖已有的关键属性 // 如果有userInfo,合并其属性但不覆盖已有的关键属性
if (userInfo) { if (userInfo) {
const userInfoCopy = {...userInfo}; const userInfoCopy = { ...userInfo };
delete userInfoCopy.userId; // 不覆盖userId delete userInfoCopy.userId; // 不覆盖userId
delete userInfoCopy.openid; // 不覆盖openid delete userInfoCopy.openid; // 不覆盖openid
delete userInfoCopy.type; // 不覆盖type delete userInfoCopy.type; // 不覆盖type

Loading…
Cancel
Save