index.tsx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. import { PageContainer } from '@ant-design/pro-layout';
  2. import type { ActionType, ProColumns } from '@jetlinks/pro-table';
  3. import ProTable from '@jetlinks/pro-table';
  4. import { useRef, useState } from 'react';
  5. import { useIntl, useHistory } from 'umi';
  6. import { BadgeStatus, PermissionButton, AIcon } from '@/components';
  7. import SearchComponent from '@/components/SearchComponent';
  8. import {
  9. DeleteOutlined,
  10. EditOutlined,
  11. ExclamationCircleOutlined,
  12. PlayCircleOutlined,
  13. PlusOutlined,
  14. StopOutlined,
  15. } from '@ant-design/icons';
  16. import { StatusColorEnum } from '@/components/BadgeStatus';
  17. import SaveModal from './save';
  18. import PasswordModal from './password';
  19. import Service from './service';
  20. import { message } from 'antd';
  21. import { getMenuPathByCode } from '@/utils/menu';
  22. export const service = new Service('api-client');
  23. export default () => {
  24. const actionRef = useRef<ActionType>();
  25. const intl = useIntl();
  26. const history = useHistory();
  27. const [param, setParam] = useState({});
  28. const [saveType, setSaveType] = useState<'save' | 'edit'>('save');
  29. const [saveVisible, setSaveVisible] = useState(false);
  30. const [passwordVisible, setPasswordVisible] = useState(false);
  31. const [editData, setEditData] = useState<any | undefined>(undefined);
  32. const { permission } = PermissionButton.usePermission('system/Platforms');
  33. const deleteById = async (id: string) => {
  34. const resp: any = await service.remove(id);
  35. if (resp.status === 200) {
  36. message.success('操作成功');
  37. actionRef.current?.reload();
  38. }
  39. };
  40. const columns: ProColumns<any>[] = [
  41. {
  42. dataIndex: 'name',
  43. title: '名称',
  44. },
  45. {
  46. dataIndex: 'username',
  47. title: '用户名',
  48. },
  49. // {
  50. // dataIndex: 'roleIdList',
  51. // title: '角色',
  52. // renderText: (record => {
  53. // console.log(record);
  54. // return ''
  55. // })
  56. // },
  57. {
  58. dataIndex: 'state',
  59. title: intl.formatMessage({
  60. id: 'pages.searchTable.titleStatus',
  61. defaultMessage: '状态',
  62. }),
  63. width: 160,
  64. valueType: 'select',
  65. renderText: (record) =>
  66. record ? (
  67. <BadgeStatus
  68. status={record.value}
  69. text={record.text}
  70. statusNames={{
  71. enabled: StatusColorEnum.processing,
  72. disabled: StatusColorEnum.error,
  73. }}
  74. />
  75. ) : (
  76. ''
  77. ),
  78. valueEnum: {
  79. disabled: {
  80. text: '禁用',
  81. status: 'disabled',
  82. },
  83. enabled: {
  84. text: '正常',
  85. status: 'enabled',
  86. },
  87. },
  88. },
  89. {
  90. dataIndex: 'description',
  91. title: intl.formatMessage({
  92. id: 'pages.system.description',
  93. defaultMessage: '说明',
  94. }),
  95. hideInSearch: true,
  96. },
  97. {
  98. title: intl.formatMessage({
  99. id: 'pages.data.option',
  100. defaultMessage: '操作',
  101. }),
  102. valueType: 'option',
  103. align: 'center',
  104. width: 200,
  105. render: (_, record: any) => [
  106. <PermissionButton
  107. key={'update'}
  108. type={'link'}
  109. style={{ padding: 0 }}
  110. isPermission={permission.update}
  111. tooltip={{
  112. title: intl.formatMessage({
  113. id: 'pages.data.option.edit',
  114. defaultMessage: '编辑',
  115. }),
  116. }}
  117. onClick={() => {
  118. setSaveType('edit');
  119. setSaveVisible(true);
  120. setEditData(record);
  121. }}
  122. >
  123. <EditOutlined />
  124. </PermissionButton>,
  125. <PermissionButton
  126. key={'empowerment'}
  127. type={'link'}
  128. style={{ padding: 0 }}
  129. isPermission={permission.empowerment}
  130. tooltip={{
  131. title: '赋权',
  132. }}
  133. onClick={() => {
  134. const url = getMenuPathByCode('system/Platforms/Api');
  135. history.push(`${url}?code=${record.id}`);
  136. }}
  137. >
  138. <AIcon type={'icon-fuquan'} />
  139. </PermissionButton>,
  140. <PermissionButton
  141. key={'api'}
  142. type={'link'}
  143. style={{ padding: 0 }}
  144. isPermission={true}
  145. tooltip={{
  146. title: '查看API',
  147. }}
  148. onClick={() => {
  149. const url = getMenuPathByCode('system/Platforms/View');
  150. history.push(`${url}?code=${record.id}`);
  151. }}
  152. >
  153. <AIcon type={'icon-chakanAPI'} />
  154. </PermissionButton>,
  155. <PermissionButton
  156. key={'password'}
  157. type={'link'}
  158. style={{ padding: 0 }}
  159. isPermission={permission.update}
  160. tooltip={{
  161. title: '重置密码',
  162. }}
  163. onClick={() => {
  164. setEditData({ id: record.userId });
  165. setPasswordVisible(true);
  166. }}
  167. >
  168. <AIcon type={'icon-zhongzhimima'} />
  169. </PermissionButton>,
  170. <PermissionButton
  171. key={'state'}
  172. type={'link'}
  173. style={{ padding: 0 }}
  174. popConfirm={{
  175. title: intl.formatMessage({
  176. id: `pages.data.option.${
  177. record.state.value !== 'disabled' ? 'disabled' : 'enabled'
  178. }.tips`,
  179. defaultMessage: '确认禁用?',
  180. }),
  181. onConfirm: () => {
  182. if (record.state.value !== 'disabled') {
  183. service.undeploy(record.id).then((resp: any) => {
  184. if (resp.status === 200) {
  185. message.success(
  186. intl.formatMessage({
  187. id: 'pages.data.option.success',
  188. defaultMessage: '操作成功!',
  189. }),
  190. );
  191. actionRef.current?.reload();
  192. }
  193. });
  194. } else {
  195. service.deploy(record.id).then((resp: any) => {
  196. if (resp.status === 200) {
  197. message.success(
  198. intl.formatMessage({
  199. id: 'pages.data.option.success',
  200. defaultMessage: '操作成功!',
  201. }),
  202. );
  203. actionRef.current?.reload();
  204. }
  205. });
  206. }
  207. },
  208. }}
  209. isPermission={permission.action}
  210. tooltip={{
  211. title: intl.formatMessage({
  212. id: `pages.data.option.${record.state.value !== 'disabled' ? 'disabled' : 'enabled'}`,
  213. defaultMessage: record.state.value !== 'disabled' ? '禁用' : '启用',
  214. }),
  215. }}
  216. >
  217. {record.state.value !== 'disabled' ? <StopOutlined /> : <PlayCircleOutlined />}
  218. </PermissionButton>,
  219. <PermissionButton
  220. key={'delete'}
  221. type={'link'}
  222. style={{ padding: 0 }}
  223. isPermission={permission.delete}
  224. disabled={record.state.value === 'enabled'}
  225. popConfirm={{
  226. title: '确认删除?',
  227. disabled: record.state.value === 'enabled',
  228. onConfirm: () => {
  229. deleteById(record.id);
  230. },
  231. }}
  232. tooltip={{
  233. title:
  234. record.state.value === 'enabled' ? <span>请先禁用,再删除</span> : <span>删除</span>,
  235. }}
  236. onClick={() => {}}
  237. >
  238. <DeleteOutlined />
  239. </PermissionButton>,
  240. ],
  241. },
  242. ];
  243. return (
  244. <PageContainer>
  245. <SearchComponent
  246. field={columns}
  247. onSearch={async (data) => {
  248. // 重置分页数据
  249. actionRef.current?.reset?.();
  250. setParam(data);
  251. }}
  252. />
  253. <ProTable
  254. rowKey="id"
  255. search={false}
  256. params={param}
  257. columns={columns}
  258. actionRef={actionRef}
  259. request={(params: any) =>
  260. service.query({
  261. ...params,
  262. sorts: [
  263. {
  264. name: 'createTime',
  265. order: 'desc',
  266. },
  267. ],
  268. })
  269. }
  270. headerTitle={[
  271. <PermissionButton
  272. key="button"
  273. type="primary"
  274. isPermission={permission.add}
  275. onClick={() => {
  276. setSaveType('save');
  277. setSaveVisible(true);
  278. }}
  279. icon={<PlusOutlined />}
  280. >
  281. {intl.formatMessage({
  282. id: 'pages.data.option.add',
  283. defaultMessage: '新增',
  284. })}
  285. </PermissionButton>,
  286. <div
  287. style={{
  288. paddingLeft: 24,
  289. background: '#fff',
  290. fontSize: 14,
  291. }}
  292. >
  293. <span style={{ marginRight: 8, fontSize: 16 }}>
  294. <ExclamationCircleOutlined />
  295. </span>
  296. 第三方平台用户是一个身份实体,代表您的组织中需要访问物联网平台资源的用户或应用程序。
  297. </div>,
  298. ]}
  299. />
  300. <SaveModal
  301. visible={saveVisible}
  302. data={editData}
  303. type={saveType}
  304. onCancel={() => {
  305. setSaveVisible(false);
  306. setEditData(undefined);
  307. }}
  308. onReload={() => {
  309. actionRef.current?.reload();
  310. }}
  311. />
  312. <PasswordModal
  313. visible={passwordVisible}
  314. onCancel={() => {
  315. setPasswordVisible(false);
  316. }}
  317. data={editData}
  318. />
  319. </PageContainer>
  320. );
  321. };