// store/modules/favor.js export default { namespaced: true, state: () => ({ favors: [{ "id": 1, "cupSize": ["大杯"], "hot": ["冰","热"], "sugar": ["标准甜","少甜","少少甜"], "cream": ["无奶油","加奶油3元"], "productName":"拿铁类" } ] // 保存店面数据的数组 }), mutations: { setFavors(state, favors) { state.favors = favors; // 更新店面数据 } }, actions: { //后端请求得到所有的店面的数据,保存入shops async fetchFavors({ commit }) { // return try { const response = await uni.request({ url: uni.$http.baseUrl + 'productFlavor/selectAll', method: 'GET', header: { 'content-type': 'application/json', 'token': uni.$http.testToken } }); if (response.data && response.data.code === '200') { // 如果请求成功并且返回的数据中包含 code 为 '200',则提交 mutation 更新店面数据 console.log(response.data); commit('setFavors', response.data.data); console.log(response.data.data,"-------------------------"); } else { console.error('请求失败或返回的数据不符合预期'); } } catch (error) { console.error('请求失败:', error); } } }, getters: { getFavorById: (state) => (id) => { console.log(id,'不算慢'); return state.favors.find(favor => favor.id == id) } // getShopById(state , shopId,prarm3,prarm4){ // console.log(state); // console.log(shopId); // console.log(prarm3); // console.log(prarm4); // console.log('-----------'); // shopId=2 // // 从 shops 数组中查找与指定 id 相匹配的店面信息 // console.log(state.shops); // let shops=state.shops // for (let idx in shops) { // console.log(idx); // // console.log(shops[idx].id==shopId); // console.log(shops[idx].id,shops[idx].id==shopId,shopId); // if (shops[idx].id==shopId){ // let shop =shops[idx] // return shop // } // } // console.log(data); // return data // } // getShopById: (state) => (shopId) => { // console.log(shopId); // // 从 shops 数组中查找与指定 id 相匹配的店面信息 // console.log(state.shops); // let shops = state.shops; // for (let idx in shops) { // console.log(idx); // console.log(shops[idx].id == shopId); // console.log(shops[idx].id, shops[idx].id == shopId, shopId); // if (shops[idx].id == shopId) { // let shop = shops[idx]; // return shop; // } // } // } }, };