64 lines
1.8 KiB
JavaScript
64 lines
1.8 KiB
JavaScript
const { getLayout } = require('../../utils/layout');
|
|
// pages/pet/achievements.js
|
|
const { api } = require('../../utils/request');
|
|
|
|
Page({
|
|
onResize: function() { this.setData(getLayout()); },
|
|
data: { loading: true, loadError: false,
|
|
achievements: [],
|
|
unlockedCount: 0,
|
|
totalCount: 0,
|
|
progressPercent: 0
|
|
},
|
|
|
|
onLoad: function() {
|
|
this.setData(getLayout());
|
|
this.loadAchievements();
|
|
},
|
|
|
|
onShow: function() {
|
|
this.loadAchievements();
|
|
},
|
|
|
|
loadAchievements: async function() {
|
|
if (this._pageLoading) return;
|
|
this._pageLoading = true;
|
|
this.setData({loading:true, loadError:false});
|
|
try {
|
|
const res = await api.petAchievements();
|
|
let list = res.achievements || [];
|
|
// 排序:已解锁的按解锁时间倒序(最新获得的排最前),未解锁的排后面
|
|
list = list.sort(function(a, b) {
|
|
var aUnlocked = a.unlocked ? 1 : 0;
|
|
var bUnlocked = b.unlocked ? 1 : 0;
|
|
if (aUnlocked !== bUnlocked) {
|
|
return bUnlocked - aUnlocked;
|
|
}
|
|
if (aUnlocked && bUnlocked) {
|
|
var ta = (a.unlocked_at || '').replace(/-/g, '').replace(/[: ]/g, '');
|
|
var tb = (b.unlocked_at || '').replace(/-/g, '').replace(/[: ]/g, '');
|
|
return tb.localeCompare(ta);
|
|
}
|
|
return 0;
|
|
});
|
|
const unlocked = list.filter(a => a.unlocked);
|
|
const total = list.length;
|
|
var pageData = {
|
|
achievements: list,
|
|
unlockedCount: unlocked.length,
|
|
totalCount: total,
|
|
progressPercent: total > 0 ? Math.round(unlocked.length / total * 100) : 0
|
|
};
|
|
this.setData(pageData);
|
|
} catch (e) {
|
|
this.setData({loadError:true});
|
|
console.error(e);
|
|
}
|
|
finally { this._pageLoading = false; this.setData({loading:false}); }
|
|
},
|
|
|
|
goBack: function() {
|
|
wx.navigateBack();
|
|
}
|
|
});
|