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
+22 -28
View File
@@ -25,45 +25,39 @@ Page({
this.setData({ isLoading: true });
wx.showLoading({ title: '加载中...' });
try {
// Fetch Config Tree
const treeRes = await request.get('/config/badge/tree');
// Parallel Fetch: Config Tree & User Badges
const [treeRes, userBadgesRes] = await Promise.all([
request.get('/config/badge/tree'),
request.get('/profile/badge')
]);
const list = Array.isArray(treeRes) ? treeRes : (treeRes.data || []);
// DEBUG: Force Unlock All
let achievedMap = {};
list.forEach(dim => {
if (dim.groups) {
dim.groups.forEach(grp => {
if (grp.badges) {
grp.badges.forEach(b => {
achievedMap[b.id] = true;
});
// Extract user badge list from response structure { list: [...] }
let userBadgeList = [];
if (Array.isArray(userBadgesRes)) {
userBadgeList = userBadgesRes;
} else if (userBadgesRes && Array.isArray(userBadgesRes.list)) {
userBadgeList = userBadgesRes.list;
} else if (userBadgesRes && userBadgesRes.data) {
userBadgeList = userBadgesRes.data || [];
}
});
}
});
// Original logic commented out for debug
/*
try {
const profile = await request.get('/profile/detail');
if (profile && profile.achievedBadges) {
profile.achievedBadges.forEach(b => {
const id = typeof b === 'string' ? b : b.id;
achievedMap[id] = true;
// Populate Achieved Map using Badge ID (not Record ID)
let achievedMap = {};
userBadgeList.forEach(b => {
const badgeId = b.badgeId || (b.badge ? b.badge.id : null);
if (badgeId) {
achievedMap[badgeId] = b;
}
});
}
} catch (e) {
// Silent fail
}
*/
this.setData({
dimensions: list,
achievedMap
});
} catch (e) {
console.error('Fetch badge tree failed', e);
console.error('Fetch badge data failed', e);
wx.showToast({ title: '加载失败', icon: 'none' });
} finally {
this.setData({ isLoading: false });
+5
View File
@@ -1,5 +1,6 @@
// pages/tasks/index.js
import request from '../../utils/request';
import { requestSubscription } from '../../utils/subscribe';
Page({
data: {
@@ -239,6 +240,9 @@ Page({
const taskId = this.data.completingTask.id;
const remark = this.data.remark || '';
// 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', {
@@ -305,6 +309,7 @@ Page({
console.error('Complete task failed', err);
wx.showToast({ title: '操作失败', icon: 'none' });
});
});
},
gotoGarden() {
+11 -9
View File
@@ -10,30 +10,32 @@
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
height: 100vh;
padding: 48rpx;
box-sizing: border-box;
padding-bottom: 20%; /* Push up visually */
}
.state-card {
background: #fff;
border-radius: 40rpx;
padding: 56rpx 48rpx;
border-radius: 48rpx;
padding: 64rpx 48rpx;
width: 100%;
display: flex;
flex-direction: column;
align-items: center;
gap: 32rpx;
box-shadow: 0 12rpx 40rpx rgba(85, 139, 47, 0.08);
gap: 40rpx;
box-shadow: 0 20rpx 60rpx rgba(85, 139, 47, 0.15);
}
/* ========== Loading State ========== */
.loading-image-wrap {
width: 280rpx;
height: 280rpx;
border-radius: 32rpx;
width: 440rpx;
height: 440rpx;
border-radius: 40rpx;
overflow: hidden;
position: relative;
box-shadow: 0 8rpx 32rpx rgba(0,0,0,0.1);
box-shadow: 0 12rpx 40rpx rgba(0,0,0,0.15);
}
.loading-preview {
+3 -3
View File
@@ -60,12 +60,12 @@
display: inline-flex;
align-items: center;
justify-content: center;
padding: 0 36rpx;
padding: 0 28rpx;
height: 72rpx;
background: #fff;
border-radius: 36rpx;
margin-right: 24rpx;
font-size: 28rpx;
margin-right: 16rpx;
font-size: 26rpx;
color: #546E7A;
font-weight: 600;
box-shadow: 0 4rpx 12rpx rgba(0,0,0,0.04);
+26 -17
View File
@@ -1,38 +1,47 @@
/**
* Request WeChat Mini Program Subscription Message
* Template ID: R7fh3NDpuV8DYqI83HpEQvC8mLJy5xMWFl1qeGN9JIo
* Template ID: iG5GYMPQAgKxIE9zZNOgKS6tCURhM9p9AC8iZ3Uj3uA
*/
const TEMPLATE_ID = 'R7fh3NDpuV8DYqI83HpEQvC8mLJy5xMWFl1qeGN9JIo';
const DEFAULT_TEMPLATE_ID = 'iG5GYMPQAgKxIE9zZNOgKS6tCURhM9p9AC8iZ3Uj3uA';
export const requestSubscription = () => {
export const requestSubscription = (tmplIds = [DEFAULT_TEMPLATE_ID], silent = false) => {
return new Promise((resolve) => {
// Check if subscription capability is available (basic check)
if (!wx.requestSubscribeMessage) {
console.warn('Current version does not support subscribe message');
if (!silent) console.warn('Current version does not support subscribe message');
resolve({ success: false, errMsg: 'Not supported' });
return;
}
wx.requestSubscribeMessage({
tmplIds: [TEMPLATE_ID],
tmplIds: tmplIds,
success(res) {
if (res[TEMPLATE_ID] === 'accept') {
resolve({ success: true, status: 'accept' });
} else {
resolve({ success: false, status: res[TEMPLATE_ID] });
}
// If any of the requested IDs are accepted
const isAccepted = tmplIds.some(id => res[id] === 'accept');
resolve({ success: isAccepted, res });
},
fail(err) {
console.error('Subscription failed', err);
resolve({ success: false, errMsg: err.errMsg });
if (!silent) console.error('Subscription failed', err);
// 20004: User closed main switch in settings
if (err.errCode === 20004 && !silent) {
wx.showModal({
title: '提示',
content: '请在设置中开启订阅消息通知',
confirmText: '去开启',
success: (modalRes) => {
if (modalRes.confirm) {
wx.openSetting();
}
}
});
}
resolve({ success: false, error: err });
}
});
});
};
export const checkSubscriptionSettings = () => {
export const checkSubscriptionSettings = (tmplId = DEFAULT_TEMPLATE_ID) => {
return new Promise((resolve) => {
if (!wx.getSetting) {
resolve(undefined);
@@ -43,7 +52,7 @@ export const checkSubscriptionSettings = () => {
withSubscriptions: true,
success(res) {
const itemSettings = (res.subscriptionsSetting && res.subscriptionsSetting.itemSettings) || {};
resolve(itemSettings[TEMPLATE_ID]);
resolve(itemSettings[tmplId]);
},
fail() {
resolve(undefined);