diff --git a/apps/app-mobile/src/api/index.ts b/apps/app-mobile/src/api/index.ts index 2ae2f0df..1396642d 100644 --- a/apps/app-mobile/src/api/index.ts +++ b/apps/app-mobile/src/api/index.ts @@ -3,21 +3,21 @@ import { message } from "antd"; import { getOperatorIdentity } from "@/config/identity"; /** - * 全局向流程引擎接口注入当前操作身份(issue #288 / #297)。 + * 全局向流程引擎接口注入当前操作身份(issue #288 / #297 / #301)。 * - *
与 {@code app-pc} 同一做法:后端控制器统一用请求参数 {@code operatorIdentity} 声明当前身份, - * 因此在 HttpClient 的公共查询参数里注入,待办查询、审批、发起等包共用一个出口, - * 页面之间不需要各自拼参数。 + *
与 {@code app-pc} 同一做法:后端控制器统一声明当前身份,因此在 HttpClient 的公共请求头里注入, + * 待办查询、审批、发起等包共用一个出口,页面之间不需要各自拼参数。 * - *
注入范围仅限 {@code /api/} 前缀(flow-core 的 {@code defaultQueryParamsPathPrefixes} 默认值): - * 宿主自有接口(如 {@code /user/login})多带一个未声明的 query 参数会报解析错误。 + *
必须走请求头而不是 query:引擎的查询接口用 {@code SearchRequest} 接收请求,会把 query 参数 + * 当作实体过滤字段解析,多带一个 {@code operatorIdentity} 就会报 + * `Field operatorIdentity not found in class hierarchy`,待办/已办/抄送/流程列表全部 500(issue #301)。 * *
返回值约定:未设置过身份时返回空对象,让后端回退到登录用户自身的身份; - * 设置过(包括显式选择「默认身份」的空串)时始终携带该参数,保证「切回默认身份」能被后端识别。 + * 设置过(包括显式选择「默认身份」的空串)时始终携带该请求头,保证「切回默认身份」能被后端识别。 */ -HttpClient.defaultQueryParams = () => { +HttpClient.defaultHeaders = () => { const identity = getOperatorIdentity(); - return identity === null ? {} : { operatorIdentity: identity }; + return identity === null ? {} : {"X-Operator-Identity": identity}; }; export const httpClient = new HttpClient(resolveHttpTimeout(),{ diff --git a/apps/app-pc/src/api/index.ts b/apps/app-pc/src/api/index.ts index 0f220e4c..9259c17f 100644 --- a/apps/app-pc/src/api/index.ts +++ b/apps/app-pc/src/api/index.ts @@ -3,23 +3,21 @@ import { message } from "antd"; import { getOperatorIdentity } from "@/config/identity"; /** - * 全局向流程引擎接口注入当前身份(issue #288)。 + * 全局向流程引擎接口注入当前身份(issue #288 / #301)。 * - *
引擎的身份来自「操作者对象」,而后端控制器统一用请求参数 {@code operatorIdentity} 声明当前身份, - * 因此这里在 HttpClient 的公共查询参数里注入。发起 / 审批 / 待办 / 设计等包共用同一个 HttpClient 出口, - * 不需要每个页面各自拼参数。 + *
引擎的身份来自「操作者对象」,后端控制器统一声明当前身份,因此这里在 HttpClient 的 + * 公共请求头里注入。发起 / 审批 / 待办 / 设计等包共用同一个 HttpClient 出口,不需要每个页面各自拼参数。 * - *
注入范围仅限 {@code /api/} 前缀(flow-core 的 {@code defaultQueryParamsPathPrefixes} 默认值): - * 宿主自有接口(如 {@code /user/list} 这类基于 SearchRequest 的列表接口)会把 query 参数当成 - * 查询字段解析,多带一个 {@code operatorIdentity} 就会报 - * `Field operatorIdentity not found in class hierarchy`。 + *
必须走请求头而不是 query:引擎的查询接口用 {@code SearchRequest} 接收请求,会把 query 参数 + * 当作实体过滤字段解析,多带一个 {@code operatorIdentity} 就会报 + * `Field operatorIdentity not found in class hierarchy`,待办/已办/抄送/流程列表全部 500(issue #301)。 * *
返回值约定:未设置过身份时返回空对象,让后端回退到登录用户自身的身份; - * 设置过(包括显式选择「默认身份」)时始终携带该参数,保证「切回默认身份」能被后端识别。 + * 设置过(包括显式选择「默认身份」)时始终携带该请求头,保证「切回默认身份」能被后端识别。 */ -HttpClient.defaultQueryParams = () => { +HttpClient.defaultHeaders = () => { const identity = getOperatorIdentity(); - return identity === null ? {} : { operatorIdentity: identity }; + return identity === null ? {} : {"X-Operator-Identity": identity}; }; export const httpClient = new HttpClient(resolveHttpTimeout(),{ diff --git a/packages/flow-core/src/http.ts b/packages/flow-core/src/http.ts index a453e247..84cd0e2d 100644 --- a/packages/flow-core/src/http.ts +++ b/packages/flow-core/src/http.ts @@ -39,11 +39,53 @@ export type Response = { total?: number; } +/** + * 全局默认请求头提供者:每次请求都会被调用,返回的键值会合并到请求头中(issue #301)。 + * + *
请求上下文(例如当前操作身份)应放在请求头而不是 query:引擎的查询接口用 + * `SearchRequest` 接收请求,会把 query 参数当作实体过滤字段解析,多带一个 + * `operatorIdentity` 就会报 `Field operatorIdentity not found in class hierarchy`。 + * + *
返回值约定:`undefined` / `null` 的键会被忽略;空字符串会被保留,
+ * 用于表达「显式选择默认身份」与「未携带」的区别。
+ */
+export type DefaultHeadersProvider = () => Record 合并规则:`undefined` / `null` 的键跳过(表示本次不携带);空字符串保留,
+ * 用于表达「显式选择默认身份」与「未携带该头」的区别。已有请求头中同名的值会被覆盖。
+ *
+ * @param headers 已有请求头
+ * @param provider 默认请求头提供者
+ * @returns 合并后的新对象,不修改入参
+ */
+export function mergeDefaultHeaders(
+ headers: Record 用于宿主向所有接口声明公共上下文(例如当前操作者身份 {@code operatorIdentity}),
* 避免每个页面、每个包各自拼参数。返回空对象表示不注入。
+ *
+ * 注意:身份等请求上下文请改用 {@link HttpClient.defaultHeaders},不要放进 query,
+ * 否则会污染基于 `SearchRequest` 的列表接口(issue #301)。
*/
export type DefaultQueryParamsProvider = () => Record
+ * HttpClient.defaultHeaders = () => ({ 'X-Operator-Identity': getOperatorIdentity() ?? undefined });
+ *
+ */
+ public static defaultHeaders: DefaultHeadersProvider = () => ({});
+
/**
* 公共查询参数生效的接口路径前缀,默认只注入流程引擎自身的接口({@code /api/})。
*
@@ -113,12 +164,15 @@ export class HttpClient {
private addRequestInterceptors() {
this.api.interceptors.request.use((config: any) => {
+ let headers: Record