Api.vue 7.2 KB

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