Api.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. import * as cryptoUtils from '../../share/cryptoUtils';
  34. const rotor = '|/-\\';
  35. const stepBound = [
  36. 0,
  37. 0,//1
  38. 18,//2
  39. 20,//3
  40. 70,//4
  41. 82,//5
  42. 84,//6
  43. 88,//7
  44. 90,//8
  45. 98,//9
  46. 99,//10
  47. 100,//11
  48. ];
  49. const componentOptions = {
  50. components: {
  51. },
  52. watch: {
  53. settings() {
  54. this.loadSettings();
  55. },
  56. },
  57. };
  58. class Api {
  59. _options = componentOptions;
  60. busyDialogVisible = false;
  61. mainMessage = '';
  62. jobMessage = '';
  63. //jsonMessage = '';
  64. progress = 0;
  65. accessToken = '';
  66. created() {
  67. this.commit = this.$store.commit;
  68. this.loadSettings();
  69. }
  70. mounted() {
  71. this.updateConfig();//no await
  72. }
  73. loadSettings() {
  74. const settings = this.settings;
  75. this.accessToken = settings.accessToken;
  76. }
  77. async updateConfig() {
  78. try {
  79. const config = await this.getConfig();
  80. this.commit('setConfig', config);
  81. } catch (e) {
  82. this.$root.stdDialog.alert(e.message, 'Ошибка');
  83. }
  84. }
  85. get config() {
  86. return this.$store.state.config;
  87. }
  88. get settings() {
  89. return this.$store.state.settings;
  90. }
  91. async showBusyDialog() {
  92. this.mainMessage = '';
  93. this.jobMessage = '';
  94. this.busyDialogVisible = true;
  95. try {
  96. let ri = 0;
  97. while (1) {// eslint-disable-line
  98. const server = await wsc.message(await wsc.send({action: 'get-worker-state', workerId: 'server_state'}));
  99. if (server.state != 'normal') {
  100. this.mainMessage = `${server.serverMessage} ${rotor[ri]}`;
  101. if (server.job == 'load inpx') {
  102. this.jobMessage = `${server.jobMessage} (${server.recsLoaded}): ${server.fileName}`;
  103. } else {
  104. this.jobMessage = server.jobMessage;
  105. }
  106. //this.jsonMessage = server;
  107. const jStep = server.jobStep;
  108. if (jStep && stepBound[jStep] !== undefined) {
  109. const sp = server.progress || 0;
  110. const delta = stepBound[jStep + 1] - stepBound[jStep];
  111. this.progress = (stepBound[jStep] + sp*delta)/100;
  112. }
  113. } else {
  114. break;
  115. }
  116. await utils.sleep(300);
  117. ri = (ri < rotor.length - 1 ? ri + 1 : 0);
  118. }
  119. } finally {
  120. this.busyDialogVisible = false;
  121. }
  122. }
  123. async showPasswordDialog() {
  124. const result = await this.$root.stdDialog.prompt(`Введите пароль для доступа:`, ' ', {
  125. inputValidator: (str) => (str ? true : 'Пароль не должен быть пустым'),
  126. });
  127. if (result && result.value) {
  128. const accessToken = utils.toHex(cryptoUtils.sha256(result.value));
  129. this.commit('setSettings', {accessToken});
  130. }
  131. }
  132. async request(params, timeoutSecs = 10) {
  133. while (1) {// eslint-disable-line
  134. if (this.accessToken)
  135. params.accessToken = this.accessToken;
  136. const response = await wsc.message(await wsc.send(params), timeoutSecs);
  137. if (response && response.error == 'server_busy') {
  138. await this.showBusyDialog();
  139. } else if (response && response.error == 'need_access_token') {
  140. await this.showPasswordDialog();
  141. } else {
  142. return response;
  143. }
  144. }
  145. }
  146. async search(query) {
  147. const response = await this.request({action: 'search', query});
  148. if (response.error) {
  149. throw new Error(response.error);
  150. }
  151. return response;
  152. }
  153. async getBookList(authorId) {
  154. const response = await this.request({action: 'get-book-list', authorId});
  155. if (response.error) {
  156. throw new Error(response.error);
  157. }
  158. return response;
  159. }
  160. async getGenreTree() {
  161. const response = await this.request({action: 'get-genre-tree'});
  162. if (response.error) {
  163. throw new Error(response.error);
  164. }
  165. return response;
  166. }
  167. async getBookLink(params) {
  168. const response = await this.request(Object.assign({action: 'get-book-link'}, params), 120);
  169. if (response.error) {
  170. throw new Error(response.error);
  171. }
  172. return response;
  173. }
  174. async getConfig() {
  175. const response = await this.request({action: 'get-config'});
  176. if (response.error) {
  177. throw new Error(response.error);
  178. }
  179. return response;
  180. }
  181. }
  182. export default vueComponent(Api);
  183. //-----------------------------------------------------------------------------
  184. </script>
  185. <style scoped>
  186. </style>