recordDownload.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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-button icon="el-icon-download" v-if="downloadFile" size="mini" title="点击下载" @click="downloadFileClientEvent()">点击下载</el-button>
  11. </el-col>
  12. </el-row>
  13. </el-dialog>
  14. </div>
  15. </template>
  16. <script>
  17. import moment from "moment";
  18. export default {
  19. name: 'recordDownload',
  20. created() {
  21. window.addEventListener('beforeunload', this.stopDownloadRecord)
  22. },
  23. data() {
  24. return {
  25. title: "四倍速下载中...",
  26. deviceId: "",
  27. channelId: "",
  28. app: "",
  29. stream: "",
  30. mediaServerId: "",
  31. showDialog: false,
  32. scale: 1,
  33. percentage: 0.00,
  34. streamInfo: null,
  35. taskId: null,
  36. getProgressRun: false,
  37. getProgressForFileRun: false,
  38. timer: null,
  39. downloadFile: null,
  40. };
  41. },
  42. methods: {
  43. openDialog: function (deviceId, channelId, app, stream, mediaServerId) {
  44. this.deviceId = deviceId;
  45. this.channelId = channelId;
  46. this.app = app;
  47. this.stream = stream;
  48. this.mediaServerId = mediaServerId;
  49. this.showDialog = true;
  50. this.getProgressRun = true;
  51. this.percentage = 0.0;
  52. this.getProgressTimer()
  53. },
  54. getProgressTimer: function (){
  55. if (!this.getProgressRun) {
  56. return;
  57. }
  58. if (this.percentage == 100 ) {
  59. this.getFileDownload();
  60. return;
  61. }
  62. setTimeout( ()=>{
  63. if (!this.showDialog) return;
  64. this.getProgress(this.getProgressTimer())
  65. }, 5000)
  66. },
  67. getProgress: function (callback){
  68. this.$axios({
  69. method: 'get',
  70. url: `/api/gb_record/download/progress/${this.deviceId}/${this.channelId}/${this.stream}`
  71. }).then((res)=> {
  72. console.log(res)
  73. if (res.data.code === 0) {
  74. this.streamInfo = res.data.data;
  75. if (parseFloat(res.data.progress) == 1) {
  76. this.percentage = 100;
  77. }else {
  78. this.percentage = (parseFloat(res.data.data.progress)*100).toFixed(1);
  79. }
  80. if (callback)callback();
  81. }else {
  82. this.$message({
  83. showClose: true,
  84. message: res.data.msg,
  85. type: "error",
  86. });
  87. this.close();
  88. }
  89. }).catch((e) =>{
  90. console.log(e)
  91. });
  92. },
  93. close: function (){
  94. if (this.streamInfo.progress < 1) {
  95. this.stopDownloadRecord();
  96. }
  97. if (this.timer !== null) {
  98. window.clearTimeout(this.timer);
  99. this.timer = null;
  100. }
  101. this.showDialog=false;
  102. this.getProgressRun = false;
  103. this.getProgressForFileRun = false;
  104. },
  105. gbScale: function (scale){
  106. this.scale = scale;
  107. },
  108. download: function (){
  109. this.getProgressRun = false;
  110. if (this.streamInfo != null ) {
  111. if (this.streamInfo.progress < 1) {
  112. // 发送停止缓存
  113. this.stopDownloadRecord((res)=>{
  114. this.getFileDownload()
  115. })
  116. }else {
  117. this.getFileDownload()
  118. }
  119. }
  120. },
  121. stopDownloadRecord: function (callback) {
  122. this.$axios({
  123. method: 'get',
  124. url: '/api/gb_record/download/stop/' + this.deviceId + "/" + this.channelId+ "/" + this.stream
  125. }).then((res)=> {
  126. if (callback) callback(res)
  127. });
  128. },
  129. getFileDownload: function (){
  130. this.$axios({
  131. method: 'get',
  132. url:`/record_proxy/${this.mediaServerId}/api/record/file/download/task/add`,
  133. params: {
  134. app: this.app,
  135. stream: this.stream,
  136. startTime: null,
  137. endTime: null,
  138. }
  139. }).then((res) =>{
  140. if (res.data.code === 0 ) {
  141. // 查询进度
  142. this.title = "录像文件处理中..."
  143. this.taskId = res.data.data;
  144. this.percentage = 0.0;
  145. this.getProgressForFileRun = true;
  146. this.getProgressForFileTimer();
  147. }
  148. }).catch(function (error) {
  149. console.log(error);
  150. });
  151. },
  152. getProgressForFileTimer: function (){
  153. if (!this.getProgressForFileRun || this.percentage == 100) {
  154. return;
  155. }
  156. setTimeout( ()=>{
  157. if (!this.showDialog) return;
  158. this.getProgressForFile(this.getProgressForFileTimer)
  159. }, 1000)
  160. },
  161. getProgressForFile: function (callback){
  162. this.$axios({
  163. method: 'get',
  164. url:`/record_proxy/${this.mediaServerId}/api/record/file/download/task/list`,
  165. params: {
  166. app: this.app,
  167. stream: this.stream,
  168. taskId: this.taskId,
  169. isEnd: true,
  170. }
  171. }).then((res) => {
  172. console.log(res)
  173. if (res.data.code === 0) {
  174. if (res.data.data.length === 0){
  175. this.percentage = 0
  176. // 往往在多次请求后(实验五分钟的视频是三次请求),才会返回数据,第一次请求通常是返回空数组
  177. if (callback)callback()
  178. return
  179. }
  180. // res.data.data应是数组类型
  181. this.percentage = parseFloat(res.data.data[0].percentage)*100
  182. if (res.data.data[0].percentage === '1') {
  183. this.getProgressForFileRun = false;
  184. this.downloadFile = res.data.data[0].downloadFile
  185. this.title = "文件处理完成,点击按扭下载"
  186. // window.open(res.data.data[0].downloadFile)
  187. }else {
  188. if (callback)callback()
  189. }
  190. }
  191. }).catch(function (error) {
  192. console.log(error);
  193. });
  194. },
  195. downloadFileClientEvent: function (){
  196. window.open(this.downloadFile )
  197. }
  198. },
  199. destroyed() {
  200. window.removeEventListener('beforeunload', this.stopDownloadRecord)
  201. }
  202. };
  203. </script>
  204. <style>
  205. </style>