四零语境前端代码仓库
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.

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