menus.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { ref, computed } from 'vue';
  2. import { defineStore } from 'pinia';
  3. import { AxiosWrapper } from '@/util/axios-wrapper';
  4. import _ from 'lodash';
  5. import type { IQueryType, IQueryResult, IQueryParams } from '@/util/types.util';
  6. const axios = new AxiosWrapper();
  7. const api = {
  8. url: `/menus`
  9. };
  10. export const MenusStore = defineStore('menus', () => {
  11. const count = ref(0);
  12. const doubleCount = computed(() => count.value * 2);
  13. function increment() {
  14. count.value++;
  15. }
  16. const query = async ({ skip = 0, limit = undefined, ...info }: IQueryParams = {}): Promise<IQueryResult> => {
  17. let cond: IQueryType = {};
  18. if (skip) cond.skip = skip;
  19. if (limit) cond.limit = limit;
  20. cond = { ...cond, ...info };
  21. const res = await axios.$get(`${api.url}`, cond);
  22. return res;
  23. };
  24. const fetch = async (payload: any): Promise<IQueryResult> => {
  25. const res = await axios.$get(`${api.url}/${payload}`);
  26. return res;
  27. };
  28. const create = async (payload: any): Promise<IQueryResult> => {
  29. const res = await axios.$post(`${api.url}`, payload);
  30. return res;
  31. };
  32. const update = async (payload: any): Promise<IQueryResult> => {
  33. const id = _.get(payload, 'id', _.get(payload, '_id'));
  34. const res = await axios.$post(`${api.url}/${id}`, payload);
  35. return res;
  36. };
  37. const del = async (payload: any): Promise<IQueryResult> => {
  38. const res = await axios.$delete(`${api.url}/${payload}`);
  39. return res;
  40. };
  41. return {
  42. count,
  43. doubleCount,
  44. increment,
  45. query,
  46. fetch,
  47. create,
  48. update,
  49. del
  50. };
  51. });