Api.vue 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <template>
  2. <div>
  3. </div>
  4. </template>
  5. <script>
  6. //-----------------------------------------------------------------------------
  7. import vueComponent from '../vueComponent.js';
  8. import wsc from './webSocketConnection';
  9. //import _ from 'lodash';
  10. const componentOptions = {
  11. components: {
  12. },
  13. watch: {
  14. },
  15. };
  16. class Api {
  17. _options = componentOptions;
  18. created() {
  19. this.commit = this.$store.commit;
  20. }
  21. mounted() {
  22. this.updateConfig();//no await
  23. }
  24. async updateConfig() {
  25. try {
  26. const config = await this.getConfig();
  27. this.commit('setConfig', config);
  28. } catch (e) {
  29. this.$root.stdDialog.alert(e.message, 'Ошибка');
  30. }
  31. }
  32. get config() {
  33. return this.$store.state.config;
  34. }
  35. async request(params) {
  36. return await wsc.message(await wsc.send(params));
  37. }
  38. async search(query) {
  39. const response = await this.request({action: 'search', query});
  40. if (response.error) {
  41. throw new Error(response.error);
  42. }
  43. return response;
  44. }
  45. async getBookList(authorId) {
  46. const response = await this.request({action: 'get-book-list', authorId});
  47. if (response.error) {
  48. throw new Error(response.error);
  49. }
  50. return response;
  51. }
  52. async getConfig() {
  53. const response = await this.request({action: 'get-config'});
  54. if (response.error) {
  55. throw new Error(response.error);
  56. }
  57. return response;
  58. }
  59. }
  60. export default vueComponent(Api);
  61. //-----------------------------------------------------------------------------
  62. </script>
  63. <style scoped>
  64. </style>