index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. }
  27. }
  28. export const modules = {
  29. index: {
  30. state: {
  31. // 产品类别
  32. pTypes: [],
  33. // 新闻类别
  34. nTypes: [],
  35. // 轮播数据
  36. carousel: [],
  37. },
  38. mutations: {
  39. [indexTypes.mutations.setProductTypes](state, pTypes){
  40. state.pTypes = pTypes;
  41. },
  42. [indexTypes.mutations.setNewsTypes](state, nTypes){
  43. state.nTypes = nTypes;
  44. },
  45. [indexTypes.mutations.setCarousel](state, carousel){
  46. state.carousel = carousel;
  47. }
  48. },
  49. actions : {
  50. async nuxtServerInit ({ commit }, { req }) {
  51. console.log('nuxtServerInit');
  52. if(req.session){
  53. if (req.session.owner) {
  54. commit('login/'+login_types.mutations.userLogin, req.session.owner)
  55. }
  56. // 获取新闻与产品类别信息
  57. let err, res;
  58. [err, res] = await handle(axios.get(baseUrl + apiMap.baseDatas.path));
  59. if(err){return console.log(err);}
  60. let result = res.data;
  61. if(result.code !== rCode.OK){return console.log(result.msg);}
  62. let data = result.data;
  63. commit(indexTypes.mutations.setProductTypes, data.pType);
  64. commit(indexTypes.mutations.setNewsTypes, data.nType);
  65. commit(indexTypes.mutations.setCarousel, data.carousel);
  66. }
  67. },
  68. },
  69. getters: {
  70. pTypes: state => state.pTypes,
  71. nTypes: state => state.nTypes,
  72. carousel: state => state.carousel,
  73. productTypes: state => {
  74. let arr = state.pTypes;
  75. let res = _transform(arr);
  76. // 首位插入一个空选项
  77. res.unshift({
  78. en_text : "全部类型",
  79. icon : "/image/all.svg",
  80. text : "全部类型",
  81. key : "all",
  82. typeKey : "all",
  83. })
  84. return res
  85. },
  86. allNewsTypes: state => {
  87. let arr = state.nTypes;
  88. return _transform(arr);
  89. },
  90. solutionTypes: state => {
  91. console.log(state.nTypes);
  92. let arr = state.nTypes.filter(item =>
  93. item.parent_type === dbField_esm.db_base.newsType.solution);
  94. return _transform(arr);
  95. },
  96. newsTypes: state => {
  97. let arr = state.nTypes.filter(item =>
  98. item.parent_type === dbField_esm.db_base.newsType.news);
  99. return _transform(arr);
  100. },
  101. }
  102. },
  103. login
  104. }