Api.vue 8.4 KB

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