favor.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // store/modules/favor.js
  2. export default {
  3. namespaced: true,
  4. state: () => ({
  5. favors: [{
  6. "id": 1,
  7. "cupSize": ["大杯"],
  8. "hot": ["冰","热"],
  9. "sugar": ["标准甜","少甜","少少甜"],
  10. "cream": ["无奶油","加奶油3元"],
  11. "productName":"拿铁类"
  12. }
  13. ] // 保存店面数据的数组
  14. }),
  15. mutations: {
  16. setFavors(state, favors) {
  17. state.favors = favors; // 更新店面数据
  18. }
  19. },
  20. actions: {
  21. //后端请求得到所有的店面的数据,保存入shops
  22. async fetchFavors({ commit }) {
  23. // return
  24. try {
  25. const response = await uni.request({
  26. url: uni.$http.baseUrl + 'productFlavor/selectAll',
  27. method: 'GET',
  28. header: {
  29. 'content-type': 'application/json',
  30. 'token': uni.$http.testToken
  31. }
  32. });
  33. if (response.data && response.data.code === '200') {
  34. // 如果请求成功并且返回的数据中包含 code 为 '200',则提交 mutation 更新店面数据
  35. console.log(response.data);
  36. commit('setFavors', response.data.data);
  37. console.log(response.data.data,"-------------------------");
  38. } else {
  39. console.error('请求失败或返回的数据不符合预期');
  40. }
  41. } catch (error) {
  42. console.error('请求失败:', error);
  43. }
  44. }
  45. },
  46. getters: {
  47. getFavorById: (state) => (id) => {
  48. console.log(id,'不算慢');
  49. return state.favors.find(favor => favor.id == id)
  50. }
  51. // getShopById(state , shopId,prarm3,prarm4){
  52. // console.log(state);
  53. // console.log(shopId);
  54. // console.log(prarm3);
  55. // console.log(prarm4);
  56. // console.log('-----------');
  57. // shopId=2
  58. // // 从 shops 数组中查找与指定 id 相匹配的店面信息
  59. // console.log(state.shops);
  60. // let shops=state.shops
  61. // for (let idx in shops) {
  62. // console.log(idx);
  63. // // console.log(shops[idx].id==shopId);
  64. // console.log(shops[idx].id,shops[idx].id==shopId,shopId);
  65. // if (shops[idx].id==shopId){
  66. // let shop =shops[idx]
  67. // return shop
  68. // }
  69. // }
  70. // console.log(data);
  71. // return data
  72. // }
  73. // getShopById: (state) => (shopId) => {
  74. // console.log(shopId);
  75. // // 从 shops 数组中查找与指定 id 相匹配的店面信息
  76. // console.log(state.shops);
  77. // let shops = state.shops;
  78. // for (let idx in shops) {
  79. // console.log(idx);
  80. // console.log(shops[idx].id == shopId);
  81. // console.log(shops[idx].id, shops[idx].id == shopId, shopId);
  82. // if (shops[idx].id == shopId) {
  83. // let shop = shops[idx];
  84. // return shop;
  85. // }
  86. // }
  87. // }
  88. },
  89. };