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

108 lines
3.3 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. // 组装最终结果
  43. state.categoryList = [parentArray, childArray]
  44. },
  45. setConfigList(state, data) {
  46. state.configList = data
  47. },
  48. setDepartmentList(state, data) {
  49. state.departmentList = data
  50. },
  51. //
  52. },
  53. actions: {
  54. // 查询配置列表
  55. async getConfig({ commit }) {
  56. const res = await api.config.queryConfigList()
  57. // 要求变成键值对的样子
  58. const config = res.result.records.reduce((acc, item) => {
  59. if (!item.paramCode) {
  60. console.log('paramCode为空', item);
  61. return acc
  62. }
  63. acc[item.paramCode] = item
  64. return acc
  65. }, {})
  66. commit('setConfigList', config)
  67. },
  68. // 查询部门列表
  69. async getDepartment({ commit }) {
  70. const res = await api.config.queryDepartmentList()
  71. commit('setDepartmentList', res.result.records)
  72. },
  73. // 获取分类列表
  74. async getCategory({ commit }) {
  75. const res = await api.config.queryCategoryList()
  76. commit('setCategoryList', res.result.records)
  77. },
  78. // 初始化数据
  79. async initData({ dispatch, state }) {
  80. // 检查是否已初始化
  81. if (state.configList.length > 0 && state.departmentList.length > 0 && state.categoryList.length > 0) {
  82. console.log('配置数据已初始化,无需重复初始化')
  83. return
  84. }
  85. try {
  86. await Promise.all([
  87. dispatch('getConfig'),
  88. dispatch('getDepartment'),
  89. dispatch('getCategory'),
  90. ])
  91. console.log('所有配置数据初始化完成')
  92. } catch (error) {
  93. console.error('配置数据初始化失败:', error)
  94. }
  95. },
  96. }
  97. })
  98. export default store