Commit 370e000e by liimin

feat(web,api-core,platform-*): 老 RPC 网关 sessionId 会话标识参与加签

积分核销等老 RPC 网关(glsx/scrm)要求把会话标识作为 sessionId 放进请求体并参与 MD5 加签。
登录接口已返回 loginToken(形如 scrm-base-<uuid>-rop),本次打通其从登录到请求的链路:

- platform-core/auth: LoginResult 补 loginToken 字段
- platform-core/session + platform-sdk/compat/access/session: LoginSession 补 loginToken
- shell/LoginForm: 登录成功后把 res.loginToken 写入本地会话
- api-core/dispatcher: RpcPrepareDeps 支持 session{name,value},并入 effectiveParams(进 body 且进签名)
- api-core/strategies: rpcGatewayStrategy.prepare 透传 session
- points/adapter/rpc: session.sessionId 取自 getLoginSession().loginToken,paramName=sessionId
- points/adapter/gateways/glsx: 网关级固定参数 source='uj-web-points'
- points/api/core/rpc-client: 构造期绑定实例方法,避免方法引用传给 react-query 时 this 丢失
- points/.env.example: 补充积分子应用环境变量示例
1 parent 5cab1b96
...@@ -52,6 +52,7 @@ export function LoginForm() { ...@@ -52,6 +52,7 @@ export function LoginForm() {
dataScope: res.dataScope, dataScope: res.dataScope,
roleCodes: res.roleCodes ?? [], roleCodes: res.roleCodes ?? [],
permCodes: res.permCodes ?? [], permCodes: res.permCodes ?? [],
loginToken: res.loginToken,
}); });
// 菜单树与用户信息都按角色/账号下发:登录即视为账号切换点,旧缓存必须作废 // 菜单树与用户信息都按角色/账号下发:登录即视为账号切换点,旧缓存必须作废
menuApi.clearCache(); menuApi.clearCache();
......
# 积分域老网关联调配置模板
# 用法:复制为 apps/web/subapps/points/.env 后填入真实值(.env 已被 gitignore,不会入库)。
# 凭据含义见 DELIVERY.md §4 与 src/adapter/credentials.ts。
#
# 取值来源:老系统 temp/scrm-micro-points/src/config/env.*.js
# - origin = 老系统 request.baseURL(开发内网 / 生产测试域二选一)
# - appKey = 老系统 sign.appKey
# - secret = 老系统 secret(glsx MD5 加签私钥)
# - version = 老系统 sign.v(默认 1.0.0)
# - format = 老系统 sign.format(默认 json)
# 必填:scrm-shopmall 网关地址
VITE_POINTS_GATEWAY_ORIGIN=http://192.168.2.42:8064/
# VITE_POINTS_GATEWAY_ORIGIN=http://scrm.test.glsx.com.cn/
# 必填:加签凭据
VITE_POINTS_GATEWAY_APP_KEY=your-app-key
VITE_POINTS_GATEWAY_SECRET=your-secret
# 选填
VITE_POINTS_GATEWAY_VERSION=1.0.0
VITE_POINTS_GATEWAY_FORMAT=json
# 选填:dj-smartcarlife 网关独立 origin(默认与 scrm-shopmall 同域;独立域名请改)
VITE_POINTS_DJ_GATEWAY_ORIGIN=
/*
* @Author: liimin 17653212@qq.com
* @Date: 2026-11-06 15:45:47
* @LastEditors: liimin 17653212@qq.com
* @LastEditTime: 2026-11-09 18:56:31
* @FilePath: \uj-admin\apps\web\subapps\points\src\adapter\gateways\glsx.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
import { createMd5Signer } from '@uj/api-core'; import { createMd5Signer } from '@uj/api-core';
import type { GatewayId, GatewayStrategy, NormalizedEnvelope, Signer } from '../types'; import type { GatewayId, GatewayStrategy, NormalizedEnvelope, Signer } from '../types';
...@@ -72,6 +80,8 @@ export function createGlsxGateway(def: GlsxGatewayDef): GatewayStrategy { ...@@ -72,6 +80,8 @@ export function createGlsxGateway(def: GlsxGatewayDef): GatewayStrategy {
...def, ...def,
signer: glxsrMd5Signer, signer: glxsrMd5Signer,
normalize: normalizeGlsxEnvelope, normalize: normalizeGlsxEnvelope,
// 老网关每次请求固定携带、且参与加签的常量参数(老系统 source='scrm-home')。
fixedParams: { source: 'uj-web-points' },
}; };
} }
......
import { createRpcGatewayClient } from '@uj/api-core/Request'; /*
* @Author: liimin 17653212@qq.com
* @Date: 2026-11-06 17:31:14
* @LastEditors: liimin 17653212@qq.com
* @LastEditTime: 2026-11-09 18:47:44
* @FilePath: \uj-admin\apps\web\subapps\points\src\adapter\rpc.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
import { createRpcGatewayClient } from '@uj/api-core';
import { requestViaHost } from '@uj/platform-sdk'; import { requestViaHost } from '@uj/platform-sdk';
import { getLoginSession } from '@/access';
// 显式 import(而非依赖 barrel 的 re-export 副作用):保证任何调用路径下网关策略都已登记, // 显式 import(而非依赖 barrel 的 re-export 副作用):保证任何调用路径下网关策略都已登记,
// 否则 resolveGateway 会抛「网关尚未接入」。 // 否则 resolveGateway 会抛「网关尚未接入」。
import './gateways'; import './gateways';
...@@ -31,6 +40,13 @@ export const request = createRpcGatewayClient({ ...@@ -31,6 +40,13 @@ export const request = createRpcGatewayClient({
contentType: false, contentType: false,
gatewayCredential: (id) => getGatewayCredential(id as GatewayId), gatewayCredential: (id) => getGatewayCredential(id as GatewayId),
gatewayToken: getGatewayToken, gatewayToken: getGatewayToken,
// sessionId 取自登录接口的 data.loginToken(老网关会话标识);facade 的 resolveSession 会在
// 签名前把它并入请求体并参与 MD5 加签(与老系统请求体一致)。source 作为网关级固定参数已登记在
// 网关策略(glsx.ts 的 GatewayStrategy.fixedParams)里,这里无需重复配置。
session: {
sessionId: () => getLoginSession()?.loginToken ?? '',
paramName: 'sessionId',
},
errorHandler: (err) => { errorHandler: (err) => {
throw err.cause ?? err; throw err.cause ?? err;
}, },
......
...@@ -10,6 +10,21 @@ import type { GatewayId } from '@/adapter'; ...@@ -10,6 +10,21 @@ import type { GatewayId } from '@/adapter';
* @/adapter/rpc.ts 的 Request → 主壳 props.request,边界由 check-boundaries.mjs 复查。 * @/adapter/rpc.ts 的 Request → 主壳 props.request,边界由 check-boundaries.mjs 复查。
*/ */
export class RpcApiClient { export class RpcApiClient {
constructor() {
// 业务模块常把 `pointsApi.pageOrders` 之类的方法引用直接传给 react-query 的
// fetcher / useExport,若不在构造期绑定 this,调用时 this 指向 undefined,
// 进而 `this.rpc is not a function`。这里把实例方法统一绑定到自身,
// 不论以「方法引用」还是「实例调用」形式使用都安全。
const proto = Object.getPrototypeOf(this);
for (const name of Object.getOwnPropertyNames(proto)) {
if (name === 'constructor') continue;
const value = (this as unknown as Record<string, unknown>)[name];
if (typeof value === 'function') {
(this as unknown as Record<string, unknown>)[name] = value.bind(this);
}
}
}
protected rpc<T>( protected rpc<T>(
gateway: GatewayId, gateway: GatewayId,
method: string, method: string,
......
...@@ -30,6 +30,8 @@ export interface RpcPrepareDeps { ...@@ -30,6 +30,8 @@ export interface RpcPrepareDeps {
resolveToken?: () => string; resolveToken?: () => string;
/** 默认超时(毫秒);老系统 RPC 网关一般 180000。 */ /** 默认超时(毫秒);老系统 RPC 网关一般 180000。 */
timeout?: number; timeout?: number;
/** 会话标识(如老网关 sessionId):在签名前并入请求体并参与加签;由 facade 的 resolveSession 解析后传入。 */
session?: { name: string; value: string };
} }
export interface RpcDispatcherDeps extends Omit<RpcPrepareDeps, 'resolveToken'> { export interface RpcDispatcherDeps extends Omit<RpcPrepareDeps, 'resolveToken'> {
...@@ -51,10 +53,14 @@ export async function prepareRpcCall(call: RpcCall, deps: RpcPrepareDeps): Promi ...@@ -51,10 +53,14 @@ export async function prepareRpcCall(call: RpcCall, deps: RpcPrepareDeps): Promi
const credential = deps.resolveCredential(call.gateway); const credential = deps.resolveCredential(call.gateway);
const needsSign = call.signed !== false; const needsSign = call.signed !== false;
// 网关级常量参数(locale / source 等)先并入,业务参数可覆盖;签名与请求体都基于这份有效参数。 // 网关级常量参数(locale / source 等)先并入,业务参数可覆盖;会话标识同样并入并参与签名。
// effectiveParams 同时驱动请求体与加签原文(与 rop 策略一致:sessionId 既进 body 也进签名)。
const effectiveParams: Record<string, unknown> = { const effectiveParams: Record<string, unknown> = {
...strategy.fixedParams, ...strategy.fixedParams,
...call.params, ...call.params,
...(deps.session && !call.params[deps.session.name]
? { [deps.session.name]: deps.session.value }
: {}),
}; };
const body: Record<string, unknown> = { ...effectiveParams, method: call.method }; const body: Record<string, unknown> = { ...effectiveParams, method: call.method };
if (needsSign) { if (needsSign) {
......
...@@ -578,7 +578,7 @@ const webHostStrategy: RequestStrategy = defineRequestStrategy({ ...@@ -578,7 +578,7 @@ const webHostStrategy: RequestStrategy = defineRequestStrategy({
*/ */
const rpcGatewayStrategy: RequestStrategy = defineRequestStrategy({ const rpcGatewayStrategy: RequestStrategy = defineRequestStrategy({
id: 'rpc-gateway', id: 'rpc-gateway',
async prepare(config) { async prepare(config, session) {
const gateway = String(config.gateway ?? ''); const gateway = String(config.gateway ?? '');
if (!gateway) { if (!gateway) {
throw new RequestError( throw new RequestError(
...@@ -607,6 +607,7 @@ const rpcGatewayStrategy: RequestStrategy = defineRequestStrategy({ ...@@ -607,6 +607,7 @@ const rpcGatewayStrategy: RequestStrategy = defineRequestStrategy({
resolveCredential, resolveCredential,
resolveToken: config.gatewayToken, resolveToken: config.gatewayToken,
timeout: config.timeout, timeout: config.timeout,
session,
}, },
); );
......
...@@ -10,6 +10,12 @@ export interface LoginSession { ...@@ -10,6 +10,12 @@ export interface LoginSession {
dataScope: number; dataScope: number;
roleCodes: string[]; roleCodes: string[];
permCodes: string[]; permCodes: string[];
/**
* 老网关会话标识(形如 scrm-base-<uuid>-rop)。
* 优加主壳后端不用它,但积分核销等老 RPC 网关(glsx/scrm)把它作为 sessionId 放进请求体并参与加签。
* 登录接口返回后由登录流程写入此处,子应用通过 getLoginSession() 读取。
*/
loginToken?: string;
} }
const SESSION_KEY = 'session'; const SESSION_KEY = 'session';
......
...@@ -29,6 +29,12 @@ export interface LoginResult { ...@@ -29,6 +29,12 @@ export interface LoginResult {
permCodes: string[]; permCodes: string[];
/** 是否需强制改密:true-本次使用的是初始/重置密码,前端应引导改密后才能正常使用 */ /** 是否需强制改密:true-本次使用的是初始/重置密码,前端应引导改密后才能正常使用 */
needChangePassword: boolean; needChangePassword: boolean;
/**
* 老网关会话标识(形如 scrm-base-<uuid>-rop)。
* 优加主壳后端不用它,但积分核销等老 RPC 网关(glsx/scrm)把它作为 sessionId 放进请求体并参与加签。
* 登录接口已返回,这里补上字段,由登录流程落库到本地会话,子应用通过 getLoginSession() 读取。
*/
loginToken?: string;
} }
/** 当前登录用户信息(角色 + 权限码 + 数据域) */ /** 当前登录用户信息(角色 + 权限码 + 数据域) */
......
...@@ -10,6 +10,12 @@ export interface LoginSession { ...@@ -10,6 +10,12 @@ export interface LoginSession {
dataScope: number; dataScope: number;
roleCodes: string[]; roleCodes: string[];
permCodes: string[]; permCodes: string[];
/**
* 老网关会话标识(形如 scrm-base-<uuid>-rop)。
* 优加主壳后端不用它,但积分核销等老 RPC 网关(glsx/scrm)把它作为 sessionId 放进请求体并参与加签。
* 登录接口返回后由登录流程写入此处,子应用通过 getLoginSession() 读取。
*/
loginToken?: string;
} }
const SESSION_KEY = 'session'; const SESSION_KEY = 'session';
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!