// pages/review/index.js
|
const app = getApp()
|
const { graphqlRequest, formatDate } = require('../../lib/utils')
|
|
const GET_RATING_STATS_QUERY = `
|
query GetRatingStats($activityPlayerId: ID!) {
|
judgeRatingsForPlayer(activityPlayerId: $activityPlayerId) {
|
hasRated
|
}
|
}
|
`
|
|
Page({
|
data: {
|
loading: false,
|
loadingMore: false,
|
hasMore: true,
|
|
// 当前选项卡 0:我未评审 1:我已评审 2:学员未评审
|
currentTab: 0,
|
|
// 搜索关键词
|
searchKeyword: '',
|
|
// 项目列表
|
projectList: [],
|
|
// 统计数据
|
unReviewedCount: 0,
|
reviewedCount: 0,
|
studentUnReviewedCount: 0,
|
|
// 分页参数
|
pageSize: 10,
|
currentPage: 1
|
},
|
|
onLoad(options) {
|
// 如果有传入的tab参数,设置当前选项卡
|
if (options.tab) {
|
this.setData({
|
currentTab: parseInt(options.tab) || 0
|
})
|
}
|
|
this.loadData()
|
},
|
|
onShow() {
|
// 页面显示时刷新数据
|
this.loadData()
|
},
|
|
onPullDownRefresh() {
|
this.loadData()
|
},
|
|
onReachBottom() {
|
if (this.data.hasMore && !this.data.loadingMore) {
|
this.loadMoreData()
|
}
|
},
|
|
// 切换选项卡
|
switchTab(e) {
|
const index = parseInt(e.currentTarget.dataset.index) || 0
|
if (index === this.data.currentTab) return
|
|
this.setData({
|
currentTab: index,
|
projectList: [],
|
currentPage: 1,
|
hasMore: true,
|
searchKeyword: ''
|
})
|
|
this.loadData()
|
},
|
|
// 搜索输入
|
onSearchInput(e) {
|
this.setData({
|
searchKeyword: e.detail.value
|
})
|
},
|
|
// 搜索确认
|
onSearch() {
|
this.setData({
|
projectList: [],
|
currentPage: 1,
|
hasMore: true
|
})
|
this.loadData()
|
},
|
|
// 清除搜索
|
clearSearch() {
|
this.setData({
|
searchKeyword: '',
|
projectList: [],
|
currentPage: 1,
|
hasMore: true
|
})
|
this.loadData()
|
},
|
|
// 加载数据
|
async loadData() {
|
if (this.data.loading) return
|
|
this.setData({ loading: true })
|
|
try {
|
await Promise.all([
|
this.loadProjectList(),
|
this.loadStatistics()
|
])
|
} catch (error) {
|
console.error('加载数据失败:', error)
|
|
// 检查是否是认证相关错误
|
if (error.message && (
|
error.message.includes('没有权限访问') ||
|
error.message.includes('请先登录') ||
|
error.message.includes('重新登录')
|
)) {
|
wx.showToast({
|
title: '正在重新登录...',
|
icon: 'loading',
|
duration: 2000
|
})
|
|
// 等待一段时间后重试
|
setTimeout(() => {
|
this.loadData()
|
}, 2000)
|
} else {
|
wx.showToast({
|
title: '加载失败,请重试',
|
icon: 'none'
|
})
|
}
|
} finally {
|
this.setData({ loading: false })
|
wx.stopPullDownRefresh()
|
}
|
},
|
|
// 加载更多数据
|
async loadMoreData() {
|
if (this.data.loadingMore || !this.data.hasMore) return
|
|
this.setData({ loadingMore: true })
|
|
try {
|
await this.loadProjectList(true)
|
} catch (error) {
|
console.error('加载更多失败:', error)
|
} finally {
|
this.setData({ loadingMore: false })
|
}
|
},
|
|
// 加载项目列表
|
async loadProjectList(isLoadMore = false) {
|
const { currentTab, searchKeyword, pageSize, currentPage } = this.data
|
|
let query = ''
|
let variables = {
|
page: isLoadMore ? currentPage + 1 : 1,
|
pageSize: pageSize,
|
searchKeyword: searchKeyword || null
|
}
|
|
// 根据当前选项卡构建不同的查询
|
switch (currentTab) {
|
case 0: // 我未评审
|
query = `
|
query GetUnReviewedProjects($page: Int!, $pageSize: Int!, $searchKeyword: String) {
|
unReviewedProjects(page: $page, pageSize: $pageSize, searchKeyword: $searchKeyword) {
|
items {
|
id
|
projectName
|
activityName
|
stageName
|
studentName
|
submitTime
|
status
|
}
|
total
|
hasMore
|
}
|
}
|
`
|
break
|
case 1: // 我已评审
|
query = `
|
query GetReviewedProjects($page: Int!, $pageSize: Int!, $searchKeyword: String) {
|
reviewedProjects(page: $page, pageSize: $pageSize, searchKeyword: $searchKeyword) {
|
items {
|
id
|
projectName
|
activityName
|
stageName
|
studentName
|
submitTime
|
reviewTime
|
score
|
status
|
}
|
total
|
hasMore
|
}
|
}
|
`
|
break
|
case 2: // 学员未评审
|
query = `
|
query GetStudentUnReviewedProjects($page: Int!, $pageSize: Int!, $searchKeyword: String) {
|
studentUnReviewedProjects(page: $page, pageSize: $pageSize, searchKeyword: $searchKeyword) {
|
items {
|
id
|
projectName
|
activityName
|
stageName
|
studentName
|
submitTime
|
status
|
}
|
total
|
hasMore
|
}
|
}
|
`
|
break
|
}
|
|
const result = await graphqlRequest(query, variables)
|
|
if (result) {
|
const dataKey = currentTab === 0 ? 'unReviewedProjects' :
|
currentTab === 1 ? 'reviewedProjects' :
|
'studentUnReviewedProjects'
|
|
const data = result[dataKey]
|
|
if (data && data.items) {
|
// 处理项目数据
|
const projects = data.items.map(item => ({
|
...item,
|
submitTime: item.submitTime ? formatDate(item.submitTime) : '',
|
reviewTime: item.reviewTime ? formatDate(item.reviewTime) : ''
|
}))
|
|
const projectsWithRatingCount = await this.enrichProjectsWithRatingCounts(projects)
|
|
this.setData({
|
projectList: isLoadMore ? [...this.data.projectList, ...projectsWithRatingCount] : projectsWithRatingCount,
|
hasMore: data.hasMore || false,
|
currentPage: variables.page
|
})
|
}
|
}
|
},
|
|
// 加载统计数据
|
async loadStatistics() {
|
try {
|
const query = `
|
query GetReviewStatistics {
|
reviewStatistics {
|
unReviewedCount
|
reviewedCount
|
studentUnReviewedCount
|
}
|
}
|
`
|
|
const result = await graphqlRequest(query)
|
|
if (result && result.reviewStatistics) {
|
this.setData({
|
unReviewedCount: result.reviewStatistics.unReviewedCount || 0,
|
reviewedCount: result.reviewStatistics.reviewedCount || 0,
|
studentUnReviewedCount: result.reviewStatistics.studentUnReviewedCount || 0
|
})
|
}
|
} catch (error) {
|
console.error('加载统计数据失败:', error)
|
}
|
},
|
|
// 跳转到评审详情页面
|
goToReviewDetail(e) {
|
const { activityPlayerId } = e.currentTarget.dataset
|
if (activityPlayerId) {
|
wx.navigateTo({
|
url: `/pages/judge/review?id=${activityPlayerId}`
|
})
|
}
|
},
|
|
async enrichProjectsWithRatingCounts(projects) {
|
if (!Array.isArray(projects) || projects.length === 0) {
|
return projects || []
|
}
|
|
try {
|
const counts = await Promise.all(projects.map(project => this.getProjectRatingCount(project.id)))
|
return projects.map((project, index) => ({
|
...project,
|
ratingCount: counts[index]
|
}))
|
} catch (error) {
|
console.error('批量获取评审次数失败:', error)
|
return projects.map(project => ({
|
...project,
|
ratingCount: typeof project.ratingCount === 'number' ? project.ratingCount : 0
|
}))
|
}
|
},
|
|
async getProjectRatingCount(activityPlayerId) {
|
if (!activityPlayerId) {
|
return 0
|
}
|
|
try {
|
const result = await graphqlRequest(GET_RATING_STATS_QUERY, { activityPlayerId })
|
const ratings = result?.judgeRatingsForPlayer || []
|
return ratings.filter(item => item?.hasRated).length
|
} catch (error) {
|
console.error(`获取项目 ${activityPlayerId} 的评审次数失败:`, error)
|
return 0
|
}
|
},
|
|
// 获取空状态文本
|
getEmptyText() {
|
const { currentTab, searchKeyword } = this.data
|
|
if (searchKeyword) {
|
return '未找到相关项目'
|
}
|
|
const emptyTexts = ['暂无待评审项目', '暂无已评审项目', '暂无学员未评审项目']
|
return emptyTexts[currentTab] || '暂无数据'
|
},
|
|
// 获取空状态描述
|
getEmptyDesc() {
|
const { currentTab, searchKeyword } = this.data
|
|
if (searchKeyword) {
|
return '请尝试其他关键词搜索'
|
}
|
|
const emptyDescs = [
|
'当前没有需要您评审的项目',
|
'您还没有完成任何评审',
|
'当前没有学员未评审的项目'
|
]
|
return emptyDescs[currentTab] || ''
|
}
|
})
|