import {url} from '../request' Page({ data: { commoditiesImage: '', commoditiesPrice: '', commoditiesName: '', businessId: '', ids: '', time: [ { title: '9:00' }, { title: '10:00' }, { title: '11:00' }, { title: '12:00' }, { title: '13:00' }, { title: '14:00' }, { title: '15:00' }, { title: '16:00' }, { title: '17:00' }, ], selectedTime: '', }, onLoad(options) { const commoditiesImage = options.commoditiesImage; const commoditiesName = options.commoditiesName; const commoditiesPrice = options.commoditiesPrice; const ids = options.ids; const businessId = options.businessId; this.setData({ commoditiesImage: commoditiesImage, commoditiesName: commoditiesName, commoditiesPrice: commoditiesPrice, ids: ids, businessId: businessId, }); }, // 切换标签 onChange(current) { this.setData({ current, }); }, onSwipeChange(e) { this.setData({ current: e.detail.current, }); }, onShow(){ this.checkUserLogin }, // 检查登录状态 checkUserLogin() { my.getStorages({ key: 'userInfo', success: (res) => { if (res.data && res.data.openId) { } else { } }, fail: (err) => { console.error('获取用户信息失败:', err); }, }); }, jiaru() { my.getStorage({ key: 'userInfo', success: (res) => { const userInfo = res.data; if (userInfo && userInfo.cookie) { my.request({ url: url + '/api/cart/add', method: 'POST', data: { businessId: this.data.businessId, commoditiesId: this.data.ids, quantity: 1, selectedOptions: "", }, headers: { 'content-type': 'application/json', 'Cookie': userInfo.cookie, // 通过头部传递 cookie }, dataType: 'json', success: (res) => { if(res.data.code===0){ console.log(res); my.alert({ content: '成功添加到购物车' }); } else if(res.data.code===40100){ my.alert({ content: '登录信息已过期,请重新登录' }); my.navigateTo({ url:'/pages/denglu/denglu' }) } }, fail: (error) => { console.error('请求失败: ', JSON.stringify(error)); my.alert({ content: '请求失败,请稍后重试' }); }, }); } else { my.alert({ content: '您未登录,请先登录。', success: () => { my.navigateTo({ url: '/pages/denglu/denglu', }); }, }); } }, }); }, selectTime(e) { const selectedTime = e.currentTarget.dataset.time; // 获取点击的时间 console.log(selectedTime); this.setData({ selectedTime: selectedTime, // 更新选中的时间 }); console.log('选中的时间:', this.data.selectedTime); // 打印选中的时间 }, });