feat: 微信分享钩子全页面接入 + 订阅信息结构化映射 + JWT 错误处理加强

- client: 全部页面接入 onShareAppMessage/onShareTimeline 分享钩子
- client: login 页增强、profile 订阅展示优化
- client/stores: 订阅字段扁平结构映射为 subInfo(plan/expiresAt/startDate)
- server: auth 路由 JWT 无效/过期统一返回 401 未授权响应
- server: models/tests 同步更新
This commit is contained in:
Yuzhiran Dev
2026-09-04 11:47:29 +08:00
parent de8ca9c420
commit 66de1dc396
37 changed files with 2138 additions and 1830 deletions
+21 -12
View File
@@ -8,11 +8,17 @@ const User = require('../../src/models/User')
describe('User Model', () => {
beforeAll(async () => {
// 连接测试数据库
const mongoURI = process.env.MONGODB_URI || 'mongodb://localhost:27017/wdkj'
const mongoURI = process.env.MONGODB_URI || 'mongodb://localhost:27017/wdkj_test'
await mongoose.connect(mongoURI)
// 确保唯一索引已构建(dropDatabase 后 mongoose 不会自动重建)
await mongoose.connection.syncIndexes()
})
afterAll(async () => {
// 防呆:只允许在测试库中 dropDatabase
if (mongoose.connection.name === 'wdkj') {
throw new Error('[安全] 不允许对生产数据库 wdkj 执行 dropDatabase')
}
// 清理并关闭连接
await mongoose.connection.dropDatabase()
await mongoose.connection.close()
@@ -42,12 +48,15 @@ describe('User Model', () => {
expect(savedUser.totalScore).toBe(0)
})
test('openid 是必需的', async () => {
const user = new User({
nickName: '测试用户'
test('username 必须唯一', async () => {
await new User({ username: 'alice', nickName: '用户A' }).save()
const duplicateUser = new User({
username: 'alice',
nickName: '用户B'
})
await expect(user.save()).rejects.toThrow()
await expect(duplicateUser.save()).rejects.toThrow()
})
test('openid 必须唯一', async () => {
@@ -134,11 +143,11 @@ describe('User Model', () => {
describe('静态方法', () => {
test('getLeaderboard 应该返回排行榜', async () => {
// 创建测试用户
// 创建测试用户(分数由 exploreData 派生,保存时自动计算)
await User.create([
{ openid: 'user1', totalScore: 300, dim1Score: 100 },
{ openid: 'user2', totalScore: 500, dim1Score: 200 },
{ openid: 'user3', totalScore: 400, dim1Score: 150 }
{ openid: 'user1', exploreData: { dim1: { bestScore: 300 } } },
{ openid: 'user2', exploreData: { dim1: { bestScore: 500 } } },
{ openid: 'user3', exploreData: { dim1: { bestScore: 400 } } }
])
const leaderboard = await User.getLeaderboard(null, 10)
@@ -151,9 +160,9 @@ describe('User Model', () => {
test('getLeaderboard 应该支持按维度排序', async () => {
await User.create([
{ openid: 'user1', dim1Score: 100, totalScore: 100 },
{ openid: 'user2', dim1Score: 200, totalScore: 200 },
{ openid: 'user3', dim1Score: 150, totalScore: 150 }
{ openid: 'user1', exploreData: { dim1: { bestScore: 100 } } },
{ openid: 'user2', exploreData: { dim1: { bestScore: 200 } } },
{ openid: 'user3', exploreData: { dim1: { bestScore: 150 } } }
])
const leaderboard = await User.getLeaderboard(1, 10)