From c846c9aeedccb78f919c454f5927c20638522d34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E9=A3=9E=E6=B4=8B?= <15778543+xufeiyang6017@user.noreply.gitee.com> Date: Tue, 6 Jan 2026 15:45:19 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E7=BC=96=E8=BE=91?= =?UTF-8?q?=E8=B4=A7=E6=BA=90=E5=92=8C=E4=B8=8B=E6=9E=B6=E6=8C=89=E9=92=AE?= =?UTF-8?q?=E7=9A=84=E6=9D=83=E9=99=90=E6=8E=A7=E5=88=B6=EF=BC=8C=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E5=88=9B=E5=BB=BA=E4=BA=BA=E4=BF=A1=E6=81=AF=E8=A2=AB?= =?UTF-8?q?=E8=A6=86=E7=9B=96=E7=9A=84=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 | 186 +++++++++++++++++++++++++++-- 1 file changed, 173 insertions(+), 13 deletions(-) diff --git a/pages/goods-update/goods-update.js b/pages/goods-update/goods-update.js index ce47d9c..98ccf90 100644 --- a/pages/goods-update/goods-update.js +++ b/pages/goods-update/goods-update.js @@ -1,6 +1,55 @@ // pages/goods-update/goods-update.js const API = require('../../utils/api.js') +// 获取当前用户的本地手机号 +function getLocalPhoneNumber() { + try { + // 尝试从多个可能的存储位置获取手机号 + const users = wx.getStorageSync('users') || {}; + const userId = wx.getStorageSync('userId'); + + if (userId && users[userId] && users[userId].phoneNumber) { + return users[userId].phoneNumber; + } + + const userInfo = wx.getStorageSync('userInfo'); + if (userInfo && userInfo.phoneNumber) { + return userInfo.phoneNumber; + } + + return wx.getStorageSync('phoneNumber') || ''; + } catch (e) { + console.error('获取本地手机号失败:', e); + return ''; + } +} + +// 检查用户是否为管理员 +function checkIsAdmin(phoneNumber) { + return new Promise((resolve, reject) => { + if (!phoneNumber) { + resolve(false); + return; + } + + API.checkPersonnelByPhone(phoneNumber) + .then(res => { + console.log('checkPersonnelByPhone响应:', res); + if (res.exists && res.data) { + // 检查projectName是否为管理员标识 + const projectName = res.data.projectName || ''; + resolve(projectName === '管理员'); + } else { + resolve(false); + } + }) + .catch(err => { + console.error('检查管理员权限失败:', err); + resolve(false); + }); + }); +} + // 根据sourceType获取对应的颜色 function getSourceTypeColor(sourceType) { const colorMap = { @@ -540,6 +589,10 @@ Page({ region: finalRegion, // 复制原始产品对象中的所有字段,确保不丢失任何数据 ...product, + // 重新设置创建者信息和创建时间,防止被product数据覆盖 + creatorName: creatorName, + formattedCreatedAt: formattedCreatedAt, + created_at: createdAt, // 添加产品包装字段(放在product之后,确保不被覆盖) producting: product.producting || '', // 添加货源描述字段 @@ -586,10 +639,50 @@ Page({ // 显示编辑弹窗 showEditModal: function() { - console.log('显示编辑弹窗'); + console.log('显示编辑弹窗 - 开始权限验证'); const goodsDetail = this.data.goodsDetail; + const contactPhone = goodsDetail.contact_phone || ''; - // 设置编辑数据 + // 权限控制 + const userPhone = getLocalPhoneNumber(); + console.log('当前用户手机号:', userPhone); + console.log('联系人电话:', contactPhone); + + // 先检查是否为管理员 + checkIsAdmin(userPhone) + .then(isAdmin => { + console.log('是否为管理员:', isAdmin); + + if (isAdmin) { + // 是管理员,允许编辑 + console.log('管理员权限验证通过,允许编辑'); + this.openEditModal(goodsDetail); + } else if (userPhone === contactPhone) { + // 手机号匹配联系人电话,允许编辑 + console.log('手机号匹配联系人电话,允许编辑'); + this.openEditModal(goodsDetail); + } else { + // 没有权限 + console.log('没有编辑权限'); + wx.showModal({ + title: '权限不足', + content: '你没有权限编辑此货源', + showCancel: false + }); + } + }) + .catch(err => { + console.error('权限验证失败:', err); + wx.showModal({ + title: '权限验证失败', + content: '无法验证权限,请稍后重试', + showCancel: false + }); + }); + }, + + // 打开编辑弹窗(实际执行打开操作的内部方法) + openEditModal: function(goodsDetail) { this.setData({ editSupply: { id: goodsDetail.id || goodsDetail.productId, @@ -702,18 +795,49 @@ Page({ // 准备上架 preparePublishSupply: function() { console.log('准备上架商品'); - const goodsDetail = this.data.goodsDetail; - const productId = goodsDetail.id || goodsDetail.productId; - wx.showModal({ - title: '确认上架', - content: '确定要上架此商品吗?', - success: (res) => { - if (res.confirm) { - this.publishSupply(productId); + // 权限控制:先获取用户手机号并检查权限 + wx.showLoading({ title: '正在验证权限...', mask: true }); + + getUserPhoneNumber() + .then(phoneNumber => { + console.log('获取到用户手机号:', phoneNumber); + return checkUserPermission(phoneNumber); + }) + .then(hasPermission => { + wx.hideLoading(); + + if (!hasPermission) { + wx.showModal({ + title: '权限不足', + content: '您没有上架商品的权限,请联系管理员。', + showCancel: false + }); + return; } - } - }); + + // 权限验证通过,继续执行上架流程 + const goodsDetail = this.data.goodsDetail; + const productId = goodsDetail.id || goodsDetail.productId; + + wx.showModal({ + title: '确认上架', + content: '确定要上架此商品吗?', + success: (res) => { + if (res.confirm) { + this.publishSupply(productId); + } + } + }); + }) + .catch(err => { + wx.hideLoading(); + console.error('权限验证失败:', err); + wx.showToast({ + title: '权限验证失败', + icon: 'none' + }); + }); }, // 上架商品 @@ -762,7 +886,43 @@ Page({ // 准备下架 prepareUnpublishSupply: function() { - console.log('准备下架商品'); + console.log('准备下架商品 - 开始权限验证'); + + // 权限控制 + const userPhone = getLocalPhoneNumber(); + console.log('当前用户手机号:', userPhone); + + // 先检查是否为管理员 + checkIsAdmin(userPhone) + .then(isAdmin => { + console.log('是否为管理员:', isAdmin); + + if (isAdmin) { + // 是管理员,允许下架 + console.log('管理员权限验证通过,允许下架'); + this.confirmUnpublish(); + } else { + // 没有权限 + console.log('没有下架权限'); + wx.showModal({ + title: '权限不足', + content: '你没有权限下架此货源', + showCancel: false + }); + } + }) + .catch(err => { + console.error('权限验证失败:', err); + wx.showModal({ + title: '权限验证失败', + content: '无法验证权限,请稍后重试', + showCancel: false + }); + }); + }, + + // 确认下架(实际执行下架确认的内部方法) + confirmUnpublish: function() { const goodsDetail = this.data.goodsDetail; const productId = goodsDetail.id || goodsDetail.productId;