navigation.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import Vue from "vue";
  2. import { i18n } from "@/lang";
  3. import { navigationSave, initialSet } from "@/api";
  4. import { $waiting } from "@/components/shared/loading";
  5. let vue = new Vue();
  6. export default {
  7. namespaced: true,
  8. state() {
  9. return {
  10. isSave: false,
  11. catalogTopology: [],
  12. currentSecondId: null,
  13. currentRootId: null,
  14. saveFristScene: false, //是否可以保存初始场景
  15. // currentCatalogRoot: {},
  16. // //当前二级分组
  17. // currentSecondary: {},
  18. // //二级分组
  19. // secondaryList: {},
  20. // //当前场景分组
  21. // currentScenesList: {},
  22. navigationTrees: [],
  23. };
  24. },
  25. getters: {
  26. isSave: (state) => state.isSave,
  27. saveFristScene: (state) => state.saveFristScene,
  28. currentRootId: (state) => state.currentRootId,
  29. currentSecondId: (state) => state.currentSecondId,
  30. },
  31. mutations: {
  32. setData(state, payload) {
  33. for (let key in payload) {
  34. state[key] = payload[key];
  35. }
  36. },
  37. },
  38. actions: {
  39. save({ commit, state, rootState }) {
  40. let list = JSON.parse(JSON.stringify(rootState.base.baseInfo.navigationTrees));
  41. // console.error(rootState.base.baseInfo.navigationTrees);
  42. list.forEach((item, index) => {
  43. item.sort = index;
  44. if (typeof item.id != "number" && item.id.indexOf("add_") != -1) {
  45. delete item.id;
  46. }
  47. item = item.children.forEach((s_item, s_index) => {
  48. s_item.sort = s_index;
  49. if (typeof s_item.id != "number" && s_item.id.indexOf("add_") != -1) {
  50. delete s_item.id;
  51. }
  52. // console.error(s_item);
  53. s_item = s_item.children.forEach((t_item, t_index) => {
  54. t_item.sort = t_index;
  55. if (typeof t_item.id != "number" && t_item.id.indexOf("add_") != -1) {
  56. delete t_item.id;
  57. }
  58. });
  59. });
  60. });
  61. // console.error(rootState.base.baseInfo.navigationTrees, list);
  62. // $waiting.hide();
  63. // return;
  64. return new Promise((resolve, reject) => {
  65. navigationSave(
  66. { list },
  67. (res) => {
  68. $waiting.hide();
  69. if (res.code == 0) {
  70. // vue.$msg.success(i18n.t("gather.save_done"));
  71. // this.commit("TakeInfoSnapShotAtSave");
  72. this.commit("base/updateBaseInfo", { navigationTrees: res.data });
  73. if (rootState.scene.currentScene) {
  74. setTimeout(() => {
  75. // console.error("有当前场景", rootState.scene.currentScene);
  76. let curreentScene = rootState.base.sceneList.find((item) => item.sid && item.sid == rootState.scene.currentScene.sid);
  77. if (curreentScene) {
  78. this.commit("scene/setCurrentScene", curreentScene);
  79. }
  80. }, 0);
  81. if (rootState.base.baseInfo.firstScene) {
  82. //如果初始场景是新增的,则需要更新id
  83. setTimeout(() => {
  84. let firstScene = rootState.base.sceneList.find((item) => item.sid && item.sid == rootState.base.baseInfo.firstScene.sid);
  85. if (firstScene) {
  86. rootState.base.baseInfo.firstScene.id = firstScene.id;
  87. }
  88. }, 0);
  89. }
  90. }
  91. this.commit("base/initDefaultData");
  92. state.isSave = true;
  93. setTimeout(() => {
  94. resolve();
  95. if (state.saveFristScene) {
  96. let data = {};
  97. // console.error("有当前场景", rootState.base.baseInfo.firstScene);
  98. if (rootState.base.baseInfo.firstScene) {
  99. let firstScene = rootState.base.sceneList.find((item) => item.sceneCode == rootState.base.baseInfo.firstScene.sceneCode);
  100. if (firstScene) {
  101. data = { navigationId: firstScene.id, operType: "add" };
  102. }
  103. } else {
  104. data = { navigationId: null, operType: "delete" };
  105. }
  106. initialSet(data, (res) => {
  107. commit("setData", { saveFristScene: false });
  108. });
  109. }
  110. }, 0);
  111. }
  112. },
  113. (err) => {
  114. $waiting.hide();
  115. reject();
  116. }
  117. );
  118. });
  119. },
  120. },
  121. };