Api.vue 7.6 KB

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