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

129 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 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. // #ifdef H5
  56. localStorage.setItem('userInfo', data)
  57. // #endif
  58. },
  59. setQrcode(state, data) {
  60. state.Qrcode = data
  61. },
  62. //
  63. },
  64. actions: {
  65. // 查询配置列表
  66. async getConfig({ commit }) {
  67. const res = await api.config.queryConfigList()
  68. // 要求变成键值对的样子
  69. const config = res.result.reduce((acc, item) => {
  70. if (!item.code) {
  71. console.log('code为空', item);
  72. return acc
  73. }
  74. acc[item.code] = item
  75. return acc
  76. }, {})
  77. console.log('configList列表为:', config);
  78. // 事先永久存儲首屏圖片的數據
  79. if (config.creen_image) {
  80. uni.setStorageSync('screen_image', config.creen_image.content)
  81. }
  82. commit('setConfigList', config)
  83. },
  84. // 查询部门列表
  85. async getDepartment({ commit }) {
  86. const res = await api.config.queryDepartmentList()
  87. commit('setDepartmentList', res.result.records)
  88. },
  89. // 获取分类列表
  90. async getCategory({ commit }) {
  91. const res = await api.config.queryCategoryList()
  92. commit('setCategoryList', res.result.records)
  93. },
  94. // 初始化数据
  95. async initData({ dispatch, state }) {
  96. // 检查是否已初始化
  97. if (state.configList.length > 0) {
  98. console.log('配置数据已初始化,无需重复初始化')
  99. return
  100. }
  101. try {
  102. await Promise.all([
  103. dispatch('getConfig'),
  104. // dispatch('getDepartment'),
  105. // dispatch('getCategory'),
  106. ])
  107. console.log('所有配置数据初始化完成')
  108. } catch (error) {
  109. console.error('配置数据初始化失败:', error)
  110. }
  111. },
  112. // 更新/存儲用戶最新數據
  113. updateUserInfo({ commit }, userInfo) {
  114. commit('setUserInfo', userInfo)
  115. },
  116. }
  117. })
  118. export default store