// import SHOP from '../staticURL.js' export default { namespaced: true, state: () => ({ projects: [{ "id": 2, "productStata":"充足", "productName": "可乐", "productIcons":"https://th.bing.com/th/id/OIP.kiwNAFNljctfF5licux1mwHaJQ?rs=1&pid=ImgDetMain", "productTypeIds":[1,2,3,4], "productBidPrice": 8, "productSellPrice":6, "avatar": "", "type":"饮料" }, { "id": 1, "productStata":"充足", "productName": "雪碧", "productIcons":"https://th.bing.com/th/id/OIP.kiwNAFNljctfF5licux1mwHaJQ?rs=1&pid=ImgDetMain", "productTypeIds":[1,2,3,4], "productBidPrice": 8, "productSellPrice":6, "avatar": "", "type":"饮料" }, { "id": 11, "productStata":"充足", "productName": "拿铁", "productIcons":"https://th.bing.com/th/id/OIP.kiwNAFNljctfF5licux1mwHaJQ?rs=1&pid=ImgDetMain", "productTypeIds":[1,2,3,4], "productBidPrice": 18, "productSellPrice":6, "avatar": "", }, { "id": 12, "productStata":"充足", "productName": "咖啡", "productIcons":"https://th.bing.com/th/id/OIP.kiwNAFNljctfF5licux1mwHaJQ?rs=1&pid=ImgDetMain", "productTypeIds":[1,2,3,4], "productBidPrice": 18, "productSellPrice":6, "avatar": "", "type":"咖啡" }, ], // 保存店面数据的数组 types:[{id:1,productName:'饮料'}, {id:2,productName:'咖啡'}, {id:3,productName:'零食'}, {id:4,productName:'水果'}] }), mutations: { setProjects(state, projects) { state.projects = projects; // 更新店面数据 } }, actions: { //后端请求得到所有的店面的数据,保存入projects async fetchProjects({ commit }) { try { const response = await uni.request({ url: uni.$http.baseUrl + 'product/selectAll', // url: SHOP.SELECTALLURL, method: 'GET', header: { 'content-type': 'application/json', 'token': uni.$http.testToken } }); if (response.data && response.data.code === '200') { console.log(response.data.data); console.log('=='); // 如果请求成功并且返回的数据中包含 code 为 '200',则提交 mutation 更新店面数据 commit('setProjects', response.data.data); } else { console.error('请求失败或返回的数据不符合预期'); } } catch (error) { console.error('请求失败:', error); } } }, getters: { getProjects(state) { return state.projects }, getTypes(state) { return state.types let types = [] state.projects.forEach(project => { if (!types.includes(project.type)) { types.push(project.type) } }) return types }, // getProjectsByType: (state) => (type) => { // if (type=='all'||type=='全部商品'){ // return state.projects // } // return state.projects.filter(project => project.type == type) // }, // getProjectsByType: (state) => (type) => { // type=String(type) // if (type === 'all' || type === '全部商品') { // return state.projects; // } // // console.log('---------'); // // console.log(state.projects); // // console.log(type); // // console.log('---------'); // return state.projects.filter(project => { // let pType = String (project.type) // const categories = pType.split(' '); // 使用空格分割多类别 // return categories.includes(type); // 包含特定类别的商品 // }); // } getProjectsByType: (state) => (typeId) => { let products=[] state.projects.forEach(project => { if (project.productTypeIds.includes(typeId)){ products.push(project) } }) return products } }, };