index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <template>
  2. <div ref="myImportProgress" :class="[ 'importProgress',position,'el-notification']" :style="{'top':top,'z-index':zIndex}" v-if="visible">
  3. <div class="icon">
  4. <img class="img" src="@/assets/rar.png" alt="">
  5. </div>
  6. <div class="line"></div>
  7. <div class="message">
  8. <div class="name">{{ file.name }}</div>
  9. <div class="progress">
  10. <div class="progress_title">{{recordMessage}}</div>
  11. <div>
  12. <el-progress style="width:100%" :percentage="percentage"></el-progress>
  13. </div>
  14. </div>
  15. <div class="btn">
  16. <template v-if="state==1">
  17. <el-button size="mini" v-if="!isStop" @click="stop">暂停</el-button>
  18. <el-button size="mini" v-else @click="keepOn">继续</el-button>
  19. </template>
  20. <el-button v-if="state<3" size="mini" @click="cancel">取消</el-button>
  21. </div>
  22. </div>
  23. <div class="close">
  24. <i class="el-icon-close" @click="del"></i>
  25. </div>
  26. </div>
  27. </template>
  28. <script>
  29. import { getFileMD5, createFileChunk } from "@/utils/file";
  30. import router from '@/router'
  31. export default {
  32. components: {},
  33. props: {},
  34. data() {
  35. return {
  36. duration:0,
  37. form:null,
  38. position:'right',
  39. visible:false,
  40. percentage:0,
  41. top:'16px',
  42. zIndex:'2200',
  43. md5:null,
  44. isStop:false,
  45. file:{},
  46. recordMessage:'',
  47. len:0,
  48. currentIndex:0,
  49. resumePromise:null,
  50. state:0
  51. };
  52. },
  53. watch: {
  54. visible(val){
  55. if(val){
  56. this.getStyle()
  57. this.init()
  58. if(this.duration != 0){
  59. this.timedShutdown()
  60. }
  61. }
  62. },
  63. percentage(val){
  64. if(val == 100){
  65. this.del()
  66. }
  67. },
  68. },
  69. computed: {},
  70. created() {},
  71. mounted() {},
  72. beforeDestroy() {
  73. this.isPaused = true;
  74. this.resumePromise = null;
  75. },
  76. methods: {
  77. init(){
  78. console.log(this.form)
  79. if(!this.form){
  80. return
  81. }
  82. if(!this.form.file){
  83. return
  84. }
  85. this.file = {
  86. name:this.form.file.name
  87. }
  88. this.upload()
  89. },
  90. upload(){
  91. // this.record('等待读取文件MD5')
  92. this.recordMessage = '等待读取文件MD5'
  93. let fileChunks = createFileChunk(this.form.file)
  94. // console.log(fileChunks)
  95. // return
  96. // fileChunks = [1,2,3,4,5]
  97. this.len = fileChunks.length + 3
  98. getFileMD5(this.form.file, async (md5) => {
  99. this.record('文件MD5:' + md5)
  100. var time = new Date().getTime()
  101. const start = await this.$api.startChunks({md5: md5, fileName: this.form.file.name,sourceId:this.$constants.sourceId,time:time})
  102. if(!start.data){
  103. return
  104. }
  105. this.state = 1
  106. await this.uploadChunks(fileChunks, md5, 0,time)
  107. this.state = 2
  108. // this.record('等待服务器合并分片')
  109. this.recordMessage = '等待服务器合并分片'
  110. const response = await this.$api.mergeChunks({ md5: md5, fileName: this.form.file.name,time:time })
  111. if(response.code != 200){
  112. return
  113. }
  114. this.state = 3
  115. this.record('分片合并完成,等待更新专利说明书')
  116. let params = {
  117. fileGuid: response.data[0],
  118. pdfType: this.form.type,
  119. remark: this.form.remark
  120. }
  121. await this.$api.batchUploadPatentInstruction(params)
  122. this.record('开始更新专利说明书')
  123. this.getConfirm()
  124. this.del()
  125. })
  126. },
  127. //弹窗
  128. getConfirm() {
  129. const h = this.$createElement;
  130. this.$msgbox({
  131. title: "提示",
  132. message: h("p", null, [
  133. h("span", null, "任务正在导入,查看任务进度请前往"),
  134. h(
  135. "span",
  136. {
  137. class: "MessageBoxClass",
  138. on: {
  139. click: () => {
  140. this.toTaskList();
  141. // 通过close关闭消息弹窗,this.$confirm就是this.$confirm.close(false),其他同理
  142. this.$msgbox.close(false);
  143. },
  144. },
  145. },
  146. "任务清单"
  147. ),
  148. ]),
  149. type: "warning",
  150. showConfirmButton: false,
  151. }).then((action) => {});
  152. },
  153. //跳转到任务清单
  154. toTaskList() {
  155. // 传专题库id是查到当前专题库的导入任务,不传是查看所有导入任务
  156. router.push({
  157. path: "/taskList",
  158. // query:{
  159. // importToId:this.form.importToId
  160. // }
  161. });
  162. },
  163. record(status, ) {
  164. this.recordMessage = status
  165. this.currentIndex += 1
  166. this.percentage =parseInt( this.currentIndex / this.len * 100)
  167. },
  168. async uploadChunks(fileChunks, md5, index=0,time) {
  169. for(var i = index;i<fileChunks.length;i++){
  170. await new Promise((resolve,reject) => {
  171. let formData = new FormData()
  172. formData.append('file', fileChunks[i].file)
  173. formData.append('md5', md5)
  174. formData.append('index', i)
  175. formData.append('time', time)
  176. formData.append('sourceId', this.$constants.sourceId)
  177. // setTimeout(()=>{
  178. // const status = `将分片上传至服务器(${i + 1}/${fileChunks.length})`
  179. // this.record(status)
  180. // resolve()
  181. // },5000)
  182. this.$api.uploadChunks(formData).then(async (response) => {
  183. const status = `将分片上传至服务器(${i + 1}/${fileChunks.length})`
  184. this.record(status)
  185. resolve()
  186. })
  187. });
  188. if (this.isStop) {
  189. // 如果循环被暂停,则等待直到被恢复
  190. await new Promise(resolve => {
  191. this.resumePromise = resolve;
  192. });
  193. }
  194. }
  195. return
  196. return new Promise((resolve, reject) => {
  197. if (index >= fileChunks.length) {
  198. resolve(true)
  199. }
  200. let formData = new FormData()
  201. formData.append('file', fileChunks[index].file)
  202. formData.append('md5', md5)
  203. formData.append('index', index)
  204. this.$api.uploadChunks(formData).then(async (response) => {
  205. const status = `将分片上传至服务器(${index + 1}/${fileChunks.length})`
  206. this.record(status)
  207. resolve(this.uploadChunks(fileChunks, md5, index + 1))
  208. })
  209. })
  210. },
  211. getStyle(){
  212. var doms = document.getElementsByClassName('el-notification')
  213. var len = doms.length
  214. if(len == 0){
  215. this.top = '16px'
  216. this.zIndex = '2200'
  217. }else{
  218. var dom = doms[len - 1]
  219. var top = dom.offsetTop
  220. var height = dom.offsetHeight
  221. var zIndex = dom.style.zIndex
  222. this.top = top + height + 16 + 'px'
  223. this.zIndex = Number(zIndex) + 10
  224. }
  225. },
  226. del(){
  227. var current = this.$refs.myImportProgress
  228. var offsetTop = current.offsetTop
  229. var height = current.offsetHeight
  230. this.visible = false
  231. var doms = document.getElementsByClassName('el-notification')
  232. for(var i = 0;i<doms.length;i++){
  233. if(doms[i].offsetTop <= offsetTop){
  234. continue
  235. }
  236. doms[i].style.top = doms[i].offsetTop - height - 16 + 'px'
  237. doms[i].style.zIndex = Number(doms[i].style.zIndex) - 10
  238. }
  239. this.state = 0
  240. },
  241. //暂停
  242. stop(){
  243. this.isStop = true
  244. },
  245. //继续
  246. keepOn(){
  247. this.isStop = false
  248. this.resumePromise()
  249. this.resumePromise = null;
  250. },
  251. //取消
  252. cancel(){
  253. this.stop()
  254. //调用取消接口
  255. if(this.state>1){
  256. }
  257. this.del()
  258. },
  259. timedShutdown(){
  260. setTimeout(function() {
  261. this.del()
  262. }, this.duration);
  263. },
  264. },
  265. };
  266. </script>
  267. <style lang="scss" scoped>
  268. .importProgress{
  269. padding: 0 10px;
  270. width: 330px;
  271. height: auto;
  272. border-radius: 8px;
  273. border: 1px solid #ebeef5;
  274. position: fixed;
  275. background-color: #fff;
  276. box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
  277. transition: opacity .3s,transform .3s,left .3s,right .3s,top .4s,bottom .3s;
  278. overflow: hidden;
  279. display: flex;
  280. // align-items: center;
  281. .close{
  282. padding-top: 10px;
  283. width: 20px;
  284. height: 100%;
  285. }
  286. .icon{
  287. padding-top: 10px;
  288. width: 50px;
  289. .img{
  290. width: 30px;
  291. height: 30px;
  292. margin-left: 10px;
  293. }
  294. }
  295. .line{
  296. width: 1px;
  297. height: auto;
  298. background: #d8d8d8;
  299. }
  300. .message{
  301. width: 260px;
  302. padding: 10px ;
  303. .name{
  304. font-size: 14px;
  305. width: 100%;
  306. overflow: hidden;
  307. white-space: nowrap;
  308. text-overflow:ellipsis;
  309. }
  310. .progress_title{
  311. color: rgb(123 126 130);
  312. font-size: 12px;
  313. margin-top: 6px;
  314. min-height: 0;
  315. overflow: hidden;
  316. text-decoration: none;
  317. text-overflow: ellipsis;
  318. white-space: nowrap;
  319. }
  320. }
  321. .btn{
  322. text-align: left;
  323. }
  324. }
  325. .right{
  326. right: 16px;
  327. }
  328. </style>