index.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <template>
  2. <div>
  3. <avue-crud :option="option" :data="data" id="out-table" ref="crud" @selection-change="selectionChange">
  4. <template slot="menuLeft">
  5. <div style="display: flex">
  6. <el-upload
  7. :headers="headers"
  8. action="/api/blade-resource/oss/endpoint/put-file"
  9. :on-success="onSuccess"
  10. :on-error="onError"
  11. :before-upload="beforeUpload"
  12. >
  13. <el-button icon="el-icon-upload" type="primary" size="mini" :disabled="disabled">点击上传</el-button>
  14. </el-upload>
  15. <!-- <el-button type="success" size="mini" :disabled="disabled" @click.stop="submit">一键保存</el-button> -->
  16. </div>
  17. </template>
  18. <template slot="fileUrl" slot-scope="{ row, index }">
  19. <span v-if="row.$cellEdit" @click.stop="rowPick(row, index)">
  20. <el-upload
  21. :headers="headers"
  22. action="/api/blade-resource/oss/endpoint/put-file"
  23. :on-success="rowonSuccess"
  24. :on-error="onError"
  25. :before-upload="beforeUpload"
  26. >
  27. <el-input placeholder="请选择上传文件" v-model="row.fileUrl" readonly size="small"></el-input>
  28. </el-upload>
  29. </span>
  30. <span v-else style="color: #1e9fff; cursor: pointer" @click="download(row, index)">
  31. {{ row.fileUrl }}
  32. </span>
  33. </template>
  34. <template slot="menu" slot-scope="{ row, index }">
  35. <el-button size="small" type="text" icon="el-icon-edit" :disabled="disabled" @click="rowEdit(row)">{{
  36. row.$cellEdit ? "保存" : "编辑"
  37. }}</el-button>
  38. <el-button size="small" type="text" icon="el-icon-delete" @click="rowDel(row, index)" :disabled="disabled"
  39. >删 除
  40. </el-button>
  41. </template>
  42. </avue-crud>
  43. <PreviewImage :visible.sync="innerVisible" :currentIndex="0" :previewImgList="[imgUrl]" />
  44. </div>
  45. </template>
  46. <script>
  47. // @ts-nocheck
  48. import { getToken } from "@/util/auth";
  49. import { updateListRemove, submitList } from "@/api/uploadFile/upload-file";
  50. import PreviewImage from "@/components/PreviewImage/index.vue";
  51. export default {
  52. name: "detailsPage",
  53. components: { PreviewImage },
  54. data() {
  55. return {
  56. innerVisible: false,
  57. imgUrl: "",
  58. rowIndex: null,
  59. headers: { "Blade-Auth": "Bearer " + getToken() },
  60. option: {
  61. maxHeight: 340,
  62. calcHeight: 30,
  63. menuWidth: 120,
  64. searchMenuSpan: 18,
  65. tip: false,
  66. border: true,
  67. index: true,
  68. addBtn: false,
  69. viewBtn: false,
  70. editBtn: false,
  71. delBtn: false,
  72. refreshBtn: false,
  73. selection: true,
  74. align: "center",
  75. menu: true,
  76. columnBtn: false,
  77. column: [
  78. {
  79. label: "原始文件名",
  80. prop: "fileName",
  81. cell: true,
  82. overHidden: true,
  83. },
  84. {
  85. label: "文件地址",
  86. prop: "fileUrl",
  87. // cell: true,
  88. overHidden: true,
  89. },
  90. {
  91. label: "文件类型",
  92. prop: "fileType",
  93. // cell: true,
  94. overHidden: true,
  95. },
  96. {
  97. label: "备注",
  98. prop: "remarks",
  99. cell: true,
  100. width: 150,
  101. overHidden: true,
  102. },
  103. ],
  104. },
  105. };
  106. },
  107. props: {
  108. data: {
  109. type: Array,
  110. default: function () {
  111. return [];
  112. },
  113. },
  114. linkKey: {
  115. type: String,
  116. default: "link",
  117. },
  118. delUrl: {
  119. type: String,
  120. default: "/gubersail-admin/bladeclaimattachment/remove",
  121. },
  122. disabled: {
  123. type: Boolean,
  124. default: false,
  125. },
  126. isImage: {
  127. type: Boolean,
  128. default: false,
  129. },
  130. },
  131. created() {},
  132. methods: {
  133. /**
  134. * @param {any} list
  135. */
  136. selectionChange(list) {
  137. this.selectionList = list;
  138. },
  139. submit() {
  140. if (this.data.length == 0) {
  141. return this.$message.error("数据不能为空");
  142. }
  143. const loading = this.$loading({
  144. lock: true,
  145. text: "加载中",
  146. spinner: "el-icon-loading",
  147. background: "rgba(255,255,255,0.7)",
  148. });
  149. submitList(this.data)
  150. .then((res) => {
  151. this.$emit("upDate", res.data.data);
  152. })
  153. .finally(() => {
  154. loading.close();
  155. });
  156. },
  157. /**
  158. * @param {{ id: any; }} row
  159. * @param {any} index
  160. */
  161. rowEdit(row, index) {
  162. // @ts-ignore
  163. if (row.$cellEdit == true) {
  164. this.$set(row, "$cellEdit", false);
  165. } else {
  166. this.$set(row, "$cellEdit", true);
  167. }
  168. },
  169. // /**
  170. // * @param {{ id: any; }} row
  171. // * @param {any} index
  172. // */
  173. download(row) {
  174. if (row.fileUrl) {
  175. const imageExtensions = [".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp"];
  176. const fileExt = row.fileUrl.substring(row.fileUrl.lastIndexOf(".")).toLowerCase();
  177. if (imageExtensions.includes(fileExt)) {
  178. this.innerVisible = true;
  179. this.imgUrl = row.fileUrl;
  180. } else {
  181. window.open(row.fileUrl);
  182. }
  183. } else {
  184. this.$message.error("请上传附件");
  185. }
  186. },
  187. /**
  188. * @param {{ id: any; }} row
  189. * @param {any} index
  190. */
  191. rowDel(row, index) {
  192. this.$confirm("确定将选择数据删除?", {
  193. confirmButtonText: "确定",
  194. cancelButtonText: "取消",
  195. type: "warning",
  196. }).then(() => {
  197. if (row.id) {
  198. updateListRemove(row.id, this.delUrl).then((res) => {
  199. if (res.data.success) {
  200. this.$message.success("操作成功!");
  201. this.data.splice(index, 1);
  202. }
  203. });
  204. } else {
  205. this.data.splice(index, 1);
  206. }
  207. });
  208. },
  209. /**
  210. * @param {string | any[]} filename
  211. */
  212. getFileExtension(filename) {
  213. return filename.slice(((filename.lastIndexOf(".") - 1) >>> 0) + 2);
  214. },
  215. /**
  216. * @param {any} res
  217. * @param {any} file
  218. * @param {any} fileList
  219. */
  220. onSuccess(res, file, fileList) {
  221. this.data.push({
  222. pid: this.pId ? this.pId : null,
  223. fileName: res.data.originalName,
  224. fileUrl: res.data[this.linkKey],
  225. fileType: this.getFileExtension(res.data.originalName),
  226. $cellEdit: true,
  227. });
  228. },
  229. rowonSuccess(res, file, fileList) {
  230. this.data[this.rowIndex].fileName = res.data.originalName;
  231. this.data[this.rowIndex].fileUrl = res.data[this.linkKey];
  232. this.data[this.rowIndex].fileType = this.getFileExtension(res.data.originalName);
  233. },
  234. rowPick(row, index) {
  235. console.log(row, index);
  236. this.rowIndex = index;
  237. },
  238. /**
  239. * @param {any} err
  240. * @param {any} file
  241. * @param {any} fileList
  242. */
  243. onError(err, file, fileList) {
  244. console.log(err);
  245. this.$message.error(err);
  246. },
  247. beforeUpload(file) {
  248. if (this.isImage) {
  249. const isImage = file.type.includes("image/");
  250. if (!isImage) {
  251. this.$message.error("只能上传图片文件!");
  252. return false;
  253. }
  254. return isImage;
  255. }
  256. },
  257. },
  258. };
  259. </script>
  260. <style lang="scss" scoped>
  261. ::v-deep .avue-crud .el-table .cell {
  262. padding: 0 2px 0 0 !important;
  263. .el-form-item {
  264. margin-bottom: 0px !important;
  265. }
  266. }
  267. ::v-deep .avue-crud .el-table .el-form-item__label {
  268. left: -1px;
  269. }
  270. ::v-deep .el-table--small td,
  271. .el-table--small th {
  272. padding: 2px !important;
  273. }
  274. ::v-deep .el-upload-list {
  275. display: none;
  276. }
  277. ::v-deep .el-upload {
  278. width: 100%;
  279. }
  280. </style>