Api.vue 7.7 KB

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