| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- import {login_types,state,mutations,actions,getters} from "./login";
- import login from "./login";
- import {apiMap, baseUrl} from "../map/apiMap";
- import {rCode} from "../map/rcodeMap_esm";
- import {handle} from "../until/handle";
- import axios from "axios";
- import dbField_esm from "../map/dbField_esm";
- function _transform(arr){
- arr = arr.map(item => {
- item.text = item.type_name;
- item.key = item.type_key;
- item.typeKey = item.type_key;
- item.href = `/${item.type_key}`;
- return item;
- })
- return arr;
- }
- export const indexTypes = {
- mutations: {
- setProductTypes: 'setProductTypes',
- setNewsTypes: 'setNewsTypes',
- setCarousel: 'setCarousel',
- }
- }
- export const modules = {
- index: {
- state: {
- // 产品类别
- pTypes: [],
- // 新闻类别
- nTypes: [],
- // 轮播数据
- carousel: [],
- },
- mutations: {
- [indexTypes.mutations.setProductTypes](state, pTypes){
- state.pTypes = pTypes;
- },
- [indexTypes.mutations.setNewsTypes](state, nTypes){
- state.nTypes = nTypes;
- },
- [indexTypes.mutations.setCarousel](state, carousel){
- state.carousel = carousel;
- }
- },
- actions : {
- async nuxtServerInit ({ commit }, { req }) {
- console.log('nuxtServerInit');
- if(req.session){
- if (req.session.owner) {
- commit('login/'+login_types.mutations.userLogin, req.session.owner)
- }
- // 获取新闻与产品类别信息
- let err, res;
- [err, res] = await handle(axios.get(baseUrl + apiMap.baseDatas.path));
- if(err){return console.log(err);}
- let result = res.data;
- if(result.code !== rCode.OK){return console.log(result.msg);}
- let data = result.data;
- commit(indexTypes.mutations.setProductTypes, data.pType);
- commit(indexTypes.mutations.setNewsTypes, data.nType);
- commit(indexTypes.mutations.setCarousel, data.carousel);
- }
- },
- },
- getters: {
- pTypes: state => state.pTypes,
- nTypes: state => state.nTypes,
- carousel: state => state.carousel,
- productTypes: state => {
- let arr = state.pTypes;
- return _transform(arr);
- },
- allNewsTypes: state => {
- let arr = state.nTypes;
- return _transform(arr);
- },
- solutionTypes: state => {
- console.log(state.nTypes);
- let arr = state.nTypes.filter(item =>
- item.parent_type === dbField_esm.db_base.newsType.solution);
- return _transform(arr);
- },
- newsTypes: state => {
- let arr = state.nTypes.filter(item =>
- item.parent_type === dbField_esm.db_base.newsType.news);
- return _transform(arr);
- },
- }
- },
- login
- }
|