feat: 分类管理页开发
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"navigationBarTitleText": "分类管理",
|
||||
"navigationBarTextStyle": "black",
|
||||
"navigationBarBackgroundColor": "#F8F0E0",
|
||||
"backgroundColor": "#F8F0E0",
|
||||
"enablePullDownRefresh": true,
|
||||
"usingComponents": {
|
||||
"toy-button": "/toy/button-v2/button"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,233 @@
|
||||
@import '../../style/theme.less';
|
||||
|
||||
page {
|
||||
min-height: 100%;
|
||||
background: @bg-header;
|
||||
}
|
||||
|
||||
.category-manage-page {
|
||||
min-height: 100vh;
|
||||
padding: 24rpx;
|
||||
background: @bg-header;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.category-manage-page__summary {
|
||||
display: flex;
|
||||
}
|
||||
|
||||
.category-manage-page__summary-card {
|
||||
flex: 1;
|
||||
padding: 28rpx 20rpx;
|
||||
background: @bg-white;
|
||||
border-radius: @radius-xl;
|
||||
text-align: center;
|
||||
box-shadow: @shadow;
|
||||
}
|
||||
|
||||
.category-manage-page__summary-card+.category-manage-page__summary-card {
|
||||
margin-left: 20rpx;
|
||||
}
|
||||
|
||||
.category-manage-page__summary-num {
|
||||
display: block;
|
||||
font-size: 40rpx;
|
||||
font-weight: 800;
|
||||
color: @text-title;
|
||||
}
|
||||
|
||||
.category-manage-page__summary-label {
|
||||
display: block;
|
||||
margin-top: 10rpx;
|
||||
font-size: 22rpx;
|
||||
color: @text-secondary;
|
||||
}
|
||||
|
||||
.category-manage-page__hint,
|
||||
.category-manage-page__loading {
|
||||
margin-top: 24rpx;
|
||||
padding: 24rpx 28rpx;
|
||||
background: @bg-white;
|
||||
border-radius: @radius;
|
||||
font-size: 24rpx;
|
||||
line-height: 1.6;
|
||||
color: @text-secondary;
|
||||
box-shadow: @shadow;
|
||||
}
|
||||
|
||||
.category-manage-page__list,
|
||||
.category-manage-page__cloud-only {
|
||||
margin-top: 24rpx;
|
||||
}
|
||||
|
||||
.category-manage-page__section-title {
|
||||
display: block;
|
||||
margin: 0 4rpx 16rpx;
|
||||
font-size: 26rpx;
|
||||
font-weight: 700;
|
||||
color: @text-secondary;
|
||||
}
|
||||
|
||||
.category-card {
|
||||
padding: 28rpx;
|
||||
margin-bottom: 20rpx;
|
||||
background: @bg-white;
|
||||
border-radius: @radius-xl;
|
||||
box-shadow: @shadow;
|
||||
border: 2rpx solid transparent;
|
||||
}
|
||||
|
||||
.category-card--synced {
|
||||
border-color: fade(#93d333, 30%);
|
||||
}
|
||||
|
||||
.category-card--missing {
|
||||
border-color: fade(#ffb703, 40%);
|
||||
}
|
||||
|
||||
.category-card--outdated {
|
||||
border-color: fade(#ff8f1f, 35%);
|
||||
}
|
||||
|
||||
.category-card--cloud-only {
|
||||
border-color: fade(#8ecae6, 35%);
|
||||
}
|
||||
|
||||
.category-card__header {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
justify-content: space-between;
|
||||
}
|
||||
|
||||
.category-card__meta {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
min-width: 0;
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.category-card__icon {
|
||||
width: 72rpx;
|
||||
height: 72rpx;
|
||||
border-radius: 50%;
|
||||
background: fade(@brand, 18%);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-size: 34rpx;
|
||||
line-height: 72rpx;
|
||||
text-align: center;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.category-card__title-box {
|
||||
min-width: 0;
|
||||
margin-left: 20rpx;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: space-between;
|
||||
}
|
||||
|
||||
.category-card__title-box-inner {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
|
||||
.category-card__id {
|
||||
margin-left: 20rpx;
|
||||
font-size: 26rpx;
|
||||
color: @text-secondary;
|
||||
}
|
||||
}
|
||||
|
||||
.category-card__title {
|
||||
display: block;
|
||||
font-size: 30rpx;
|
||||
font-weight: 700;
|
||||
color: @text-title;
|
||||
}
|
||||
|
||||
.category-card__subtitle {
|
||||
display: block;
|
||||
margin-top: 6rpx;
|
||||
font-size: 22rpx;
|
||||
color: @text-secondary;
|
||||
}
|
||||
|
||||
.category-card__badge {
|
||||
margin-left: 16rpx;
|
||||
padding: 8rpx 16rpx;
|
||||
border-radius: 999rpx;
|
||||
font-size: 22rpx;
|
||||
font-weight: 700;
|
||||
color: #888;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.category-card__right {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: flex-end;
|
||||
flex-shrink: 0;
|
||||
margin-left: 16rpx;
|
||||
gap: 16rpx;
|
||||
}
|
||||
|
||||
.category-card__diff {
|
||||
display: block;
|
||||
margin-top: 20rpx;
|
||||
font-size: 24rpx;
|
||||
line-height: 1.6;
|
||||
color: @text-secondary;
|
||||
}
|
||||
|
||||
.category-card__detail {
|
||||
margin-top: 16rpx;
|
||||
padding: 20rpx 22rpx;
|
||||
background: fade(@brand, 12%);
|
||||
border-radius: @radius;
|
||||
}
|
||||
|
||||
.category-card__detail-label {
|
||||
display: block;
|
||||
font-size: 22rpx;
|
||||
font-weight: 700;
|
||||
color: @text-secondary;
|
||||
}
|
||||
|
||||
.category-card__detail-text {
|
||||
display: block;
|
||||
margin-top: 8rpx;
|
||||
font-size: 24rpx;
|
||||
line-height: 1.6;
|
||||
color: @text-title;
|
||||
}
|
||||
|
||||
.category-card__action {
|
||||
margin-left: 16rpx;
|
||||
padding: 0 22rpx;
|
||||
height: 56rpx;
|
||||
line-height: 56rpx;
|
||||
border-radius: 999rpx;
|
||||
border: none;
|
||||
background: @brand;
|
||||
color: @text-selected-btn;
|
||||
font-size: 24rpx;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.category-card__action[disabled] {
|
||||
opacity: 0.6;
|
||||
}
|
||||
|
||||
.category-card__action--missing {
|
||||
background: #f7ee47;
|
||||
color: #1f2937;
|
||||
}
|
||||
|
||||
.category-card__action--outdated {
|
||||
background: #93d333;
|
||||
color: #1f2937;
|
||||
}
|
||||
@@ -0,0 +1,259 @@
|
||||
import { CATEGORY_LIST } from '../../core/data/categories';
|
||||
|
||||
type CloudCategory = {
|
||||
_id: string;
|
||||
name: string;
|
||||
icon: string;
|
||||
color: string;
|
||||
sortOrder: number;
|
||||
parentId?: string | null;
|
||||
};
|
||||
|
||||
type CompareStatus = 'synced' | 'missing' | 'outdated';
|
||||
|
||||
type CompareItem = {
|
||||
id: string;
|
||||
name: string;
|
||||
icon: string;
|
||||
sortOrder: number;
|
||||
status: CompareStatus;
|
||||
statusText: string;
|
||||
diffText: string;
|
||||
actionText: string;
|
||||
cloudCategory: CloudCategory | null;
|
||||
};
|
||||
|
||||
type CloudFunctionResult<T> = {
|
||||
success?: boolean;
|
||||
message?: string;
|
||||
data?: T;
|
||||
};
|
||||
|
||||
const DEFAULT_CATEGORY_COLORS = [
|
||||
'#F7EE47',
|
||||
'#93D333',
|
||||
'#FFB703',
|
||||
'#8ECAE6',
|
||||
'#FF8FAB',
|
||||
'#CDB4DB',
|
||||
];
|
||||
|
||||
function getDefaultCategoryColor(id: string, index: number): string {
|
||||
const hashBase = `${id}-${index}`;
|
||||
let hash = 0;
|
||||
for (let i = 0; i < hashBase.length; i++) {
|
||||
hash = (hash * 33 + hashBase.charCodeAt(i)) | 0;
|
||||
}
|
||||
return DEFAULT_CATEGORY_COLORS[
|
||||
Math.abs(hash) % DEFAULT_CATEGORY_COLORS.length
|
||||
];
|
||||
}
|
||||
|
||||
function buildCompareItems(cloudCategories: CloudCategory[]): {
|
||||
items: CompareItem[];
|
||||
cloudOnlyCategories: CloudCategory[];
|
||||
syncedCount: number;
|
||||
missingCount: number;
|
||||
outdatedCount: number;
|
||||
} {
|
||||
const cloudMap = new Map<string, CloudCategory>();
|
||||
for (const item of cloudCategories) {
|
||||
cloudMap.set(item._id, item);
|
||||
}
|
||||
|
||||
let syncedCount = 0;
|
||||
let missingCount = 0;
|
||||
let outdatedCount = 0;
|
||||
|
||||
const items = CATEGORY_LIST.map((localCategory, index) => {
|
||||
const cloudCategory = cloudMap.get(localCategory.id) || null;
|
||||
const sortOrder = index;
|
||||
|
||||
if (!cloudCategory) {
|
||||
missingCount += 1;
|
||||
return {
|
||||
id: localCategory.id,
|
||||
name: localCategory.name,
|
||||
icon: localCategory.icon,
|
||||
sortOrder,
|
||||
status: 'missing' as CompareStatus,
|
||||
statusText: '待新增',
|
||||
diffText: '',
|
||||
actionText: '新增',
|
||||
cloudCategory: null,
|
||||
};
|
||||
}
|
||||
|
||||
const diffFields: string[] = [];
|
||||
if (cloudCategory.name !== localCategory.name) diffFields.push('名称');
|
||||
if (cloudCategory.icon !== localCategory.icon) diffFields.push('图标');
|
||||
if ((cloudCategory.sortOrder ?? 0) !== sortOrder)
|
||||
diffFields.push('排序');
|
||||
|
||||
if (diffFields.length > 0) {
|
||||
outdatedCount += 1;
|
||||
return {
|
||||
id: localCategory.id,
|
||||
name: localCategory.name,
|
||||
icon: localCategory.icon,
|
||||
sortOrder,
|
||||
status: 'outdated' as CompareStatus,
|
||||
statusText: '待更新',
|
||||
diffText: `字段不一致:${diffFields.join('、')}。`,
|
||||
actionText: '更新',
|
||||
cloudCategory,
|
||||
};
|
||||
}
|
||||
|
||||
syncedCount += 1;
|
||||
return {
|
||||
id: localCategory.id,
|
||||
name: localCategory.name,
|
||||
icon: localCategory.icon,
|
||||
sortOrder,
|
||||
status: 'synced' as CompareStatus,
|
||||
statusText: '已同步',
|
||||
diffText: '',
|
||||
actionText: '',
|
||||
cloudCategory,
|
||||
};
|
||||
});
|
||||
|
||||
const localIds = new Set(CATEGORY_LIST.map((item) => item.id));
|
||||
const cloudOnlyCategories = cloudCategories.filter(
|
||||
(item) => !localIds.has(item._id),
|
||||
);
|
||||
|
||||
return {
|
||||
items,
|
||||
cloudOnlyCategories,
|
||||
syncedCount,
|
||||
missingCount,
|
||||
outdatedCount,
|
||||
};
|
||||
}
|
||||
|
||||
async function callCategoryFunction<T>(
|
||||
name: string,
|
||||
data?: Record<string, unknown>,
|
||||
): Promise<CloudFunctionResult<T>> {
|
||||
const response = (await wx.cloud.callFunction({
|
||||
name,
|
||||
data: data || {},
|
||||
})) as { result?: CloudFunctionResult<T> };
|
||||
|
||||
return response.result || {};
|
||||
}
|
||||
|
||||
Page({
|
||||
data: {
|
||||
loading: false,
|
||||
syncingId: '',
|
||||
compareItems: [] as CompareItem[],
|
||||
cloudOnlyCategories: [] as CloudCategory[],
|
||||
syncedCount: 0,
|
||||
missingCount: 0,
|
||||
outdatedCount: 0,
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
void this.loadCategories();
|
||||
},
|
||||
|
||||
async onPullDownRefresh() {
|
||||
await this.loadCategories();
|
||||
wx.stopPullDownRefresh();
|
||||
},
|
||||
|
||||
async loadCategories() {
|
||||
this.setData({ loading: true });
|
||||
|
||||
try {
|
||||
const result =
|
||||
await callCategoryFunction<CloudCategory[]>('categoriesQuery');
|
||||
|
||||
if (!result.success) {
|
||||
throw new Error(result.message || '获取分类失败');
|
||||
}
|
||||
|
||||
const compareData = buildCompareItems(result.data || []);
|
||||
this.setData({
|
||||
loading: false,
|
||||
compareItems: compareData.items,
|
||||
cloudOnlyCategories: compareData.cloudOnlyCategories,
|
||||
syncedCount: compareData.syncedCount,
|
||||
missingCount: compareData.missingCount,
|
||||
outdatedCount: compareData.outdatedCount,
|
||||
});
|
||||
} catch (error) {
|
||||
this.setData({ loading: false });
|
||||
wx.showToast({
|
||||
title: error instanceof Error ? error.message : '获取分类失败',
|
||||
icon: 'none',
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
async onTapSync(e: WechatMiniprogram.TouchEvent) {
|
||||
const id = e.currentTarget.dataset.id as string;
|
||||
if (!id || this.data.syncingId) return;
|
||||
|
||||
const item = this.data.compareItems.find((entry) => entry.id === id);
|
||||
if (!item || !item.actionText) return;
|
||||
|
||||
const confirmed = await new Promise<boolean>((resolve) => {
|
||||
wx.showModal({
|
||||
title: `${item.actionText}分类`,
|
||||
content: `确认将「${item.name}」同步到云端吗?`,
|
||||
success: (res) => resolve(!!res.confirm),
|
||||
fail: () => resolve(false),
|
||||
});
|
||||
});
|
||||
|
||||
if (!confirmed) return;
|
||||
|
||||
this.setData({ syncingId: id });
|
||||
|
||||
const payload = {
|
||||
id: item.id,
|
||||
name: item.name,
|
||||
icon: item.icon,
|
||||
color:
|
||||
item.cloudCategory?.color ||
|
||||
getDefaultCategoryColor(item.id, item.sortOrder),
|
||||
sortOrder: item.sortOrder,
|
||||
parentId: item.cloudCategory?.parentId ?? null,
|
||||
};
|
||||
|
||||
try {
|
||||
const functionName =
|
||||
item.status === 'missing'
|
||||
? 'categoriesCreate'
|
||||
: 'categoriesUpdate';
|
||||
const result = await callCategoryFunction<CloudCategory>(
|
||||
functionName,
|
||||
payload,
|
||||
);
|
||||
|
||||
if (!result.success) {
|
||||
throw new Error(result.message || `${item.actionText}失败`);
|
||||
}
|
||||
|
||||
wx.showToast({
|
||||
title: `${item.actionText}成功`,
|
||||
icon: 'success',
|
||||
});
|
||||
await this.loadCategories();
|
||||
} catch (error) {
|
||||
wx.showToast({
|
||||
title:
|
||||
error instanceof Error
|
||||
? error.message
|
||||
: `${item.actionText}失败`,
|
||||
icon: 'none',
|
||||
});
|
||||
} finally {
|
||||
this.setData({ syncingId: '' });
|
||||
}
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,110 @@
|
||||
<view class="category-manage-page">
|
||||
<view class="category-manage-page__summary">
|
||||
<view class="category-manage-page__summary-card">
|
||||
<text class="category-manage-page__summary-num"
|
||||
>{{syncedCount}}</text
|
||||
>
|
||||
<text class="category-manage-page__summary-label">已同步</text>
|
||||
</view>
|
||||
<view class="category-manage-page__summary-card">
|
||||
<text class="category-manage-page__summary-num"
|
||||
>{{missingCount}}</text
|
||||
>
|
||||
<text class="category-manage-page__summary-label">待新增</text>
|
||||
</view>
|
||||
<view class="category-manage-page__summary-card">
|
||||
<text class="category-manage-page__summary-num"
|
||||
>{{outdatedCount}}</text
|
||||
>
|
||||
<text class="category-manage-page__summary-label">待更新</text>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view class="category-manage-page__hint">
|
||||
以本地 `CATEGORY_LIST` 为基准,逐项比对云端 `categories` 集合。
|
||||
</view>
|
||||
|
||||
<view wx:if="{{loading}}" class="category-manage-page__loading">
|
||||
正在加载分类数据...
|
||||
</view>
|
||||
|
||||
<view wx:else class="category-manage-page__list">
|
||||
<view
|
||||
wx:for="{{compareItems}}"
|
||||
wx:key="id"
|
||||
class="category-card category-card--{{item.status}}">
|
||||
<view class="category-card__header">
|
||||
<view class="category-card__meta">
|
||||
<text class="category-card__icon">{{item.icon}}</text>
|
||||
<view class="category-card__title-box">
|
||||
<view class="category-card__title-box-inner">
|
||||
<text class="category-card__title"
|
||||
>{{item.name}}</text
|
||||
>
|
||||
<text class="category-card__id">{{item.id}}</text>
|
||||
</view>
|
||||
<text class="category-card__subtitle">
|
||||
排序 {{item.sortOrder}}
|
||||
</text>
|
||||
</view>
|
||||
</view>
|
||||
<view class="category-card__right">
|
||||
<text class="category-card__badge"
|
||||
>{{item.statusText}}</text
|
||||
>
|
||||
<toy-button
|
||||
wx:if="{{item.actionText}}"
|
||||
type="primary"
|
||||
size="small"
|
||||
data-id="{{item.id}}"
|
||||
disabled="{{syncingId === item.id}}"
|
||||
bindtap="onTapSync">
|
||||
{{syncingId === item.id ? '处理中...' :
|
||||
item.actionText}}
|
||||
</toy-button>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<text
|
||||
wx:if="{{item.status === 'outdated'}}"
|
||||
class="category-card__diff">
|
||||
{{item.diffText}}
|
||||
</text>
|
||||
|
||||
<view
|
||||
wx:if="{{item.status === 'outdated' && item.cloudCategory}}"
|
||||
class="category-card__detail">
|
||||
<text class="category-card__detail-label">云端</text>
|
||||
<text class="category-card__detail-text">
|
||||
{{item.cloudCategory.name}} · {{item.cloudCategory.icon}} ·
|
||||
排序 {{item.cloudCategory.sortOrder}}
|
||||
</text>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view
|
||||
wx:if="{{!loading && cloudOnlyCategories.length}}"
|
||||
class="category-manage-page__cloud-only">
|
||||
<text class="category-manage-page__section-title"
|
||||
>仅云端存在的分类</text
|
||||
>
|
||||
<view
|
||||
wx:for="{{cloudOnlyCategories}}"
|
||||
wx:key="_id"
|
||||
class="category-card category-card--cloud-only">
|
||||
<view class="category-card__header">
|
||||
<view class="category-card__meta">
|
||||
<text class="category-card__icon">{{item.icon}}</text>
|
||||
<view class="category-card__title-box">
|
||||
<text class="category-card__title">{{item.name}}</text>
|
||||
<text class="category-card__subtitle">
|
||||
{{item._id}} · 排序 {{item.sortOrder}}
|
||||
</text>
|
||||
</view>
|
||||
</view>
|
||||
<text class="category-card__badge">仅云端</text>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
Reference in New Issue
Block a user