From d765d6013179f301dfb570f34f7da736b67ddffd Mon Sep 17 00:00:00 2001 From: Default User Date: Sat, 10 Jan 2026 13:36:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=90=8C=E4=B8=80=E7=94=B5?= =?UTF-8?q?=E8=AF=9D=E5=8F=B7=E7=A0=81=E5=AF=B9=E5=BA=94=E5=A4=9A=E4=B8=AA?= =?UTF-8?q?=E8=BA=AB=E4=BB=BD=E6=97=B6=E7=9A=84=E6=9D=83=E9=99=90=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/goods-update/goods-update.js | 276 ++++++++++++++++++++++++++--- server-example/server-mysql.js | 4 +- 2 files changed, 254 insertions(+), 26 deletions(-) diff --git a/pages/goods-update/goods-update.js b/pages/goods-update/goods-update.js index 6e489e5..1373ae9 100644 --- a/pages/goods-update/goods-update.js +++ b/pages/goods-update/goods-update.js @@ -161,6 +161,39 @@ function formatDateTime(dateString) { return dateString; } +// 获取用户手机号 +function getUserPhoneNumber() { + return new Promise((resolve, reject) => { + try { + // 尝试从多个可能的存储位置获取手机号 + const users = wx.getStorageSync('users') || {}; + const userId = wx.getStorageSync('userId'); + + if (userId && users[userId] && users[userId].phoneNumber) { + resolve(users[userId].phoneNumber); + return; + } + + const userInfo = wx.getStorageSync('userInfo'); + if (userInfo && userInfo.phoneNumber) { + resolve(userInfo.phoneNumber); + return; + } + + const phoneNumber = wx.getStorageSync('phoneNumber'); + if (phoneNumber) { + resolve(phoneNumber); + return; + } + + reject(new Error('未找到手机号')); + } catch (e) { + console.error('获取手机号失败:', e); + reject(e); + } + }); +} + // 处理净重件数对应数据,支持逗号分隔的价格与规格一一对应 function processWeightAndQuantityData(weightSpecString, quantityString, specString, price = '', costprice = '') { console.log('===== 处理净重、件数和规格数据 ====='); @@ -771,22 +804,119 @@ Page({ console.log('当前用户手机号:', userPhone); console.log('联系人电话:', contactPhone); - // 先检查是否为管理员 - checkIsAdmin(userPhone) - .then(isAdmin => { - console.log('是否为管理员:', isAdmin); + // 使用更详细的请求参数,确保后端返回所有记录 + const requestData = { + phone: userPhone, + // 添加额外参数,确保后端返回所有记录 + getAll: true, + pageSize: 100 // 设置较大的页大小,确保能返回所有记录 + }; + + // 直接调用request函数获取完整的人员信息,现在该端点返回所有匹配的记录 + API.request('/api/personnel/get', 'POST', requestData) + .then(res => { + console.log('直接请求personnel表响应:', res); + let hasPermission = false; - if (isAdmin) { - // 是管理员,允许编辑 - console.log('管理员权限验证通过,允许编辑'); - this.openEditModal(goodsDetail); - } else if (userPhone === contactPhone) { - // 手机号匹配联系人电话,允许编辑 - console.log('手机号匹配联系人电话,允许编辑'); + // 详细记录响应数据结构,便于调试 + console.log('响应数据类型:', typeof res.data); + console.log('响应数据是否为数组:', Array.isArray(res.data)); + if (typeof res.data === 'object') { + console.log('响应数据的所有属性:', Object.keys(res.data)); + // 检查是否有其他可能包含数据的属性 + if (res.data.rows) { + console.log('响应数据包含rows属性,类型:', typeof res.data.rows, '是否为数组:', Array.isArray(res.data.rows)); + } + if (res.data.data) { + console.log('响应数据包含data属性,类型:', typeof res.data.data, '是否为数组:', Array.isArray(res.data.data)); + } + } + + if (res && res.code === 200 && res.data) { + // 处理数据库返回的各种格式,确保能获取到所有记录 + let allPersonnelData = []; + + // 处理不同的数据格式 + if (Array.isArray(res.data)) { + // 情况1:data直接是数组(现在后端返回的是这种格式) + allPersonnelData = res.data; + } else if (typeof res.data === 'object') { + // 情况2:data是对象 + // 检查是否有rows属性(常见于分页接口) + if (Array.isArray(res.data.rows)) { + allPersonnelData = res.data.rows; + } + // 检查是否有data属性(嵌套data) + else if (Array.isArray(res.data.data)) { + allPersonnelData = res.data.data; + } + // 检查是否有children属性 + else if (Array.isArray(res.data.children)) { + allPersonnelData = res.data.children; + } + // 检查是否有list属性 + else if (Array.isArray(res.data.list)) { + allPersonnelData = res.data.list; + } + // 检查是否有items属性 + else if (Array.isArray(res.data.items)) { + allPersonnelData = res.data.items; + } + // 情况3:兼容旧格式,单个对象转换为数组 + else { + allPersonnelData = [res.data]; + } + } + + console.log('最终处理后的所有身份记录:', allPersonnelData); + console.log('用户所有身份:', allPersonnelData.map(item => { + console.log('单个身份记录:', item); + return item.projectName || item.position || item.role || '未知身份'; + })); + + // 检查是否有管理员、采购员或销售员身份 + // 优先级:管理员 > 采购员/销售员 + const isAdmin = allPersonnelData.some(item => + (item.projectName && item.projectName === '管理员') || + (item.position && item.position === '管理员') || + (item.role && item.role === '管理员') + ); + const isBuyer = allPersonnelData.some(item => + (item.projectName && item.projectName === '采购员') || + (item.position && item.position === '采购员') || + (item.role && item.role === '采购员') + ); + const isSeller = allPersonnelData.some(item => + (item.projectName && item.projectName === '销售员') || + (item.position && item.position === '销售员') || + (item.role && item.role === '销售员') + ); + + console.log('权限检查结果 - 管理员:', isAdmin, '采购员:', isBuyer, '销售员:', isSeller); + + if (isAdmin) { + // 管理员权限最高,直接允许编辑 + hasPermission = true; + console.log('管理员权限验证通过,允许编辑'); + } else if (isBuyer || isSeller) { + // 采购员或销售员,检查是否为联系人 + if (userPhone === contactPhone) { + hasPermission = true; + console.log('采购员/销售员且为联系人,允许编辑'); + } else { + console.log('采购员/销售员但非联系人,无编辑权限'); + } + } else { + console.log('无匹配身份,无编辑权限'); + } + } else { + console.log('未找到用户信息,无编辑权限'); + } + + if (hasPermission) { this.openEditModal(goodsDetail); } else { // 没有权限 - console.log('没有编辑权限'); wx.showModal({ title: '权限不足', content: '你没有权限编辑此货源', @@ -1162,22 +1292,120 @@ Page({ console.log('商品sellerId(创建人):', productSellerId, '(类型:', typeof productSellerId, ')'); console.log('商品联系人电话:', contactPhone); - Promise.all([ - checkIsAdmin(userPhone), - Promise.resolve({ isCreator: String(currentUserId) === String(productSellerId) }) - ]) - .then(([adminResult, creatorResult]) => { - const isAdmin = adminResult; - const isCreator = creatorResult.isCreator; - const isContact = userPhone === contactPhone; + // 使用更详细的请求参数,确保后端返回所有记录 + const requestData = { + phone: userPhone, + // 添加额外参数,确保后端返回所有记录 + getAll: true, + pageSize: 100 // 设置较大的页大小,确保能返回所有记录 + }; + + // 直接调用request函数获取完整的人员信息,现在该端点返回所有匹配的记录 + API.request('/api/personnel/get', 'POST', requestData) + .then(res => { + console.log('直接请求personnel表响应:', res); + let hasPermission = false; + + // 详细记录响应数据结构,便于调试 + console.log('响应数据类型:', typeof res.data); + console.log('响应数据是否为数组:', Array.isArray(res.data)); + if (typeof res.data === 'object') { + console.log('响应数据的所有属性:', Object.keys(res.data)); + // 检查是否有其他可能包含数据的属性 + if (res.data.rows) { + console.log('响应数据包含rows属性,类型:', typeof res.data.rows, '是否为数组:', Array.isArray(res.data.rows)); + } + if (res.data.data) { + console.log('响应数据包含data属性,类型:', typeof res.data.data, '是否为数组:', Array.isArray(res.data.data)); + } + } - console.log('权限检查结果 - 管理员:', isAdmin, '创建人:', isCreator, '联系人:', isContact); + if (res && res.code === 200 && res.data) { + // 处理数据库返回的各种格式,确保能获取到所有记录 + let allPersonnelData = []; + + // 处理不同的数据格式 + if (Array.isArray(res.data)) { + // 情况1:data直接是数组(现在后端返回的是这种格式) + allPersonnelData = res.data; + } else if (typeof res.data === 'object') { + // 情况2:data是对象 + // 检查是否有rows属性(常见于分页接口) + if (Array.isArray(res.data.rows)) { + allPersonnelData = res.data.rows; + } + // 检查是否有data属性(嵌套data) + else if (Array.isArray(res.data.data)) { + allPersonnelData = res.data.data; + } + // 检查是否有children属性 + else if (Array.isArray(res.data.children)) { + allPersonnelData = res.data.children; + } + // 检查是否有list属性 + else if (Array.isArray(res.data.list)) { + allPersonnelData = res.data.list; + } + // 检查是否有items属性 + else if (Array.isArray(res.data.items)) { + allPersonnelData = res.data.items; + } + // 情况3:兼容旧格式,单个对象转换为数组 + else { + allPersonnelData = [res.data]; + } + } + + console.log('最终处理后的所有身份记录:', allPersonnelData); + console.log('用户所有身份:', allPersonnelData.map(item => { + console.log('单个身份记录:', item); + return item.projectName || item.position || item.role || '未知身份'; + })); + + // 检查是否为管理员 + const isAdmin = allPersonnelData.some(item => + (item.projectName && item.projectName === '管理员') || + (item.position && item.position === '管理员') || + (item.role && item.role === '管理员') + ); + // 检查是否为创建人 + const isCreator = String(currentUserId) === String(productSellerId); + // 检查是否为联系人 + const isContact = userPhone === contactPhone; + // 检查是否为采购员或销售员 + const isBuyerOrSeller = allPersonnelData.some(item => + (item.projectName && item.projectName === '采购员') || + (item.projectName && item.projectName === '销售员') || + (item.position && item.position === '采购员') || + (item.position && item.position === '销售员') || + (item.role && item.role === '采购员') || + (item.role && item.role === '销售员') + ); + + console.log('权限检查结果 - 管理员:', isAdmin, '创建人:', isCreator, '联系人:', isContact, '采购员/销售员:', isBuyerOrSeller); + + if (isAdmin) { + // 管理员权限最高,直接允许下架 + hasPermission = true; + console.log('管理员权限验证通过,允许下架'); + } else if (isCreator) { + // 创建人允许下架 + hasPermission = true; + console.log('创建人权限验证通过,允许下架'); + } else if (isContact && isBuyerOrSeller) { + // 采购员或销售员且为联系人,允许下架 + hasPermission = true; + console.log('采购员/销售员且为联系人,允许下架'); + } else { + console.log('没有下架权限'); + } + } else { + console.log('未找到用户信息,无下架权限'); + } - if (isAdmin || isCreator || isContact) { - console.log('权限验证通过,允许下架'); + if (hasPermission) { this.confirmUnpublish(); } else { - console.log('没有下架权限'); wx.showModal({ title: '权限不足', content: '您没有权限下架此货源', diff --git a/server-example/server-mysql.js b/server-example/server-mysql.js index 62d447e..25327ae 100644 --- a/server-example/server-mysql.js +++ b/server-example/server-mysql.js @@ -8909,12 +8909,12 @@ app.post('/api/personnel/get', async (req, res) => { }); } - // 返回业务员信息 + // 返回所有匹配的业务员信息 res.status(200).json({ success: true, code: 200, message: '获取业务员信息成功', - data: personnel[0] + data: personnel }); } catch (error) { console.error('获取业务员信息失败:', error);