recordDownload.vue 6.4 KB

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