recordDownload.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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="percentage < 100" size="mini" title="点击下载可将以缓存部分下载到本地" @click="download()">停止缓存并下载</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. getProgressForFileRun: false,
  37. timer: 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. this.getFileDownload();
  58. return;
  59. }
  60. setTimeout( ()=>{
  61. if (!this.showDialog) return;
  62. this.getProgress(this.getProgressTimer())
  63. }, 5000)
  64. },
  65. getProgress: function (callback){
  66. this.$axios({
  67. method: 'get',
  68. url: `/api/gb_record/download/progress/${this.deviceId}/${this.channelId}/${this.stream}`
  69. }).then((res)=> {
  70. console.log(res)
  71. if (res.data.code === 0) {
  72. this.streamInfo = res.data.data;
  73. if (parseFloat(res.data.progress) == 1) {
  74. this.percentage = 100;
  75. }else {
  76. this.percentage = (parseFloat(res.data.data.progress)*100).toFixed(1);
  77. }
  78. if (callback)callback();
  79. }else {
  80. this.$message({
  81. showClose: true,
  82. message: res.data.msg,
  83. type: "error",
  84. });
  85. this.close();
  86. }
  87. }).catch((e) =>{
  88. console.log(e)
  89. });
  90. },
  91. close: function (){
  92. if (this.streamInfo.progress < 1) {
  93. this.stopDownloadRecord();
  94. }
  95. if (this.timer !== null) {
  96. window.clearTimeout(this.timer);
  97. this.timer = null;
  98. }
  99. this.showDialog=false;
  100. this.getProgressRun = false;
  101. this.getProgressForFileRun = false;
  102. },
  103. gbScale: function (scale){
  104. this.scale = scale;
  105. },
  106. download: function (){
  107. this.getProgressRun = false;
  108. if (this.streamInfo != null ) {
  109. if (this.streamInfo.progress < 1) {
  110. // 发送停止缓存
  111. this.stopDownloadRecord((res)=>{
  112. this.getFileDownload()
  113. })
  114. }else {
  115. this.getFileDownload()
  116. }
  117. }
  118. },
  119. stopDownloadRecord: function (callback) {
  120. this.$axios({
  121. method: 'get',
  122. url: '/api/gb_record/download/stop/' + this.deviceId + "/" + this.channelId+ "/" + this.stream
  123. }).then((res)=> {
  124. if (callback) callback(res)
  125. });
  126. },
  127. getFileDownload: function (){
  128. this.$axios({
  129. method: 'get',
  130. url:`/record_proxy/${this.mediaServerId}/api/record/file/download/task/add`,
  131. params: {
  132. app: this.app,
  133. stream: this.stream,
  134. startTime: null,
  135. endTime: null,
  136. }
  137. }).then((res) =>{
  138. if (res.data.code === 0 ) {
  139. // 查询进度
  140. this.title = "录像文件处理中..."
  141. this.taskId = res.data.data;
  142. this.percentage = 0.0;
  143. this.getProgressForFileRun = true;
  144. this.getProgressForFileTimer();
  145. }
  146. }).catch(function (error) {
  147. console.log(error);
  148. });
  149. },
  150. getProgressForFileTimer: function (){
  151. if (!this.getProgressForFileRun || this.percentage == 100) {
  152. return;
  153. }
  154. setTimeout( ()=>{
  155. if (!this.showDialog) return;
  156. this.getProgressForFile(this.getProgressForFileTimer)
  157. }, 1000)
  158. },
  159. getProgressForFile: function (callback){
  160. this.$axios({
  161. method: 'get',
  162. url:`/record_proxy/${this.mediaServerId}/api/record/file/download/task/list`,
  163. params: {
  164. app: this.app,
  165. stream: this.stream,
  166. taskId: this.taskId,
  167. isEnd: true,
  168. }
  169. }).then((res) => {
  170. console.log(res)
  171. if (res.data.code === 0) {
  172. if (res.data.data.length === 0){
  173. this.percentage = 0
  174. // 往往在多次请求后(实验五分钟的视频是三次请求),才会返回数据,第一次请求通常是返回空数组
  175. if (callback)callback()
  176. return
  177. }
  178. // res.data.data应是数组类型
  179. this.percentage = parseFloat(res.data.data[0].percentage)*100
  180. if (res.data.data[0].percentage === '1') {
  181. this.getProgressForFileRun = false;
  182. window.open(res.data.data[0].downloadFile)
  183. this.close();
  184. }else {
  185. if (callback)callback()
  186. }
  187. }
  188. }).catch(function (error) {
  189. console.log(error);
  190. });
  191. }
  192. },
  193. destroyed() {
  194. window.removeEventListener('beforeunload', this.stopDownloadRecord)
  195. }
  196. };
  197. </script>
  198. <style>
  199. </style>