177 lines
3.5 KiB
JavaScript
177 lines
3.5 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[]} tags
|
|
* @returns {Promise}
|
|
*/
|
|
const updateChaishuTags = (tags) => {
|
|
return post('/chaishu/tags/update', { tags });
|
|
};
|
|
|
|
/**
|
|
* 绑定邮箱(邮箱一经绑定不可更改;需先获取并验证 OTP 验证码)
|
|
* @param {string} email
|
|
* @param {string} code 邮箱验证码
|
|
* @returns {Promise}
|
|
*/
|
|
const bindEmail = (email, code) => {
|
|
return post('/user/email/bind', { email, code });
|
|
};
|
|
|
|
/**
|
|
* 发送邮箱绑定 OTP 验证码
|
|
* @param {string} email
|
|
* @returns {Promise}
|
|
*/
|
|
const sendEmailOtp = (email) => {
|
|
return post('/user/email/otp/send', { email });
|
|
};
|
|
|
|
/**
|
|
* 获取共拆搭子推荐(游客=最新活动发布人;登录=按角色矩阵推荐,最多3人)
|
|
* @returns {Promise}
|
|
*/
|
|
const getChaishuBuddies = () => {
|
|
return get('/chaishu/buddies?limit=3');
|
|
};
|
|
|
|
/**
|
|
* 搜索拆书帮会员(共拆搭子更多页)
|
|
* @param {object} params - { keyword, branch, tag, page, pageSize }
|
|
* @returns {Promise}
|
|
*/
|
|
const getChaishuMembers = (params = {}) => {
|
|
const qs = Object.keys(params)
|
|
.filter(k => params[k])
|
|
.map(k => `${k}=${encodeURIComponent(params[k])}`)
|
|
.join('&');
|
|
return get(`/chaishu/members${qs ? '?' + qs : ''}`);
|
|
};
|
|
|
|
module.exports = {
|
|
wechatLogin,
|
|
login,
|
|
logout,
|
|
deleteAccount,
|
|
getProfile,
|
|
updateProfile,
|
|
uploadAvatar,
|
|
getProfileCompletion,
|
|
getUserStats,
|
|
getChaishuBranches,
|
|
bindChaishu,
|
|
deleteChaishuTag,
|
|
updateChaishuTags,
|
|
bindEmail,
|
|
sendEmailOtp,
|
|
getChaishuBuddies,
|
|
getChaishuMembers,
|
|
};
|