Api.vue 7.3 KB

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