PictureUpload.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. <template>
  2. <footer class="picture-upload-footer">
  3. <a-popover :popup-visible="showUpload" position="tl" @popup-visible-change="handlePopupChange">
  4. <div class="text-hide">
  5. <div class="upload-img-container" @click="handleUploadClick">
  6. <img src="@/assets/document/6.svg" class="img-normal" v-if="!showUpload" />
  7. <img src="@/assets/document/7.svg" class="img-hover" v-if="!showUpload" />
  8. <img src="@/assets/document/8.svg" class="img-active" v-if="showUpload" />
  9. </div>
  10. <p>{{ file_status_label_tips }}</p>
  11. </div>
  12. <template #content>
  13. <div :class="{ 'upload-wrap': true, 'upload-wrap-narrow': uploadFile }">
  14. <header>
  15. <p>{{ uploadFile ? "上传图片" : "上传图片" }}</p>
  16. <div @click="handleUploadClose">
  17. <icon-close size="large" />
  18. </div>
  19. </header>
  20. <div class="xiaoshuhuitu-image-upload">
  21. <section v-if="tableData.length === 0">
  22. <a-upload
  23. :draggable="true"
  24. list-type="picture"
  25. :multiple="false"
  26. :limit="1"
  27. :show-file-list="false"
  28. :custom-request="customRequest"
  29. :show-upload-button="{ showOnExceedLimit: false }"
  30. accept=".png,.jpg"
  31. @before-upload="onBeforeUpload"
  32. @exceed-limit="onExceedLimit"
  33. @before-remove="onBeforeRemove"
  34. >
  35. <template #upload-button>
  36. <div class="upload-main">
  37. <p>拖拽图片到这里,或<span>点此上传</span></p>
  38. <span>
  39. 支持上传 .png, .jpg,大小不超过10M
  40. </span>
  41. </div>
  42. </template>
  43. </a-upload>
  44. </section>
  45. <!-- <div v-if="tableData.length > 0">
  46. <p>
  47. <span>已选择图片列表</span>
  48. <span>共{{ tableData.length }}个</span>
  49. </p>
  50. <div>
  51. <a-table
  52. :pagination="false"
  53. row-key="name"
  54. :columns="columns"
  55. :data="tableData"
  56. v-model:selectedKeys="selectedKeys"
  57. >
  58. <template #methods="{ record }">
  59. <BtnImgDownload @download="downloadDocument(record)"/>
  60. <BtnImgDelete @delete="deleteDocument"/>
  61. </template>
  62. </a-table>
  63. </div>
  64. </div> -->
  65. <FileListTable
  66. v-if="tableData.length > 0"
  67. :tableData="tableData"
  68. :columns="columns"
  69. :selectedKeys="selectedKeys"
  70. @download="downloadDocument"
  71. @delete="deleteDocument"
  72. />
  73. </div>
  74. </div>
  75. <div class="bottom-buttons">
  76. <div>
  77. <a-button type="outline" style="margin-right: 10px" @click="handleUploadClose">取消</a-button>
  78. <a-button :loading="buttonLoading" type="primary" @click="selectFiles(true, 'paper')">
  79. 去{{ uploadFile ? '提问' : '提问' }} ({{ fileList.length}})
  80. </a-button>
  81. </div>
  82. </div>
  83. </template>
  84. </a-popover>
  85. <span class="placeholder-width-5px"></span>
  86. <BtnIframeComfyui />
  87. </footer>
  88. </template>
  89. <script>
  90. import dayjs from "dayjs";
  91. import { defineComponent, ref, inject, reactive, onBeforeUnmount, computed } from "vue";
  92. import { Message } from '@arco-design/web-vue';
  93. import BtnIframeComfyui from './btn-iframe-comfyui/BtnIframeComfyui.vue';
  94. import ImgEditBtn from './image-editor/ImgEditBtn.vue';
  95. import BtnImgDownload from './picture-upload/BtnImgDownload.vue';
  96. import BtnImgDelete from './picture-upload/BtnImgDelete.vue';
  97. import FileListTable from './picture-upload/FileListTable.vue';
  98. import {huituSetTempImageFile} from './dialog-wrap/temp-image'
  99. import {useImageUploadStatus} from '../store/modules/xs-huitu-image-upload/index';
  100. export default defineComponent({
  101. props: ["data", "uploadFile", "isQuestion", "agentId", "agengtName"],
  102. components: {
  103. BtnImgDownload,
  104. BtnImgDelete,
  105. FileListTable,
  106. BtnIframeComfyui,
  107. ImgEditBtn
  108. },
  109. setup(props) {
  110. const { helper, ajax } = inject("$global");
  111. const imageUploadStatus = useImageUploadStatus();
  112. const maxSize = 30 * 1024 * 1024;
  113. const showUpload = ref(false);
  114. let fileList = ref([]);
  115. const tableData = reactive([]);
  116. let formData = new FormData();
  117. let inputData = ref([]);
  118. const showDeleteTooltip = ref(false);
  119. const deleteId = ref("");
  120. const buttonLoading = ref(false);
  121. const file_status_label_tips = computed(() => {
  122. const can_upload = imageUploadStatus.canUploadContinue;
  123. if(!can_upload){
  124. // 不允许上传
  125. return `已保存 1 个图片`
  126. }else{
  127. return `已选择 ${fileList.value.length} 个图片`
  128. }
  129. });
  130. const handlePopupChange = () => {
  131. showDeleteTooltip.value = false;
  132. };
  133. const handleUploadClick = () => {
  134. // 检查上一次传图后有没有发布文字,如果没有发布文字,则不允许再次上传图片
  135. const can_upload = imageUploadStatus.canUploadContinue;
  136. if(can_upload){
  137. // console.log('允许继续上传图片', can_upload);
  138. }else{
  139. Message.warning('图片已保存,请输入文字进行提问');
  140. return;
  141. }
  142. showUpload.value = true;
  143. }
  144. const handleUploadClose = () => {
  145. showUpload.value = false;
  146. }
  147. const handleCancelDelete = () => {
  148. showDeleteTooltip.value = false;
  149. };
  150. const selectFiles = async (isSelect, type) => {
  151. if (isSelect) {
  152. if (props.uploadFile) {
  153. const data = await ajax.message.createDialog(props.agentId);
  154. fileList.value.forEach(file => {
  155. formData.append('file', file)
  156. });
  157. buttonLoading.value = true;
  158. const result = await ajax.file.uploadFile(props.agentId, data.chat_id, formData);
  159. showUpload.value = false;
  160. helper.dialogConfig = {
  161. isQuestion: props.isQuestion,
  162. selectedKeys: selectedKeys.value,
  163. type
  164. };
  165. buttonLoading.value = false;
  166. // if (result) {
  167. // helper.write('docId', result.doc_ids[0]);
  168. // }
  169. // 以下为判断是毕生还是regflow
  170. // TODO 判断是毕生还是regflow
  171. // 以下为毕生
  172. inputData.value.push({
  173. name: result.file_name,
  174. file_path: result.file_path,
  175. value: ""
  176. });
  177. const sendData = {
  178. chatHistory: [],
  179. chat_id: data.chat_id,
  180. agent_id: props.agentId,
  181. name: "小数绘图",
  182. description: "小数绘图",
  183. inputs: {
  184. report_name: inputData.value.map(item => item.name).join(', '),
  185. id: "Report-PRcYL",
  186. data: inputData.value
  187. }
  188. };
  189. if (sendData) {
  190. helper.write('sendData', sendData);
  191. }
  192. } else {
  193. // 图片上传的状态
  194. let temp_file = null;
  195. fileList.value.forEach(file => {
  196. formData.append('file', file)
  197. temp_file = file;
  198. });
  199. const data = await ajax.message.createDialog(props.agentId);
  200. const result = await ajax.file.uploadFile(props.agentId, data.chat_id, formData);
  201. /**
  202. * 临时保存file里的图片
  203. * 此图片用于在客户端进行展示
  204. * 当用户使用图生文功能时,需要上传一张图片,但是用户上传的照片目前(2024-11-27)无法通过服务器拿到在前端展示
  205. * 于是前端将此图片保存,临时在界面上展示
  206. * 此方案在用户刷新页面后就会失效
  207. */
  208. huituSetTempImageFile(temp_file, props.agentId, data.chat_id);
  209. imageUploadStatus.uploadFinish(); // 记录图片的数据
  210. // 保存小数绘图数据
  211. helper.write('sendData', {
  212. name: '小数绘图',
  213. agent_id: props.agentId,
  214. chat_id: props.chat_id,
  215. upload_file_id: result.id,
  216. mime_type: result.mime_type,
  217. name: result.name,
  218. created_by: result.created_by,
  219. });
  220. // 隐藏 popover
  221. showUpload.value = false;
  222. Message.info('图片已保存,请输入文字进行提问');
  223. // 文件清空
  224. tableData.length = 0;
  225. fileList.value = [];
  226. }
  227. }else{
  228. }
  229. };
  230. const handleDeleteButton = (id) => {
  231. showDeleteTooltip.value = true;
  232. deleteId.value = id;
  233. };
  234. const onBeforeUpload = (file) => {
  235. if (file.size > maxSize) {
  236. helper.message("error", `上传文件大小不能超过10M`);
  237. return false;
  238. }
  239. return true;
  240. };
  241. const onExceedLimit = () => {
  242. helper.message("error", `最多上传1个文件`);
  243. };
  244. const customRequest = async (option) => {
  245. const { onSuccess, fileItem } = option;
  246. const { file } = fileItem;
  247. fileList.value.push(file);
  248. tableData.push({
  249. key: file.uid,
  250. name: file.name,
  251. type: file.type,
  252. size: helper.formatSize(file.size),
  253. time: dayjs(file.lastModified).format("YYYY-MM-DD HH:mm:ss")
  254. });
  255. onSuccess();
  256. };
  257. const onBeforeRemove = (data) => {
  258. fileList.value = fileList.value.filter((item) => item.id !== data.uid);
  259. return true;
  260. };
  261. const selectedKeys = ref([]);
  262. const rowSelection = reactive({
  263. type: "checkbox",
  264. showCheckedAll: true,
  265. onlyCurrent: false
  266. });
  267. const columns = [
  268. {
  269. title: "文件名称",
  270. dataIndex: "name",
  271. ellipsis: true,
  272. tooltip: true,
  273. width: 150
  274. },
  275. {
  276. title: "类型",
  277. dataIndex: "type",
  278. ellipsis: true,
  279. tooltip: true,
  280. width: 150
  281. },
  282. {
  283. title: "大小",
  284. dataIndex: "size"
  285. },
  286. {
  287. title: "上传时间",
  288. dataIndex: "time",
  289. ellipsis: true,
  290. tooltip: true,
  291. width: 150
  292. },
  293. {
  294. title: "操作",
  295. slotName: "methods"
  296. }
  297. ];
  298. const downloadDocument = (record) => {
  299. console.log('下载图片');
  300. };
  301. const deleteDocument = () => {
  302. // const index = tableData.findIndex(item => item.key === deleteId.value);
  303. // tableData.splice(index, 1);
  304. // fileList.value = fileList.value.filter(item => item.uid !== deleteId.value);
  305. tableData.length = 0;
  306. fileList.value = [];
  307. };
  308. // 组件卸载
  309. onBeforeUnmount(() => {
  310. });
  311. return {
  312. agengtName: props.agengtName,
  313. buttonLoading,
  314. fileList,
  315. selectedKeys,
  316. getAssetURL: helper.getAssetURL,
  317. showUpload,
  318. onBeforeUpload,
  319. onExceedLimit,
  320. onBeforeRemove,
  321. customRequest,
  322. columns,
  323. tableData,
  324. rowSelection,
  325. downloadDocument,
  326. deleteDocument,
  327. showDeleteTooltip,
  328. deleteId,
  329. handleDeleteButton,
  330. selectFiles,
  331. // image_list
  332. handlePopupChange,
  333. handleCancelDelete,
  334. handleUploadClick,
  335. handleUploadClose,
  336. file_status_label_tips,
  337. };
  338. }
  339. });
  340. </script>
  341. <style lang="scss" scoped>
  342. .bottom-buttons {
  343. display: flex;
  344. justify-content: right;
  345. >div {
  346. display: flex;
  347. }
  348. }
  349. .delete-buttons {
  350. margin-top: 10px;
  351. display: flex;
  352. justify-content: center;
  353. }
  354. .method-button {
  355. display: inline-block;
  356. padding: 4px 8px;
  357. border-radius: 5px;
  358. cursor: pointer;
  359. &:hover {
  360. background: rgba(150, 171, 185, 0.2);
  361. }
  362. }
  363. /**宽度占位 */
  364. .placeholder-width-5px{
  365. display: inline-block;
  366. width: 10px;
  367. }
  368. .pictures-upload-info{
  369. display: flex;
  370. align-items: center;
  371. justify-content: flex-start;
  372. }
  373. .upload-main {
  374. width: 800px;
  375. height: 109px;
  376. background: url("@/assets/document/upload.svg");
  377. margin: 0 auto;
  378. padding: 24px 0px 24px 277px;
  379. background-size: 100%;
  380. >p {
  381. color: #000;
  382. font-weight: bold;
  383. margin-bottom: 5px;
  384. margin-top: 10px;
  385. >span {
  386. color: #00aea3;
  387. text-decoration: underline;
  388. }
  389. }
  390. >span {
  391. font-size: 13px;
  392. color: #7f7f7f;
  393. }
  394. }
  395. footer.picture-upload-footer{
  396. display: flex;
  397. justify-content: flex-start;
  398. align-items: center;
  399. .text-hide {
  400. // width: 110px;
  401. overflow: hidden;
  402. display: flex;
  403. align-items: center;
  404. }
  405. .upload-img-container{
  406. cursor: pointer;
  407. // width: 110px;
  408. height: 32px;
  409. .img-normal {
  410. display: block;
  411. }
  412. .img-hover {
  413. display: none;
  414. }
  415. &:hover {
  416. .img-normal {
  417. display: none;
  418. }
  419. .img-hover {
  420. display: block;
  421. }
  422. }
  423. >img {
  424. width: 110px;
  425. height: 32px;
  426. }
  427. }
  428. }
  429. main {
  430. height: 100%;
  431. display: flex;
  432. flex-direction: column;
  433. overflow: hidden;
  434. // >section {
  435. // overflow-y: auto;
  436. // display: flex;
  437. // flex-wrap: wrap;
  438. // gap: 20px;
  439. // &.section-full {
  440. // flex: 1;
  441. // }
  442. // >div {
  443. // width: 382px;
  444. // height: 120px;
  445. // padding: 20px 24px;
  446. // border-radius: 8px;
  447. // display: flex;
  448. // >img {
  449. // width: 38px;
  450. // height: 38px;
  451. // margin-right: 12px;
  452. // }
  453. // >div {
  454. // font-size: 18px;
  455. // font-weight: bold;
  456. // >span {
  457. // font-size: 15px;
  458. // font-weight: 400;
  459. // color: #797d7f;
  460. // margin-top: 8px;
  461. // overflow: hidden;
  462. // text-overflow: ellipsis;
  463. // display: -webkit-box;
  464. // -webkit-line-clamp: 2;
  465. // -webkit-box-orient: vertical;
  466. // line-height: 1.4;
  467. // }
  468. // }
  469. // }
  470. // }
  471. >footer {
  472. display: flex;
  473. justify-content: space-between;
  474. align-items: center;
  475. margin-top: auto;
  476. >div {
  477. display: inline-flex;
  478. align-items: center;
  479. gap: 8px;
  480. height: 32px;
  481. border-radius: 21px;
  482. border: 1px solid #e3e3e3;
  483. white-space: nowrap;
  484. overflow: hidden;
  485. transition-duration: 0.3s;
  486. width: 250px;
  487. /**
  488. &.text-hide {
  489. width: 110px;
  490. overflow: hidden;
  491. }
  492. */
  493. &.text-narrow {
  494. width: 100px;
  495. overflow: hidden;
  496. >div>img {
  497. width: 100px;
  498. }
  499. }
  500. >p {
  501. overflow: hidden;
  502. white-space: nowrap;
  503. }
  504. >div {
  505. cursor: pointer;
  506. width: 110px;
  507. height: 32px;
  508. .img-normal {
  509. display: block;
  510. }
  511. .img-hover {
  512. display: none;
  513. }
  514. &:hover {
  515. .img-normal {
  516. display: none;
  517. }
  518. .img-hover {
  519. display: block;
  520. }
  521. }
  522. >img {
  523. width: 110px;
  524. height: 32px;
  525. }
  526. }
  527. }
  528. >section {
  529. display: flex;
  530. align-items: center;
  531. background: rgba(150, 171, 185, 0.2);
  532. border-radius: 20px;
  533. padding: 5px 10px;
  534. >p {
  535. margin-right: 5px;
  536. }
  537. }
  538. }
  539. }
  540. .upload-wrap {
  541. width: 800px;
  542. height: 300px;
  543. display: flex;
  544. flex-direction: column;
  545. padding-bottom: 30px;
  546. &.upload-wrap-narrow {
  547. height: auto;
  548. }
  549. >header {
  550. display: flex;
  551. align-items: center;
  552. justify-content: space-between;
  553. margin-bottom: 13px;
  554. >p {
  555. font-size: 18px;
  556. font-weight: bold;
  557. }
  558. >div {
  559. width: 38px;
  560. height: 38px;
  561. border-radius: 6px;
  562. cursor: pointer;
  563. justify-content: center;
  564. align-items: center;
  565. display: flex;
  566. &:hover {
  567. background: rgba(0, 0, 0, 0.05);
  568. }
  569. }
  570. }
  571. >div {
  572. flex: 1;
  573. overflow-y: auto;
  574. >div {
  575. >p {
  576. margin: 20px 0;
  577. color: #7f7f7f;
  578. font-size: 13px;
  579. display: flex;
  580. justify-content: space-between;
  581. }
  582. }
  583. }
  584. }
  585. </style>