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