layoutLocal.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { defineStore } from "pinia";
  2. import { DwLayout } from "@/stores/interface";
  3. import { DwLayout as userLayout } from "@/api/interface/index";
  4. import { GetDwLyoutApi, SetDwLyoutApi } from "@/api/modules/common";
  5. import { ElMessage } from "element-plus";
  6. import { useUserStore } from "./user";
  7. import piniaPersistConfig from "@/config/piniaPersist";
  8. export const useLayoutLocalStore = defineStore({
  9. id: "longjoe-layout-local",
  10. state: (): DwLayout.LayoutState => ({
  11. // 布局列表
  12. dwlayout: [],
  13. userIdx: 0
  14. }),
  15. getters: {
  16. // 布局列表
  17. dwlayoutGet: state => state.dwlayout
  18. },
  19. actions: {
  20. /**
  21. * 记录当前用户的布局数据
  22. * @param data 布局数组
  23. */
  24. setLayoutList(data: DwLayout.LayoutItem["layoutList"]) {
  25. this.dwlayout[this.userIdx].layoutList = data;
  26. },
  27. /**
  28. * 记录当前用户在dwlayout的索引值
  29. * @param idx 索引
  30. */
  31. setUserIdx(idx: DwLayout.LayoutState["userIdx"]) {
  32. this.userIdx = idx;
  33. },
  34. findUserLayout() {
  35. const { userInfo } = useUserStore();
  36. console.log("userInfo.userid :>> ", userInfo.empid);
  37. console.log("this.dwlayout :>> ", this.dwlayout);
  38. let dwIdx = this.dwlayout.findIndex(item => item.empid == userInfo.empid);
  39. console.log("dwIdx :>> ", dwIdx);
  40. let userLayout = { empid: userInfo.empid ?? 0, layoutList: <any>[] };
  41. if (dwIdx > -1) {
  42. userLayout = this.dwlayout[dwIdx];
  43. console.log("findUserLayoutuserLayout userLayout:>> ", userLayout);
  44. } else {
  45. this.dwlayout.push(userLayout);
  46. dwIdx = this.dwlayout.length - 1;
  47. }
  48. this.setUserIdx(dwIdx);
  49. console.log("findUserLayoutuserLayout :>> ", userLayout);
  50. return { layoutList: userLayout.layoutList };
  51. },
  52. // /**
  53. // * @description 获取窗口布局
  54. // * @return {Object} fx_user_dwlayout
  55. // * */
  56. // getLocalDwLayout(dwname: string) {
  57. // try {
  58. // const { layoutList } = this.findUserLayout();
  59. // const { userInfo } = useUserStore();
  60. // console.log("userLayout userLayout:>> ", layoutList);
  61. // let dwItem = layoutList.find((item: any) => item.itemname == dwname);
  62. // console.log("dwItem :>> ", dwItem);
  63. // if (dwItem) {
  64. // return Promise.resolve(dwItem);
  65. // }
  66. // return this.getDwLayout(dwname);
  67. // } catch (error) {
  68. // ElMessage.error("获取布局失败:" + dwname);
  69. // }
  70. // },
  71. /**
  72. * @description 获取窗口布局
  73. * @return {Object} fx_user_dwlayout
  74. * */
  75. async getDwLayout(dwname: string) {
  76. const { userInfo } = useUserStore();
  77. try {
  78. const { layoutList } = this.findUserLayout();
  79. let dwItem = layoutList.find((item: any) => item.itemname == dwname);
  80. console.log("dwItem :>> ", dwItem);
  81. if (dwItem) {
  82. return Promise.resolve(dwItem);
  83. }
  84. let layout: userLayout.ReqSetDwLayout = {
  85. empid: userInfo.empid,
  86. dwname: import.meta.env.VITE_GLOB_PRODUCT_CODE,
  87. itemname: dwname,
  88. ifcompress: 0
  89. };
  90. let { itemvalue } = await GetDwLyoutApi(layout);
  91. console.log("getDwLayout layout :>> ", itemvalue);
  92. if (itemvalue) {
  93. layout.itemvalue = itemvalue;
  94. layoutList.push(layout);
  95. this.setLayoutList(layoutList);
  96. console.log("getDwLayout this.dwlayout :>> ", this.dwlayout);
  97. return Promise.resolve(layout);
  98. } else {
  99. return Promise.resolve({});
  100. }
  101. } catch (error) {
  102. ElMessage.error("获取布局失败:" + dwname);
  103. }
  104. },
  105. /**
  106. * @description 保存窗口布局
  107. * @return void
  108. * */
  109. async setDwLayout(dwname: string, value: any, t?: any, empid?: number) {
  110. try {
  111. const { layoutList } = this.findUserLayout();
  112. console.log("setDwLayout layoutList :>> ", layoutList);
  113. const { userInfo } = useUserStore();
  114. let layout = {
  115. empid: empid ?? userInfo.empid,
  116. dwname: import.meta.env.VITE_GLOB_PRODUCT_CODE,
  117. itemname: dwname,
  118. itemvalue: JSON.stringify(value),
  119. ifcompress: 0
  120. };
  121. await SetDwLyoutApi(layout);
  122. let dwItemIdx = layoutList.findIndex((item: any) => item.itemname == dwname);
  123. if (dwItemIdx > -1) {
  124. layoutList.splice(dwItemIdx, 1);
  125. }
  126. layoutList.push(layout);
  127. this.setLayoutList(layoutList);
  128. console.log("setDwLayout t :>>>>>>>>>>>>>>>>>>>>>", JSON.stringify(t));
  129. if (t) {
  130. ElMessage({
  131. message: t("sys.api.successToLayout"),
  132. type: "success"
  133. });
  134. }
  135. return Promise.resolve();
  136. } catch (error) {
  137. ElMessage.error("设置布局失败:" + dwname);
  138. return Promise.reject(error);
  139. }
  140. },
  141. /**
  142. * @description 快速获取页面布局参数
  143. * @argument string dwname 页面标识
  144. */
  145. async getLayoutAttr(dwname: string) {
  146. let layout = await this.getDwLayout(dwname);
  147. console.log("getLayoutStore layout :>> ", layout);
  148. let localLayout = {};
  149. if (layout?.itemvalue) {
  150. localLayout = JSON.parse(layout.itemvalue.replace(/'/g, '"'));
  151. }
  152. return localLayout;
  153. }
  154. },
  155. persist: piniaPersistConfig("longjoe-layout-local")
  156. });