Api.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <template>
  2. <div>
  3. <q-dialog v-model="busyDialogVisible" no-route-dismiss no-esc-dismiss no-backdrop-dismiss>
  4. <div class="q-pa-lg bg-white column" style="width: 400px">
  5. <div style="font-weight: bold; font-size: 120%;">
  6. {{ mainMessage }}
  7. </div>
  8. <div v-show="jobMessage" class="q-mt-sm" style="width: 350px; white-space: nowrap; overflow: hidden">
  9. {{ jobMessage }}
  10. </div>
  11. <div v-show="jobMessage">
  12. <q-linear-progress stripe rounded size="30px" :value="progress" color="green">
  13. <div class="absolute-full flex flex-center">
  14. <div class="text-black bg-white" style="font-size: 10px; padding: 1px 4px 1px 4px; border-radius: 4px">
  15. {{ (progress*100).toFixed(2) }}%
  16. </div>
  17. </div>
  18. </q-linear-progress>
  19. </div>
  20. <!--div class="q-ml-sm">
  21. {{ jsonMessage }}
  22. </div-->
  23. </div>
  24. </q-dialog>
  25. </div>
  26. </template>
  27. <script>
  28. //-----------------------------------------------------------------------------
  29. import vueComponent from '../vueComponent.js';
  30. //import _ from 'lodash';
  31. import wsc from './webSocketConnection';
  32. import * as utils from '../../share/utils';
  33. const rotor = '|/-\\';
  34. const stepBound = [
  35. 0,
  36. 0,//1
  37. 18,//2
  38. 20,//3
  39. 70,//4
  40. 82,//5
  41. 84,//6
  42. 88,//7
  43. 90,//8
  44. 98,//9
  45. 99,//10
  46. 100,//11
  47. ];
  48. const componentOptions = {
  49. components: {
  50. },
  51. watch: {
  52. },
  53. };
  54. class Api {
  55. _options = componentOptions;
  56. busyDialogVisible = false;
  57. mainMessage = '';
  58. jobMessage = '';
  59. //jsonMessage = '';
  60. progress = 0;
  61. created() {
  62. this.commit = this.$store.commit;
  63. }
  64. mounted() {
  65. this.updateConfig();//no await
  66. }
  67. async updateConfig() {
  68. try {
  69. const config = await this.getConfig();
  70. this.commit('setConfig', config);
  71. } catch (e) {
  72. this.$root.stdDialog.alert(e.message, 'Ошибка');
  73. }
  74. }
  75. get config() {
  76. return this.$store.state.config;
  77. }
  78. async showBusyDialog() {
  79. this.mainMessage = '';
  80. this.jobMessage = '';
  81. this.busyDialogVisible = true;
  82. try {
  83. let ri = 0;
  84. while (1) {// eslint-disable-line
  85. const server = await wsc.message(await wsc.send({action: 'get-worker-state', workerId: 'server_state'}));
  86. if (server.state != 'normal') {
  87. this.mainMessage = `${server.serverMessage} ${rotor[ri]}`;
  88. if (server.job == 'load inpx') {
  89. this.jobMessage = `${server.jobMessage} (${server.recsLoaded}): ${server.fileName}`;
  90. } else {
  91. this.jobMessage = server.jobMessage;
  92. }
  93. //this.jsonMessage = server;
  94. const jStep = server.jobStep;
  95. if (jStep && stepBound[jStep] !== undefined) {
  96. const sp = server.progress || 0;
  97. const delta = stepBound[jStep + 1] - stepBound[jStep];
  98. this.progress = (stepBound[jStep] + sp*delta)/100;
  99. }
  100. } else {
  101. break;
  102. }
  103. await utils.sleep(300);
  104. ri = (ri < rotor.length - 1 ? ri + 1 : 0);
  105. }
  106. } finally {
  107. this.busyDialogVisible = false;
  108. }
  109. }
  110. async request(params) {
  111. while (1) {// eslint-disable-line
  112. const response = await wsc.message(await wsc.send(params));
  113. if (response && response.error == 'server_busy') {
  114. await this.showBusyDialog();
  115. } else {
  116. return response;
  117. }
  118. }
  119. }
  120. async search(query) {
  121. const response = await this.request({action: 'search', query});
  122. if (response.error) {
  123. throw new Error(response.error);
  124. }
  125. return response;
  126. }
  127. async getBookList(authorId) {
  128. const response = await this.request({action: 'get-book-list', authorId});
  129. if (response.error) {
  130. throw new Error(response.error);
  131. }
  132. return response;
  133. }
  134. async getGenreTree() {
  135. const response = await this.request({action: 'get-genre-tree'});
  136. if (response.error) {
  137. throw new Error(response.error);
  138. }
  139. return response;
  140. }
  141. async getConfig() {
  142. const response = await this.request({action: 'get-config'});
  143. if (response.error) {
  144. throw new Error(response.error);
  145. }
  146. return response;
  147. }
  148. }
  149. export default vueComponent(Api);
  150. //-----------------------------------------------------------------------------
  151. </script>
  152. <style scoped>
  153. </style>