recordDownload.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <template>
  2. <div id="recordDownload" >
  3. <el-dialog :title="title" v-if="showDialog" width="45rem" :append-to-body="true" :close-on-click-modal="false" :visible.sync="showDialog" :destroy-on-close="true" @close="close()" center>
  4. <el-row>
  5. <el-col :span="18" style="padding-top: 7px;">
  6. <el-progress :percentage="percentage"></el-progress>
  7. </el-col>
  8. <el-col :span="6" >
  9. <el-button icon="el-icon-download" v-if="downloadFile" size="mini" title="点击下载" @click="downloadFileClientEvent()">下载</el-button>
  10. </el-col>
  11. </el-row>
  12. </el-dialog>
  13. </div>
  14. </template>
  15. <script>
  16. import moment from "moment";
  17. export default {
  18. name: 'recordDownload',
  19. created() {
  20. window.addEventListener('beforeunload', this.stopDownloadRecord)
  21. },
  22. data() {
  23. return {
  24. title: "下载中...",
  25. deviceId: "",
  26. channelId: "",
  27. app: "",
  28. stream: "",
  29. mediaServerId: "",
  30. showDialog: false,
  31. scale: 1,
  32. percentage: 0.00,
  33. streamInfo: null,
  34. taskId: null,
  35. getProgressRun: false,
  36. timer: null,
  37. downloadFile: null,
  38. };
  39. },
  40. methods: {
  41. openDialog: function (deviceId, channelId, app, stream, mediaServerId) {
  42. this.deviceId = deviceId;
  43. this.channelId = channelId;
  44. this.app = app;
  45. this.stream = stream;
  46. this.mediaServerId = mediaServerId;
  47. this.showDialog = true;
  48. this.getProgressRun = true;
  49. this.percentage = 0.0;
  50. this.getProgressTimer()
  51. },
  52. getProgressTimer: function (){
  53. if (!this.getProgressRun) {
  54. return;
  55. }
  56. if (this.percentage == 100 ) {
  57. return;
  58. }
  59. setTimeout( ()=>{
  60. if (!this.showDialog) return;
  61. this.getProgress(this.getProgressTimer())
  62. }, 5000)
  63. },
  64. getProgress: function (callback){
  65. this.$axios({
  66. method: 'get',
  67. url: `/api/gb_record/download/progress/${this.deviceId}/${this.channelId}/${this.stream}`
  68. }).then((res)=> {
  69. if (res.data.code === 0) {
  70. this.streamInfo = res.data.data;
  71. if (parseFloat(res.data.progress) == 1) {
  72. this.percentage = 100;
  73. }else {
  74. this.percentage = (parseFloat(res.data.data.progress)*100).toFixed(1);
  75. }
  76. if (this.streamInfo.downLoadFilePath) {
  77. if (location.protocol === "https:") {
  78. this.downloadFile = this.streamInfo.downLoadFilePath.httpsPath;
  79. }else {
  80. this.downloadFile = this.streamInfo.downLoadFilePath.httpPath;
  81. }
  82. this.getProgressRun = false;
  83. this.downloadFileClientEvent()
  84. }
  85. if (callback)callback();
  86. }else {
  87. this.$message({
  88. showClose: true,
  89. message: res.data.msg,
  90. type: "error",
  91. });
  92. this.close();
  93. }
  94. }).catch((e) =>{
  95. console.log(e)
  96. });
  97. },
  98. close: function (){
  99. if (this.streamInfo.progress < 1) {
  100. this.stopDownloadRecord();
  101. }
  102. if (this.timer !== null) {
  103. window.clearTimeout(this.timer);
  104. this.timer = null;
  105. }
  106. this.showDialog=false;
  107. this.getProgressRun = false;
  108. },
  109. gbScale: function (scale){
  110. this.scale = scale;
  111. },
  112. stopDownloadRecord: function (callback) {
  113. this.$axios({
  114. method: 'get',
  115. url: '/api/gb_record/download/stop/' + this.deviceId + "/" + this.channelId+ "/" + this.stream
  116. }).then((res)=> {
  117. if (callback) callback(res)
  118. });
  119. },
  120. downloadFileClientEvent: function (){
  121. // window.open(this.downloadFile )
  122. let x = new XMLHttpRequest();
  123. x.open("GET", this.downloadFile, true);
  124. x.responseType = 'blob';
  125. x.onload=(e)=> {
  126. let url = window.URL.createObjectURL(x.response)
  127. let a = document.createElement('a');
  128. a.href = url
  129. a.download = this.deviceId + "-" + this.channelId + ".mp4";
  130. a.click()
  131. }
  132. x.send();
  133. }
  134. },
  135. destroyed() {
  136. window.removeEventListener('beforeunload', this.stopDownloadRecord)
  137. }
  138. };
  139. </script>
  140. <style>
  141. </style>