disk.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <template>
  2. <div
  3. :data-id="diskInfo && diskInfo.id"
  4. class="disk reverse flex flex-col h-full"
  5. >
  6. <header class="disk-header"></header>
  7. <main class="disk-main flex-auto"></main>
  8. <footer class="disk-footer h-50rpx"></footer>
  9. <v-overlay absolute :value="loading">
  10. <div class="flex flex-col justify-center items-center">
  11. <v-progress-circular indeterminate class="w-32rpx h-32rpx"></v-progress-circular>
  12. <div v-if="loadingLabel" class="label mt-2">{{ loadingLabel }}</div>
  13. </div>
  14. </v-overlay>
  15. </div>
  16. </template>
  17. <script>
  18. export default {
  19. name: 'Disk',
  20. props: {
  21. userCardId: {
  22. type: Number,
  23. default: null,
  24. },
  25. },
  26. data() {
  27. return {
  28. diskInfo: null,
  29. connectInfo: null,
  30. loading: false,
  31. loadingLabel: null,
  32. };
  33. },
  34. async fetch() {
  35. // await this.getDiskInfo();
  36. },
  37. watch: {
  38. userCardId: {
  39. async handler(userCardId) {
  40. if (userCardId) {
  41. try {
  42. this.loading = true;
  43. await this.getDiskInfo(userCardId);
  44. await this.connect(userCardId);
  45. } finally {
  46. this.loading = false;
  47. }
  48. }
  49. },
  50. immediate: true,
  51. },
  52. },
  53. methods: {
  54. // 查询云机
  55. async getDiskInfo(userCardId) {
  56. const res = await this.$axios.$get(
  57. '/resources/v5/client/disk/info/userCard/single',
  58. {
  59. params: {
  60. userCardId,
  61. },
  62. },
  63. );
  64. this.diskInfo = res.data;
  65. },
  66. /**
  67. * 轮询连接云机,因为超分需要挂载
  68. * @param {number} userCardId
  69. * @param {number} 超时
  70. */
  71. async connect(userCardId) {
  72. const getConnectData = () =>
  73. this.$axios.$post('/resources/user/cloud/connect', {
  74. userCardId,
  75. });
  76. const res = await getConnectData().catch((error) => {
  77. if (error.response.data.status === 5200) {
  78. // 云手机挂载中, 开启轮询
  79. return new Promise((resolve, reject) => {
  80. this.loadingLabel = '云手机挂载中...';
  81. const startTime = Date.now();
  82. const maxTime = 1000 * 20;
  83. this._watchConnectInterval = setInterval(async () => {
  84. try {
  85. if (Date.now() - startTime >= maxTime) {
  86. throw new Error('云手机挂载超时');
  87. }
  88. const res = await getConnectData().catch((error) => {
  89. if (error.response.data.status === 5200) {
  90. return false;
  91. }
  92. throw error;
  93. });
  94. if (res) {
  95. clearInterval(this._watchConnectInterval);
  96. resolve(res);
  97. }
  98. } catch (error) {
  99. clearInterval(this._watchConnectInterval);
  100. reject(error);
  101. }
  102. }, 1000);
  103. });
  104. }
  105. throw error;
  106. });
  107. this.connectInfo = res.data;
  108. return res;
  109. // const src = await new Promise((resolve, reject) => {
  110. // const startTime = Date.now();
  111. // this._watchConnectInterval = setInterval(async () => {
  112. // if (Date.now() - startTime >= time) {
  113. // clearInterval(this._watchConnectInterval);
  114. // reject(new Error('挂载超时'));
  115. // return;
  116. // }
  117. // await this.$axios
  118. // .$post('/resources/user/cloud/connect', {
  119. // userCardId,
  120. // })
  121. // .then();
  122. // }, interval);
  123. // });
  124. // return src;
  125. },
  126. },
  127. };
  128. </script>
  129. <style lang="scss" scoped>
  130. // .disk {
  131. // position: relative;
  132. // display: flex;
  133. // flex-direction: column;
  134. // height: 100%;
  135. // }
  136. // .disk-header {
  137. // position: relative;
  138. // height: 0;
  139. // flex: none;
  140. // }
  141. // .disk-main {
  142. // position: relative;
  143. // flex: 1;
  144. // }
  145. // .disk-footer {
  146. // height: 50px;
  147. // flex: none;
  148. // }
  149. </style>