index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import {login_types,state,mutations,actions,getters} from "./login";
  2. import login from "./login";
  3. import {apiMap, baseUrl} from "../map/apiMap";
  4. import {rCode} from "../map/rcodeMap_esm";
  5. import {handle} from "../until/handle";
  6. import axios from "axios";
  7. import dbField_esm from "../map/dbField_esm";
  8. function _transform(arr){
  9. arr = arr.map(item => {
  10. item.en_text = item.type_name;
  11. item.icon = item.type_logo;
  12. item.text = item.type_name;
  13. item.key = item.type_key;
  14. item.typeKey = item.type_key;
  15. item.href = `/${item.type_key}`;
  16. item.value = item.type_key;
  17. return item;
  18. })
  19. return arr;
  20. }
  21. export const indexTypes = {
  22. mutations: {
  23. setProductTypes: 'setProductTypes',
  24. setNewsTypes: 'setNewsTypes',
  25. setCarousel: 'setCarousel',
  26. setShowBlocks: 'setShowBlocks',
  27. setPlatform: 'setPlatform'
  28. }
  29. }
  30. export const modules = {
  31. index: {
  32. state: {
  33. // 平台基础信息
  34. platform: {},
  35. // 产品类别
  36. pTypes: [],
  37. // 新闻类别
  38. nTypes: [],
  39. // 轮播数据
  40. carousel: [],
  41. // 显示区块数据
  42. showBlocks: []
  43. },
  44. mutations: {
  45. [indexTypes.mutations.setProductTypes](state, pTypes){
  46. state.pTypes = pTypes;
  47. },
  48. [indexTypes.mutations.setNewsTypes](state, nTypes){
  49. state.nTypes = nTypes;
  50. },
  51. [indexTypes.mutations.setCarousel](state, carousel){
  52. state.carousel = carousel;
  53. },
  54. [indexTypes.mutations.setPlatform](state, platform){
  55. state.platform = platform;
  56. },
  57. [indexTypes.mutations.setShowBlocks](state, showBlocks){
  58. state.showBlocks = showBlocks;
  59. }
  60. },
  61. actions : {
  62. async nuxtServerInit ({ commit }, { req }) {
  63. console.log('nuxtServerInit');
  64. if(req.session){
  65. if (req.session.owner) {
  66. commit('login/'+login_types.mutations.userLogin, req.session.owner)
  67. }
  68. // 获取新闻与产品类别信息
  69. let err, res;
  70. [err, res] = await handle(axios.get(baseUrl + apiMap.baseDatas.path));
  71. if(err){return console.log(err);}
  72. let result = res.data;
  73. if(result.code !== rCode.OK){return console.log(result.msg);}
  74. let data = result.data;
  75. commit(indexTypes.mutations.setPlatform, data.baseInfo);
  76. commit(indexTypes.mutations.setProductTypes, data.pType);
  77. commit(indexTypes.mutations.setNewsTypes, data.nType);
  78. commit(indexTypes.mutations.setCarousel, data.carousel);
  79. commit(indexTypes.mutations.setShowBlocks, data.showBlocks);
  80. }
  81. },
  82. },
  83. getters: {
  84. platform: state => state.platform,
  85. pTypes: state => state.pTypes,
  86. nTypes: state => state.nTypes,
  87. carousel: state => state.carousel,
  88. showBlocks: state => state.showBlocks,
  89. productTypes: state => {
  90. let arr = state.pTypes;
  91. let res = _transform(arr);
  92. // 首位插入一个空选项
  93. res.unshift({
  94. en_text : "全部类型",
  95. icon : "/image/all.svg",
  96. text : "全部类型",
  97. key : "all",
  98. typeKey : "all",
  99. })
  100. return res
  101. },
  102. allNewsTypes: state => {
  103. let arr = state.nTypes;
  104. return _transform(arr);
  105. },
  106. solutionTypes: state => {
  107. console.log(state.nTypes);
  108. let arr = state.nTypes.filter(item =>
  109. item.parent_type === dbField_esm.db_base.newsType.solution);
  110. let res = _transform(arr);
  111. res.unshift({
  112. en_text : "全部类型",
  113. icon : "/image/all.svg",
  114. text : "全部类型",
  115. key : "all",
  116. typeKey : "all",
  117. })
  118. return res;
  119. },
  120. newsTypes: state => {
  121. let arr = state.nTypes.filter(item =>
  122. item.parent_type === dbField_esm.db_base.newsType.news);
  123. let res = _transform(arr);
  124. res.unshift({
  125. en_text : "全部类型",
  126. icon : "/image/all.svg",
  127. text : "全部类型",
  128. key : "all",
  129. typeKey : "all",
  130. })
  131. return res;
  132. },
  133. }
  134. },
  135. login
  136. }