172 lines
4.5 KiB
JavaScript

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.loadProfile();
this.resetAndLoad();
},
onPullDownRefresh() {
this.loadProfile();
this.resetAndLoad()
.finally(() => wx.stopPullDownRefresh());
},
onRefresh() {
if (this.data.refresherTriggered) return;
this.setData({ refresherTriggered: true });
this.loadProfile();
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;
this.setData({ loading: true });
auth
.fetchMyProfile()
.then((profile) => {
const p = profile || {};
this.setData({
nickName: p.nickName || '',
certificateCount: p.certificateCount != null ? p.certificateCount : '-'
});
})
.catch(() => {})
.finally(() => this.setData({ loading: false }));
}
,
/** 重置并加载 */
resetAndLoad() {
this.setData({ records: [], pageNo: 1, noMore: false });
return this.loadRecords();
},
/** 触底加载更多 */
onLoadMore() {
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}`,
timeStr: item.createdTime ? formatTime(item.createdTime) : (item.timeStr || '')
}));
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' })
});
}
});