Files
aiinvite2/wechat-miniprogram/api/user.js
T
kenyang 33a303b6f1 fix(security): 消除服务端 19 处 '|| user-1' 身份回退,堵住游客态数据泄露
- 读路由 7 处回退改 null:游客 joined/created/templates 自然空集、stats 零计数
- 写路由 8 处加 authenticateUser 401 守卫(profile/account/avatar/share/join/checkin/collab)
- admin 守卫路由 3 处改用 req.authUser.id,消除 header 伪造面
- multer 文件名回退改 guest;isJoined 改显式布尔(undefined 会被 JSON 序列化丢弃)
- 并入前期已上线未提交改动:拆书 level/level1 迁移与 memberLevelLabel、登录/资料编辑页、后台管理页
2026-11-15 02:14:18 +08:00

132 lines
2.4 KiB
JavaScript

// api/user.js - 用户相关接口
const { get, post, put, del, uploadFile } = require('../utils/request');
const { getWechatCode } = require('../utils/helpers');
/**
* 微信登录
* @returns {Promise}
*/
const wechatLogin = async () => {
const code = await getWechatCode();
return post('/auth/login', { code });
};
/**
* 登录
* @param {string} code - 微信 code
* @returns {Promise}
*/
const login = (code) => {
return post('/auth/login', { code });
};
/**
* 退出登录
* @returns {Promise}
*/
const logout = () => {
return post('/auth/logout');
};
/**
* 注销账号
* @returns {Promise}
*/
const deleteAccount = () => {
return del('/user/account');
};
/**
* 获取用户信息
* @returns {Promise}
*/
const getProfile = () => {
return get('/user/profile');
};
/**
* 更新用户信息
* @param {object} data - 用户信息
* @returns {Promise}
*/
const updateProfile = (data) => {
return put('/user/profile', data);
};
/**
* 上传头像
* @param {string} filePath - 头像文件路径
* @returns {Promise}
*/
const uploadAvatar = (filePath) => {
return uploadFile('/user/avatar', filePath, 'file');
};
/**
* 获取资料完善度
* @returns {Promise}
*/
const getProfileCompletion = () => {
return get('/user/profile/completion');
};
/**
* 获取用户统计数据
* @returns {Promise}
*/
const getUserStats = () => {
return get('/user/stats');
};
/**
* 获取拆书帮分舵列表
* @returns {Promise}
*/
const getChaishuBranches = () => {
return get('/chaishu/branches');
};
/**
* 绑定拆书帮身份
* @param {string} name - 拆书帮姓名
* @param {string} branch - 分舵名称
* @returns {Promise}
*/
const bindChaishu = (name, branch, uid) => {
return post('/chaishu/bind', uid ? { uid } : { name, branch });
};
/**
* 删除自己拆书帮身份上的一个标签
* @param {string} tag
* @returns {Promise}
*/
const deleteChaishuTag = (tag) => {
return post('/chaishu/tags/delete', { tag });
};
/**
* 绑定/修改邮箱(邮箱为拆书帮会员身份认定凭证)
* @param {string} email
* @returns {Promise}
*/
const bindEmail = (email) => {
return post('/user/email/bind', { email });
};
module.exports = {
wechatLogin,
login,
logout,
deleteAccount,
getProfile,
updateProfile,
uploadAvatar,
getProfileCompletion,
getUserStats,
getChaishuBranches,
bindChaishu,
bindEmail,
};