Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion apps/app-pc/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import {HttpClient, resolveHttpTimeout} from "@coding-flow/flow-core";
import { message } from "antd";
import { getOperatorIdentity } from "@/config/identity";

/**
* 全局向流程引擎接口注入当前身份(issue #288)。
*
* <p>引擎的身份来自「操作者对象」,而后端控制器统一用请求参数 {@code operatorIdentity} 声明当前身份,
* 因此这里在 HttpClient 的公共查询参数里注入。发起 / 审批 / 待办 / 设计等包共用同一个 HttpClient 出口,
* 不需要每个页面各自拼参数。
*
* <p>注入范围仅限 {@code /api/} 前缀(flow-core 的 {@code defaultQueryParamsPathPrefixes} 默认值):
* 宿主自有接口(如 {@code /user/list} 这类基于 SearchRequest 的列表接口)会把 query 参数当成
* 查询字段解析,多带一个 {@code operatorIdentity} 就会报
* `Field operatorIdentity not found in class hierarchy`。
*
* <p>返回值约定:未设置过身份时返回空对象,让后端回退到登录用户自身的身份;
* 设置过(包括显式选择「默认身份」)时始终携带该参数,保证「切回默认身份」能被后端识别。
*/
HttpClient.defaultQueryParams = () => {
const identity = getOperatorIdentity();
return identity === null ? {} : { operatorIdentity: identity };
};

export const httpClient = new HttpClient(resolveHttpTimeout(),{
success:(msg:string)=>{
Expand All @@ -9,4 +30,4 @@ export const httpClient = new HttpClient(resolveHttpTimeout(),{
console.log(msg);
message.error(msg)
}
})
})
72 changes: 72 additions & 0 deletions apps/app-pc/src/components/identity-switch/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import React from "react";
import { AutoComplete, Button, Card, Space, Tag, Typography } from "antd";
import { IDENTITY_OPTIONS, getOperatorIdentity, setOperatorIdentity } from "@/config/identity";

/**
* 当前身份切换器(issue #288 演示用)。
*
* <p>身份是运行期选择的:同一用户切到 department1 / department2 后,后续所有请求都会带上
* {@code operatorIdentity},引擎据此把记录、待办都落在该身份上,从而验证「同一人、多身份、
* 待办互相隔离」的能力。
*
* <p>切换后整页刷新:身份同时影响列表查询、详情与审批面板,刷新是最简单可靠的重新拉取方式。
*/
const IdentitySwitch: React.FC = () => {

// 展示值必须随外部变化同步:登录态失效会清 token,其他标签页 / 路由切换也可能改动身份。
// 若只在首次挂载时读一次,就会出现「卡片显示 department1、请求其实按默认身份发出」的假象。
const [applied, setApplied] = React.useState<string>(getOperatorIdentity() ?? '');
const [value, setValue] = React.useState<string>(applied);

React.useEffect(() => {
const sync = () => {
const next = getOperatorIdentity() ?? '';
setApplied(next);
setValue(next);
};
sync();
window.addEventListener('hashchange', sync);
window.addEventListener('storage', sync);
return () => {
window.removeEventListener('hashchange', sync);
window.removeEventListener('storage', sync);
};
}, []);

const dirty = applied !== value;

const handleApply = () => {
setOperatorIdentity(value);
window.location.reload();
};

return (
<div style={{position: 'fixed', right: 16, bottom: 16, zIndex: 1000}}>
<Card size={"small"} title={"身份切换(演示)"}>
<Space direction={"vertical"} size={4}>
<Space size={4}>
<span>当前身份</span>
<Tag color={applied ? 'blue' : 'default'}>{applied || '默认身份'}</Tag>
</Space>
<Space.Compact>
<AutoComplete
value={value}
options={IDENTITY_OPTIONS}
onChange={(next) => setValue(next)}
placeholder={"如 department1"}
style={{width: 180}}
/>
<Button type={"primary"} disabled={!dirty} onClick={handleApply}>
切换
</Button>
</Space.Compact>
<Typography.Text type={"secondary"} style={{fontSize: 12}}>
切换后页面自动刷新,待办 / 已办 / 抄送会按身份隔离
</Typography.Text>
</Space>
</Card>
</div>
)
};

export default IdentitySwitch;
38 changes: 38 additions & 0 deletions apps/app-pc/src/config/identity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* 示例应用的身份(operatorIdentity)配置。
*
* <p>身份是**运行期选择**:同一用户(如 user1)可以在不同事项上以 department1 / department2 参与流程。
* 示例把当前身份存在 localStorage 并通过所有请求的 query 参数 {@code operatorIdentity} 传给后端,
* 后端据此加载「带身份的操作者副本」,记录与待办都落在该身份上。
*
* <p>真实项目应把身份选项替换为自己的组织模型(部门 / 岗位 / 任职关系),
* 并用同样的方式随请求下发。
*/

/** localStorage 中保存当前身份标识的键 */
export const FLOW_OPERATOR_IDENTITY_KEY = 'flow_operator_identity';

