import commonApi from '../../../api/common'; import userApi from '../../../api/user'; const app = getApp(); Page({ savedTimePeriodId:'', /** * 页面的初始数据 */ data: { stationList:[], sendType:'DELIVERY_AT_DOORSTEP',//默认方式 tempImgs:[], imgUploading:false, imgOrderCount:0, maxChooseImgCount:10, sendTypeKV:userApi.order.orderDeliverStatusKV, timePeriods:[], timePeriodsIndex:-1, isShowDeliverType:false }, validator:{ imgOrderCount:{min:1,shake:true} }, async bottomBarButtonTap(){ if(this.data.tempImgs&&this.data.tempImgs.length>0){ const valid = app.validateForm(this.validator,this); console.log(this.validator,valid); if(valid.length>0){ wx.showToast({ icon:'none', title: '请选择包裹数量', }) return; } this.setData({ imgUploading:true }) const rs = await this.uploadImages(); this.setData({ imgUploading:false }) if(rs instanceof Error){ return; } } const data = []; this.data.stationList.map((item)=>{ if(item.package.length>0&&item.package[0]!=''){ data.push({ station_id:item.id, station_name:item.name, pickup_codes:item.package.filter((item)=>item!='').join(',') }); } }) //录入了有效取件码 或者 上传了取件图片 if(data.length>0||this.data.tempImgs.length>0){ let priceRequest = { packages:data }; if(this.data.tempImgs.length>0){ let imgs = []; this.data.tempImgs.map((item)=>{ imgs.push(item.serverUrl); }); priceRequest.pickup_images_count = this.data.imgOrderCount; priceRequest.pickup_images = imgs.join(',') } if(this.data.timePeriodsIndex==-1){ wx.showToast({ icon:'none', title:'请选择配送时段' }); this.setData({ scrollToViewId:'scrollViewDispatch' }) return; } console.log('this.data.sendType',this.data.sendType) wx.setStorage({ key:'pre-order', data:{ price_request:priceRequest, delivery_method:this.data.sendType, community_time_period_id:this.data.timePeriods[this.data.timePeriodsIndex].communtiy_time_period_id, delivery_date:this.data.timePeriods[this.data.timePeriodsIndex].time_period_date }, success(){ wx.navigateBack(); } }) }else{ wx.removeStorage({ key: 'pre-order', success(){ wx.navigateBack(); } }) } }, addPackage(event){ const index = event.currentTarget.dataset.index; let packages = this.data.stationList[index].package; if(!packages){ packages = []; } if(packages.length>1){ if(packages.filter((item)=>item==packages[packages.length-1]).length>1){ wx.showToast({ icon:'none', title: '取件码重复' }) this.data.stationList[index].focus = true; this.setData({ [`stationList[${index}].focus`]:true }); return; } } let hasEmptyInput = packages.find((item)=>item=='')!=undefined; if(!hasEmptyInput){ packages.push(''); } this.data.stationList[index].focus = true; this.setData({ [`stationList[${index}]`]:this.data.stationList[index] }); }, deletePackage(event){ const itemIndex = event.currentTarget.dataset.index; const packageIndex = event.currentTarget.dataset.p_index; this.data.stationList[itemIndex].package.splice(packageIndex,1); this.setData({ [`stationList[${itemIndex}].package`]:this.data.stationList[itemIndex].package }) }, setPackageCode(event){ const itemIndex = event.currentTarget.dataset.index; const packageIndex = event.currentTarget.dataset.p_index; this.data.stationList[itemIndex].package[packageIndex] = event.detail.value; }, checkInput(event){ const itemIndex = event.currentTarget.dataset.index; const packageIndex = event.currentTarget.dataset.p_index; let packages = this.data.stationList[itemIndex].package; if(packages.length>1){ if(packages.filter((item)=>item==packages[packages.length-1]).length>1){ wx.showToast({ icon:'none', title: '取件码重复' }) this.setData({ [`stationList[${itemIndex}].package[${packageIndex}]`]:'' }); } } }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { let preOrder = wx.getStorageSync('pre-order'); this.savedTimePeriodId = preOrder.community_time_period_id; commonApi.station.list(options.communityId).then((data)=>{ data.items.map((item,index)=>{ if(preOrder){ const __item = preOrder.price_request.packages.find((_item)=>_item.station_id==item.id); if(__item){ item.package = __item.pickup_codes.split(',')||[]; return; } } item.package = []; }); let tempImgs = [],imgOrderCount = 0; if(preOrder?.price_request?.pickup_images){ const imgs = preOrder.price_request.pickup_images.split(','); imgs.map((item)=>{ tempImgs.push({ serverUrl:item, uploaded:true }) }); imgOrderCount = preOrder.price_request.pickup_images_count||0; } this.setData({ sendType:preOrder.delivery_method||this.data.sendType, stationList:data.items, tempImgs, imgOrderCount }); //获取配送时段 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 }); }) }, chooseImage(){ wx.chooseMedia({ count:this.data.maxChooseImgCount - this.data.tempImgs.length, mediaType:['image'], success:(res)=>{ this.setData({ tempImgs:this.data.tempImgs.concat(res.tempFiles) }); wx.nextTick(()=>{ this.uploadImages(); }) } }) }, async uploadImages(){ let imgIndex = -1; const file = this.data.tempImgs.find((item,index)=>{ imgIndex = index; return !item.uploaded; }); if(!file){ return; } var onProgress = (res)=>{ //进度 this.setData({ [`tempImgs[${imgIndex}].progress`]:res.progress }) } //无奈之举,不大范围改动代码的同时,我需要获取到上传任务task,来中断上传操作,不然要出问题task在上传时被附加到了onProgress this.data.tempImgs[imgIndex].onProgress = onProgress; let uploadResult = {}; try{ uploadResult = await commonApi.uploadImg(file,onProgress); }catch(e){ // await this.uploadImages(); // return; } if(uploadResult.url){ this.setData({ [`tempImgs[${imgIndex}].uploaded`]:true, [`tempImgs[${imgIndex}].serverUrl`]:uploadResult.url }) await this.uploadImages(); }else{ //上传失败 wx.showToast({ icon:'error', title: '图片上传失败', }) return new Error('失败') } }, removeImage(event){ const index = event.currentTarget.dataset.index; if(this.data.tempImgs[index].onProgress&&this.data.tempImgs[index].onProgress.task){ this.data.tempImgs[index].onProgress.task.abort(); } console.log('remove',new Date().getTime()); this.data.tempImgs.splice(index,1); this.setData({ tempImgs:this.data.tempImgs }); }, reduceImgOrderCount(){ if(this.data.imgOrderCount<=1)return; this.setData({ imgOrderCount:this.data.imgOrderCount-1 }) }, plusImgOrderCount(){ this.setData({ imgOrderCount:this.data.imgOrderCount+1 }) }, 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 }) } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })