Api.vue 7.9 KB

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