2026-10-28 17:30:35 +08:00
import { Text , ReadingContext , useReadingScale } from '../typography' ;
import { workspaceTasks , shortcutOrder } from '../core/workspace-tasks' ;
import { readPreference , savePreference } from '../core/preferences' ;
import React , { useContext , useEffect , useState } from 'react' ;
import { AppState , Image , Modal , Pressable , ScrollView , View , useWindowDimensions } from 'react-native' ;
2026-10-18 08:49:30 +08:00
import Constants from 'expo-constants' ;
import * as Updates from 'expo-updates' ;
import { AppearanceContext , Avatar , Button , Card , Heading , Icon , MenuItem , Note , s , useTheme , type IconName } from '../ui' ;
import { imagePath } from '../core/account' ;
import { usePrivateImage } from '../files' ;
import { ApiClient } from '../core/network' ;
import type { Module , Row } from '../core/modules' ;
2026-10-21 15:13:39 +08:00
import { workspaceColumns } from '../core/workspace-layout' ;
2026-10-18 08:49:30 +08:00
2026-10-28 17:30:35 +08:00
const icons :Record < string , IconName > = { arrival : 'checkmark-circle-outline' , use : 'file-tray-outline' , move : 'swap-horizontal-outline' , count : 'clipboard-outline' , stock : 'cube-outline' , purchase : 'cart-outline' , pending : 'download-outline' , in : 'file-tray-full-outline' , loans : 'swap-horizontal-outline' , check : 'clipboard-outline' , out : 'file-tray-outline' , production : 'construct-outline' , files : 'folder-outline' } ;
2026-10-30 15:02:54 +08:00
const hints :Record < string , string > = { arrival : '登记到货、逐次验收合格数量' , use : '选择库存,核对后物料领用' , move : '同项目库存更换实际存放库区' , count : '现场清点,登记实际数量' , stock : '查询物品、数量和所在库区' , purchase : '采购明细与报价进展' , pending : '核对物品,现场办理入库' , in : '追溯每一次入库' , loans : '查询项目间借还进度' , check : '查看盘点记录、确认盘点' , out : '查看领用与退回记录' , production : '查询任务、产量与执行状态' , files : '查看、上传和下载共享文件' } ;
2026-10-28 17:30:35 +08:00
type Props = { api :ApiClient ; user :Row ; module s : Module [ ] ; scope :string ; open : ( name :string , query? :Row ) = > void ; go : ( tab :string ) = > void ; ai :boolean } ;
export function Workspace ( { api , user , module s , scope , open , go , ai } : Props ) {
const t = useTheme ( ) , scale = useReadingScale ( ) , { width , fontScale } = useWindowDimensions ( ) , columns = workspaceColumns ( width , fontScale * scale ) ;
const [ order , setOrder ] = useState < string [ ] > ( ( ) = > shortcutOrder ( null , module s ) ) , [ editing , setEditing ] = useState ( false ) , [ counts , setCounts ] = useState < Record < string , number | null > > ( { } ) , [ loading , setLoading ] = useState ( true ) , [ retry , setRetry ] = useState ( 0 ) , [ error , setError ] = useState ( '' ) ;
const tasks = workspaceTasks ( module s ) , signature = module s.map ( m = > m . key ) . join ( ',' ) ;
useEffect ( ( ) = > { let alive = true ; setOrder ( shortcutOrder ( null , module s ) ) ; void readPreference ( scope , 'shortcuts' ) . then ( value = > { if ( alive ) setOrder ( shortcutOrder ( value , module s ) ) ; } ) . catch ( ( ) = > { } ) ; return ( ) = > { alive = false ; } ; } , [ scope , signature ] ) ;
useEffect ( ( ) = > { const ctl = new AbortController ( ) ; setLoading ( true ) ; setCounts ( { } ) ; void Promise . all ( tasks . map ( async task = > { try { const data = await api . request < Row > ( task . path , { query : { . . . task . query , pageNum :1 , pageSize :1 } , signal :ctl.signal } ) ; return [ task . id , Math . max ( 0 , Number ( data . total ? ? data . data ? . total ) || 0 ) ] as const ; } catch { return [ task . id , null ] as const ; } } ) ) . then ( values = > { if ( ! ctl . signal . aborted ) { setCounts ( Object . fromEntries ( values ) ) ; setLoading ( false ) ; } } ) ; return ( ) = > ctl . abort ( ) ; } , [ api , signature , retry ] ) ;
useEffect ( ( ) = > { const listener = AppState . addEventListener ( 'change' , state = > { if ( state === 'active' ) setRetry ( n = > n + 1 ) ; } ) ; return ( ) = > listener . remove ( ) ; } , [ ] ) ;
const quick = order . map ( key = > module s.find ( m = > m . key === key ) ) . filter ( ( m ) : m is Module = > ! ! m ) ;
function reorder ( key :string , delta :number ) { const i = order . indexOf ( key ) , next = i + delta ; if ( i < 0 | | next < 0 | | next > = order . length ) return ; const values = [ . . . order ] ; [ values [ i ] , values [ next ] ] = [ values [ next ] , values [ i ] ] ; setOrder ( values ) ; }
async function save ( ) { try { await savePreference ( scope , 'shortcuts' , order ) ; setEditing ( false ) ; setError ( '' ) ; } catch { setError ( '未能保存常用操作,请重试。' ) ; } }
return < ScrollView contentContainerStyle = { [ s . page , { paddingTop :4 , gap :12 } ] } >
< View style = { { flexDirection : 'row' , alignItems : 'center' , justifyContent : 'space-between' , paddingVertical :4 } } > < Text numberOfLines = { 1 } style = { { flex :1 , minWidth :0 , fontSize :16 , fontWeight : '600' , color :t.text } } > { user . nickName || user . userName } < / Text > < Text style = { { fontSize :12 , color :t.muted } } > { new Date ( ) . toLocaleDateString ( 'zh-CN' , { month : 'long' , day : 'numeric' , weekday : 'short' } ) } < / Text > < / View >
{ ! ! tasks . length && < View > < View style = { [ s . row , { justifyContent : 'space-between' , marginBottom :6 } ] } > < Text style = { { fontWeight : '600' , color :t.text } } > 待 办 事 项 < / Text > < Pressable accessibilityRole = "button" accessibilityLabel = "刷新待办" onPress = { ( ) = > setRetry ( n = > n + 1 ) } style = { { minHeight :36 , justifyContent : 'center' } } > < Text style = { { fontSize :12 , color :t.accent } } > { loading ? '刷新中…' : '刷新' } < / Text > < / Pressable > < / View > < View style = { { flexDirection : 'row' , flexWrap : 'wrap' , gap :8 } } > { tasks . map ( task = > < Pressable key = { task . id } accessibilityRole = "button" accessibilityLabel = { task . label } onPress = { ( ) = > open ( task . key , task . openQuery ) } style = { { flexBasis :columns === 1 ? '100%' : '47%' , flexGrow :1 , padding :12 , borderWidth :1 , borderColor :t.line , backgroundColor :t.card , borderRadius :10 } } > < Text style = { { fontSize :22 , lineHeight :28 , fontWeight : '700' , color :t.accent } } > { counts [ task . id ] ? ? '—' } < / Text > < Text style = { { fontSize :12 , color :t.text } } > { task . label } { ! loading && counts [ task . id ] === null ? ' · 暂未获取' : '' } < / Text > < / Pressable > ) } < / View > < / View > }
< View style = { [ s . row , { justifyContent : 'space-between' } ] } > < Text style = { { fontSize :15 , fontWeight : '600' , color :t.text } } > 常 用 操 作 < / Text > < Pressable accessibilityRole = "button" accessibilityLabel = "编辑常用操作" onPress = { ( ) = > setEditing ( true ) } style = { { minHeight :40 , justifyContent : 'center' } } > < Text style = { { fontSize :12 , color :t.accent } } > 编 辑 < / Text > < / Pressable > < / View >
< View style = { { flexDirection : 'row' , flexWrap : 'wrap' , gap :10 } } > { quick . map ( m = > < Pressable accessibilityRole = "button" accessibilityLabel = { m . title } key = { m . key } onPress = { ( ) = > open ( m . key ) } style = { ( { pressed } ) = > ( { flexBasis :columns === 1 ? '100%' : '47%' , flexGrow :1 , minWidth :0 , padding :12 , gap :9 , borderRadius :10 , backgroundColor :t.card , borderWidth :1 , borderColor :t.line , flexDirection : 'row' , alignItems : 'center' , minHeight :58 , opacity :pressed?.7 : 1 } ) } > < Icon name = { icons [ m . key ] || 'grid-outline' } size = { 21 } / > < Text style = { { color :t.text , fontSize :14 , fontWeight : '500' , flex :1 } } > { m . title } < / Text > < / Pressable > ) } < / View >
{ ! quick . length && < Note > 点 击 “ 编 辑 ” 添 加 常 用 操 作 。 < / Note > }
< Button title = "全部业务" secondary onPress = { ( ) = > go ( 'business' ) } / >
{ ai && < Pressable accessibilityRole = "button" accessibilityLabel = "与 AI 对话" onPress = { ( ) = > go ( 'assistant' ) } style = { { flexDirection : 'row' , alignItems : 'center' , gap :10 , padding :12 , borderRadius :10 , backgroundColor :t.soft } } > < Image source = { require ( '../../assets/assistant.png' ) } style = { { width :28 , height :28 , borderRadius :14 } } / > < Text style = { { flex :1 , color :t.text , fontSize :14 } } > AI 助 手 < / Text > < Icon name = "arrow-forward" size = { 18 } / > < / Pressable > }
< Modal visible = { editing } transparent animationType = "slide" onRequestClose = { ( ) = > setEditing ( false ) } > < View style = { { flex :1 , justifyContent : 'flex-end' , backgroundColor : '#0007' } } > < View style = { { maxHeight : '85%' , backgroundColor :t.card , borderTopLeftRadius :18 , borderTopRightRadius :18 , padding :16 , gap :12 } } > < Heading > 常 用 操 作 < / Heading > < Note > 勾 选 入 口 , 使 用 上 移 、 下 移 调 整 顺 序 。 < / Note > < ScrollView > { [ . . . quick , . . . module s.filter ( m = > ! order . includes ( m . key ) ) ] . map ( m = > < View key = { m . key } style = { { flexDirection : 'row' , alignItems : 'center' , borderBottomWidth :1 , borderColor :t.line , gap :6 } } > < Pressable accessibilityRole = "checkbox" accessibilityState = { { checked :order.includes ( m . key ) } } accessibilityLabel = { m . title } onPress = { ( ) = > setOrder ( old = > old . includes ( m . key ) ? old . filter ( k = > k !== m . key ) : [ . . . old , m . key ] ) } style = { { flex :1 , minHeight :48 , flexDirection : 'row' , alignItems : 'center' , gap :10 } } > < Icon name = { order . includes ( m . key ) ? 'checkbox' : 'square-outline' } size = { 20 } / > < Text style = { { color :t.text } } > { m . title } < / Text > < / Pressable > { order . includes ( m . key ) && < > < Pressable accessibilityRole = "button" accessibilityLabel = { m . title + '上移' } disabled = { order . indexOf ( m . key ) === 0 } onPress = { ( ) = > reorder ( m . key , - 1 ) } style = { { padding :10 } } > < Icon name = "arrow-up" size = { 18 } color = { order . indexOf ( m . key ) === 0?t.muted :t.accent } / > < / Pressable > < Pressable accessibilityRole = "button" accessibilityLabel = { m . title + '下移' } disabled = { order . indexOf ( m . key ) === order . length - 1 } onPress = { ( ) = > reorder ( m . key , 1 ) } style = { { padding :10 } } > < Icon name = "arrow-down" size = { 18 } / > < / Pressable > < / > } < / View > ) } < / ScrollView > { ! ! error && < Note > { error } < / Note > } < View style = { s . row } > < Button title = "关闭" secondary onPress = { ( ) = > setEditing ( false ) } / > < Button title = "保存常用操作" onPress = { ( ) = > void save ( ) } / > < / View > < / View > < / View > < / Modal >
2026-10-18 08:49:30 +08:00
< / ScrollView > ;
}
export function BusinessHub ( { module s , open } : { module s : Module [ ] ; open : ( name :string ) = > void } ) {
2026-10-28 17:30:35 +08:00
const t = useTheme ( ) ; return < ScrollView contentContainerStyle = { s . page } > { [ [ '现场办理' , [ 'arrival' , 'pending' , 'use' , 'move' , 'count' ] ] , [ '查询与记录' , [ 'stock' , 'purchase' , 'production' , 'in' , 'out' , 'loans' , 'check' , 'files' ] ] ] . map ( ( [ label , keys ] ) = > { const entries = ( keys as string [ ] ) . map ( key = > module s.find ( m = > m . key === key ) ) . filter ( ( m ) : m is Module = > ! ! m ) ; return entries . length ? < View key = { label as string } style = { { gap :10 , marginTop :6 } } > < Text style = { { color :t.muted , fontSize :13 , fontWeight : '600' , paddingLeft :4 } } > { label as string } < / Text > < Card > { entries . map ( ( m , i ) = > < MenuItem key = { m . key } title = { m . title } detail = { hints [ m . key ] } icon = { icons [ m . key ] } onPress = { ( ) = > open ( m . key ) } last = { i === entries . length - 1 } / > ) } < / Card > < / View > : null ; } ) } { ! module s.length && < Note > 暂 未 分 配 移 动 业 务 , 可 请 管 理 员 协 助 确 认 。 < / Note > } < / ScrollView > ;
2026-10-18 08:49:30 +08:00
}
export function Me ( { api , user , open } : { api :ApiClient ; user :Row ; open : ( name :string ) = > void } ) {
2026-10-28 17:30:35 +08:00
const t = useTheme ( ) , uri = usePrivateImage ( api , imagePath ( user . avatar ) ) ; return < ScrollView contentContainerStyle = { s . page } > < Pressable accessibilityRole = "button" accessibilityLabel = "个人资料" onPress = { ( ) = > open ( 'profile' ) } > < View style = { [ s . row , { paddingVertical :12 , gap :16 } ] } > < Avatar name = { user . nickName || user . userName } uri = { uri } size = { 48 } / > < View style = { { flex :1 , gap :7 } } > < Text style = { { color :t.text , fontSize :18 , fontWeight : '700' } } > { user . nickName || user . userName } < / Text > < Text style = { { color :t.muted , fontSize :13 } } > 账 号 { user . userName } < / Text > < Text style = { { color :t.muted , fontSize :12 } } > { user . dept ? . deptName || '个人工作空间' } < / Text > < / View > < Icon name = "chevron-forward" color = { t . muted } / > < / View > < / Pressable >
2026-10-18 08:49:30 +08:00
< Card > < MenuItem title = "个人资料" detail = "昵称、手机号码和邮箱" icon = "person-outline" onPress = { ( ) = > open ( 'profile' ) } / > < MenuItem title = "账号与密码" detail = "管理当前账号的登录密码" icon = "lock-closed-outline" onPress = { ( ) = > open ( 'password' ) } last / > < / Card >
2026-10-20 13:39:44 +08:00
< Card > < MenuItem title = "消息中心" detail = "查看站内消息、通知公告和未读提醒" icon = "notifications-outline" onPress = { ( ) = > open ( 'messages' ) } last / > < / Card >
2026-10-18 08:49:30 +08:00
< Card > < MenuItem title = "问题与建议" detail = "发现问题、提出需求,支持添加图片" icon = "chatbubble-ellipses-outline" onPress = { ( ) = > open ( 'feedback' ) } / > < MenuItem title = "我的反馈" detail = "查看处理进展与负责人回复" icon = "receipt-outline" onPress = { ( ) = > open ( 'feedback-list' ) } last / > < / Card >
< Card > < MenuItem title = "设置" detail = "外观、内网连接及应用更新" icon = "settings-outline" onPress = { ( ) = > open ( 'settings' ) } / > < MenuItem title = "关于应用" icon = "information-circle-outline" onPress = { ( ) = > open ( 'about' ) } last / > < / Card >
< Text style = { { color :t.muted , fontSize :11 , textAlign : 'center' , letterSpacing :2 , marginTop :8 } } > CMVR · 让 工 作 更 简 单 < / Text >
< / ScrollView > ;
}
2026-10-30 15:02:54 +08:00
export function Settings ( { environment , baseUrl , connection , versionUpdate , logout , about = false } : { environment :string ; baseUrl :string ; connection? :React.ReactNode ; versionUpdate? :React.ReactNode ; logout : ( ) = > void ; about? :boolean } ) {
2026-10-28 17:30:35 +08:00
const t = useTheme ( ) , { mode , setMode } = useContext ( AppearanceContext ) , reading = useContext ( ReadingContext ) ;
2026-10-18 08:49:30 +08:00
return < ScrollView contentContainerStyle = { s . page } > { about ? < > < View style = { { alignItems : 'center' , paddingVertical :24 , gap :12 } } > < View style = { { backgroundColor :t.accent , padding :20 , borderRadius :25 } } > < Icon name = "layers-outline" color = "#fff" size = { 43 } / > < / View > < Heading > CMVR 移 动 工 作 空 间 < / Heading > < Note > 把 业 务 带 到 现 场 , 让 协 作 更 轻 松 。 < / Note > < / View > < Card > < Note > 应 用 版 本 { Constants . expoConfig ? . version } < / Note > < Note > 当 前 环 境 { environment === 'prod' ? '正式环境' : '开发环境' } < / Note > < Note > 更 新 标 识 { Updates . updateId ? . slice ( 0 , 8 ) || '内置版本' } < / Note > < / Card > < Card > < Heading > 数 据 与 安 全 < / Heading > < Note > 业 务 数 据 来 自 公 司 内 网 服 务 。 可 见 数 据 和 可 用 操 作 由 后 台 权 限 决 定 ; AI 操 作 须 经 人 工 确 认 , 查 询 直 接 返 回 。 < / Note > < Note > 请 仅 在 可 信 的 公 司 网 络 使 用 。 不 要 在 反 馈 或 AI 对 话 中 填 写 密 码 和 授 权 码 。 < / Note > < / Card > < / > : < >
< Card > < Heading > 外 观 < / Heading > < Note > 选 择 适 合 你 的 阅 读 方 式 。 < / Note > < View style = { s . row } > { ( [ [ 'system' , '跟随系统' ] , [ 'light' , '浅色' ] , [ 'dark' , '深色' ] ] as const ) . map ( ( [ value , label ] ) = > < Button key = { value } title = { ` ${ mode === value ? '✓ ' : '' } ${ label } ` } secondary = { mode !== value } onPress = { ( ) = > setMode ( value ) } / > ) } < / View > < / Card >
2026-10-28 17:30:35 +08:00
< Card > < Heading > 文 字 大 小 < / Heading > < Note > 默 认 紧 凑 字 号 , 同 时 遵 循 手 机 系 统 字 体 设 置 。 < / Note > < View style = { s . row } > { ( [ [ 'compact' , '紧凑' ] , [ 'standard' , '标准' ] , [ 'large' , '大字' ] ] as const ) . map ( ( [ key , label ] ) = > < Button key = { key } title = { label } secondary = { reading . size !== key } onPress = { ( ) = > reading . setSize ( key ) } / > ) } < / View > < Text style = { { color :t.text } } > 物 品 名 称 · 库 存 数 量 128 个 < / Text > < / Card > { connection || < Card > < Heading > 内 网 连 接 < / Heading > < Note > 当 前 : { environment === 'prod' ? '正式环境' : '开发环境' } < / Note > < Text selectable style = { { color :t.text , lineHeight :22 } } > { baseUrl } < / Text > < Note > 正 式 服 务 由 安 装 包 固 定 , 避 免 误 切 环 境 。 < / Note > < / Card > }
2026-10-30 15:02:54 +08:00
{ versionUpdate }
2026-10-18 08:49:30 +08:00
< Button title = "退出登录" secondary onPress = { logout } / >
< / > } < / ScrollView > ;
}