/** 示例身份选项:与 issue #288 的演示场景对应 */
export const IDENTITY_OPTIONS = [
{ label: '默认身份', value: '' },
{ label: '部门1(department1)', value: 'department1' },
{ label: '部门2(department2)', value: 'department2' },
];

/**
* 读取当前身份标识。
*
* @return 身份标识,空串表示默认身份;未设置时返回 null(由后端回退到登录用户自身身份)
*/
export const getOperatorIdentity = (): string | null => {
return localStorage.getItem(FLOW_OPERATOR_IDENTITY_KEY);
};

/**
* 保存当前身份标识
*
* @param identity 身份标识,空串表示默认身份
*/
export const setOperatorIdentity = (identity: string) => {
localStorage.setItem(FLOW_OPERATOR_IDENTITY_KEY, identity);
};
18 changes: 18 additions & 0 deletions apps/app-pc/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,29 @@ import zhCN from 'antd/locale/zh_CN';
import dayjs from 'dayjs';
import 'dayjs/locale/zh-cn';
import "./index.css";
import IdentitySwitch from "@/components/identity-switch";

dayjs.locale('zh');


// 注册逻辑动态加载:注册依赖(antd Form、表单组件、flow-design 等)不进入首屏 chunk
const Registrations = React.lazy(() => import('@/hooks/registrations'));

/**
* 身份切换器只在登录后的页面展示(登录页没有身份概念)
*/
const IdentitySwitchHost: React.FC = () => {
const [visible, setVisible] = React.useState(() => !window.location.hash.startsWith('#/login'));

React.useEffect(() => {
const sync = () => setVisible(!window.location.hash.startsWith('#/login'));
window.addEventListener('hashchange', sync);
return () => window.removeEventListener('hashchange', sync);
}, []);

return visible ? <IdentitySwitch/> : null;
}

