feat: 小程序管理员门禁——isAdmin 全局态、创建入口隐藏与拦截

This commit is contained in:
2026-11-14 20:33:38 +08:00
parent 02f71e6497
commit 97f5b1aabd
8 changed files with 96 additions and 10 deletions
+17
View File
@@ -11,6 +11,8 @@ App({
token: null,
// 用户ID
userId: null,
// 是否为活动管理员(可创建/编辑/删除活动)
isAdmin: false,
},
onLaunch(options) {
@@ -54,6 +56,14 @@ App({
if (res.statusCode !== 200 || !res.data || !res.data.success) {
console.log('用户不存在于当前数据库,清除登录状态');
this.clearLoginInfo();
} else if (res.data.data) {
// 同步管理员身份(后台可随时调整用户角色,每次启动刷新)
const isAdmin = !!res.data.data.isAdmin;
if (this.globalData.isAdmin !== isAdmin) {
console.log('管理员状态已更新:', isAdmin);
}
this.globalData.isAdmin = isAdmin;
wx.setStorageSync('isAdmin', isAdmin);
}
} catch (err) {
console.log('验证用户存在性失败,清除登录状态', err);
@@ -98,11 +108,16 @@ App({
this.globalData.token = token;
this.globalData.userId = userId;
}
this.globalData.isAdmin = wx.getStorageSync('isAdmin') || false;
},
// 设置用户信息
setUserInfo(userInfo) {
this.globalData.userInfo = userInfo;
if (userInfo && typeof userInfo.isAdmin !== 'undefined') {
this.globalData.isAdmin = !!userInfo.isAdmin;
wx.setStorageSync('isAdmin', !!userInfo.isAdmin);
}
},
// 设置 token
@@ -122,8 +137,10 @@ App({
this.globalData.token = null;
this.globalData.userId = null;
this.globalData.userInfo = null;
this.globalData.isAdmin = false;
wx.removeStorageSync('token');
wx.removeStorageSync('userId');
wx.removeStorageSync('isAdmin');
},
// 检查是否已登录
@@ -44,6 +44,12 @@ Page({
if (this.checkAuditModeSync()) {
return;
}
// 权限兜底:仅管理员可进入创建/编辑页(后端接口同样强制校验)
if (!wx.getStorageSync('isAdmin')) {
wx.showToast({ title: '仅管理员可创建活动', icon: 'none', duration: 2000 });
setTimeout(() => wx.navigateBack({ delta: 1 }), 1500);
return;
}
const { id, mode } = options;
if (mode === 'edit' && id) {
this.setData({ mode: 'edit', activityId: id });
+49 -4
View File
@@ -26,6 +26,8 @@ Page({
error: null,
// 审核模式
auditMode: false,
// 是否管理员(可创建活动)
isAdmin: false,
},
onLoad() {
@@ -71,13 +73,46 @@ Page({
this.setData({
isLoggedIn: loggedIn,
userInfo: user,
isAdmin: wx.getStorageSync('isAdmin') || false,
});
if (loggedIn) {
// 管理员身份以后台为准,异步刷新一次
this.refreshIsAdmin();
this.loadProfileCompletion();
this.syncProfileToStorage();
}
},
// 从服务端刷新管理员身份(后台改权限后小程序即时生效)
async refreshIsAdmin() {
const app = getApp();
try {
const res = await new Promise((resolve, reject) => {
wx.request({
url: `${app.globalData.apiBaseUrl}/user/profile`,
method: 'GET',
header: {
'Authorization': `Bearer ${app.globalData.token}`,
'x-user-id': app.globalData.userId,
},
timeout: 5000,
success: resolve,
fail: reject,
});
});
if (res.statusCode === 200 && res.data && res.data.success) {
const isAdmin = !!res.data.data.isAdmin;
app.globalData.isAdmin = isAdmin;
wx.setStorageSync('isAdmin', isAdmin);
if (this.data.isAdmin !== isAdmin) {
this.setData({ isAdmin });
}
}
} catch (err) {
console.log('刷新管理员身份失败,沿用本地缓存', err);
}
},
// 从后端同步最新用户资料到 storage
async syncProfileToStorage() {
try {
@@ -186,6 +221,8 @@ Page({
// 切换 Tab
onTabChange(e) {
const tab = e.currentTarget.dataset.tab;
// "我发起的活动"仅管理员可用(tab 已隐藏,此处兜底防异常态)
if (tab === 'created' && !this.data.isAdmin) return;
this.setData({
currentTab: tab,
page: 1,
@@ -203,8 +240,12 @@ Page({
});
},
// 编辑自己的活动
// 编辑自己的活动(仅管理员)
handleEditActivity(e) {
if (!this.data.isAdmin) {
wx.showToast({ title: '仅管理员可编辑活动', icon: 'none', duration: 2000 });
return;
}
const { id } = e.currentTarget.dataset;
wx.navigateTo({
url: `/pages/create-activity/index?id=${id}&mode=edit`,
@@ -331,9 +372,13 @@ Page({
});
return;
}
if (!this.data.isLoggedIn) {
// 游客可浏览:不强制登录,仅提示后放行进入创建页面
wx.showToast({ title: '登录后才能发布,可先浏览', icon: 'none', duration: 2000 });
if (!this.data.isAdmin) {
wx.showToast({
title: '仅管理员可创建活动',
icon: 'none',
duration: 2000,
});
return;
}
wx.navigateTo({
url: '/pages/create-activity/index',
+4 -2
View File
@@ -14,7 +14,7 @@
<!-- 快捷入口 -->
<view class="shortcuts">
<view class="shortcut-item shortcut-create" bindtap="goToCreateActivity" wx:if="{{!auditMode}}">
<view class="shortcut-item shortcut-create" bindtap="goToCreateActivity" wx:if="{{isAdmin && !auditMode}}">
<view class="shortcut-icon">
<text class="shortcut-plus">+</text>
</view>
@@ -97,6 +97,7 @@
class="tab-item {{currentTab === 'created' ? 'tab-active' : ''}}"
data-tab="created"
bindtap="onTabChange"
wx:if="{{isAdmin}}"
>
我发起的活动
</view>
@@ -187,7 +188,8 @@
</view>
<!-- 创建活动浮窗按钮 -->
<view class="create-btn-wrapper" wx:if="{{!auditMode}}">
<!-- 创建活动浮窗按钮(仅管理员可见) -->
<view class="create-btn-wrapper" wx:if="{{isAdmin && !auditMode}}">
<view class="create-btn" bindtap="goToCreateActivity">
<text class="create-btn-icon">+</text>
</view>
+2
View File
@@ -7,6 +7,7 @@ const saveLoginState = (token, userId, userInfo) => {
wx.setStorageSync('token', token);
wx.setStorageSync('userId', userId);
wx.setStorageSync('isLoggedIn', true);
wx.setStorageSync('isAdmin', !!(userInfo && userInfo.isAdmin));
if (userInfo) {
wx.setStorageSync('userInfo', userInfo);
}
@@ -14,6 +15,7 @@ const saveLoginState = (token, userId, userInfo) => {
app.globalData.token = token;
app.globalData.userId = userId;
app.globalData.userInfo = userInfo;
app.globalData.isAdmin = !!(userInfo && userInfo.isAdmin);
}
};
+7 -1
View File
@@ -9,6 +9,7 @@ Page({
stats: null,
loading: false,
isLoggedIn: false,
isAdmin: false,
},
onLoad() {
@@ -25,6 +26,7 @@ Page({
this.setData({
isLoggedIn: loggedIn,
userInfo: user,
isAdmin: wx.getStorageSync('isAdmin') || false,
});
if (loggedIn) {
this.loadUserData(refresh);
@@ -44,7 +46,11 @@ Page({
...profileRes.data,
avatarUrl: getFullImageUrl(profileRes.data.avatarUrl, noCache),
};
this.setData({ userInfo });
// 同步管理员身份(后台调整权限后进入个人中心即时生效)
const isAdmin = !!profileRes.data.isAdmin;
getApp().globalData.isAdmin = isAdmin;
wx.setStorageSync('isAdmin', isAdmin);
this.setData({ userInfo, isAdmin });
}
if (statsRes.success) {
+3 -3
View File
@@ -26,13 +26,13 @@
<text class="edit-icon">›</text>
</view>
<!-- 统计数据 -->
<!-- 统计数据(非管理员不显示"创建活动") -->
<view class="stats-wrapper" wx:if="{{stats}}">
<view class="stats-item">
<view class="stats-item" wx:if="{{isAdmin}}">
<text class="stats-num">{{stats.createdCount || 0}}</text>
<text class="stats-label">创建活动</text>
</view>
<view class="stats-divider"></view>
<view class="stats-divider" wx:if="{{isAdmin}}"></view>
<view class="stats-item">
<text class="stats-num">{{stats.joinedCount || 0}}</text>
<text class="stats-label">参与活动</text>
+8
View File
@@ -20,11 +20,17 @@ const getUserId = () => {
return wx.getStorageSync('userId') || null;
};
// 是否为活动管理员(可创建/编辑/删除活动)
const getIsAdmin = () => {
return wx.getStorageSync('isAdmin') || false;
};
// 保存登录状态
const saveLoginState = (token, userId, userInfo) => {
wx.setStorageSync('token', token);
wx.setStorageSync('userId', userId);
wx.setStorageSync('isLoggedIn', true);
wx.setStorageSync('isAdmin', !!(userInfo && userInfo.isAdmin));
if (userInfo) {
wx.setStorageSync('userInfo', userInfo);
}
@@ -35,6 +41,7 @@ const clearLoginState = () => {
wx.removeStorageSync('token');
wx.removeStorageSync('userId');
wx.removeStorageSync('isLoggedIn');
wx.removeStorageSync('isAdmin');
wx.removeStorageSync('userInfo');
};
@@ -43,6 +50,7 @@ module.exports = {
getUserInfo,
getToken,
getUserId,
getIsAdmin,
saveLoginState,
clearLoginState,
};