Files

122 lines
2.1 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 });
};
module.exports = {
wechatLogin,
login,
logout,
deleteAccount,
getProfile,
updateProfile,
uploadAvatar,
getProfileCompletion,
getUserStats,
getChaishuBranches,
bindChaishu,
};