|
|
@ -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) => { |
|
|
@ -939,7 +942,7 @@ module.exports = { |
|
|
// 需要检查两种组合:user_phone <-> manager_phone 和 manager_phone <-> user_phone
|
|
|
// 需要检查两种组合:user_phone <-> manager_phone 和 manager_phone <-> user_phone
|
|
|
const chatExists = Array.isArray(chatList) && chatList.some(chat => { |
|
|
const chatExists = Array.isArray(chatList) && chatList.some(chat => { |
|
|
return (chat.user_phone === user_phone && chat.manager_phone === manager_phone) || |
|
|
return (chat.user_phone === user_phone && chat.manager_phone === manager_phone) || |
|
|
(chat.user_phone === manager_phone && chat.manager_phone === user_phone); |
|
|
(chat.user_phone === manager_phone && chat.manager_phone === user_phone); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
if (chatExists) { |
|
|
if (chatExists) { |
|
|
@ -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接口查询采购员数据
|
|
|
@ -1520,7 +1523,7 @@ module.exports = { |
|
|
console.log('获取personnel表数据成功:', res); |
|
|
console.log('获取personnel表数据成功:', res); |
|
|
// 适配不同的数据返回格式
|
|
|
// 适配不同的数据返回格式
|
|
|
const data = res && res.data && Array.isArray(res.data) ? res.data : |
|
|
const data = res && res.data && Array.isArray(res.data) ? res.data : |
|
|
res && Array.isArray(res) ? res : []; |
|
|
res && Array.isArray(res) ? res : []; |
|
|
resolve(data); |
|
|
resolve(data); |
|
|
}) |
|
|
}) |
|
|
.catch(err => { |
|
|
.catch(err => { |
|
|
@ -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 { |
|
|
@ -1765,59 +1768,59 @@ module.exports = { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 获取本地存储的users信息
|
|
|
// 获取本地存储的users信息
|
|
|
const users = wx.getStorageSync('users') || {}; |
|
|
const users = wx.getStorageSync('users') || {}; |
|
|
let currentType = ''; |
|
|
let currentType = ''; |
|
|
|
|
|
|
|
|
// 如果有userId,从users中获取当前类型
|
|
|
|
|
|
if (userId && users[userId] && users[userId].type) { |
|
|
|
|
|
currentType = users[userId].type; |
|
|
|
|
|
console.log('从本地存储获取用户类型:', currentType); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 先设置基础用户类型
|
|
|
// 如果有userId,从users中获取当前类型
|
|
|
userType = currentType || ''; |
|
|
if (userId && users[userId] && users[userId].type) { |
|
|
|
|
|
currentType = users[userId].type; |
|
|
// 使用客服身份判断逻辑:查询personnel表
|
|
|
console.log('从本地存储获取用户类型:', currentType); |
|
|
Promise.all([ |
|
|
|
|
|
this.checkIfUserIsCustomerService(phoneNumber), |
|
|
|
|
|
this.getManagerIdByPhone(phoneNumber) |
|
|
|
|
|
]).then(([isCustomerService, managerId]) => { |
|
|
|
|
|
if (isCustomerService && managerId) { |
|
|
|
|
|
// 如果是客服,确保userType包含manager
|
|
|
|
|
|
if (!userType.includes('manager')) { |
|
|
|
|
|
userType = userType ? userType + ',manager' : 'manager'; |
|
|
|
|
|
} |
|
|
|
|
|
// 保存managerId到本地存储,用于WebSocket认证
|
|
|
|
|
|
console.log(`保存客服的managerId: ${managerId} 到本地存储`); |
|
|
|
|
|
wx.setStorageSync('managerId', managerId); |
|
|
|
|
|
|
|
|
|
|
|
// 更新users存储,包含managerId
|
|
|
|
|
|
if (userId) { |
|
|
|
|
|
if (!users[userId]) { |
|
|
|
|
|
users[userId] = {}; |
|
|
|
|
|
} |
|
|
} |
|
|
users[userId].managerId = managerId; |
|
|
|
|
|
} |
|
|
|
|
|
console.log('用户被识别为客服:', phoneNumber, '用户类型:', userType); |
|
|
|
|
|
} else { |
|
|
|
|
|
// 如果不是客服,移除manager标识
|
|
|
|
|
|
userType = userType.replace(/,?manager/g, '').replace(/^,|,$/g, ''); |
|
|
|
|
|
// 清除managerId
|
|
|
|
|
|
wx.removeStorageSync('managerId'); |
|
|
|
|
|
console.log('用户被识别为普通用户:', phoneNumber, '用户类型:', userType); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 更新users存储中的类型信息
|
|
|
// 先设置基础用户类型
|
|
|
if (userId) { |
|
|
userType = currentType || ''; |
|
|
if (!users[userId]) { |
|
|
|
|
|
users[userId] = {}; |
|
|
// 使用客服身份判断逻辑:查询personnel表
|
|
|
} |
|
|
Promise.all([ |
|
|
users[userId].type = userType; // 直接存储用户类型
|
|
|
this.checkIfUserIsCustomerService(phoneNumber), |
|
|
wx.setStorageSync('users', users); |
|
|
this.getManagerIdByPhone(phoneNumber) |
|
|
} |
|
|
]).then(([isCustomerService, managerId]) => { |
|
|
|
|
|
if (isCustomerService && managerId) { |
|
|
|
|
|
// 如果是客服,确保userType包含manager
|
|
|
|
|
|
if (!userType.includes('manager')) { |
|
|
|
|
|
userType = userType ? userType + ',manager' : 'manager'; |
|
|
|
|
|
} |
|
|
|
|
|
// 保存managerId到本地存储,用于WebSocket认证
|
|
|
|
|
|
console.log(`保存客服的managerId: ${managerId} 到本地存储`); |
|
|
|
|
|
wx.setStorageSync('managerId', managerId); |
|
|
|
|
|
|
|
|
|
|
|
// 更新users存储,包含managerId
|
|
|
|
|
|
if (userId) { |
|
|
|
|
|
if (!users[userId]) { |
|
|
|
|
|
users[userId] = {}; |
|
|
|
|
|
} |
|
|
|
|
|
users[userId].managerId = managerId; |
|
|
|
|
|
} |
|
|
|
|
|
console.log('用户被识别为客服:', phoneNumber, '用户类型:', userType); |
|
|
|
|
|
} else { |
|
|
|
|
|
// 如果不是客服,移除manager标识
|
|
|
|
|
|
userType = userType.replace(/,?manager/g, '').replace(/^,|,$/g, ''); |
|
|
|
|
|
// 清除managerId
|
|
|
|
|
|
wx.removeStorageSync('managerId'); |
|
|
|
|
|
console.log('用户被识别为普通用户:', phoneNumber, '用户类型:', userType); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// 存储用户类型信息
|
|
|
// 更新users存储中的类型信息
|
|
|
wx.setStorageSync('userType', userType); |
|
|
if (userId) { |
|
|
|
|
|
if (!users[userId]) { |
|
|
|
|
|
users[userId] = {}; |
|
|
|
|
|
} |
|
|
|
|
|
users[userId].type = userType; // 直接存储用户类型
|
|
|
|
|
|
wx.setStorageSync('users', users); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 存储用户类型信息
|
|
|
|
|
|
wx.setStorageSync('userType', userType); |
|
|
|
|
|
|
|
|
// 更新全局用户信息
|
|
|
// 更新全局用户信息
|
|
|
if (getApp && getApp().globalData) { |
|
|
if (getApp && getApp().globalData) { |
|
|
@ -2162,44 +2165,44 @@ module.exports = { |
|
|
userType = currentType || ''; |
|
|
userType = currentType || ''; |
|
|
|
|
|
|
|
|
// 检查客服身份并获取managerId
|
|
|
// 检查客服身份并获取managerId
|
|
|
Promise.all([ |
|
|
Promise.all([ |
|
|
this.checkIfUserIsCustomerService(phoneNumber), |
|
|
this.checkIfUserIsCustomerService(phoneNumber), |
|
|
this.getManagerIdByPhone(phoneNumber) |
|
|
this.getManagerIdByPhone(phoneNumber) |
|
|
]).then(([isCustomerService, managerId]) => { |
|
|
]).then(([isCustomerService, managerId]) => { |
|
|
if (isCustomerService && managerId) { |
|
|
if (isCustomerService && managerId) { |
|
|
// 如果是客服,确保userType包含manager
|
|
|
// 如果是客服,确保userType包含manager
|
|
|
if (!userType.includes('manager')) { |
|
|
if (!userType.includes('manager')) { |
|
|
userType = userType ? userType + ',manager' : 'manager'; |
|
|
userType = userType ? userType + ',manager' : 'manager'; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 保存managerId到本地存储,用于WebSocket认证
|
|
|
|
|
|
console.log(`保存客服的managerId: ${managerId} 到本地存储`); |
|
|
|
|
|
wx.setStorageSync('managerId', managerId); |
|
|
|
|
|
|
|
|
|
|
|
// 更新users存储,包含managerId
|
|
|
|
|
|
if (userId) { |
|
|
|
|
|
if (!users[userId]) { |
|
|
|
|
|
users[userId] = {}; |
|
|
|
|
|
} |
|
|
|
|
|
users[userId].managerId = managerId; |
|
|
|
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
|
// 如果不是客服,移除manager标识
|
|
|
|
|
|
userType = userType.replace(/,?manager/g, '').replace(/^,|,$/g, ''); |
|
|
|
|
|
// 清除managerId
|
|
|
|
|
|
wx.removeStorageSync('managerId'); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 更新users存储
|
|
|
// 保存managerId到本地存储,用于WebSocket认证
|
|
|
|
|
|
console.log(`保存客服的managerId: ${managerId} 到本地存储`); |
|
|
|
|
|
wx.setStorageSync('managerId', managerId); |
|
|
|
|
|
|
|
|
|
|
|
// 更新users存储,包含managerId
|
|
|
if (userId) { |
|
|
if (userId) { |
|
|
if (!users[userId]) { |
|
|
if (!users[userId]) { |
|
|
users[userId] = {}; |
|
|
users[userId] = {}; |
|
|
} |
|
|
} |
|
|
users[userId].type = userType; |
|
|
users[userId].managerId = managerId; |
|
|
wx.setStorageSync('users', users); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
|
// 如果不是客服,移除manager标识
|
|
|
|
|
|
userType = userType.replace(/,?manager/g, '').replace(/^,|,$/g, ''); |
|
|
|
|
|
// 清除managerId
|
|
|
|
|
|
wx.removeStorageSync('managerId'); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
wx.setStorageSync('userType', userType); |
|
|
// 更新users存储
|
|
|
|
|
|
if (userId) { |
|
|
|
|
|
if (!users[userId]) { |
|
|
|
|
|
users[userId] = {}; |
|
|
|
|
|
} |
|
|
|
|
|
users[userId].type = userType; |
|
|
|
|
|
wx.setStorageSync('users', users); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
wx.setStorageSync('userType', userType); |
|
|
|
|
|
|
|
|
// 更新全局用户信息
|
|
|
// 更新全局用户信息
|
|
|
if (getApp && getApp().globalData) { |
|
|
if (getApp && getApp().globalData) { |
|
|
@ -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
|
|
|
|