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
21 changes: 20 additions & 1 deletion apps/app-mobile/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import {HttpClient, resolveHttpTimeout} from "@coding-flow/flow-core";
import { message } from "antd";
import { getOperatorIdentity } from "@/config/identity";

/**
* 全局向流程引擎接口注入当前操作身份(issue #288 / #297)。
*
* <p>与 {@code app-pc} 同一做法:后端控制器统一用请求参数 {@code operatorIdentity} 声明当前身份,
* 因此在 HttpClient 的公共查询参数里注入,待办查询、审批、发起等包共用一个出口,
* 页面之间不需要各自拼参数。
*
* <p>注入范围仅限 {@code /api/} 前缀(flow-core 的 {@code defaultQueryParamsPathPrefixes} 默认值):
* 宿主自有接口(如 {@code /user/login})多带一个未声明的 query 参数会报解析错误。
*
* <p>返回值约定:未设置过身份时返回空对象,让后端回退到登录用户自身的身份;
* 设置过(包括显式选择「默认身份」的空串)时始终携带该参数,保证「切回默认身份」能被后端识别。
*/
HttpClient.defaultQueryParams = () => {
const identity = getOperatorIdentity();
return identity === null ? {} : { operatorIdentity: identity };
};

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

/**
* 移动端「操作身份」切换条(演示用)。
*
* <p>身份是「以什么身份办事」,不是「能看到什么数据」(issue #297):切换后所有 {@code /api/} 请求
* 都会带上 {@code operatorIdentity},引擎据此决定取人脚本走哪条分支,并把本次操作身份写进记录留痕;
* 但列表可见范围不随身份变化(默认按人员跨身份查询)。
*
* <p>移动端用**页内横条 + 底部弹层**,不用悬浮球:审批页底部是「同意 / 拒绝」操作区,
* 悬浮球会盖住按钮,误触代价太大。
*
* <p>切换后整页刷新:身份同时影响审批面板的可用动作、取人脚本上下文与本次提交的留痕身份。
*/
const DEFAULT_OPTION_VALUE = '__default__';

const toOptionValue = (identity: string): string => identity || DEFAULT_OPTION_VALUE;

const fromOptionValue = (value: string): string => (
value === DEFAULT_OPTION_VALUE ? '' : value
);

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

// 展示值必须随外部变化同步:登录态失效、其他标签页改动身份都要反映到界面上,
// 否则会出现「横条显示 department1、请求其实按默认身份发出」的假象。
const [applied, setApplied] = React.useState<string>(getOperatorIdentity() ?? '');
const [open, setOpen] = React.useState(false);
const [value, setValue] = React.useState<string>(applied);

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

const handleOpen = () => {
// 每次打开都以「已生效身份」为初值,避免上次未确认的选择残留
setValue(applied);
setOpen(true);
};

const handleApply = () => {
setOperatorIdentity(value);
setOpen(false);
Toast.show({content: `已切换为${value || '默认身份'}`, duration: 800});
window.location.reload();
};

return (
<>
<div
onClick={handleOpen}
style={{
display: 'flex',
alignItems: 'center',
gap: 8,
padding: '8px 12px',
background: '#fff',
borderRadius: 8,
margin: '8px 0',
}}
>
<span style={{fontSize: 13, color: '#666'}}>操作身份</span>
<Tag color={applied ? 'primary' : 'default'}>{applied || '默认身份'}</Tag>
<span style={{marginLeft: 'auto', fontSize: 13, color: '#1677ff'}}>切换</span>
</div>

<Popup
visible={open}
onMaskClick={() => setOpen(false)}
onClose={() => setOpen(false)}
bodyStyle={{borderTopLeftRadius: 12, borderTopRightRadius: 12}}
>
<div style={{padding: 16}}>
<div style={{fontSize: 16, fontWeight: 600, marginBottom: 12}}>选择操作身份</div>
<Selector
columns={1}
value={[toOptionValue(value)]}
options={IDENTITY_OPTIONS.map(item => ({
label: item.label,
value: toOptionValue(item.value),
}))}
onChange={(selected) => {
if (selected.length === 0) {
// antd-mobile 允许点空已选项,这里不允许「无选择」
return;
}
setValue(fromOptionValue(selected[0]));
}}
/>
<div style={{fontSize: 12, color: '#999', margin: '8px 0 16px'}}>
身份只影响流程操作与脚本匹配,不影响列表可见范围
</div>
<Button
block
color={"primary"}
disabled={value === applied}
onClick={handleApply}
>
确定
</Button>
</div>
</Popup>
</>
);
};

