Browse Source

feat: 添加编辑货源和下架按钮的权限控制,修复创建人信息被覆盖的问题

pull/9/head
徐飞洋 2 months ago
parent
commit
c846c9aeed
  1. 186
      pages/goods-update/goods-update.js

186
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;

Loading…
Cancel
Save