四零语境前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.6 KiB

1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. import * as api from '@/api'
  4. Vue.use(Vuex)
  5. const store = new Vuex.Store({
  6. state: {
  7. // 存放状态
  8. configList: [],
  9. departmentList: [],
  10. categoryList: [],
  11. userInfo:{}
  12. },
  13. mutations: {
  14. // 构造用于uv-picker的树状结构数组
  15. setCategoryList(state, data) {
  16. // 将返回的平面数组data 根据pid和hasChild组装成两个数组,其一为pid为0的父级一维数组,其二为pid不为0的子级二维数组,其中pid相同的排一起 不同的分为不同子数组,并且按顺序排序,比如第一个为[中国,美国] 第二个按顺序为[[上海,福建],[纽约,华盛顿]]
  17. // 分离父级和子级数据
  18. const parentCategories = data.filter(item => item.pid === 0 || item.pid === '0')
  19. const childCategories = data.filter(item => item.pid !== 0 && item.pid !== '0')
  20. // 构建父级一维数组
  21. const parentArray = parentCategories.map((item) => {
  22. return {
  23. label: item.name || item.title || item.categoryName,
  24. value: item.id
  25. }
  26. })
  27. // 构建子级二维数组
  28. const childArray = []
  29. parentCategories.forEach(parent => {
  30. const children = childCategories
  31. .filter(child => child.pid === parent.id)
  32. .map(child => {
  33. return {
  34. label: child.name || child.title || child.categoryName,
  35. value: child.id
  36. }
  37. })
  38. childArray.push(children.length > 0 ? children : [{
  39. label: '全部',
  40. value: 0
  41. }])
  42. })
  43. // 组装最终结果
  44. state.categoryList = [parentArray, childArray]
  45. },
  46. setConfigList(state, data) {
  47. state.configList = data
  48. },
  49. setDepartmentList(state, data) {
  50. state.departmentList = data
  51. },
  52. setUserInfo(state, data) {
  53. state.userInfo = data
  54. },
  55. //
  56. },
  57. actions: {
  58. // 查询配置列表
  59. async getConfig({ commit }) {
  60. const res = await api.config.queryConfigList()
  61. // 要求变成键值对的样子
  62. const config = res.result.reduce((acc, item) => {
  63. if (!item.code) {
  64. console.log('code为空', item);
  65. return acc
  66. }
  67. acc[item.code] = item
  68. return acc
  69. }, {})
  70. console.log('configList列表为:', config);
  71. // 事先永久存儲首屏圖片的數據
  72. if (config.creen_image) {
  73. uni.setStorageSync('screen_image', config.creen_image.content)
  74. }
  75. commit('setConfigList', config)
  76. },
  77. // 查询部门列表
  78. async getDepartment({ commit }) {
  79. const res = await api.config.queryDepartmentList()
  80. commit('setDepartmentList', res.result.records)
  81. },
  82. // 获取分类列表
  83. async getCategory({ commit }) {
  84. const res = await api.config.queryCategoryList()
  85. commit('setCategoryList', res.result.records)
  86. },
  87. // 初始化数据
  88. async initData({ dispatch, state }) {
  89. // 检查是否已初始化
  90. if (state.configList.length > 0) {
  91. console.log('配置数据已初始化,无需重复初始化')
  92. return
  93. }
  94. try {
  95. await Promise.all([
  96. dispatch('getConfig'),
  97. // dispatch('getDepartment'),
  98. // dispatch('getCategory'),
  99. ])
  100. console.log('所有配置数据初始化完成')
  101. } catch (error) {
  102. console.error('配置数据初始化失败:', error)
  103. }
  104. },
  105. // 更新/存儲用戶最新數據
  106. updateUserInfo({ commit }, userInfo) {
  107. commit('setUserInfo', userInfo)
  108. },
  109. }
  110. })
  111. export default store