Api.vue 8.6 KB

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