Files
danyang-match/miniapp/services/authService.js
T

52 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 认证服务(规范 §131 services 层 / API_INVENTORY 认证域)
*
* 仅封装 auth/wechat/login、auth/phone/bind、auth/refresh、auth/logout 四个端点。
* Token 持久化、Session 失效回调由 utils/api.js 统一处理;本层不读写本地存储。
* 刷新 Token 在 401 自动重试由 AuthStore 协调(单飞锁),见 stores/authStore。
*/
const api = require('../utils/api');
/**
* 微信登录(API_INVENTORY 行 8)
* @param {{ code: string }} payload wx.login 拿到的 code
* @returns {Promise<{ token: string, refreshToken: string, expiresIn: number, needPhone: boolean, needProfile: boolean }>}
*/
function wechatLogin(payload) {
return api.post('/auth/wechat/login', payload, undefined, payload.code);
}
/**
* 绑定手机号(API_INVENTORY 行 9)
* @param {{ phoneCode: string }} payload wx.getPhoneNumber 拿到的 encryptedData/iv 或 phoneCode
* @returns {Promise<{ bound: true }>}
*/
function bindPhone(payload) {
return api.post('/auth/phone/bind', payload, undefined, payload.phoneCode || payload.encryptedData);
}
/**
* 刷新 Token(API_INVENTORY 行 10)
* @returns {Promise<{ token: string, refreshToken: string, expiresIn: number }>}
*/
function refresh() {
const refreshToken = api.getRefreshToken();
return api.post('/auth/refresh', { refreshToken });
}
/**
* 登出(API_INVENTORY 行 11)
* 幂等:Idempotency-Key = "logout:" + 当前毫秒时间,便于后端去重。
* @returns {Promise<void>}
*/
function logout() {
return api.post('/auth/logout', {}, undefined, 'logout:' + Date.now());
}
module.exports = {
wechatLogin,
bindPhone,
refresh,
logout,
};