展品维保小程序前端代码接口
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.

110 lines
3.4 KiB

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