index.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. import { useModel, useRequest } from 'umi'
  2. import { Delete } from '@icon-park/react'
  3. import ProForm, { ProFormDependency, ProFormSwitch, ProFormCheckbox } from '@ant-design/pro-form'
  4. import type { FormInstance } from 'antd'
  5. import { message, Table, Tabs, Radio, Space, Popconfirm } from 'antd'
  6. import React, { useRef, useMemo, useState, useEffect } from 'react'
  7. import {
  8. fetchRoleStaffListByRoleId,
  9. updateRolePermission,
  10. getRolePermissions,
  11. updatePermDataByRoleId,
  12. deleteStaff
  13. } from '@/services/user/api'
  14. import type { ColumnsType } from 'antd/lib/table'
  15. import { formatPermission } from '@/utils/utils'
  16. import RoleMenu from '../System/components/RoleMenu'
  17. import ConnectModal from '../System/components/ConnectModal'
  18. import FormItem from 'antd/lib/form/FormItem'
  19. import { permData } from '../Customer'
  20. import { PieChartOutlined } from '@ant-design/icons'
  21. const Statistic = () => {
  22. const { TabPane } = Tabs
  23. const formRef = useRef<FormInstance>(null)
  24. const formRef2 = useRef<FormInstance>(null)
  25. const { initialState } = useModel('@@initialState')
  26. const menuId = useMemo(() => {
  27. return initialState?.menuList?.find(item => item.name === '统计')?.id
  28. }, initialState.menuList)
  29. const [state, setState] = useState({
  30. id: '',
  31. roleStaff: [],
  32. rolePermission: {},
  33. dataPermission: {},
  34. activeKey: ''
  35. })
  36. const onSelect = (id: string) => {
  37. setState({ ...state, id })
  38. }
  39. const { run: tryGetRoleStaffList } = useRequest(
  40. (id: string) => fetchRoleStaffListByRoleId({ id }),
  41. {
  42. manual: true,
  43. onSuccess: result => {
  44. setState({ ...state, roleStaff: result })
  45. }
  46. }
  47. )
  48. const { run: tryGetRolePermissions } = useRequest((id: string) => getRolePermissions({ id }), {
  49. manual: true,
  50. onSuccess: (result: API.GetRolePermissionResultModel) => {
  51. const values = {
  52. client: [],
  53. company: [],
  54. business: [],
  55. ...formatPermission('init', result.permission)
  56. }
  57. if (
  58. values['showSoftware-usage'] ||
  59. values['showInvoice-aggregate'] ||
  60. values['showInvoice-trends'] ||
  61. values['showInvoice-receivables']
  62. ) {
  63. values.showProduct = true
  64. delete values['showSoftware-usage']
  65. delete values['showInvoice-aggregate']
  66. delete values['showInvoice-trends']
  67. delete values['showInvoice-receivables']
  68. }
  69. if (values['showCustomer-additions']) {
  70. values.showCustomer = true
  71. delete values['showCustomer-additions']
  72. }
  73. setState({
  74. ...state,
  75. rolePermission: values,
  76. dataPermission: result.dataPermission ? JSON.parse(result.dataPermission) : null
  77. })
  78. formRef.current?.setFieldsValue({ ...values })
  79. formRef2.current?.setFieldsValue(
  80. result.dataPermission ? JSON.parse(result.dataPermission) : null
  81. )
  82. }
  83. })
  84. const { run: tryDeleteStaff } = useRequest(
  85. (params: API.DeleteStaff) => {
  86. return deleteStaff(params)
  87. },
  88. {
  89. manual: true,
  90. onSuccess: () => {
  91. message.success('移除员工成功')
  92. tryGetRoleStaffList(state.id)
  93. }
  94. }
  95. )
  96. useEffect(() => {
  97. if (state.id) {
  98. tryGetRoleStaffList(state.id)
  99. tryGetRolePermissions(state.id)
  100. }
  101. if (state.activeKey === '2') {
  102. formRef.current?.setFieldsValue({ ...state.rolePermission })
  103. }
  104. if (state.activeKey === '3') {
  105. formRef2.current?.setFieldsValue({ ...state.dataPermission })
  106. }
  107. }, [state.id, state.activeKey])
  108. const columns: ColumnsType<API.RoleStaffListItem> = [
  109. {
  110. title: '用户',
  111. dataIndex: 'username',
  112. width: '15%'
  113. },
  114. {
  115. title: '手机',
  116. dataIndex: 'phone',
  117. width: '20%'
  118. },
  119. {
  120. title: '部门',
  121. dataIndex: 'departmentName',
  122. width: '20%'
  123. },
  124. {
  125. title: '岗位',
  126. dataIndex: 'position',
  127. width: '15%'
  128. },
  129. {
  130. title: '角色',
  131. dataIndex: 'age',
  132. width: '20%'
  133. },
  134. {
  135. title: '操作',
  136. dataIndex: 'opreate',
  137. width: '20%',
  138. render: (_, record) => (
  139. <Popconfirm
  140. title="确认删除吗?"
  141. okText="确认"
  142. cancelText="取消"
  143. onConfirm={() => tryDeleteStaff({ id: state.id, staffId: record.staffId })}>
  144. <div className="pl-2 text-hex-fd3995 cursor-pointer hover:text-hex-e7026e">
  145. <Delete />
  146. </div>
  147. </Popconfirm>
  148. )
  149. }
  150. ]
  151. return (
  152. <div className="h-full w-full flex flex-row">
  153. <RoleMenu menuId={menuId} onSelect={onSelect} itemCount={state.roleStaff?.length || 0} />
  154. <div className="w-max-3/4">
  155. <div className="ml-8 bg-white p-4 shadow-md shadow-hex-3e2c5a relative">
  156. <div className="absolute right-4 top-4 z-100">
  157. {state.id && (
  158. <ConnectModal
  159. title="关联员工"
  160. dataId={state.id}
  161. postUrl="/role/staff/add"
  162. closeAfterSelect={false}
  163. onReload={() => tryGetRoleStaffList(state.id)}
  164. />
  165. )}
  166. </div>
  167. <Tabs
  168. defaultActiveKey="1"
  169. type="card"
  170. onChange={key => setState({ ...state, activeKey: key })}>
  171. <TabPane tab="员工列表" key="1">
  172. <Table<API.RoleStaffListItem>
  173. dataSource={state.roleStaff}
  174. columns={columns}
  175. rowKey={row => row.staffId}
  176. />
  177. </TabPane>
  178. <TabPane tab="角色权限" key="2">
  179. <div className="ml-4">
  180. {state.id && (
  181. <ProForm
  182. formRef={formRef}
  183. layout="horizontal"
  184. onFinish={async values => {
  185. const newValues = formatPermission('submit', values)
  186. await updateRolePermission({
  187. permission: JSON.stringify(newValues),
  188. id: state.id
  189. })
  190. message.success('设置成功')
  191. }}>
  192. <ProFormSwitch
  193. fieldProps={{
  194. onChange(checked) {
  195. if (!checked) {
  196. formRef.current?.setFieldsValue({
  197. 'software-usage': [],
  198. 'invoice-aggregate': [],
  199. 'invoice-trends': [],
  200. 'customer-additions': [],
  201. 'invoice-receivables': []
  202. })
  203. }
  204. }
  205. }}
  206. name="showProduct"
  207. label={
  208. <span className="flex items-center">
  209. <PieChartOutlined theme="outline" className="flex items-baseline mr-1" />
  210. 产品销售
  211. </span>
  212. }
  213. />
  214. <div className="ml-25px">
  215. <ProFormDependency name={['showProduct']}>
  216. {({ showProduct }) => (
  217. <>
  218. <ProFormCheckbox.Group
  219. name="software-usage"
  220. label="软件锁用量"
  221. options={[{ value: 'access', label: '查看', disabled: !showProduct }]}
  222. />
  223. <ProFormCheckbox.Group
  224. name="invoice-aggregate"
  225. label="发票收款入账汇总"
  226. options={[{ value: 'access', label: '查看', disabled: !showProduct }]}
  227. />
  228. <ProFormCheckbox.Group
  229. name="invoice-trends"
  230. label="发票收款入账趋势"
  231. options={[{ value: 'access', label: '查看', disabled: !showProduct }]}
  232. />
  233. <ProFormCheckbox.Group
  234. name="invoice-receivables"
  235. label="应收款统计"
  236. options={[{ value: 'access', label: '查看', disabled: !showProduct }]}
  237. />
  238. </>
  239. )}
  240. </ProFormDependency>
  241. </div>
  242. <ProFormSwitch
  243. fieldProps={{
  244. onChange(checked) {
  245. if (!checked) {
  246. formRef.current?.setFieldsValue({
  247. 'customer-additions': []
  248. })
  249. }
  250. }
  251. }}
  252. name="showCustomer"
  253. label={
  254. <span className="flex items-center">
  255. <PieChartOutlined theme="outline" className="flex items-baseline mr-1" />
  256. 客户数据
  257. </span>
  258. }
  259. />
  260. <div className="ml-25px">
  261. <ProFormDependency name={['showCustomer']}>
  262. {({ showCustomer }) => (
  263. <ProFormCheckbox.Group
  264. name="customer-additions"
  265. label="客户新增"
  266. options={[{ value: 'access', label: '查看', disabled: !showCustomer }]}
  267. />
  268. )}
  269. </ProFormDependency>
  270. </div>
  271. </ProForm>
  272. )}
  273. </div>
  274. </TabPane>
  275. <TabPane tab="数据权限" key="3">
  276. {state.id && (
  277. <ProForm
  278. formRef={formRef2}
  279. layout="vertical"
  280. onFinish={async values => {
  281. try {
  282. await updatePermDataByRoleId({
  283. id: state.id,
  284. dataPermission: JSON.stringify(values)
  285. })
  286. } catch (error) {
  287. return message.error(error.toString())
  288. }
  289. message.success('设置成功')
  290. return true
  291. }}>
  292. <FormItem name="access" label="产品销售模块可见" required>
  293. <Radio.Group>
  294. <Space direction="vertical">
  295. {permData.map(item => (
  296. <Radio key={item.value} value={item.value}>
  297. {item.label}
  298. </Radio>
  299. ))}
  300. </Space>
  301. </Radio.Group>
  302. </FormItem>
  303. </ProForm>
  304. )}
  305. </TabPane>
  306. </Tabs>
  307. </div>
  308. </div>
  309. </div>
  310. )
  311. }
  312. export default Statistic