index.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. import login from '@/apis/login'
  4. import common from '@/apis/common'
  5. Vue.use(Vuex);
  6. export default new Vuex.Store({
  7. //所有的数据都放在state中
  8. state: {
  9. token: '',
  10. user_account: '',
  11. userInfo: null,
  12. checkGoodsList: [],
  13. header: {
  14. serviceTel: {}
  15. },//页头配置
  16. footer: [],//页尾配置
  17. links: null,//友情链接
  18. },
  19. getters: {
  20. userInfo: state => state.userInfo,
  21. token: state => state.token,
  22. checkGoodsList: state => state.checkGoodsList,
  23. header: state => state.header,
  24. footer: state => state.footer,
  25. links: state => state.links,
  26. },
  27. //操作数据,唯一的通道是mutations
  28. mutations: {
  29. setUserInfo(state, data) {
  30. state.userInfo = data;
  31. },
  32. setCheckGoodsList(state, data) {
  33. state.checkGoodsList = data;
  34. },
  35. setHomeSetList(state, data) {
  36. data.forEach(item => {
  37. if (item.configKey === 'home.header') {
  38. state.header = JSON.parse(item.configValue)
  39. console.log(state.header)
  40. }
  41. if (item.configKey === 'home.footer') {
  42. state.footer = JSON.parse(item.configValue)
  43. console.log(state.footer)
  44. }
  45. if (item.configKey === 'home.links') {
  46. state.links = JSON.parse(item.configValue)
  47. console.log(state.links)
  48. }
  49. })
  50. }
  51. },
  52. //actions,可以来做异步操作,然后提交给mutations,而后再对state(数据)进行操作
  53. actions: {
  54. /**
  55. *
  56. * @param {*} context
  57. * @returns
  58. * 获取用户信息
  59. */
  60. getUserInfo(context) {
  61. return new Promise(resolve => {
  62. login.getInfo().then(res => {
  63. context.commit('setUserInfo', res.data)
  64. resolve()
  65. })
  66. })
  67. },
  68. /**
  69. *
  70. * @param {*} context
  71. * @returns
  72. * 获取首页配置
  73. */
  74. getCommonBaseHomeList(context) {
  75. return new Promise(resolve => {
  76. common.getCommonBaseHomeList().then(res => {
  77. context.commit('setHomeSetList', res.rows)
  78. resolve()
  79. })
  80. })
  81. }
  82. }
  83. })