beefast-mini-deliveryman/pages/index/index.js
2025-02-20 01:42:47 +08:00

358 lines
8.7 KiB
JavaScript

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
},
onLoad(){
const windowInfo = wx.getWindowInfo();
this.setData({
statusBarHeight:windowInfo.statusBarHeight,
scrollViewHeight:windowInfo.windowHeight-windowInfo.statusBarHeight-44 - 125
});
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.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}`;
}
const data = await orderApi.buildingList(cid,_status);
this.setData({
buildingList:data
});
},
loadList(){
if(this.data.pager.loading||this.data.pager.loadAll){
return;
}
this.setData({
"pager.loading":true
});
let params = {
building_id:this.data.buildingList[this.data.buildingIndex].building_id,
skip:this.data.pager.pageIndex*this.data.pager.limit,
limit:this.data.pager.limit,
}
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<this.data.pager.limit){
this.data.pager.loadAll = true;
}
data.items.map((item)=>{
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.setData({
isShowLeftPanel:true,
});
wx.nextTick(()=>{
this.setData({
isShowLeftPanelMask:true,
leftPanelMoveViewX:560
})
})
},
closeLeftPanel(){
this.setData({
leftPanelMoveViewX:0,
isShowLeftPanelMask:false
});
setTimeout(()=>{
this.setData({
isShowLeftPanel:false,
})
},400)
},
refund(){
wx.showModal({
title: '取消订单需联系客户说明原因',
content: '',
placeholderText:'请输入退款原因',
editable:true,
complete: (res) => {
if (res.confirm) {
}
}
})
},
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;
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(){
console.log(this.currentOrder);
if(this.data.tempImgs.length==0){
wx.showToast({
icon:'error',
title: '请选择快递照片',
})
return;
}
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
})
this.refreshList();
wx.showToast({
icon:'success',
title: '订单已完成',
})
})
});
},
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',
})
}
})
}
})