2026-05-09 16:19:37 +08:00

185 lines
5.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const auth = require('../../../utils/auth');
const { formatTime } = require('../../../utils/format');
const PAGE_SIZE = 10;
Page({
data: {
nickName: '',
certificateCount: '-',
headerBg: 'https://yuedong-wechatapplet.oss-cn-shanghai.aliyuncs.com/static/event_miniapp/book.png',
tabLineImg: 'https://yuedong-wechatapplet.oss-cn-shanghai.aliyuncs.com/static/event_miniapp/point-b.png',
activeTab: 1, // 1-个人证书 2-团队证书
records: [],
loading: false,
noMore: false,
pageNo: 1,
refresherTriggered: false
},
onShow() {
const accessToken = wx.getStorageSync('accessToken') || '';
if (!accessToken) {
wx.redirectTo({ url: '/subpackages/mine/login/login' });
return;
}
this.resetAndLoad();
},
onPullDownRefresh() {
this.resetAndLoad()
.finally(() => wx.stopPullDownRefresh());
},
onRefresh() {
if (this.data.refresherTriggered) return;
this.setData({ refresherTriggered: true });
this.resetAndLoad()
.finally(() => this.setData({ refresherTriggered: false }));
},
/** 返回 */
onBack() {
wx.navigateBack({ delta: 1 });
},
/** 切换Tab */
onSwitchTab(e) {
const tab = Number(e.currentTarget.dataset.tab);
if (tab === this.data.activeTab) return;
this.setData({ activeTab: tab });
this.resetAndLoad();
},
loadProfile() {
if (this.data.loading) return;
auth
.fetchMyProfile()
.then((profile) => {
console.log('profile', profile);
const p = profile || {};
this.setData({
nickName: p.nickName || '',
certificateCount: p.certificateCount != null ? p.certificateCount : '-'
});
})
.catch(() => {});
},
/** 重置并加载 */
resetAndLoad() {
this.setData({ records: [], pageNo: 1, noMore: false });
this.loadProfile();
return this.loadRecords();
},
/** 上拉加载更多 */
onReachBottom() {
if (this.data.loading || this.data.noMore) return;
this.loadRecords();
},
/** 加载证书列表 */
loadRecords() {
if (this.data.loading) return Promise.resolve();
this.setData({ loading: true });
const { pageNo, activeTab } = this.data;
return auth
.fetchMyCertificates({ pageNo, pageSize: PAGE_SIZE, type: activeTab })
.then((result) => {
const res = result || {};
const rows = Array.isArray(res.rows) ? res.rows : (Array.isArray(res.list) ? res.list : []);
const total = typeof res.total === 'number' ? res.total : (typeof res.count === 'number' ? res.count : rows.length);
const formatted = rows.map((item, idx) => ({
...item,
id: item.id != null ? item.id : `${activeTab}-${pageNo}-${idx}`,
// 赛事名称
eventName: item.eventName || '',
// 项目-组别展示,优先用 projectGroupDisplay,否则拼接 projectName + groupName
projectGroupDisplay:
item.projectGroupDisplay ||
[item.projectName, item.groupName].filter(Boolean).join(' - ') ||
'-',
// 证书链接
certificateUrl: item.certificateUrl || '',
// 证书名称(用于查看证书时的标题)
certificateName: item.certificateName || item.eventName || '证书',
// 颁发时间
timeStr: item.issuedTime
? formatTime(item.issuedTime)
: item.createdTime
? formatTime(item.createdTime)
: ''
}));
const newRecords = this.data.records.concat(formatted);
const currentTotal = pageNo * PAGE_SIZE;
this.setData({
records: newRecords,
pageNo: pageNo + 1,
noMore: currentTotal >= total
});
})
.catch(() => {
wx.showToast({ title: '加载失败', icon: 'none' });
})
.finally(() => {
this.setData({ loading: false });
});
},
/** 查看证书 */
onViewCert(e) {
const url = e.currentTarget.dataset.url;
const name = e.currentTarget.dataset.name || '证书';
if (!url) {
wx.showToast({ title: '暂无证书链接', icon: 'none' });
return;
}
const lower = String(url).toLowerCase();
const isImage = lower.endsWith('.png') || lower.endsWith('.jpg') || lower.endsWith('.jpeg') || lower.includes('image');
const isPdf = lower.endsWith('.pdf');
if (isImage) {
wx.previewImage({ urls: [url], current: url });
return;
}
if (isPdf) {
wx.showLoading({ title: '打开中...' });
wx.downloadFile({
url,
success: (res) => {
wx.openDocument({
filePath: res.tempFilePath,
showMenu: true,
success: () => {},
fail: () => wx.showToast({ title: '打开失败', icon: 'none' }),
complete: () => wx.hideLoading()
});
},
fail: () => {
// wx.hideLoading();
wx.showToast({ title: '下载失败', icon: 'none' });
}
});
return;
}
wx.setClipboardData({
data: url,
success: () => wx.showToast({ title: `${name}链接已复制`, icon: 'none' })
});
}
});