layout.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. export const useLayoutStore = defineStore({
  8. id: "longjoe-layout",
  9. state: (): DwLayout.LayoutState => ({
  10. // 布局列表
  11. dwlayout: [],
  12. userIdx: 0
  13. }),
  14. getters: {
  15. // 布局列表
  16. dwlayoutGet: state => state.dwlayout
  17. },
  18. actions: {
  19. /**
  20. * 记录当前用户的布局数据
  21. * @param data 布局数组
  22. */
  23. setLayoutList(data: DwLayout.LayoutItem["layoutList"]) {
  24. this.dwlayout[this.userIdx].layoutList = data;
  25. },
  26. /**
  27. * 记录当前用户在dwlayout的索引值
  28. * @param idx 索引
  29. */
  30. setUserIdx(idx: DwLayout.LayoutState["userIdx"]) {
  31. this.userIdx = idx;
  32. },
  33. /**
  34. * @description 本地布局缓存列表
  35. */
  36. findUserLayout() {
  37. const { userInfo } = useUserStore();
  38. console.log("userInfo.userid :>> ", userInfo);
  39. console.log("this.dwlayout :>> ", this.dwlayout);
  40. let dwIdx = this.dwlayout.findIndex(item => item.empid == userInfo.empid && item.account == userInfo.account);
  41. console.log("dwIdx :>> ", dwIdx);
  42. let userLayout = { empid: userInfo.empid ?? 0, account: userInfo.account ?? "", layoutList: <any>[] };
  43. if (dwIdx > -1) {
  44. userLayout = this.dwlayout[dwIdx];
  45. } else {
  46. this.dwlayout.push(userLayout);
  47. dwIdx = this.dwlayout.length - 1;
  48. }
  49. this.setUserIdx(dwIdx);
  50. console.log("findUserLayoutuserLayout :>> ", userLayout);
  51. return { layoutList: userLayout.layoutList };
  52. },
  53. /**
  54. * @description 获取窗口布局
  55. * @param {string} dwname 布局名称
  56. * @param {boolean} autoDownload 是否自动获取布局信息
  57. * @param {number} ifdel 是否删除token.empid的布局信息
  58. * @return {Object} fx_user_dwlayout
  59. */
  60. async getDwLayout_online(empid: number, dwname: string, ifdel = 0) {
  61. let layout: userLayout.ReqSetDwLayout = {
  62. empid,
  63. dwname: import.meta.env.VITE_GLOB_PRODUCT_CODE,
  64. itemname: dwname,
  65. ifcompress: 0,
  66. ifdel: ifdel
  67. };
  68. let { itemvalue } = await GetDwLyoutApi(layout);
  69. console.log("getDwLayout layout :>> ", itemvalue);
  70. if (itemvalue) {
  71. layout.itemvalue = itemvalue;
  72. console.log("getDwLayout this.dwlayout :>> ", this.dwlayout);
  73. return Promise.resolve(layout);
  74. } else {
  75. return Promise.resolve({});
  76. }
  77. },
  78. /**
  79. * @description 获取窗口布局
  80. * @param {string} dwname 布局名称
  81. * @param {boolean} autoDownload 是否自动获取布局信息
  82. * @return {Object} fx_user_dwlayout
  83. * */
  84. async getDwLayout(dwname: string, autoDownload: boolean = true) {
  85. try {
  86. const { layoutList } = this.findUserLayout();
  87. const { userInfo } = useUserStore();
  88. console.log("userLayout userLayout:>> ", layoutList);
  89. let dwItem = layoutList.find((item: any) => item.itemname == dwname);
  90. console.log("getDwLayout dwItem :>> ", dwItem);
  91. if (dwItem) {
  92. return Promise.resolve(dwItem);
  93. }
  94. if (!autoDownload) return Promise.resolve({});
  95. let layout: userLayout.ReqSetDwLayout = {
  96. empid: userInfo.empid,
  97. dwname: import.meta.env.VITE_GLOB_PRODUCT_CODE,
  98. itemname: dwname,
  99. ifcompress: 0
  100. };
  101. let { itemvalue } = await GetDwLyoutApi(layout);
  102. console.log("getDwLayout layout :>> ", itemvalue);
  103. if (itemvalue) {
  104. layout.itemvalue = itemvalue;
  105. layoutList.push(layout);
  106. this.setLayoutList(layoutList);
  107. console.log("getDwLayout this.dwlayout :>> ", this.dwlayout);
  108. return Promise.resolve(layout);
  109. } else {
  110. return Promise.resolve({});
  111. }
  112. } catch (error) {
  113. ElMessage.error("获取布局失败:" + dwname);
  114. }
  115. },
  116. /**
  117. * @description 保存窗口布局
  118. * @return void
  119. * */
  120. async setDwLayout(dwname: string, value: any, t: any, autoUpload: boolean = true) {
  121. try {
  122. const { layoutList } = this.findUserLayout();
  123. const { userInfo } = useUserStore();
  124. let layout = {
  125. empid: userInfo.empid,
  126. dwname: import.meta.env.VITE_GLOB_PRODUCT_CODE,
  127. itemname: dwname,
  128. itemvalue: JSON.stringify(value),
  129. ifcompress: 0
  130. };
  131. autoUpload && (await SetDwLyoutApi(layout));
  132. let dwItemIdx = layoutList.findIndex((item: any) => item.itemname == dwname);
  133. if (dwItemIdx > -1) {
  134. layoutList.splice(dwItemIdx, 1);
  135. }
  136. layoutList.push(layout);
  137. this.setLayoutList(layoutList);
  138. // autoUpload &&
  139. // ElMessage({
  140. // message: t("sys.api.operationSuccess"),
  141. // type: "success"
  142. // });
  143. } catch (error) {
  144. ElMessage.error("设置布局失败:" + dwname);
  145. }
  146. },
  147. /**
  148. * @description 获取窗口布局
  149. * @param {string} dwname 布局名称
  150. * @param {boolean} value 布局信息
  151. * @param {number} ifdelother 是否删除其他用户的布局信息
  152. * @param {number} iffilesave 是否保存到文件
  153. * @return {Object} fx_user_dwlayout
  154. */
  155. async saveDwLayout_online(empid: number, dwname: string, value: any, ifdelother: number = 0, iffilesave: number = 0) {
  156. try {
  157. let layout = {
  158. empid: empid,
  159. dwname: import.meta.env.VITE_GLOB_PRODUCT_CODE,
  160. itemname: dwname,
  161. itemvalue: JSON.stringify(value),
  162. ifcompress: 0,
  163. iffilesave: iffilesave,
  164. ifdelother: ifdelother
  165. };
  166. await SetDwLyoutApi(layout);
  167. } catch (error) {
  168. ElMessage.error("设置布局失败:" + dwname);
  169. }
  170. }
  171. }
  172. // persist: piniaPersistConfig("longjoe-layout")
  173. });