204 lines
5.7 KiB
JavaScript
204 lines
5.7 KiB
JavaScript
/**
|
||
* 认证模块 - 登录、注册、绑定相关接口
|
||
*/
|
||
import RequestManager from '../request';
|
||
|
||
/**
|
||
* 保存登录Token到本地存储
|
||
* 支持新版双Token格式和旧版单Token格式
|
||
* @param {Object|String} data 登录响应数据
|
||
* @param {String} data.accessToken 访问令牌(新版)
|
||
* @param {String} data.refreshToken 刷新令牌(新版)
|
||
* @param {Number} data.expiresIn 过期时间(秒)(新版)
|
||
*/
|
||
export const saveLoginTokens = (data) => {
|
||
if (typeof data === 'string') {
|
||
// 旧版格式:直接返回token字符串
|
||
uni.setStorageSync('token', data);
|
||
} else if (data && typeof data === 'object') {
|
||
// 新版格式:返回包含accessToken、refreshToken、expiresIn的对象
|
||
if (data.accessToken) {
|
||
uni.setStorageSync('token', data.accessToken);
|
||
uni.setStorageSync('accessToken', data.accessToken);
|
||
}
|
||
if (data.refreshToken) {
|
||
uni.setStorageSync('refreshToken', data.refreshToken);
|
||
}
|
||
if (data.expiresIn) {
|
||
// 计算过期时间戳(当前时间 + expiresIn秒)
|
||
const tokenExpireTime = Date.now() + (data.expiresIn * 1000);
|
||
uni.setStorageSync('tokenExpireTime', tokenExpireTime);
|
||
}
|
||
// 兼容旧版:如果响应中有token字段(向后兼容)
|
||
if (data.token && !data.accessToken) {
|
||
uni.setStorageSync('token', data.token);
|
||
}
|
||
}
|
||
};
|
||
|
||
/**
|
||
* 清除所有登录Token
|
||
*/
|
||
export const clearLoginTokens = () => {
|
||
uni.removeStorageSync('token');
|
||
uni.removeStorageSync('accessToken');
|
||
uni.removeStorageSync('refreshToken');
|
||
uni.removeStorageSync('tokenExpireTime');
|
||
uni.removeStorageSync('userinfo');
|
||
};
|
||
|
||
/**
|
||
* 获取当前存储的Token信息
|
||
* @returns {Object} Token信息对象
|
||
*/
|
||
export const getStoredTokens = () => {
|
||
return {
|
||
token: uni.getStorageSync('token'),
|
||
accessToken: uni.getStorageSync('accessToken'),
|
||
refreshToken: uni.getStorageSync('refreshToken'),
|
||
tokenExpireTime: uni.getStorageSync('tokenExpireTime')
|
||
};
|
||
};
|
||
|
||
/**
|
||
* 检查Access Token是否即将过期(提前5分钟)
|
||
* @returns {Boolean} 是否即将过期
|
||
*/
|
||
export const isTokenExpiringSoon = () => {
|
||
const tokenExpireTime = uni.getStorageSync('tokenExpireTime');
|
||
if (!tokenExpireTime) {
|
||
return false;
|
||
}
|
||
// 提前5分钟(300秒)认为即将过期
|
||
const bufferTime = 5 * 60 * 1000;
|
||
return Date.now() >= (tokenExpireTime - bufferTime);
|
||
};
|
||
|
||
/**
|
||
* 检查Access Token是否已过期
|
||
* @returns {Boolean} 是否已过期
|
||
*/
|
||
export const isTokenExpired = () => {
|
||
const tokenExpireTime = uni.getStorageSync('tokenExpireTime');
|
||
if (!tokenExpireTime) {
|
||
return false;
|
||
}
|
||
return Date.now() >= tokenExpireTime;
|
||
};
|
||
|
||
/**
|
||
* 微信登录
|
||
* @param {Object} params 登录参数
|
||
* @param {String} params.code 微信登录code
|
||
* @param {String} params.nickname 昵称
|
||
* @param {String} params.headimg 头像
|
||
* @param {String} params.pid 推荐人ID
|
||
* @returns {Promise} 登录结果
|
||
*/
|
||
export const wxLogin = async (params = {}) => {
|
||
return await RequestManager.post('/login', params);
|
||
};
|
||
|
||
/**
|
||
* 手机号验证码登录
|
||
* @param {String} mobile 手机号
|
||
* @param {String} code 验证码
|
||
* @param {String} pid 推荐人ID
|
||
* @returns {Promise} 登录结果
|
||
*/
|
||
export const mobileLogin = async (mobile, code, pid = '') => {
|
||
return await RequestManager.post('/mobileLogin', {
|
||
mobile,
|
||
code,
|
||
pid
|
||
});
|
||
};
|
||
|
||
/**
|
||
* 发送短信验证码
|
||
* @param {String} phone 手机号
|
||
* @returns {Promise} 发送结果
|
||
*/
|
||
export const sendSms = async (phone) => {
|
||
return await RequestManager.post('/v2/account/sendSms', { phone });
|
||
};
|
||
|
||
/**
|
||
* 微信绑定手机号(通过微信授权)
|
||
* @param {String} code 微信授权code
|
||
* @returns {Promise} 绑定结果
|
||
*/
|
||
export const bindMobileByWx = async (code) => {
|
||
return await RequestManager.post('/login_bind_mobile', { code });
|
||
};
|
||
|
||
/**
|
||
* 验证码绑定手机号(H5端)
|
||
* @param {String} mobile 手机号
|
||
* @param {String} code 验证码
|
||
* @returns {Promise} 绑定结果
|
||
*/
|
||
export const bindMobileByCode = async (mobile, code) => {
|
||
return await RequestManager.post('/login_bind_mobile_h5', {
|
||
mobile,
|
||
code
|
||
});
|
||
};
|
||
|
||
/**
|
||
* 刷新Token
|
||
* @param {String} refreshToken 刷新令牌
|
||
* @returns {Promise} 刷新结果,包含新的accessToken、refreshToken、expiresIn
|
||
*/
|
||
export const refreshToken = async (refreshToken) => {
|
||
return await RequestManager.post('/refresh', {
|
||
refreshToken
|
||
});
|
||
};
|
||
|
||
/**
|
||
* 退出登录(撤销Refresh Token)
|
||
* @returns {Promise} 退出结果
|
||
*/
|
||
export const logout = async () => {
|
||
const storedRefreshToken = uni.getStorageSync('refreshToken');
|
||
if (storedRefreshToken) {
|
||
try {
|
||
await RequestManager.post('/logout', {
|
||
refreshToken: storedRefreshToken
|
||
});
|
||
} catch (error) {
|
||
console.log('撤销Token失败,继续清除本地存储:', error);
|
||
}
|
||
}
|
||
// 无论后端调用是否成功,都清除本地存储
|
||
clearLoginTokens();
|
||
};
|
||
|
||
/**
|
||
* 记录登录
|
||
* @returns {Promise} 记录结果
|
||
*/
|
||
export const recordLogin = async () => {
|
||
return await RequestManager.post('/login_record', {});
|
||
};
|
||
|
||
/**
|
||
* 账号注销
|
||
* @returns {Promise} 注销结果
|
||
*/
|
||
export const logOff = async () => {
|
||
return await RequestManager.post('/user_log_off', {});
|
||
};
|
||
|
||
/**
|
||
* 绑定邀请码
|
||
* @param {String} inviteCode 邀请码
|
||
* @returns {Promise} 绑定结果
|
||
*/
|
||
export const bindInviteCode = async (inviteCode) => {
|
||
return await RequestManager.post('/bind_invite_code', {
|
||
invite_code: inviteCode
|
||
});
|
||
};
|