index.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. import { PageContainer } from '@ant-design/pro-layout';
  2. import SearchComponent from '@/components/SearchComponent';
  3. import { useRef, useState } from 'react';
  4. import type { ActionType, ProColumns } from '@jetlinks/pro-table';
  5. import { PermissionButton, ProTableCard } from '@/components';
  6. import {
  7. CloseCircleOutlined,
  8. DeleteOutlined,
  9. EditOutlined,
  10. InfoCircleOutlined,
  11. PlayCircleOutlined,
  12. PlusOutlined,
  13. } from '@ant-design/icons';
  14. import { useIntl } from '@@/plugin-locale/localeExports';
  15. import { Badge, Space } from 'antd';
  16. import { DuerOSItem } from '@/pages/Northbound/DuerOS/types';
  17. import DuerOSCard from '@/components/ProTableCard/CardItems/duerOs';
  18. import { history } from '@@/core/history';
  19. import { getMenuPathByCode, getMenuPathByParams, MENUS_CODE } from '@/utils/menu';
  20. import Service from './service';
  21. import { onlyMessage } from '@/utils/util';
  22. export const service = new Service('dueros/product');
  23. export default () => {
  24. const actionRef = useRef<ActionType>();
  25. const intl = useIntl();
  26. const [searchParams, setSearchParams] = useState<any>({});
  27. const { permission } = PermissionButton.usePermission('Northbound/DuerOS');
  28. const Tools = (record: any, type: 'card' | 'table') => {
  29. return [
  30. <PermissionButton
  31. key={'update'}
  32. type={'link'}
  33. style={{ padding: 0 }}
  34. isPermission={permission.update}
  35. tooltip={
  36. type === 'table'
  37. ? {
  38. title: intl.formatMessage({
  39. id: 'pages.data.option.edit',
  40. defaultMessage: '编辑',
  41. }),
  42. }
  43. : undefined
  44. }
  45. onClick={() => {
  46. history.push(getMenuPathByParams(MENUS_CODE['Northbound/DuerOS/Detail'], record.id));
  47. }}
  48. >
  49. <EditOutlined />
  50. {type !== 'table' &&
  51. intl.formatMessage({
  52. id: 'pages.data.option.edit',
  53. defaultMessage: '编辑',
  54. })}
  55. </PermissionButton>,
  56. <PermissionButton
  57. style={{ padding: 0 }}
  58. isPermission={permission.action}
  59. type="link"
  60. key="changeState"
  61. popConfirm={{
  62. title: intl.formatMessage({
  63. id: `pages.data.option.${
  64. record.state?.value === 'enabled' ? 'disabled' : 'enabled'
  65. }.tips`,
  66. defaultMessage: `确认${record.state?.value === 'enabled' ? '禁用' : '启用'}?`,
  67. }),
  68. onConfirm: async () => {
  69. const map = {
  70. disabled: 'enable',
  71. enabled: 'disable',
  72. };
  73. const resp = await service.changeState(record.id, map[record.state?.value]);
  74. if (resp.status === 200) {
  75. onlyMessage(
  76. intl.formatMessage({
  77. id: 'pages.data.option.success',
  78. defaultMessage: '操作成功!',
  79. }),
  80. );
  81. } else {
  82. onlyMessage('操作失败!', 'error');
  83. }
  84. actionRef.current?.reload();
  85. },
  86. }}
  87. tooltip={{
  88. title: intl.formatMessage({
  89. id: `pages.data.option.${record.state?.value === 'enabled' ? 'disabled' : 'enabled'}`,
  90. defaultMessage: record.state?.value === 'enabled' ? '禁用' : '启用',
  91. }),
  92. }}
  93. >
  94. {record.state?.value === 'enabled' ? (
  95. <>
  96. {' '}
  97. <CloseCircleOutlined /> {type === 'card' && '禁用'}
  98. </>
  99. ) : (
  100. <>
  101. <PlayCircleOutlined /> {type === 'card' && '启用'}
  102. </>
  103. )}
  104. </PermissionButton>,
  105. <PermissionButton
  106. key={'delete'}
  107. type={'link'}
  108. style={{ padding: 0 }}
  109. isPermission={permission.delete}
  110. disabled={record.state?.value === 'enabled'}
  111. tooltip={{
  112. title: record.state?.value === 'disabled' ? '删除' : '请先禁用该数据,再删除。',
  113. }}
  114. popConfirm={{
  115. title: '确认删除?',
  116. onConfirm: async () => {
  117. await service.remove(record.id);
  118. onlyMessage('删除成功!');
  119. actionRef.current?.reload();
  120. },
  121. }}
  122. >
  123. <DeleteOutlined />
  124. </PermissionButton>,
  125. ];
  126. };
  127. const columns: ProColumns<DuerOSItem>[] = [
  128. {
  129. title: intl.formatMessage({
  130. id: 'pages.table.name',
  131. defaultMessage: '名称',
  132. }),
  133. dataIndex: 'name',
  134. fixed: 'left',
  135. ellipsis: true,
  136. },
  137. {
  138. title: intl.formatMessage({
  139. id: 'page.cloud.duerOS.productName',
  140. defaultMessage: '产品',
  141. }),
  142. dataIndex: 'productName',
  143. hideInSearch: true,
  144. ellipsis: true,
  145. valueType: 'select',
  146. request: async () => {
  147. const res = await service.getProduct();
  148. if (res.status === 200) {
  149. return res.result.map((pItem: any) => ({ label: pItem.name, value: pItem.id }));
  150. }
  151. return [];
  152. },
  153. },
  154. {
  155. title: intl.formatMessage({
  156. id: 'pages.cloud.duerOS.applianceType',
  157. defaultMessage: '设备类型',
  158. }),
  159. dataIndex: 'applianceType',
  160. renderText: (data) => data.text,
  161. valueType: 'select',
  162. request: async () => {
  163. const res = await service.getTypes();
  164. if (res.status === 200) {
  165. return res.result.map((pItem: any) => ({ label: pItem.name, value: pItem.id }));
  166. }
  167. return [];
  168. },
  169. },
  170. {
  171. title: '说明',
  172. dataIndex: 'description',
  173. hideInSearch: true,
  174. },
  175. {
  176. title: '状态',
  177. dataIndex: 'state',
  178. width: 90,
  179. renderText: (data) => {
  180. const map = {
  181. disabled: <Badge status="error" text="禁用" />,
  182. enabled: <Badge status="success" text="正常" />,
  183. };
  184. return map[data.value];
  185. },
  186. valueType: 'select',
  187. valueEnum: {
  188. disabled: {
  189. text: '禁用',
  190. status: 'disabled',
  191. },
  192. enabled: {
  193. text: '正常',
  194. status: 'enabled',
  195. },
  196. },
  197. },
  198. {
  199. title: intl.formatMessage({
  200. id: 'pages.data.option',
  201. defaultMessage: '操作',
  202. }),
  203. valueType: 'option',
  204. align: 'center',
  205. width: 200,
  206. fixed: 'right',
  207. render: (text, record) => Tools(record, 'table'),
  208. },
  209. ];
  210. return (
  211. <PageContainer>
  212. <SearchComponent<DuerOSItem>
  213. field={columns}
  214. target="northbound-dueros"
  215. onSearch={(data) => {
  216. actionRef.current?.reset?.();
  217. setSearchParams(data);
  218. }}
  219. />
  220. <div style={{ backgroundColor: 'white', width: '100%', height: 60, padding: 20 }}>
  221. <div
  222. style={{
  223. padding: 10,
  224. width: '100%',
  225. color: 'rgba(0, 0, 0, 0.55)',
  226. backgroundColor: '#f6f6f6',
  227. }}
  228. >
  229. <InfoCircleOutlined style={{ marginRight: 10 }} />
  230. 将平台产品通过API的方式同步DuerOS平台
  231. </div>
  232. </div>
  233. <ProTableCard<DuerOSItem>
  234. rowKey="id"
  235. search={false}
  236. columns={columns}
  237. actionRef={actionRef}
  238. params={searchParams}
  239. scroll={{ x: 1366 }}
  240. options={{ fullScreen: true }}
  241. request={(params) =>
  242. service.query({ ...params, sorts: [{ name: 'createTime', order: 'desc' }] })
  243. }
  244. cardRender={(record) => <DuerOSCard {...record} action={Tools(record, 'card')} />}
  245. headerTitle={
  246. <Space>
  247. <PermissionButton
  248. isPermission={permission.add}
  249. onClick={() => {
  250. history.push(getMenuPathByCode(MENUS_CODE['Northbound/DuerOS/Detail']));
  251. }}
  252. key="button"
  253. icon={<PlusOutlined />}
  254. type="primary"
  255. >
  256. {intl.formatMessage({
  257. id: 'pages.data.option.add',
  258. defaultMessage: '新增',
  259. })}
  260. </PermissionButton>
  261. </Space>
  262. }
  263. />
  264. {/*<Save/>*/}
  265. </PageContainer>
  266. );
  267. };