import commonApi from '../../../api/common'; import userApi from '../../../api/user'; const app = getApp(); Page({ savedTimePeriodId:'', /** * 页面的初始数据 */ data: { stationList:[], choosedStationList:[], sendType:'DELIVERY_AT_DOORSTEP',//默认方式 imgUploading:false, sendTypeKV:userApi.order.orderDeliverStatusKV, timePeriods:[], timePeriodsIndex:-1, isShowDeliverType:false, recogniting:false, isShowStationEditor:false, currentEditStation:{} }, async bottomBarButtonTap(){ const data = []; let hasSame = false; this.data.choosedStationList.find((item,index)=>{ if(item.pickup_codes.length>0&&item.pickup_codes[0]!=''){ //倒着找相同的索引 因为需要从后往前找 const codes = item.pickup_codes.join(',').split(',').reverse(); let findIndex = codes.findIndex((_item)=>{ let sames = codes.filter((__item)=>_item==__item); //找到了相同的 if(sames.length>1){ hasSame = true; return true; } }) if(findIndex>-1){ //再反转取得最后一个相同的 index findIndex = item.pickup_codes.length - findIndex - 1; this.data.choosedStationList[index].focus = true; this.data.choosedStationList[index].focusIndex = findIndex; wx.showToast({ icon:'none', title: '取件码重复', }) console.log(this.data.choosedStationList[index],'----'); this.setData({ [`choosedStationList[${index}]`]:this.data.choosedStationList[index] }) return true; } data.push({ id:item.id||'', name:item.name, imgUrl:item.imgUrl||'', service_text:item.service_text, pickup_codes:item.pickup_codes.filter((item)=>item!='') }); } }) if(hasSame)return; //录入了有效取件码 或者 上传了取件图片||this.data.tempImgs.length>0 if(data.length>0){ let priceRequest = { packages:data }; if(this.data.timePeriodsIndex==-1){ wx.showToast({ icon:'none', title:'请选择配送时段' }); this.setData({ scrollToViewId:'scrollViewDispatch' }) return; } const periodId = this.data.timePeriods[this.data.timePeriodsIndex].communtiy_time_period_id; const deliveryDate = this.data.timePeriods[this.data.timePeriodsIndex].time_period_date; wx.setStorage({ key:'pre-order', data:{ price_request:priceRequest, delivery_method:this.data.sendType, community_time_period_id:periodId, delivery_date:deliveryDate }, success(){ wx.navigateBack(); } }) }else{ wx.removeStorage({ key: 'pre-order', success(){ wx.navigateBack(); } }) } }, addPackage(event){ const index = event.currentTarget.dataset.index; let packages = this.data.choosedStationList[index].pickup_codes; let hasEmptyInput = packages.find((item)=>item=='')!=undefined; if(!hasEmptyInput){ packages.push(''); } this.data.choosedStationList[index].focus = true; this.setData({ [`choosedStationList[${index}]`]:this.data.choosedStationList[index] }); }, deletePackage(event){ const itemIndex = event.currentTarget.dataset.index; const packageIndex = event.currentTarget.dataset.p_index; this.data.choosedStationList[itemIndex].pickup_codes.splice(packageIndex,1); if(this.data.choosedStationList[itemIndex].pickup_codes.length==0){ //code 删完了就删除station this.data.choosedStationList.splice(itemIndex,1); this.setData({ choosedStationList:this.data.choosedStationList }) }else{ //仅仅删除 code this.setData({ [`choosedStationList[${itemIndex}].pickup_codes`]:this.data.choosedStationList[itemIndex].pickup_codes }) } }, setPackageCode(event){ const itemIndex = event.currentTarget.dataset.index; const packageIndex = event.currentTarget.dataset.p_index; this.data.choosedStationList[itemIndex].pickup_codes[packageIndex] = event.detail.value; }, checkInput(event){ const itemIndex = event.currentTarget.dataset.index; const packageIndex = event.currentTarget.dataset.p_index; let packages = this.data.choosedStationList[itemIndex].pickup_codes; if(packages.length>1){ if(packages.filter((item)=>item==packages[packages.length-1]).length>1){ wx.showToast({ icon:'none', title: '取件码重复' }) //清空 // this.setData({ // [`choosedStationList[${itemIndex}].pickup_codes[${packageIndex}]`]:'' // }); //体验不好 // this.data.choosedStationList[itemIndex].focus = true; // this.data.choosedStationList[itemIndex].focusIndex = packageIndex; // this.setData({ // [`choosedStationList[${itemIndex}]`]:this.data.choosedStationList[itemIndex] // }) } } //清除焦点 有时候 setdata 会触发获取焦点 this.setData({ [`choosedStationList[${itemIndex}].focus`]:false }) }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { let preOrder = wx.getStorageSync('pre-order'); this.savedTimePeriodId = preOrder.community_time_period_id; if(preOrder&&preOrder.price_request){ this.setData({ choosedStationList:preOrder.price_request.packages }) } commonApi.station.list(options.communityId).then((data)=>{ this.setData({ sendType:preOrder.delivery_method||this.data.sendType, stationList:data.items, }); //获取配送时段 return commonApi.community.timePeriods(options.communityId); }).then((data)=>{ let timePeriodsIndex = data.findIndex((item)=>{ //找到之前选择的时段 if(item.communtiy_time_period_id==this.savedTimePeriodId){ //如果没满 if(!item.order_full){ return true; } } }); //如果还是没找到,就去找is_default 还是没有就让用户去选择,这里应该是没有可选的了 if(timePeriodsIndex==-1){ timePeriodsIndex = data.findIndex((item)=>item.is_default); } data.map((item)=>{ // item.order_full = true; }) this.setData({ timePeriods:data, timePeriodsIndex }); }) }, manuallyAdd(){ let stationNames = this.data.stationList.map((item)=>item.name); if(stationNames.length==0){ wx.showToast({ icon:'none', title: '该小区未配置驿站', }) return; } wx.showActionSheet({ itemList: stationNames, success:(res)=>{ const station = this.data.stationList[res.tapIndex]; const alreadyIndex = this.data.choosedStationList.findIndex((item)=>item.id==station.id); if(alreadyIndex>-1){ const _package = this.data.choosedStationList[alreadyIndex].pickup_codes; if((_package[_package.length-1]||'').trim()!=''){ _package.push(''); } this.data.choosedStationList[alreadyIndex].focus = true; this.setData({ [`choosedStationList[${alreadyIndex}]`]:this.data.choosedStationList[alreadyIndex] }) }else{ this.data.choosedStationList.unshift({ id:station.id, name:station.name, service_text:station.service_text, pickup_codes:[''], focus:true }) this.setData({ choosedStationList:this.data.choosedStationList }) // this.addAnimation(0); } } }) }, addAnimation(index){ const duration = 2000; // let animation = wx.createAnimation({ duration:duration }); animation.opacity(1).height('390rpx').step(); wx.nextTick(()=>{ console.log(this.data.choosedStationList[index]); this.setData({ [`choosedStationList[${index}].animation`]:animation.export() }) setTimeout(() => { this.setData({ [`choosedStationList[${index}].animationOver`]:true, [`choosedStationList[${index}].animation`]:null }) }, duration); }) }, chooseImage(){ if(this.data.imgUploading||this.data.recogniting)return; wx.chooseMedia({ count:1, mediaType:['image'], success:(res)=>{ this.setData({ imgUploading:true }) commonApi.uploadImg(res.tempFiles[0]).then((data)=>{ this.setData({ imgUploading:false }) this.recognition(data.url); }) } }) }, async recognition(url){ this.setData({ recogniting:true }) commonApi.getPickupCodeWidthImgUrl(url).then((data)=>{ if(data[0]&&data[0].stations){ const stations = data[0].stations; let s = []; stations.map((item)=>{ s.push({ imgUrl:url, name:item.name, pickup_codes:item.pickup_codes }); }) this.setData({ choosedStationList:s.concat(this.data.choosedStationList) }) } wx.showToast({ icon:'none', title: '识别成功', }) }).catch(()=>{ }).finally(()=>{ this.setData({ recogniting:false }) }) }, showStationName(event){ const index = event.currentTarget.dataset.index; let item = this.data.choosedStationList[index]; this.setData({ isShowStationEditor:true, currentEditStation:item }) }, editStationName(event){ if(event.detail.trim()==''){ wx.showToast({ icon:'none', title: '驿站名称不能为空', }) return; } const index = this.data.choosedStationList.findIndex((item)=>item.imgUrl==this.data.currentEditStation.imgUrl); this.setData({ [`choosedStationList[${index}].name`]:event.detail }) }, showSendType(){ this.setData({ isShowDeliverType:true }) }, confirmDeliverType(){ this.setData({ isShowDeliverType:false }) }, chooseDeliverType(event){ this.setData({ sendType:event.currentTarget.dataset.index }) }, chooseTime(event){ const index = event.currentTarget.dataset.index; const item = this.data.timePeriods[index]; if(!item.order_full){ this.setData({ timePeriodsIndex:index }) } }, viewImage(event){ const url = event.currentTarget.dataset.url; wx.previewImage({ urls: [url], }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })