Api.vue 7.4 KB

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