export default IdentitySwitch;
43 changes: 43 additions & 0 deletions apps/app-mobile/src/config/identity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* 移动端示例应用的「操作身份」(operatorIdentity)配置。
*
* <p>与 {@code app-pc} 保持同一口径(issue #288 / #297):身份是**运行期选择的操作视角**,
* 同一用户(如 user1)可以在不同事项上以 department1 / department2 参与流程。
* 示例把当前身份存在 localStorage,并通过所有 {@code /api/} 请求的 query 参数
* {@code operatorIdentity} 传给后端:取人脚本按该身份走对应分支,本次操作身份写入记录留痕
* ({@code createOperatorIdentity} / {@code submitOperatorIdentity})。
*
* <p>身份**不决定数据可见范围**(issue #297):待办 / 已办 / 抄送默认按人员跨身份查询,
* 只有显式传 {@code identityScope=LOCKED} 才按身份锁定。
*
* <p>两个示例应用各自维护一份,是为了让示例保持「可整块删除」的独立性;
* 真实项目应把身份选项替换为自己的组织模型(部门 / 岗位 / 任职关系),并用同样的方式随请求下发。
*/

/** localStorage 中保存当前身份标识的键(与 app-pc 共用同一个键,便于两端联调同一身份) */
export const FLOW_OPERATOR_IDENTITY_KEY = 'flow_operator_identity';

/** 示例身份选项:与多身份演示场景对应(issue #288 / #297) */
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);
};
38 changes: 24 additions & 14 deletions apps/app-mobile/src/pages/todo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {done, list, notify, todo} from "@/api/record.ts";
import dayjs from "dayjs";
import {TextIcon} from "@coding-flow/flow-mobile-ui";
import {useNavigate} from "react-router";
import IdentitySwitch from "@/components/identity-switch";

interface TodoListProps {
type: 'todo' | 'done' | 'notify' | 'list'; // 固定类型,用于区分不同列表
Expand Down Expand Up @@ -68,6 +69,8 @@ const TodoList: React.FC<TodoListProps> = ({type}) => {
<>
<div>{record.nodeName} {record.workTitle}</div>
<div>实例编码:{record.processId}</div>
{/* 记录归属身份(留痕信息):列表本身不按身份过滤(issue #297) */}
<div>身份:{record.currentOperatorIdentity || '默认身份'}</div>
</>
)}
extra={
Expand All @@ -88,20 +91,27 @@ const TodoPage: React.FC = () => {
const [currentKey, setCurrentKey] = React.useState('todo');

return (
<Tabs activeKey={currentKey} onChange={setCurrentKey}>
<Tabs.Tab title="待办" key="todo">
<TodoList type="todo"/>
</Tabs.Tab>
<Tabs.Tab title="已办" key="done">
<TodoList type="done"/>
</Tabs.Tab>
<Tabs.Tab title="抄送" key="notify">
<TodoList type="notify"/>
</Tabs.Tab>
<Tabs.Tab title="全部" key="list">
<TodoList type="list"/>
</Tabs.Tab>
</Tabs>
<div>
{/*
操作身份切换(issue #297):身份决定取人脚本走哪条分支、本次操作写哪个身份留痕,
但不影响列表可见范围——待办 / 已办 / 抄送默认按人员跨身份查询。
*/}
<IdentitySwitch/>
<Tabs activeKey={currentKey} onChange={setCurrentKey}>
<Tabs.Tab title="待办" key="todo">
<TodoList type="todo"/>
</Tabs.Tab>
<Tabs.Tab title="已办" key="done">
<TodoList type="done"/>
</Tabs.Tab>
<Tabs.Tab title="抄送" key="notify">
<TodoList type="notify"/>
</Tabs.Tab>
<Tabs.Tab title="全部" key="list">
<TodoList type="list"/>
</Tabs.Tab>
</Tabs>
</div>
);
};

Expand Down
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)
}
})
})
77 changes: 77 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,77 @@
import React from "react";
import { AutoComplete, Button, Card, Space, Tag, Typography } from "antd";
import { IDENTITY_OPTIONS, getOperatorIdentity, setOperatorIdentity } from "@/config/identity";

/**
* 当前操作身份切换器(演示用)。
*
* <p>身份是「以什么身份办事」,不是「能看到什么数据」(issue #297):
* 切换后后续请求仍带上 {@code operatorIdentity},引擎据此决定取人脚本走哪条路径
* (例如 a 身份对应 L1 领导、b 身份对应 L2 领导),并把本次操作身份写进记录留痕
* ({@code createOperatorIdentity} / {@code submitOperatorIdentity})。
*
* <p>但列表可见范围不再随身份变化:待办 / 已办 / 抄送默认按人员跨身份查询,
* 只有显式传 {@code identityScope=LOCKED} 才锁定身份。
*
* <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;
Loading
Loading