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

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