project.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // import SHOP from '../staticURL.js'
  2. export default {
  3. namespaced: true,
  4. state: () => ({
  5. projects: [{
  6. "id": 2,
  7. "productStata":"充足",
  8. "productName": "可乐",
  9. "productIcons":"https://th.bing.com/th/id/OIP.kiwNAFNljctfF5licux1mwHaJQ?rs=1&pid=ImgDetMain",
  10. "productTypeIds":[1,2,3,4],
  11. "productBidPrice": 8,
  12. "productSellPrice":6,
  13. "avatar": "",
  14. "type":"饮料"
  15. },
  16. {
  17. "id": 1,
  18. "productStata":"充足",
  19. "productName": "雪碧",
  20. "productIcons":"https://th.bing.com/th/id/OIP.kiwNAFNljctfF5licux1mwHaJQ?rs=1&pid=ImgDetMain",
  21. "productTypeIds":[1,2,3,4],
  22. "productBidPrice": 8,
  23. "productSellPrice":6,
  24. "avatar": "",
  25. "type":"饮料"
  26. },
  27. {
  28. "id": 11,
  29. "productStata":"充足",
  30. "productName": "拿铁",
  31. "productIcons":"https://th.bing.com/th/id/OIP.kiwNAFNljctfF5licux1mwHaJQ?rs=1&pid=ImgDetMain",
  32. "productTypeIds":[1,2,3,4],
  33. "productBidPrice": 18,
  34. "productSellPrice":6,
  35. "avatar": "",
  36. },
  37. {
  38. "id": 12,
  39. "productStata":"充足",
  40. "productName": "咖啡",
  41. "productIcons":"https://th.bing.com/th/id/OIP.kiwNAFNljctfF5licux1mwHaJQ?rs=1&pid=ImgDetMain",
  42. "productTypeIds":[1,2,3,4],
  43. "productBidPrice": 18,
  44. "productSellPrice":6,
  45. "avatar": "",
  46. "type":"咖啡"
  47. },
  48. ], // 保存店面数据的数组
  49. types:[{id:1,productName:'饮料'}, {id:2,productName:'咖啡'}, {id:3,productName:'零食'}, {id:4,productName:'水果'}]
  50. }),
  51. mutations: {
  52. setProjects(state, projects) {
  53. state.projects = projects; // 更新店面数据
  54. }
  55. },
  56. actions: {
  57. //后端请求得到所有的店面的数据,保存入projects
  58. async fetchProjects({ commit }) {
  59. try {
  60. const response = await uni.request({
  61. url: uni.$http.baseUrl + 'product/selectAll',
  62. // url: SHOP.SELECTALLURL,
  63. method: 'GET',
  64. header: {
  65. 'content-type': 'application/json',
  66. 'token': uni.$http.testToken
  67. }
  68. });
  69. if (response.data && response.data.code === '200') {
  70. console.log(response.data.data);
  71. console.log('==');
  72. // 如果请求成功并且返回的数据中包含 code 为 '200',则提交 mutation 更新店面数据
  73. commit('setProjects', response.data.data);
  74. } else {
  75. console.error('请求失败或返回的数据不符合预期');
  76. }
  77. } catch (error) {
  78. console.error('请求失败:', error);
  79. }
  80. }
  81. },
  82. getters: {
  83. getProjects(state) {
  84. return state.projects
  85. },
  86. getTypes(state) {
  87. return state.types
  88. let types = []
  89. state.projects.forEach(project => {
  90. if (!types.includes(project.type)) {
  91. types.push(project.type)
  92. }
  93. })
  94. return types
  95. },
  96. // getProjectsByType: (state) => (type) => {
  97. // if (type=='all'||type=='全部商品'){
  98. // return state.projects
  99. // }
  100. // return state.projects.filter(project => project.type == type)
  101. // },
  102. // getProjectsByType: (state) => (type) => {
  103. // type=String(type)
  104. // if (type === 'all' || type === '全部商品') {
  105. // return state.projects;
  106. // }
  107. // // console.log('---------');
  108. // // console.log(state.projects);
  109. // // console.log(type);
  110. // // console.log('---------');
  111. // return state.projects.filter(project => {
  112. // let pType = String (project.type)
  113. // const categories = pType.split(' '); // 使用空格分割多类别
  114. // return categories.includes(type); // 包含特定类别的商品
  115. // });
  116. // }
  117. getProjectsByType: (state) => (typeId) => {
  118. let products=[]
  119. state.projects.forEach(project => {
  120. if (project.productTypeIds.includes(typeId)){
  121. products.push(project)
  122. }
  123. })
  124. return products
  125. }
  126. },
  127. };