Files
doodle-mini/cloudfunctions/userSettings/index.js
T

52 lines
1.4 KiB
JavaScript

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: '账户已注销' };
}