feat(database): 迁移用户关联字段并优化打卡系统

- 修改数据库配置将MYSQL_DB从mys_info_v3改为mys_info_test
- 新增auth_users表wechat_openid字段并建立唯一索引
- 为plan_checkins表添加计划性能索引(idx_plan_period_student)
- 将checkin_records_old表关联方式从student_id改为user_id
- 将plan_checkins表关联方式从student_id改为user_id并更新附件路径
- 移除routes/auth.py中的uuid导入使用安全token生成函数
- 重构routes/checkin.py打卡逻辑去除course_type字段并优化返回结构
- 添加打卡时间限制验证功能防止过期打卡操作
- 实现小组统计功能支持老师查看各组打卡情况
- 更新主页index.html界面适配新的打卡和计划功能
- 调整计划提交时间规则为周五至周四周期
- 添加月度和年度计划提交状态验证机制
This commit is contained in:
王宏豪
2026-09-10 14:11:58 +08:00
parent 1dd4fa3f41
commit cd04c1b5ef
19 changed files with 1457 additions and 407 deletions

View File

@@ -1,78 +1,94 @@
const api = require('../../utils/api');
const auth = require('../../utils/auth');
function errMessage(err) {
if (err && typeof err === 'object') {
if (err.detail) return String(err.detail);
if (err.error) return String(err.error);
if (err.message) return String(err.message);
}
return '加载失败,请稍后再试';
}
const H5_HOME_URL = api.H5_BASE_URL + '/';
Page({
data: {
// 登录态标记
isLoggedIn: false,
// web-view 模式
webviewUrl: '',
webviewError: false,
errorMsg: '',
webviewKey: 0,
// 游客模式
greeting: '',
displayName: '',
encouragement: '',
modules: [],
introData: null,
loading: true,
error: '',
skeletonItems: [0, 1, 2, 3, 4, 5, 6, 7, 8]
introData: null
},
onShow() { this.loadHome(); },
_webviewLoaded: false,
_lastToken: null,
_webviewKey: 0,
loadHome(silent) {
if (!silent) {
this.setData({ loading: true, error: '' });
onShow() {
const currentToken = auth.getToken();
const isLoggedIn = !!currentToken;
if (isLoggedIn) {
// 已登录:每次进入首页都重新加载 H5 首页(确保从内部页面回到首页)
this._lastToken = currentToken;
this.setData({ isLoggedIn: true });
this.loadWebview();
this._webviewLoaded = true;
} else {
// 游客模式:token 未变化且已有数据时不重新加载
if (currentToken === this._lastToken && this.data.displayName) {
return;
}
this._lastToken = currentToken;
this.setData({ isLoggedIn: false });
this.loadGuestHome();
this._webviewLoaded = false;
}
},
// ---- 已登录:web-view 模式 ----
loadWebview() {
const token = auth.getToken();
if (!token) {
// 游客模式:展示公开内容,不强制登录
this.setData({
loading: false,
error: '',
greeting: '欢迎来到明缘云道场',
displayName: '游客',
encouragement: '登录后即可打卡、提交计划',
modules: this.getGuestModules(),
introData: this.getIntroData()
});
return Promise.resolve();
}
return auth.ensureLogin()
.then((token) => {
if (!token) {
this.setData({
loading: false,
error: '',
greeting: '欢迎来到明缘云道场',
displayName: '游客',
encouragement: '登录后即可打卡、提交计划',
modules: this.getGuestModules(),
introData: this.getIntroData()
});
return;
}
return api.request({ url: '/api/v1/pages/home' });
})
.then((res) => {
if (!res) return;
this.setData({
loading: false,
error: '',
greeting: res.greeting || '',
displayName: res.display_name || '',
encouragement: res.encouragement || '',
modules: (res.modules || []).filter((m) => m.visible),
introData: null
});
})
.catch((err) => {
this.setData({ loading: false, error: errMessage(err) });
});
const url = api.BASE_URL + '/api/v1/webview/redirect'
+ '?token=' + encodeURIComponent(token)
+ '&target=' + encodeURIComponent(H5_HOME_URL)
+ '&_t=' + Date.now();
// key 自增,强制 web-view 组件销毁重建,解决切 tab 回来白屏问题
// 时间戳参数确保 url 每次不同,强制重新加载并跳转到 H5 首页
this._webviewKey += 1;
this.setData({
webviewUrl: url,
webviewError: false,
errorMsg: '',
webviewKey: this._webviewKey
});
},
onWebviewError(e) {
this.setData({
webviewError: true,
errorMsg: (e && e.detail && e.detail.errMsg) || '加载失败,请检查网络后重试'
});
},
onRetry() {
this.setData({ webviewUrl: '', webviewError: false, errorMsg: '' });
setTimeout(() => {
this.loadWebview();
}, 50);
},
// ---- 未登录:游客原生首页 ----
loadGuestHome() {
this.setData({
greeting: '欢迎来到明缘云道场',
displayName: '游客',
encouragement: '登录后即可打卡、提交计划',
modules: this.getGuestModules(),
introData: this.getIntroData()
});
},
getGuestModules() {
@@ -111,47 +127,36 @@ Page({
};
},
onPullDownRefresh() {
this.loadHome(true).then(() => {
wx.stopPullDownRefresh();
});
},
onRetry() {
this.loadHome();
},
goLogin() {
wx.switchTab({ url: '/pages/profile/profile' });
},
onModuleTap(e) {
const url = e.currentTarget.dataset.url;
const token = auth.getToken();
// 游客模式:引导登录
if (!token) {
wx.showModal({
title: '登录提示',
content: '登录后使用此功能,是否前往登录?',
confirmText: '去登录',
confirmColor: '#C67B30',
success: (res) => {
if (res.confirm) {
wx.switchTab({ url: '/pages/profile/profile' });
}
wx.showModal({
title: '登录提示',
content: '登录后使用此功能,是否前往登录?',
confirmText: '去登录',
confirmColor: '#C67B30',
success: (res) => {
if (res.confirm) {
wx.switchTab({ url: '/pages/profile/profile' });
}
});
return;
}
if (!url) return;
}
});
},
// ---- 分享 ----
onShareAppMessage() {
return {
title: '明缘云道场 · 学习成长,每日精进',
path: '/pages/home/home'
};
},
// 通过 webview redirect 接口:后端验证 token 后设置 HttpOnly cookie,再 302 到目标页
// H5 页面内的同域请求会自动携带 cookie,H5 端零改动
const redirectUrl = api.BASE_URL + '/api/v1/webview/redirect'
+ '?token=' + encodeURIComponent(token)
+ '&target=' + encodeURIComponent(url);
wx.navigateTo({ url: '/pages/webview/webview?url=' + encodeURIComponent(redirectUrl) });
onShareTimeline() {
return {
title: '明缘云道场 · 打卡学习好工具',
query: 'from=timeline'
};
}
});

View File

@@ -1,5 +1,4 @@
{
"usingComponents": {},
"enablePullDownRefresh": true,
"backgroundTextStyle": "dark"
}
}

View File

@@ -1,15 +1,16 @@
<view class="page">
<block wx:if="{{loading}}">
<view class="hero hero-skeleton"></view>
<view class="grid">
<view class="module module-skeleton" wx:for="{{skeletonItems}}" wx:key="*this">
<view class="sk sk-circle"></view>
<view class="sk sk-line w-70"></view>
<view class="sk sk-line w-40"></view>
</view>
<!-- 已登录:web-view 模式 -->
<block wx:if="{{isLoggedIn}}">
<web-view src="{{webviewUrl}}" wx:if="{{webviewUrl && !webviewError}}" key="{{webviewKey}}" binderror="onWebviewError"></web-view>
<view class="error-page" wx:if="{{webviewError}}">
<view class="error-icon">!</view>
<view class="error-title">页面加载失败</view>
<view class="error-desc">{{errorMsg}}</view>
<view class="error-retry" hover-class="btn-hover" bindtap="onRetry">重新加载</view>
</view>
</block>
<!-- 未登录:原生游客首页 -->
<block wx:else>
<view class="hero">
<view class="hero-chip" wx:if="{{greeting}}">{{greeting}}</view>
@@ -20,11 +21,6 @@
</view>
</view>
<view class="error-bar" wx:if="{{error}}">
<text class="error-text">{{error}}</text>
<view class="error-retry" hover-class="link-hover" bindtap="onRetry">重试</view>
</view>
<view class="grid">
<view
class="module"
@@ -40,7 +36,6 @@
</view>
</view>
<!-- 游客模式:明缘介绍 -->
<view class="intro-section" wx:if="{{introData && introData.sections}}">
<view class="intro-title">{{introData.title}}</view>
<view class="intro-card" wx:for="{{introData.sections}}" wx:key="heading">
@@ -51,7 +46,5 @@
</view>
</view>
</view>
<view class="empty" wx:if="{{modules.length === 0 && !error}}">暂无功能入口</view>
</block>
</view>

View File

@@ -36,12 +36,17 @@
color: #E8DCCB;
line-height: 1.6;
}
.hero-skeleton {
min-height: 240rpx;
background: linear-gradient(90deg, #EFE6D9 25%, #FAF4EC 50%, #EFE6D9 75%);
background-size: 200% 100%;
animation: shimmer 1.4s ease-in-out infinite;
.hero-login-tip {
margin-top: 24rpx;
display: inline-block;
padding: 12rpx 28rpx;
background: rgba(198, 123, 48, 0.25);
border: 1rpx solid rgba(227, 179, 124, 0.4);
border-radius: 999rpx;
color: #E3B37C;
font-size: 24rpx;
}
.btn-hover { opacity: 0.7; }
/* ---- 模块网格 ---- */
.grid {
@@ -98,72 +103,6 @@
white-space: nowrap;
}
/* ---- 骨架屏 ---- */
.module-skeleton {
box-shadow: none;
}
.sk {
background: #EDE3D6;
border-radius: 10rpx;
}
.sk-circle {
width: 84rpx;
height: 84rpx;
border-radius: 50%;
margin-bottom: 16rpx;
}
.sk-line {
height: 24rpx;
margin-top: 12rpx;
}
.w-70 { width: 70%; }
.w-40 { width: 40%; }
/* ---- 错误 / 空状态 ---- */
.error-bar {
display: flex;
align-items: center;
justify-content: space-between;
gap: 20rpx;
margin-top: 32rpx;
padding: 22rpx 24rpx;
border-radius: 14rpx;
background: #FDEAEA;
color: #C0392B;
font-size: 24rpx;
}
.error-text {
flex: 1;
min-width: 0;
}
.error-retry {
flex-shrink: 0;
color: #C67B30;
font-weight: 700;
padding: 4rpx 0 4rpx 16rpx;
}
.link-hover { opacity: 0.7; }
.empty {
padding: 80rpx 0;
text-align: center;
color: #B6A996;
font-size: 26rpx;
}
@keyframes shimmer {
0% { background-position: 200% 0; }
100% { background-position: -200% 0; }
}
.hero-login-tip {
margin-top: 24rpx;
display: inline-block;
padding: 12rpx 28rpx;
background: rgba(198, 123, 48, 0.25);
border: 1rpx solid rgba(227, 179, 124, 0.4);
border-radius: 999rpx;
color: #E3B37C;
font-size: 24rpx;
}
/* ---- intro section ---- */
.intro-section {
margin-top: 40rpx;
@@ -205,4 +144,48 @@
font-size: 25rpx;
color: #5A4A3A;
line-height: 1.8;
}
}
/* ---- web-view 错误页 ---- */
.error-page {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 120rpx 60rpx;
height: 100vh;
box-sizing: border-box;
}
.error-icon {
width: 120rpx;
height: 120rpx;
border-radius: 50%;
background: #FDEAEA;
color: #C0392B;
font-size: 64rpx;
font-weight: 700;
display: flex;
align-items: center;
justify-content: center;
margin-bottom: 32rpx;
}
.error-title {
font-size: 32rpx;
font-weight: 700;
color: #2C1810;
margin-bottom: 16rpx;
}
.error-desc {
font-size: 26rpx;
color: #8B7A6A;
text-align: center;
line-height: 1.6;
margin-bottom: 40rpx;
}
.error-retry {
padding: 20rpx 60rpx;
background: #C67B30;
color: #fff;
font-size: 28rpx;
border-radius: 999rpx;
}