Files
danyang-match/miniapp/services/authService.js
T
WorkBuddy 377429995c
CI/CD Pipeline / Test Shell Guard (Phase 23 Gate) (push) Waiting to run
CI/CD Pipeline / Backend Build & Test (push) Waiting to run
CI/CD Pipeline / Admin Web Build (push) Waiting to run
CI/CD Pipeline / Mini Program Compile Check (push) Waiting to run
CI/CD Pipeline / Migration Validation (push) Blocked by required conditions
CI/CD Pipeline / UI Contracts (source-structure gates) (push) Waiting to run
CI/CD Pipeline / E2E (live stack) (push) Blocked by required conditions
release: freeze V1.2 demo acceptance 2026-10-25
2026-10-25 14:29:29 +08:00

73 lines
2.5 KiB
JavaScript
Raw Permalink 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<{ accessToken: string, refreshToken: string, expiresIn: number,
* userId: string, isNewUser: boolean }>}
* 注意是 accessToken(不是 token)、isNewUser(不是 needPhone/needProfile)——
* 这两处字段名此前都写错,导致 token 落空与「误判已绑手机号」,见 authStore。
*/
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);
}
function sendPhoneCode(phone) {
return api.post('/auth/phone/send-code', { phone });
}
function verifyPhone(phone, code) {
return api.post('/auth/phone/verify', { phone, code });
}
function register(payload) {
return api.post('/auth/register', payload, undefined, payload.verificationTransaction);
}
/**
* 刷新 Token(API_INVENTORY 行 10)
* 后端 /auth/refresh 只强制要求 refreshToken(userId 由令牌 subject 自证身份);
* 这里仍传 userId,让后端校验它与令牌主体一致 —— 串号时能被提前拦下。
* @param {string} userId 当前登录用户 ID(来自 AuthStore.state.userId)
* @returns {Promise<{ accessToken: string, refreshToken: string, expiresIn: number }>}
*/
function refresh(userId) {
const refreshToken = api.getRefreshToken();
return api.post('/auth/refresh', { userId, refreshToken });
}
/**
* 登出(API_INVENTORY 行 11)
* 幂等:键由 api 层托管,网络异常重试时复用同一把,避免后端重复记账。
* @returns {Promise<void>}
*/
function logout() {
return api.post('/auth/logout', {}, undefined, api.idempotencyKey('logout'));
}
module.exports = {
wechatLogin,
bindPhone,
sendPhoneCode,
verifyPhone,
register,
refresh,
logout,
};