import orderApi from '../../api/order'; import userApi from '../../api/user'; const app = getApp(); Page({ currentOrder:null, data: { statusBarHeight:0, list:[], leftPanelEvent:null, scrollViewHeight:0, pager:{limit:10,loading:false,loadAll:false,pageIndex:0,refreshTrigger:false}, isShowConfirm:false, isShowLeftPanel:false, isShowLeftPanelMask:false, leftPanelMoveViewX:0, tempImgs:[], maxChooseImgCount:10, userInfo:{}, userInfoTrigger:false, summary:{}, orderSummary:{}, statusDetail:{ created:{ key:'CREATED',value:0,text:"待接单" }, received:{ key:'RECEIVED',value:0,text:"待取货" }, delivering:{ key:'DELIVERING',value:0,text:"送货中" }, completed:{ key:'COMPLETED',value:0,text:"已送达" } }, statusDetailKey:'created', buildingList:[], buildingIndex:0, orderStatus:orderApi.status, orderStatusKV:orderApi.statusKV, deliverStatusKV:orderApi.deliverStatusKV, genderKV:userApi.genderKV, completing:false, isShowRefundConfirm:false }, onLoad(){ const windowInfo = wx.getWindowInfo(); this.setData({ statusBarHeight:windowInfo.statusBarHeight, scrollViewHeight:windowInfo.windowHeight-windowInfo.statusBarHeight-44 - 91 }); this.getUserInfo().then(()=>{ return this.loadStatusDetail(); }).then((data)=>{ return this.loadBuilding(); }).then((data)=>{ this.loadList(); }); app.getSummary().then((data)=>{ this.setData({ summary:data }); }); }, async getUserInfo(){ await app.forceGetUserInfo().then((data)=>{ this.setData({ userInfo:data, userInfoTrigger:false }) }); userApi.orderSummary().then((data)=>{ this.setData({ orderSummary:data }) }); userApi.summary().then((data)=>{ this.setData({ summary:data }) }) }, setStatus(event){ const status = event.currentTarget.dataset.item; console.log(status); //先不setData 让加载出来之后再设置 this.data.statusDetailKey = status.key.toLowerCase(); this.loadBuilding().then((data)=>{ this.data.pager.pageIndex = 0; this.data.pager.loadAll = false; this.loadList(); }) }, setBuilding(event){ const buildingIndex = event.currentTarget.dataset.index; //先不setData 让加载出来之后再设置 this.data.buildingIndex = buildingIndex; this.data.pager.pageIndex = 0; this.data.pager.loadAll = false; this.loadList(); }, refreshList(){ this.loadStatusDetail().then(()=>{ return this.loadBuilding(); }).then(()=>{ this.data.pager.pageIndex = 0; this.data.pager.loadAll = false; this.loadList(); }) }, async loadStatusDetail(){ const data = await orderApi.statusDetail(this.data.userInfo.community_id); this.data.statusDetail.created.value = 0; this.data.statusDetail.received.value = 0; this.data.statusDetail.delivering.value = 0; this.data.statusDetail.completed.value = 0; data.map((item)=>{ if(item.status==this.data.orderStatus.unpaid||item.status==this.data.orderStatus.completed){ this.data.statusDetail.completed.value += item.count; }else{ if(this.data.statusDetail[item.status.toLowerCase()]){ this.data.statusDetail[item.status.toLowerCase()].value = item.count; } } }); console.log(this.data.statusDetail); this.setData({ statusDetail:this.data.statusDetail }) }, //加载楼栋 async loadBuilding(){ const cid = this.data.userInfo.community_id; const status = this.data.statusDetail[this.data.statusDetailKey]; let _status = status.key; if(status.key==this.data.orderStatus.completed){ _status = `${this.data.orderStatus.unpaid},${this.data.orderStatus.completed}`; } let data = await orderApi.buildingList(cid,_status); let totalCount = 0; (data||[]).map((item)=>{ totalCount+=item.order_count; }) data = [{building_name:'全部',order_count:totalCount}].concat(data); this.setData({ buildingList:data }); }, loadList(){ if(this.data.pager.loading||this.data.pager.loadAll){ return; } this.setData({ "pager.loading":true }); let params = { skip:this.data.pager.pageIndex*this.data.pager.limit, limit:this.data.pager.limit, } const buildingId = this.data.buildingList[this.data.buildingIndex].building_id; //有buildingId 就传 没有(全部) buildingId就传community_id 因为要过滤小区 if(buildingId){ params.building_id = buildingId; }else{ params.community_id = this.data.userInfo.community_id; } if(this.data.statusDetailKey=='completed'){ params.status = `${this.data.orderStatus.unpaid},${this.data.orderStatus.completed}` }else{ params.status = this.data.statusDetail[this.data.statusDetailKey].key; } orderApi.list(params).then((data)=>{ if(this.data.pager.pageIndex==0){ this.data.list = data.items; }else{ this.data.list = this.data.list.concat(data.items); } this.data.pager.loading = false; this.data.pager.pageIndex++; this.data.pager.refreshTrigger = false; if(data.items.length{ item.packages.map((pItem)=>{ pItem.pickup_codes = pItem.pickup_codes.split(','); }) }) this.setData({ list:this.data.list, pager:this.data.pager, statusDetailKey:this.data.statusDetailKey, buildingIndex:this.data.buildingIndex }); console.log(this.data.list); }) }, openLeftPanel(){ this.getUserInfo(); this.setData({ isShowLeftPanel:true, }); wx.nextTick(()=>{ this.setData({ isShowLeftPanelMask:true, leftPanelMoveViewX:560 }) }) }, closeLeftPanel(){ this.setData({ leftPanelMoveViewX:0, isShowLeftPanelMask:false }); setTimeout(()=>{ this.setData({ isShowLeftPanel:false, }) },400) }, showRefundConfirm(event){ const item = event.currentTarget.dataset.item; const index = event.currentTarget.dataset.index; if(item.receiving)return; this.refundOrderIndex = index; this.setData({ isShowRefundConfirm:true }) }, refund(event){ const index = this.refundOrderIndex; const item = this.data.list[index]; if(item.receiving)return; this.setData({ [`list[${index}].receiving`]:true }) orderApi.cancel(item.orderid,event.detail).then((data)=>{ wx.showToast({ title: '取消成功', icon:'success' }); this.refreshList(); }) }, chooseImage(){ wx.chooseMedia({ count:this.data.maxChooseImgCount - this.data.tempImgs.length, mediaType:['image'], sourceType:['camera'], success:(res)=>{ console.log(res); this.setData({ tempImgs:this.data.tempImgs.concat(res.tempFiles) }); } }); }, navToOrderDetail(event){ const id = event.currentTarget.dataset.id; wx.navigateTo({ url: `/pages/order-detail/index?id=${id}`, }) }, //接单 getOrder(event){ const item = event.currentTarget.dataset.item; const index = event.currentTarget.dataset.index; if(item.receiving)return; this.setData({ [`list[${index}].receiving`]:true }) orderApi.receive(item.orderid).then((data)=>{ wx.showToast({ icon:'success', title: '接单成功', }) this.refreshList(); }) }, //取货完毕 receivedOrder(event){ const item = event.currentTarget.dataset.item; orderApi.pickup(item.orderid).then((data)=>{ wx.showToast({ icon:'success', title: '取货成功', }) this.refreshList(); }) }, //完成配送 选择图片 confirmSend(event){ this.currentOrder = event.currentTarget.dataset.item; this.setData({ isShowConfirm:true }) }, //完成配送 uploadAndConfirmSend(){ if(this.data.tempImgs.length==0){ wx.showToast({ icon:'error', title: '请选择快递照片', }) return; } this.setData({ completing:true }) this.uploadImages().then(()=>{ let urls = []; this.data.tempImgs.map((item)=>{ urls.push(item.serverUrl); }) orderApi.complete(this.currentOrder.orderid,urls).then((data)=>{ this.setData({ isShowConfirm:false, completing:false, tempImgs:[] }) this.refreshList(); wx.showToast({ icon:'success', title: '订单已完成', }) }) }).catch(()=>{ this.setData({ completing:false }) }); }, async uploadImages(){ let imgIndex = -1; const file = this.data.tempImgs.find((item,index)=>{ imgIndex = index; return !item.uploaded; }); if(!file){ return; } const uploadResult = await userApi.uploadImg(file,(res)=>{ //进度 this.setData({ [`tempImgs[${imgIndex}].progress`]:res.progress }) }); console.log(uploadResult); if(uploadResult.url){ this.setData({ [`tempImgs[${imgIndex}].uploaded`]:true, [`tempImgs[${imgIndex}].serverUrl`]:uploadResult.url }) await this.uploadImages(); }else{ //上传失败 return new Error('失败') } }, leftPanelMove(event){ this.setData({ leftPanelEvent:event }); }, leftPanelMoveCancel(){ const leftPanelWidth = 280; if(this.data.leftPanelEvent.detail.x<280/4*3){ this.closeLeftPanel(); }else{ this.setData({ leftPanelMoveViewX:560 }) } }, logout(){ wx.removeStorage({ key: 'accessToken', success(){ wx.redirectTo({ url: '/pages/login/index', }) } }) }, concatUser(event){ const item = event.currentTarget.dataset.item; wx.makePhoneCall({ phoneNumber: item.address.phone, }) }, emptyFun(){} })