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

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