upload.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. <template>
  2. <div>
  3. <el-upload
  4. v-if="isUpload"
  5. ref="my-upload"
  6. action="#"
  7. :auto-upload="false"
  8. :show-file-list="false"
  9. :on-change="handleChange"
  10. :drag="!$slots.default"
  11. multiple
  12. :accept="calcFileType"
  13. >
  14. <div style="width: 100%">
  15. <slot v-if="$slots.default" />
  16. <div class="upload-demo" style="width: 400px" v-else>
  17. <i class="el-icon-upload"></i>
  18. <div class="el-upload__text">
  19. {{ $t("将文件拖到此处,或") + " " }}<em>{{ $t("点击上传") }}</em>
  20. </div>
  21. </div>
  22. </div>
  23. </el-upload>
  24. <div
  25. class="progress_panel"
  26. v-for="file in filelist"
  27. :key="file.uid || file.attachmentid"
  28. >
  29. <img
  30. v-if="file.type === 'doc' || file.type === 'docx'"
  31. width="30"
  32. src="../../../assets/file_icons/word.png"
  33. alt=""
  34. />
  35. <img
  36. v-else-if="file.type === 'pdf'"
  37. width="30"
  38. src="../../../assets/file_icons/PDF.png"
  39. alt=""
  40. />
  41. <img
  42. v-else-if="file.type === 'mp4' || file.type === 'avi'"
  43. width="30"
  44. src="../../../assets/file_icons/video.png"
  45. alt=""
  46. />
  47. <img
  48. v-else-if="file.type === 'xls' || file.type === 'xlsx'"
  49. width="30"
  50. src="../../../assets/file_icons/excel.png"
  51. alt=""
  52. />
  53. <img
  54. v-else-if="file.type === 'ppt' || file.type === 'pptx'"
  55. width="30"
  56. src="../../../assets/file_icons/PPT.png"
  57. alt=""
  58. />
  59. <span
  60. v-else-if="
  61. ['jpg', 'png', 'jpeg', 'bmp', 'gif', 'webp', 'svg', 'tiff'].includes(
  62. file.type
  63. )
  64. "
  65. >
  66. <el-image
  67. v-if="file.url"
  68. style="width: 30px; height: 30px"
  69. :src="file.url"
  70. fit="fit"
  71. ></el-image>
  72. <img
  73. v-else
  74. width="30"
  75. src="../../../assets/file_icons/image.png"
  76. alt=""
  77. />
  78. </span>
  79. <img
  80. v-else
  81. width="30"
  82. src="../../../assets/file_icons/unknow.png"
  83. alt=""
  84. />
  85. <div>
  86. <div style="display: flex; margin-bottom: 6px; width: 90%">
  87. <p>{{ file.name || file.document || $t("未命名文件") }}</p>
  88. <span
  89. v-if="file.attachmentid"
  90. style="color: #67c23a; margin-left: 4px"
  91. >●</span
  92. >
  93. </div>
  94. <el-progress
  95. :percentage="file.progress"
  96. :show-text="false"
  97. ></el-progress>
  98. </div>
  99. <el-popconfirm
  100. :title="$t('是否确定删除') + '?'"
  101. @confirm="deleteFile(file)"
  102. v-if="isDelete && file.attachmentid"
  103. >
  104. <el-button slot="reference" type="text" class="icon iconfont"
  105. >&#xe688;</el-button
  106. >
  107. </el-popconfirm>
  108. </div>
  109. </div>
  110. </template>
  111. <script>
  112. import { Message } from "element-ui";
  113. export default {
  114. name: "upload",
  115. data() {
  116. return {
  117. filelist: [],
  118. loading: false,
  119. linksids: [],
  120. };
  121. },
  122. watch: {
  123. loading(newCount) {
  124. this.$emit("oploadIng", newCount);
  125. },
  126. },
  127. props: {
  128. classType: {
  129. type: Array,
  130. default: () => {
  131. return [];
  132. },
  133. },
  134. ownertable: {
  135. type: String,
  136. default: "temporary",
  137. },
  138. ownerid: {
  139. type: [String, Number],
  140. default: (Date.now() / 99 / 45).toFixed(0),
  141. },
  142. usetype: {
  143. type: String,
  144. default: "default",
  145. },
  146. isDelete: {
  147. type: Boolean,
  148. default: true,
  149. },
  150. isUpload: {
  151. type: Boolean,
  152. default: true,
  153. },
  154. oploadIng: {
  155. type: Function,
  156. },
  157. },
  158. computed: {
  159. calcFileType() {
  160. if (this.classType.length == 0) return "";
  161. let obj = {
  162. image: ".jpg,.png,.jpg,.jpeg,.bmp,.gif,.webp,.svg,.tiff",
  163. video: ".mp4,.ogg,.webm",
  164. document: ".txt,.doc,.docx,.xls,.xlsx,.ppt,.pptx",
  165. pdf: ".pdf",
  166. };
  167. return this.classType.map((v) => (obj[v] ? obj[v] : v)).join(",");
  168. },
  169. },
  170. methods: {
  171. handleChange(file, filelist) {
  172. this.$set(this.filelist, this.filelist.length, file);
  173. this.loading = true;
  174. let filename = file.raw.name,
  175. filetype = file.name.substr(file.raw.name.lastIndexOf(".") + 1);
  176. this.$api
  177. .requested({
  178. classname: "system.attachment.huawei.OBS",
  179. method: "getFileName",
  180. content: {
  181. filename,
  182. filetype,
  183. parentid: JSON.parse(sessionStorage.getItem("folderid"))
  184. .appfolderid,
  185. },
  186. })
  187. .then((res) => {
  188. console.log("获取上传地址", res);
  189. if (res.code == 0) return this.failUpload(res, file);
  190. this.upoladFileToServer(
  191. res.data.uploadurl,
  192. file,
  193. filetype,
  194. res.data.serialfilename
  195. );
  196. });
  197. },
  198. // 上传到华为云
  199. async upoladFileToServer(url, file, ext, obsfilename) {
  200. let that = this,
  201. item = this.filelist.find((v) => v.uid === file.uid);
  202. console.log("itemitem",item)
  203. let config = {
  204. headers:
  205. ext === "pdf"
  206. ? { "Content-Type": "application/pdf" }
  207. : { "Content-Type": "application/octet-stream" },
  208. onUploadProgress: function (progressEvent) {
  209. let percent = (progressEvent.loaded / progressEvent.total) * 100;
  210. that.$set(item, "type", ext);
  211. that.$set(item, "progress", percent);
  212. },
  213. };
  214. const res = await this.$upload.hw_upload(url, file.raw, config);
  215. if (res.code == 0) return this.failUpload(res, file);
  216. console.log("上传到华为云", res);
  217. this.createFileRecord(obsfilename, file);
  218. },
  219. // 上传成功以后生成附件记录
  220. async createFileRecord(obsfilename, file) {
  221. this.$api
  222. .requested({
  223. classname: "system.attachment.huawei.OBS",
  224. method: "uploadSuccess",
  225. content: {
  226. serialfilename: obsfilename,
  227. ownertable: this.ownertable,
  228. ownerid: this.ownerid,
  229. usetype: this.usetype,
  230. },
  231. })
  232. .then((res) => {
  233. console.log("res", res, file);
  234. if (res.code == 0) return this.failUpload(res, file);
  235. let data = JSON.parse(res.attinfos).data,
  236. index = this.filelist.findIndex((v) => v.uid == file.uid),
  237. attachmentid = res.data.attachmentids[0],
  238. item = data.find((v) => v.attachmentid == attachmentid);
  239. console.log(item);
  240. console.log("this.filelist",this.filelist)
  241. delete file.row;
  242. this.$set(
  243. this.filelist,
  244. index,
  245. Object.assign(this.filelist[index], item)
  246. );
  247. console.log("this.filelist",this.filelist)
  248. this.loading = this.filelist.some((v) => v.row);
  249. this.filelist = this.filelist;
  250. });
  251. },
  252. failUpload(res, file) {
  253. console.log("失败",res)
  254. Message.error(file.name + res.msg);
  255. this.filelist = this.filelist.filter((v) => v.uid !== file.uid);
  256. this.loading = this.filelist.some((v) => v.row);
  257. },
  258. deleteFile(e) {
  259. if (e.ownertable == "temporary") {
  260. this.$api
  261. .requested({
  262. classname: "system.attachment.Attachment",
  263. method: "deleteFileLink",
  264. content: {
  265. linksids: [e.linksid],
  266. },
  267. })
  268. .then((res) => {
  269. console.log("删除临时文件", res);
  270. this.filelist = this.filelist.filter((v) => v.uid !== e.uid);
  271. this.loading = this.filelist.some((v) => v.row);
  272. });
  273. } else {
  274. this.filelist = this.filelist.filter((v) => v.uid !== e.uid);
  275. this.loading = this.filelist.some((v) => v.row);
  276. this.linksids.push(e.linksid);
  277. }
  278. },
  279. rebinding(ownertable, ownerid, usetype = "default") {
  280. return new Promise((resolve, reject) => {
  281. if (this.filelist.length == 0) return resolve(true);
  282. this.$api
  283. .requested({
  284. classname: "system.attachment.Attachment",
  285. method: "createFileLink",
  286. content: {
  287. ownertable,
  288. ownerid,
  289. usetype,
  290. attachmentids: this.filelist.map((v) => v.attachmentid),
  291. },
  292. })
  293. .then((res) => {
  294. resolve(res.code == 1);
  295. console.log("改绑", res);
  296. });
  297. });
  298. },
  299. deleteAllTemporaryFiles(list = []) {
  300. return new Promise((resolve, reject) => {
  301. let linksids = list
  302. ? list
  303. : this.filelist
  304. .filter((v) => v.ownertable == "temporary")
  305. .map((v) => v.linksid);
  306. if (linksids.length == 0) return resolve(true);
  307. this.$api
  308. .requested({
  309. classname: "system.attachment.Attachment",
  310. method: "deleteFileLink",
  311. content: {
  312. linksids,
  313. },
  314. })
  315. .then((res) => {
  316. resolve(res.code == 1);
  317. this.filelist = this.filelist.filter(
  318. (v) => v.ownertable != "temporary"
  319. );
  320. console.log("删除所有临时文件", res);
  321. });
  322. });
  323. },
  324. },
  325. };
  326. </script>
  327. <style>
  328. .upload-demo > div {
  329. width: 100% !important;
  330. }
  331. .upload-demo .el-upload-dragger {
  332. width: 100% !important;
  333. }
  334. </style>
  335. <style scoped>
  336. .progress_panel {
  337. position: relative;
  338. display: flex;
  339. align-items: center;
  340. padding: 10px;
  341. margin: 10px 0;
  342. border-radius: 5px;
  343. transition: linear 0.2s all;
  344. overflow: hidden;
  345. }
  346. .progress_panel:hover {
  347. box-shadow: 0px 0px 5px #ccc;
  348. }
  349. .progress_panel > div {
  350. flex: 1;
  351. padding: 0 10px;
  352. }
  353. .progress_panel > div > p {
  354. line-height: 30px;
  355. }
  356. .upload .el-button {
  357. width: 100%;
  358. }
  359. .progress_panel .icon {
  360. position: absolute;
  361. color: #999;
  362. top: 0;
  363. right: 0;
  364. font-size: 14px;
  365. padding: 4px 8px;
  366. opacity: 0;
  367. user-select: none;
  368. }
  369. .progress_panel .icon:hover {
  370. color: #333;
  371. }
  372. .progress_panel:hover .icon {
  373. opacity: 1;
  374. }
  375. </style>