feat: 我的、设置、打印指南页面开发完成
This commit is contained in:
@@ -287,6 +287,10 @@ exports.main = async (event) => {
|
||||
if (page === 'home') {
|
||||
const { homeData } = await buildHomeData(db, homeEvent);
|
||||
config.home = homeData;
|
||||
// [v3.0] 清空 category/age 字段,分类页已改为实时接口查询
|
||||
// 如需恢复静态配置,注释掉以下两行即可
|
||||
config.category = null;
|
||||
config.age = null;
|
||||
config.version = (config.version || 0) + 1;
|
||||
config.updatedAt = new Date().toISOString();
|
||||
|
||||
@@ -307,7 +311,10 @@ exports.main = async (event) => {
|
||||
if (page === 'all') {
|
||||
const { categoryData, stats } = await buildCategoryData(db);
|
||||
const { homeData } = await buildHomeData(db, homeEvent);
|
||||
config.category = categoryData;
|
||||
// [v3.0] 清空 category/age 字段,分类页已改为实时接口查询
|
||||
// 如需恢复静态配置,将下面 null 改回 categoryData 即可
|
||||
config.category = null;
|
||||
config.age = null;
|
||||
config.home = homeData;
|
||||
config.version = (config.version || 0) + 1;
|
||||
config.updatedAt = new Date().toISOString();
|
||||
|
||||
@@ -27,6 +27,8 @@ exports.main = async (event) => {
|
||||
return await handleAdd(userId, event);
|
||||
case 'list':
|
||||
return await handleList(userId, event);
|
||||
case 'count':
|
||||
return await handleCount(userId);
|
||||
default:
|
||||
return { code: -1, message: '未知操作' };
|
||||
}
|
||||
@@ -96,3 +98,9 @@ async function handleList(userId, event) {
|
||||
|
||||
return { code: 0, data: result };
|
||||
}
|
||||
|
||||
async function handleCount(userId) {
|
||||
const collection = db.collection('download_logs');
|
||||
const res = await collection.where({ userId }).count();
|
||||
return { code: 0, data: { count: res.total || 0 } };
|
||||
}
|
||||
|
||||
@@ -29,6 +29,8 @@ exports.main = async (event) => {
|
||||
return await handleRemove(userId, event);
|
||||
case 'list':
|
||||
return await handleList(userId, event);
|
||||
case 'count':
|
||||
return await handleCount(userId);
|
||||
case 'check':
|
||||
return await handleCheck(userId, event);
|
||||
case 'batchCheck':
|
||||
@@ -153,6 +155,12 @@ async function handleList(userId, event) {
|
||||
return { code: 0, data: result };
|
||||
}
|
||||
|
||||
async function handleCount(userId) {
|
||||
const collection = db.collection('favorites');
|
||||
const res = await collection.where({ userId }).count();
|
||||
return { code: 0, data: { count: res.total || 0 } };
|
||||
}
|
||||
|
||||
async function handleCheck(userId, event) {
|
||||
const { worksheetId } = event;
|
||||
if (!worksheetId) return { code: -1, message: 'worksheetId 不能为空' };
|
||||
|
||||
@@ -18,6 +18,20 @@ exports.main = async (event, context) => {
|
||||
}
|
||||
};
|
||||
|
||||
function serializeUser(user) {
|
||||
return {
|
||||
...user,
|
||||
createdAt:
|
||||
user.createdAt instanceof Date
|
||||
? user.createdAt
|
||||
: user.createdAt?.toDate?.() || user.createdAt || null,
|
||||
lastActiveAt:
|
||||
user.lastActiveAt instanceof Date
|
||||
? user.lastActiveAt
|
||||
: user.lastActiveAt?.toDate?.() || user.lastActiveAt || null,
|
||||
};
|
||||
}
|
||||
|
||||
async function handleSilentLogin(openid, unionid) {
|
||||
const usersCollection = db.collection('users');
|
||||
|
||||
@@ -32,10 +46,13 @@ async function handleSilentLogin(openid, unionid) {
|
||||
updateData.unionid = unionid;
|
||||
}
|
||||
await usersCollection.doc(user._id).update({ data: updateData });
|
||||
const latest = (
|
||||
await usersCollection.doc(user._id).get()
|
||||
).data;
|
||||
|
||||
return {
|
||||
code: 0,
|
||||
data: { ...user, ...updateData, lastActiveAt: new Date() },
|
||||
data: serializeUser(latest),
|
||||
};
|
||||
} else {
|
||||
const newUser = {
|
||||
@@ -48,15 +65,11 @@ async function handleSilentLogin(openid, unionid) {
|
||||
lastActiveAt: now,
|
||||
};
|
||||
const { _id } = await usersCollection.add({ data: newUser });
|
||||
const latest = (await usersCollection.doc(_id).get()).data;
|
||||
|
||||
return {
|
||||
code: 0,
|
||||
data: {
|
||||
_id,
|
||||
...newUser,
|
||||
createdAt: new Date(),
|
||||
lastActiveAt: new Date(),
|
||||
},
|
||||
data: serializeUser(latest),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -74,11 +87,13 @@ async function handleUpdateProfile(event, openid) {
|
||||
const updateData = {};
|
||||
if (nickName !== undefined) updateData.nickName = nickName;
|
||||
if (avatarUrl !== undefined) updateData.avatarUrl = avatarUrl;
|
||||
updateData.lastActiveAt = db.serverDate();
|
||||
|
||||
await usersCollection.doc(data[0]._id).update({ data: updateData });
|
||||
const latest = (await usersCollection.doc(data[0]._id).get()).data;
|
||||
|
||||
return {
|
||||
code: 0,
|
||||
data: { ...data[0], ...updateData },
|
||||
data: serializeUser(latest),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
const cloud = require('wx-server-sdk');
|
||||
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV });
|
||||
|
||||
const db = cloud.database();
|
||||
|
||||
exports.main = async (event) => {
|
||||
const wxContext = cloud.getWXContext();
|
||||
const openid = wxContext.OPENID;
|
||||
const { action } = event;
|
||||
|
||||
switch (action) {
|
||||
case 'clearProfile':
|
||||
return await handleClearProfile(openid);
|
||||
case 'deleteAccount':
|
||||
return await handleDeleteAccount(openid);
|
||||
default:
|
||||
return { code: -1, message: '未知操作' };
|
||||
}
|
||||
};
|
||||
|
||||
async function handleClearProfile(openid) {
|
||||
const usersCollection = db.collection('users');
|
||||
const { data } = await usersCollection.where({ openid }).limit(1).get();
|
||||
|
||||
if (data.length === 0) {
|
||||
return { code: -1, message: '用户不存在' };
|
||||
}
|
||||
|
||||
await usersCollection.doc(data[0]._id).update({
|
||||
data: {
|
||||
nickName: null,
|
||||
avatarUrl: null,
|
||||
lastActiveAt: db.serverDate(),
|
||||
},
|
||||
});
|
||||
|
||||
return { code: 0, message: '已清除头像和昵称' };
|
||||
}
|
||||
|
||||
async function handleDeleteAccount(openid) {
|
||||
const usersCollection = db.collection('users');
|
||||
const { data } = await usersCollection.where({ openid }).limit(1).get();
|
||||
|
||||
if (data.length === 0) {
|
||||
return { code: -1, message: '用户不存在' };
|
||||
}
|
||||
|
||||
await usersCollection.doc(data[0]._id).remove();
|
||||
|
||||
return { code: 0, message: '账户已注销' };
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"name": "user-settings",
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"wx-server-sdk": "^3.0.4"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user