index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <template>
  2. <Modal
  3. width="800px"
  4. title="媒体库"
  5. :open="visible"
  6. @ok="okHandler"
  7. :afterClose="afterClose"
  8. @cancel="emit('update:visible', false)"
  9. okText="确定"
  10. cancelText="取消"
  11. class="model-table"
  12. >
  13. <div>
  14. <div className="model-header">
  15. <p class="header-desc">
  16. 已选择数据<span>( {{ rowSelection.selectedRowKeys.length }} )</span>
  17. </p>
  18. <div class="up-se">
  19. <span class="upload fun-ctrls" v-if="!readonly">
  20. <ui-input
  21. class="input"
  22. :accept="ft"
  23. :maxSize="maxSize"
  24. @update:modelValue="(file: File) => uploadHandler(file)"
  25. type="file"
  26. >
  27. <template v-slot:replace>
  28. <Button type="primary" ghost>
  29. <ui-icon type="add" class="icon" />上传文件
  30. </Button>
  31. </template>
  32. </ui-input>
  33. </span>
  34. <Search
  35. v-if="Object.keys(allData).length"
  36. className="content-header-search"
  37. placeholder="输入名称搜索"
  38. v-model:value="params.name"
  39. allow-clear
  40. style="width: 244px"
  41. />
  42. <Button
  43. type="primary"
  44. ghost
  45. v-if="useType === 'trace_evidence'"
  46. style="margin-left: 10px"
  47. @click="foreceRefresh"
  48. >
  49. 刷新
  50. </Button>
  51. </div>
  52. </div>
  53. <div class="table-layout">
  54. <Table
  55. v-if="Object.keys(allData).length"
  56. :row-key="(record: Material) => record.id"
  57. :columns="cloumns"
  58. :rowSelection="rowSelection"
  59. :data-source="origin.list"
  60. :pagination="{ ...origin, current: origin.pageNum }"
  61. @change="handleTableChange"
  62. >
  63. <template #bodyCell="{ column, record }">
  64. <template v-if="column.key === 'name'">
  65. <div class="name">
  66. <span>
  67. {{ record.name }}
  68. </span>
  69. </div>
  70. </template>
  71. <template v-if="column.key === 'caseTitle'">
  72. <div class="name">
  73. <span>
  74. {{ record.caseTitle || "-" }}
  75. </span>
  76. </div>
  77. </template>
  78. <template v-if="column.key === 'size'">
  79. {{ getSizeStr(record.size) }}
  80. </template>
  81. <template v-if="column.key === 'status'">
  82. {{
  83. record.status === 1
  84. ? "上传成功"
  85. : record.status === -1
  86. ? "上传失败"
  87. : "上传中"
  88. }}
  89. </template>
  90. <template v-if="column.key === 'group'">
  91. <span class="group-str">{{ record.group }}</span>
  92. </template>
  93. <template v-else-if="column.key === 'action'">
  94. <span v-if="record.useType !== 'animation'">
  95. <a @click="delHandler(record.id)">删除</a>
  96. </span>
  97. </template>
  98. </template>
  99. </Table>
  100. <div style="padding: 1px" v-else>
  101. <Empty
  102. description="暂无结果"
  103. :image="Empty.PRESENTED_IMAGE_SIMPLE"
  104. className="ant-empty ant-empty-normal"
  105. />
  106. </div>
  107. </div>
  108. </div>
  109. </Modal>
  110. </template>
  111. <script lang="ts" setup>
  112. import { Modal, Input, Table, Empty, TableProps, Button } from "ant-design-vue";
  113. import { computed, reactive, readonly, ref, watch } from "vue";
  114. import { createLoadPack, debounceStack, getSizeStr } from "@/utils";
  115. import type { PagingResult } from "@/api";
  116. import {
  117. addMaterial,
  118. delMaterial,
  119. fetchMaterialGroups,
  120. fetchMaterialPage,
  121. Material,
  122. MaterialGroup,
  123. MaterialPageProps,
  124. syncMaterialAll,
  125. } from "@/api/material";
  126. import Message from "bill/components/message/message.vue";
  127. import { Dialog } from "bill/expose-common";
  128. const props = defineProps<{
  129. uploadFormat?: string[];
  130. format?: string[];
  131. maxSize?: number;
  132. visible: boolean;
  133. count?: number;
  134. readonly?: boolean;
  135. groupIds: number[];
  136. useType?: string;
  137. afterClose?: () => void;
  138. }>();
  139. const emit = defineEmits<{
  140. (e: "update:visible", v: boolean): void;
  141. (e: "selectMaterials", v: Material[]): void;
  142. }>();
  143. const ft = computed(() => {
  144. const ft = props.uploadFormat || props.format;
  145. if (!ft) return void 0;
  146. return ft.map((t) => `.${t}`).join(",");
  147. });
  148. const Search = Input.Search;
  149. const params = reactive({
  150. pageNum: 1,
  151. pageSize: 10,
  152. formats: props.format,
  153. useType: props.useType,
  154. groupIds: props.groupIds,
  155. }) as MaterialPageProps;
  156. const origin = ref<PagingResult<Material[]>>({
  157. list: [],
  158. pageNum: 1,
  159. pageSize: 10,
  160. total: 0,
  161. });
  162. const groups = ref<MaterialGroup[]>([]);
  163. // const selectKeys = ref<Material["id"][]>([]);
  164. const allData: Record<Material["id"], Material> = reactive({});
  165. const rowSelection = ref({
  166. selectedRowKeys: [] as Material["id"][],
  167. onChange: (ids: number[]) => {
  168. console.log(ids);
  169. const otherPageKeys = rowSelection.value.selectedRowKeys.filter(
  170. (key) => !origin.value.list.some((item) => key === item.id)
  171. );
  172. const newKeys = Array.from(new Set([...otherPageKeys, ...ids]));
  173. if (typeof props.count !== "number" || props.count >= newKeys.length) {
  174. rowSelection.value.selectedRowKeys = newKeys;
  175. } else {
  176. Message.error(`最多选择${props.count}项`);
  177. }
  178. },
  179. hideSelectAll: true,
  180. onSelectAll: (selected: boolean, selectedRows: Material[], changeRows: Material[]) => {
  181. // 显式处理全选逻辑
  182. console.log("全选状态变化:", selected);
  183. },
  184. getCheckboxProps: (record: Material) => {
  185. return {
  186. disabled:
  187. (props.format && !props.format.includes(record.format)) ||
  188. (props.maxSize && record.size > props.maxSize) ||
  189. record.status !== 1,
  190. };
  191. },
  192. });
  193. const cloumns = computed(() => {
  194. const groupIds = params.groupIds;
  195. const cloumns =
  196. props.useType === "trace_evidence"
  197. ? [
  198. {
  199. title: "名称",
  200. dataIndex: "name",
  201. key: "name",
  202. },
  203. {
  204. width: "200px",
  205. title: "案件",
  206. dataIndex: "caseTitle",
  207. key: "caseTitle",
  208. },
  209. {
  210. width: "100px",
  211. title: "分组",
  212. dataIndex: "group",
  213. key: "group",
  214. filteredValue: groupIds,
  215. filters: props.readonly
  216. ? undefined
  217. : groups.value.map((g) => ({
  218. text: g.name,
  219. value: g.id,
  220. })),
  221. // filters: groups.value.map((g) => ({
  222. // text: g.name,
  223. // value: g.id,
  224. // })),
  225. },
  226. ]
  227. : [
  228. {
  229. title: "名称",
  230. dataIndex: "name",
  231. key: "name",
  232. },
  233. {
  234. width: "100px",
  235. title: "格式",
  236. dataIndex: "format",
  237. key: "format",
  238. },
  239. {
  240. width: "100px",
  241. title: "大小",
  242. dataIndex: "size",
  243. key: "size",
  244. },
  245. {
  246. width: "100px",
  247. title: "状态",
  248. dataIndex: "status",
  249. key: "status",
  250. },
  251. {
  252. width: "100px",
  253. title: "分组",
  254. dataIndex: "group",
  255. key: "group",
  256. filteredValue: groupIds,
  257. filters: props.readonly
  258. ? undefined
  259. : groups.value.map((g) => ({
  260. text: g.name,
  261. value: g.id,
  262. })),
  263. // filters: groups.value.map((g) => ({
  264. // text: g.name,
  265. // value: g.id,
  266. // })),
  267. },
  268. ];
  269. if (!props.readonly) {
  270. cloumns.push({
  271. width: "100px",
  272. title: "操作",
  273. key: "action",
  274. } as any);
  275. }
  276. return cloumns;
  277. });
  278. const fetchData = createLoadPack(() =>
  279. Promise.all([fetchMaterialGroups(props.useType), fetchMaterialPage(params)])
  280. );
  281. const refresh = debounceStack(
  282. fetchData,
  283. ([group, pag]) => {
  284. groups.value = group;
  285. origin.value = pag;
  286. pag.list.forEach((item) => (allData[item.id] = item));
  287. if (pag.pageNum > 1 && pag.pageNum > Math.ceil(pag.total / pag.pageSize)) {
  288. params.pageNum = Math.ceil(pag.total / pag.pageSize);
  289. refresh();
  290. }
  291. },
  292. 160
  293. );
  294. const foreceRefresh = async () => {
  295. await syncMaterialAll();
  296. await refresh();
  297. };
  298. const uploadHandler = async (file: File) => {
  299. await addMaterial(file);
  300. refresh();
  301. };
  302. watch(params, refresh, { immediate: true, deep: true });
  303. const addHandler = async (file: File) => {
  304. await addMaterial(file);
  305. refresh();
  306. };
  307. const delHandler = async (id: Material["id"]) => {
  308. if (await Dialog.confirm("确定要删除此数据吗?")) {
  309. await delMaterial(id);
  310. const ndx = rowSelection.value.selectedRowKeys.indexOf(id);
  311. console.log(rowSelection.value.selectedRowKeys, id);
  312. if (~ndx) {
  313. rowSelection.value.selectedRowKeys.splice(ndx, 1);
  314. }
  315. refresh();
  316. }
  317. };
  318. const okHandler = () => {
  319. emit(
  320. "selectMaterials",
  321. rowSelection.value.selectedRowKeys.map((id) => allData[id])
  322. );
  323. };
  324. const handleTableChange: TableProps["onChange"] = (pag, filters) => {
  325. params.pageSize = pag.pageSize!;
  326. params.pageNum = pag.current!;
  327. params.groupIds = (filters.group || props.groupIds) as number[];
  328. };
  329. </script>
  330. <style lang="less" scoped>
  331. .model-header {
  332. display: flex;
  333. justify-content: space-between;
  334. padding-bottom: 24px;
  335. align-items: center;
  336. }
  337. .table-layout {
  338. max-height: 500px;
  339. overflow-y: auto;
  340. }
  341. .up-se {
  342. display: flex;
  343. align-items: center;
  344. .upload {
  345. margin-right: 10px;
  346. width: 100px;
  347. // overflow: hidden;
  348. }
  349. }
  350. .group-str {
  351. display: block;
  352. overflow: hidden;
  353. text-overflow: ellipsis; //文本溢出显示省略号
  354. white-space: nowrap; //文本不会换行
  355. max-width: 200px;
  356. }
  357. </style>
  358. <style lang="less">
  359. .model-header .header-desc {
  360. margin-bottom: 0;
  361. }
  362. .ant-modal-root .ant-table-tbody > tr > td {
  363. word-break: break-all;
  364. }
  365. .content-header-search {
  366. flex: 1;
  367. }
  368. .ant-table-filter-dropdown .ant-dropdown-menu-title-content > span {
  369. max-width: 200px;
  370. overflow: hidden;
  371. display: inline-block;
  372. vertical-align: middle;
  373. text-overflow: ellipsis;
  374. white-space: nowrap;
  375. }
  376. .name {
  377. position: relative;
  378. display: flex;
  379. align-items: center;
  380. span {
  381. position: absolute;
  382. max-width: 100%;
  383. display: block;
  384. overflow: hidden;
  385. text-overflow: ellipsis; //文本溢出显示省略号
  386. white-space: nowrap; //文本不会换行
  387. }
  388. }
  389. </style>