index.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.text = item.type_name;
  11. item.key = item.type_key;
  12. item.typeKey = item.type_key;
  13. item.href = `/${item.type_key}`;
  14. item.value = item.type_key;
  15. return item;
  16. })
  17. return arr;
  18. }
  19. export const indexTypes = {
  20. mutations: {
  21. setProductTypes: 'setProductTypes',
  22. setNewsTypes: 'setNewsTypes',
  23. setCarousel: 'setCarousel',
  24. }
  25. }
  26. export const modules = {
  27. index: {
  28. state: {
  29. // 产品类别
  30. pTypes: [],
  31. // 新闻类别
  32. nTypes: [],
  33. // 轮播数据
  34. carousel: [],
  35. },
  36. mutations: {
  37. [indexTypes.mutations.setProductTypes](state, pTypes){
  38. state.pTypes = pTypes;
  39. },
  40. [indexTypes.mutations.setNewsTypes](state, nTypes){
  41. state.nTypes = nTypes;
  42. },
  43. [indexTypes.mutations.setCarousel](state, carousel){
  44. state.carousel = carousel;
  45. }
  46. },
  47. actions : {
  48. async nuxtServerInit ({ commit }, { req }) {
  49. console.log('nuxtServerInit');
  50. if(req.session){
  51. if (req.session.owner) {
  52. commit('login/'+login_types.mutations.userLogin, req.session.owner)
  53. }
  54. // 获取新闻与产品类别信息
  55. let err, res;
  56. [err, res] = await handle(axios.get(baseUrl + apiMap.baseDatas.path));
  57. if(err){return console.log(err);}
  58. let result = res.data;
  59. if(result.code !== rCode.OK){return console.log(result.msg);}
  60. let data = result.data;
  61. commit(indexTypes.mutations.setProductTypes, data.pType);
  62. commit(indexTypes.mutations.setNewsTypes, data.nType);
  63. commit(indexTypes.mutations.setCarousel, data.carousel);
  64. }
  65. },
  66. },
  67. getters: {
  68. pTypes: state => state.pTypes,
  69. nTypes: state => state.nTypes,
  70. carousel: state => state.carousel,
  71. productTypes: state => {
  72. let arr = state.pTypes;
  73. return _transform(arr);
  74. },
  75. allNewsTypes: state => {
  76. let arr = state.nTypes;
  77. return _transform(arr);
  78. },
  79. solutionTypes: state => {
  80. console.log(state.nTypes);
  81. let arr = state.nTypes.filter(item =>
  82. item.parent_type === dbField_esm.db_base.newsType.solution);
  83. return _transform(arr);
  84. },
  85. newsTypes: state => {
  86. let arr = state.nTypes.filter(item =>
  87. item.parent_type === dbField_esm.db_base.newsType.news);
  88. return _transform(arr);
  89. },
  90. }
  91. },
  92. login
  93. }