feat(tags): 拆书帮标签支持增删改 — 昵称下方标签区(点改/×删/+添加)+ /chaishu/tags/update 全量更新端点

This commit is contained in:
2026-11-15 06:51:42 +08:00
parent 1176a62a1f
commit 61c7babe31
5 changed files with 157 additions and 11 deletions
+11
View File
@@ -106,6 +106,15 @@ const deleteChaishuTag = (tag) => {
return post('/chaishu/tags/delete', { tag });
};
/**
* 全量更新自己拆书帮身份上的标签(增/删/改统一入口)
* @param {string[]} tags
* @returns {Promise}
*/
const updateChaishuTags = (tags) => {
return post('/chaishu/tags/update', { tags });
};
/**
* 绑定邮箱(邮箱一经绑定不可更改;需先获取并验证 OTP 验证码)
* @param {string} email
@@ -158,6 +167,8 @@ module.exports = {
getUserStats,
getChaishuBranches,
bindChaishu,
deleteChaishuTag,
updateChaishuTags,
bindEmail,
sendEmailOtp,
getChaishuBuddies,
+73 -2
View File
@@ -1,5 +1,5 @@
// pages/edit-profile/index.js
const { getProfile, updateProfile, uploadAvatar, getChaishuBranches, bindChaishu, deleteChaishuTag, bindEmail, sendEmailOtp } = require('../../api/user');
const { getProfile, updateProfile, uploadAvatar, getChaishuBranches, bindChaishu, deleteChaishuTag, updateChaishuTags, bindEmail, sendEmailOtp } = require('../../api/user');
const { chooseImages, getFullImageUrl } = require('../../utils/helpers');
Page({
@@ -195,8 +195,8 @@ Page({
try {
const res = await deleteChaishuTag(tag);
if (res.success) {
this.setData({ 'chaishu.tags': res.data.tags });
wx.showToast({ title: '已删除', icon: 'success' });
this.loadProfile();
} else {
wx.showToast({ title: res.message || '删除失败', icon: 'none' });
}
@@ -205,6 +205,77 @@ Page({
}
},
// 添加标签(昵称下方「+添加」)
onAddChaishuTag() {
const tags = (this.data.chaishu && this.data.chaishu.tags) || [];
if (tags.length >= 10) {
wx.showToast({ title: '标签最多 10 个', icon: 'none' });
return;
}
wx.showModal({
title: '添加标签',
editable: true,
placeholderText: '如:拆书课、AI工具(10字内)',
confirmColor: '#07c160',
success: (m) => {
if (!m.confirm) return;
const val = (m.content || '').trim();
if (!val) {
wx.showToast({ title: '标签不能为空', icon: 'none' });
return;
}
this.doSaveChaishuTags([...tags, val]);
},
});
},
// 编辑已有标签(点标签文字)
onEditChaishuTag(e) {
const oldTag = e.currentTarget.dataset.tag;
if (!oldTag) return;
const tags = (this.data.chaishu && this.data.chaishu.tags) || [];
wx.showModal({
title: '修改标签',
editable: true,
content: oldTag,
confirmColor: '#07c160',
success: (m) => {
if (!m.confirm) return;
const val = (m.content || '').trim();
if (!val) {
wx.showToast({ title: '标签不能为空', icon: 'none' });
return;
}
if (val === oldTag) return;
this.doSaveChaishuTags(tags.map(t => (t === oldTag ? val : t)));
},
});
},
// 全量保存标签(增/改统一入口;后端再做清洗去重与校验)
async doSaveChaishuTags(tags) {
const cleaned = [...new Set(tags.map(t => String(t || '').trim()).filter(Boolean))];
if (cleaned.some(t => t.length > 10)) {
wx.showToast({ title: '单个标签不能超过 10 个字', icon: 'none' });
return;
}
if (cleaned.length > 10) {
wx.showToast({ title: '标签最多 10 个', icon: 'none' });
return;
}
try {
const res = await updateChaishuTags(cleaned);
if (res.success) {
this.setData({ 'chaishu.tags': res.data.tags });
wx.showToast({ title: '已保存', icon: 'success' });
} else {
wx.showToast({ title: res.message || '保存失败', icon: 'none' });
}
} catch (err) {
wx.showToast({ title: err.message || '保存失败', icon: 'none' });
}
},
async handleChooseAvatar() {
try {
const [tempFilePath] = await chooseImages(1);
@@ -21,6 +21,21 @@
/>
</view>
<!-- 标签(拆书帮身份;点文字改、点 × 删、+添加) -->
<view class="form-item form-item-column nickname-tags-block" wx:if="{{chaishu}}">
<text class="form-label">标签</text>
<view class="chaishu-tags-wrap">
<view class="chaishu-tag-chip" wx:for="{{chaishu.tags}}" wx:key="*this">
<text class="chaishu-tag-text" data-tag="{{item}}" bindtap="onEditChaishuTag">{{item}}</text>
<text class="chaishu-tag-del" data-tag="{{item}}" catchtap="onDeleteChaishuTag">×</text>
</view>
<view class="chaishu-tag-chip chaishu-tag-add" bindtap="onAddChaishuTag">
<text class="chaishu-tag-add-text">+添加</text>
</view>
</view>
<text class="nickname-tags-tip">点击标签可修改,× 删除;最多 10 个</text>
</view>
<view class="form-item form-item-column email-section-anchor">
<view class="email-label-row">
<text class="form-label">邮箱</text>
@@ -140,15 +155,6 @@
<text class="chaishu-key">师父</text>
<text class="chaishu-val">{{chaishu.shifuName}}</text>
</view>
<view class="chaishu-row" wx:if="{{chaishu.tags.length}}">
<text class="chaishu-key">标签</text>
<view class="chaishu-tags-wrap">
<view class="chaishu-tag-chip" wx:for="{{chaishu.tags}}" wx:key="*this">
<text class="chaishu-tag-text">{{item}}</text>
<text class="chaishu-tag-del" data-tag="{{item}}" catchtap="onDeleteChaishuTag">×</text>
</view>
</view>
</view>
</view>
<!-- 未绑定:姓名 + 分舵 picker + 绑定按钮 -->
@@ -167,6 +167,29 @@
padding: 0 4rpx;
}
/* 昵称下方标签编辑区 */
.nickname-tags-block {
align-items: flex-start;
}
.nickname-tags-block .chaishu-tags-wrap {
margin-top: 8rpx;
}
.chaishu-tag-add {
background-color: #ffffff;
border: 2rpx dashed #4a90e2;
padding: 6rpx 20rpx;
}
.chaishu-tag-add-text {
font-size: 24rpx;
color: #4a90e2;
}
.nickname-tags-tip {
display: block;
margin-top: 12rpx;
font-size: 22rpx;
color: #999999;
}
.chaishu-bind-btn {
margin-top: 20rpx;
width: 100%;
+35
View File
@@ -2499,6 +2499,41 @@ app.post('/user/email/bind', (req, res) => {
}
});
// 全量更新自己身份上的标签(仅绑定正主可改;增/删/改统一走此端点)
app.post('/chaishu/tags/update', requireEmail, (req, res) => {
try {
const userId = req.headers['x-user-id'];
if (!userId) {
return res.json({ success: false, message: '未登录' });
}
const user = db.prepare('SELECT chaishu_uid FROM users WHERE id = ?').get(userId);
if (!user || !user.chaishu_uid) {
return res.json({ success: false, message: '尚未绑定拆书帮身份' });
}
const { tags } = req.body || {};
if (!Array.isArray(tags)) {
return res.json({ success: false, message: '参数错误:tags 须为数组' });
}
// 清洗:去首尾空格、丢空串、去重(保留先出现的)
const cleaned = [...new Set(tags.map(t => String(t || '').trim()).filter(Boolean))];
if (cleaned.some(t => t.length > 10)) {
return res.json({ success: false, message: '单个标签不能超过 10 个字' });
}
if (cleaned.length > 10) {
return res.json({ success: false, message: '标签最多 10 个' });
}
const m = db.prepare('SELECT uid FROM chaishu_members WHERE uid = ?').get(user.chaishu_uid);
if (!m) {
return res.json({ success: false, message: '会员不存在' });
}
db.prepare('UPDATE chaishu_members SET tags = ? WHERE uid = ?').run(JSON.stringify(cleaned), m.uid);
res.json({ success: true, data: { tags: cleaned } });
} catch (error) {
console.error('更新拆书帮标签失败:', error);
res.json({ success: false, message: '更新拆书帮标签失败' });
}
});
// 删除自己身份上的一个标签(仅绑定正主可删)
app.post('/chaishu/tags/delete', requireEmail, (req, res) => {
try {