feat: 完成任务添加订阅

This commit is contained in:
Blizzard
2026-02-14 15:38:48 +08:00
parent 2d8ffd842a
commit 5800466e69
5 changed files with 123 additions and 113 deletions
+61 -56
View File
@@ -1,5 +1,6 @@
// pages/tasks/index.js
import request from '../../utils/request';
import { requestSubscription } from '../../utils/subscribe';
Page({
data: {
@@ -239,71 +240,75 @@ Page({
const taskId = this.data.completingTask.id;
const remark = this.data.remark || '';
wx.showLoading({ title: '提交中...', mask: true });
// Attempt to subscribe (silent mode avoids error popups if disabled)
// This encourages "Always Allow" behavior for seamless experience
requestSubscription(undefined, true).then(() => {
wx.showLoading({ title: '提交中...', mask: true });
request.post('/plant/completeTask', {
taskId: taskId,
remark: remark
}).then(res => {
wx.hideLoading();
request.post('/plant/completeTask', {
taskId: taskId,
remark: remark
}).then(res => {
wx.hideLoading();
// Handle Rewards
const queue = [];
// Check if res has level up or badge data
// Note: res is already data.data from request.js
if (res && res.isLevelUp && res.currentLevel) {
queue.push({ type: 'level', data: res.currentLevel });
}
// Handle Rewards
const queue = [];
// Check if res has level up or badge data
// Note: res is already data.data from request.js
if (res && res.isLevelUp && res.currentLevel) {
queue.push({ type: 'level', data: res.currentLevel });
}
// Check for Badge using IsGetBadge flag (allowing for casing variance)
if (res && (res.IsGetBadge === true || res.isGetBadge === true) && res.newBadge) {
queue.push({ type: 'badge', data: res.newBadge });
}
// Check for Badge using IsGetBadge flag (allowing for casing variance)
if (res && (res.IsGetBadge === true || res.isGetBadge === true) && res.newBadge) {
queue.push({ type: 'badge', data: res.newBadge });
}
this._popupQueue = queue;
this._popupQueue = queue;
// Optimistic UI Update Logic
const groups = this.data.groupedTasks;
let updated = false;
// Need to deep clone possibly, but here we modify and set back
for (let g of groups) {
// g is an object. groupedTasks is array of objects.
if (g.tasks) {
const t = g.tasks.find(x => x && x.id === taskId);
if (t) {
t.isCompleted = true;
t.isOverdue = false;
updated = true;
break;
// Optimistic UI Update Logic
const groups = this.data.groupedTasks;
let updated = false;
// Need to deep clone possibly, but here we modify and set back
for (let g of groups) {
// g is an object. groupedTasks is array of objects.
if (g.tasks) {
const t = g.tasks.find(x => x && x.id === taskId);
if (t) {
t.isCompleted = true;
t.isOverdue = false;
updated = true;
break;
}
}
}
}
// Trigger Animation and Close Modal
this.setData({
completingTask: null,
remark: '',
groupedTasks: groups, // Update UI
tasks: groups,
showSunshine: true
// Trigger Animation and Close Modal
this.setData({
completingTask: null,
remark: '',
groupedTasks: groups, // Update UI
tasks: groups,
showSunshine: true
});
// Hide Animation after duration and Start showing modals
setTimeout(() => {
this.setData({ showSunshine: false });
// Show rewards after sunshine animation
if (this._popupQueue.length > 0) {
this.processPopupQueue();
}
}, 1000); // 1.0s delay usually covers animation
// Sync with backend silently
this.fetchTodayTasks();
}).catch(err => {
wx.hideLoading();
console.error('Complete task failed', err);
wx.showToast({ title: '操作失败', icon: 'none' });
});
// Hide Animation after duration and Start showing modals
setTimeout(() => {
this.setData({ showSunshine: false });
// Show rewards after sunshine animation
if (this._popupQueue.length > 0) {
this.processPopupQueue();
}
}, 1000); // 1.0s delay usually covers animation
// Sync with backend silently
this.fetchTodayTasks();
}).catch(err => {
wx.hideLoading();
console.error('Complete task failed', err);
wx.showToast({ title: '操作失败', icon: 'none' });
});
},