const App = () => {

return (
Expand All @@ -28,6 +44,8 @@ const App = () => {
<React.Suspense fallback={null}>
<RouterProvider router={hashRouters} />
</React.Suspense>
{/* 全局身份切换:验证「同一用户多身份」能力 */}
<IdentitySwitchHost/>
</ConfigProvider>
</React.StrictMode>
)
Expand Down
23 changes: 22 additions & 1 deletion apps/app-pc/src/pages/todo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { done, list, notify, todo } from "@/api/record.ts";
import { forceSync, forceSyncPermission, type ForceSyncResult } from "@/api/workflow.ts";
import { ApprovalPanelDrawer, FlowTitle, WorkflowSelectModal } from "@coding-flow/flow-pc-approval";
import { Table, type TableProps } from "@coding-flow/flow-pc-ui";
import { Button, message, Popconfirm, Space, Tabs, type TabsProps } from "antd";
import { Alert, Button, message, Popconfirm, Space, Tabs, type TabsProps } from "antd";
import dayjs from "dayjs";
import { EventBus, type ActionType } from "@coding-flow/flow-core";
import { getOperatorIdentity } from "@/config/identity";

const TodoPage: React.FC = () => {

Expand Down Expand Up @@ -92,6 +93,14 @@ const TodoPage: React.FC = () => {
dataIndex: 'currentOperatorName',
title: '审批人',
},
{
dataIndex: 'currentOperatorIdentity',
title: '身份',
render: (value) => {
// 身份为空表示默认身份(issue #288);待办 / 已办 / 抄送均按身份隔离
return value || '默认身份';
}
},
{
dataIndex: 'recordState',
title: '状态',
Expand Down Expand Up @@ -248,6 +257,18 @@ const TodoPage: React.FC = () => {

return (
<div>
{/*
身份提示:待办/已办/抄送都按「人员 + 身份」隔离查询。
用户以非默认身份发起流程、审批人却是默认身份的人员时,会在默认身份下才能看到该待办,
这里把当前身份显式展示出来,避免误判成「待办丢了」。
*/}
<Alert
type={"info"}
showIcon
style={{marginBottom: 12}}
message={`当前身份:${getOperatorIdentity() || '默认身份'}`}
description={"待办 / 已办 / 抄送按身份隔离,只显示落在当前身份下的记录;切换身份后列表会变化。发起流程时审批人未指定身份,其待办落在「默认身份」下。"}
/>
<Tabs
items={items}
centered={true}
Expand Down
6 changes: 6 additions & 0 deletions packages/flow-approval-presenter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @coding-flow/flow-approval-presenter

## 0.3.0

### Minor Changes

- 版本号升级至 0.3.0(配合后端 0.3.0「操作者身份标识」issue #288,本包无对外功能变更)

## 0.2.22

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/flow-approval-presenter/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@coding-flow/flow-approval-presenter",
"version": "0.2.22",
"version": "0.3.0",
"description": "flow-engine approval presenter framework ",
"type": "module",
"sideEffects": [
Expand Down
6 changes: 6 additions & 0 deletions packages/flow-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @coding-flow/flow-core

## 0.3.0

### Minor Changes

- 版本号升级至 0.3.0(配合后端 0.3.0「操作者身份标识」issue #288,本包无对外功能变更)

## 0.2.22

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/flow-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@coding-flow/flow-core",
"version": "0.2.22",
"version": "0.3.0",
"description": "flow-engine core lib framework ",
"type": "module",
"sideEffects": [
Expand Down
65 changes: 64 additions & 1 deletion packages/flow-core/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,34 @@ export type Response = {
total?: number;
}

/**
* 全局默认查询参数提供者:每次请求都会被调用,返回的键值会合并到请求的 query 参数中。
*
* <p>用于宿主向所有接口声明公共上下文(例如当前操作者身份 {@code operatorIdentity}),
* 避免每个页面、每个包各自拼参数。返回空对象表示不注入。
*/
export type DefaultQueryParamsProvider = () => Record<string, any>;

export class HttpClient {
/**
* 全局默认查询参数提供者。宿主应在应用启动时设置一次,对所有 HttpClient 实例生效。
*
* <pre>
* HttpClient.defaultQueryParams = () => ({ operatorIdentity: localStorage.getItem('flow_operator_identity') || undefined });
* </pre>
*/
public static defaultQueryParams: DefaultQueryParamsProvider = () => ({});

/**
* 公共查询参数生效的接口路径前缀,默认只注入流程引擎自身的接口({@code /api/})。
*
* <p>**不要**把默认值放宽成「所有请求」:宿主的自有接口常把 query 参数直接映射成查询条件
* (例如基于 `SearchRequest` / `FastRepository` 的列表接口),多一个未声明的参数就会
* 抛 `Field xxx not found in class hierarchy`,表现为页面 500。缩到 {@code /api/} 前缀
* 即可避免污染宿主接口,同时不影响流程的发起、查询、审批等全部引擎接口。
*/
public static defaultQueryParamsPathPrefixes: string[] = ['/api/'];

private readonly api: AxiosInstance;
private messageBox: MessageBox;

Expand All @@ -63,6 +90,27 @@ export class HttpClient {
}


/**
* 判断当前请求是否落在公共查询参数的注入范围内。
*
* <p>比较时把 {@code baseURL} 与 {@code url} 拼成完整路径,避免宿主配置了 baseURL 后前缀匹配失效。
* 白名单为空时不注入任何请求(显式关闭)。
*/
private static matchesDefaultQueryScope(config: any): boolean {
const prefixes = HttpClient.defaultQueryParamsPathPrefixes;
if (!prefixes || prefixes.length === 0) {
return false;
}
const baseURL: string = config?.baseURL || '';
const url: string = config?.url || '';
// 绝对地址(http/https)说明请求发往第三方,不注入身份等上下文
if (/^https?:\/\//i.test(url)) {
return false;
}
const target = `${baseURL.replace(/\/$/, '')}${url}`;
return prefixes.some(prefix => target.startsWith(prefix));
}

private addRequestInterceptors() {
this.api.interceptors.request.use((config: any) => {
const token = localStorage.getItem("token");
Expand All @@ -71,6 +119,18 @@ export class HttpClient {
Authorization: `${token}`,
} as any;
}
// 合并宿主注入的公共 query 参数(例如当前操作者身份),
// POST 请求同样以 query 形式携带,后端用 request.getParameter 统一读取。
// 仅对白名单前缀(默认流程引擎的 /api/)生效,避免污染宿主自有接口的查询参数解析。
if (HttpClient.matchesDefaultQueryScope(config)) {
const defaultParams = HttpClient.defaultQueryParams();
if (defaultParams && Object.keys(defaultParams).length > 0) {
config.params = {
...(config.params || {}),
...defaultParams,
};
}
}
return config;
}, (error: any) => {
return Promise.reject(error);
Expand All @@ -97,7 +157,10 @@ export class HttpClient {
if ("token.expire" === errCode || "token.error" === errCode) {
this.messageBox.error(FlowMessageRegistry.getInstance().get(FlowMessageKey.HTTP_TOKEN_EXPIRED));
await sleep(1500);
localStorage.clear();
// 只清登录态,不能 localStorage.clear():
// 那会顺手抹掉宿主存放的身份(operatorIdentity)、接口超时等配置,
// 表现为「切换器显示 department1、请求却按默认身份发出」,排查成本很高
localStorage.removeItem("token");
window.location.href = '/#login';
} else {
if ("login.error" === errCode) {
Expand Down
6 changes: 6 additions & 0 deletions packages/flow-design/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @coding-flow/flow-design

## 0.3.0

### Minor Changes

- 版本号升级至 0.3.0(配合后端 0.3.0「操作者身份标识」issue #288,本包无对外功能变更)

## 0.2.22

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/flow-design/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@coding-flow/flow-design",
"version": "0.2.22",
"version": "0.3.0",
"description": "flow-engine design components ",
"type": "module",
"sideEffects": [
Expand Down
6 changes: 6 additions & 0 deletions packages/flow-icons/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @coding-flow/flow-icons

## 0.3.0

### Minor Changes

- 版本号升级至 0.3.0(配合后端 0.3.0「操作者身份标识」issue #288,本包无对外功能变更)

## 0.2.22

### Patch Changes
Expand Down
Loading
Loading