beefast-mini-deliveryman/pages/index/index.js
2025-03-16 20:11:17 +08:00

626 lines
16 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import orderApi from '../../api/order';
import userApi from '../../api/user';
import {hidePhoneNumber,getStatusNavBarHeight} from '../../utils/util';
const app = getApp();
Page({
currentOrder:null,
haveNewOrder:false,
data: {
statusBarHeight:0,
snBarHeight: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,
isStartLoopOrder:false,
bgNoticeLoadResult:{
error:false,
loading:true
},
unReadOrderCount:0,
selectedOrderId:'',
markupStatusKV:orderApi.markUp.statusKV,
appConfig:{},
isShowConfirmDelivering:false
},
onLoad(){
this.orderBackgroundNotice = this.selectComponent('#orderBackgroundNotice');
const snHeight = getStatusNavBarHeight();
this.setData({
statusBarHeight:snHeight.statusBar,
snBarHeight:snHeight.navBar+snHeight.statusBar
});
this.getUserInfo().then(()=>{
return this.loadStatusDetail();
}).then((data)=>{
return this.loadBuilding();
}).then((data)=>{
this.loadList();
});
},
findNewOrder(){
orderApi.check().then((data)=>{
if(data.has_new_order){
this.orderBackgroundNotice.notice();
this.setData({
unReadOrderCount:this.data.unReadOrderCount+data.order_ids.length
})
}
})
},
bgNoticeSuccess(){
this.setData({
bgNoticeLoadResult:{
error:false,
loading:false
}
})
},
stopBGNotice(){
this.setData({
isStartLoopOrder:false
})
},
bgNoticeError(){
wx.showToast({
icon:'error',
title: '资源加载失败',
})
this.setData({
bgNoticeLoadResult:{
error:true,
loading:false
}
})
},
bgNoticeRetry(){
this.setData({
bgNoticeLoadResult:{
error:false,
loading:true
}
})
this.orderBackgroundNotice.downloadResource();
},
async getUserInfo(){
await app.forceGetUserInfo().then((data)=>{
this.setData({
userInfo:data,
userInfoTrigger:false
})
});
userApi.orderSummary().then((data)=>{
this.setData({
orderSummary:data
})
});
app.forceGetSummary().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.data.buildingIndex = 0;
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();
})
},
refreshCurrentList(){
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)=>{
const date = new Date();
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)=>{
if(item.status==this.data.orderStatus.created||item.status==this.data.orderStatus.completed||item.status==this.data.orderStatus.unpaid){
item.address.phone = hidePhoneNumber(item.address.phone);
}
item.packages.map((pItem)=>{
pItem.receivedAll = this.getPackageReceiveStatus(item.orderid,pItem);
})
})
let needSetData = {
list:this.data.list,
pager:this.data.pager,
statusDetailKey:this.data.statusDetailKey,
buildingIndex:this.data.buildingIndex
}
if(this.data.statusDetailKey==this.data.statusDetail.created.key.toLowerCase()){
needSetData.unReadOrderCount = 0;
}
this.setData(needSetData);
})
},
openLeftPanel(){
this.getUserInfo();
this.setData({
isShowLeftPanel:true,
});
wx.nextTick(()=>{
this.setData({
isShowLeftPanelMask:true,
leftPanelMoveViewX:510
})
});
app.getAppConfig().then((data)=>{
this.setData({
appConfig:data
});
})
},
closeLeftPanel(){
this.setData({
leftPanelMoveViewX:0,
isShowLeftPanelMask:false
});
setTimeout(()=>{
this.setData({
isShowLeftPanel:false,
})
},400)
},
showMoreAS(event){
const item = event.currentTarget.dataset.item;
const index = event.currentTarget.dataset.index;
//这里不能记录 index 因为列表可能会因为网络等各种原因刷新,后续的操作就可能找到其他订单
this.currentOrder = item;
const markupAction = ()=>{
const markupView = this.selectComponent('#markupView');
markupView.show(this.currentOrder)
}
const makePhoneCallAction = ()=>{
wx.makePhoneCall({
phoneNumber: this.currentOrder.address.phone,
});
}
const refundAction = ()=>{
if(this.currentOrder.receiving)return;
this.setData({
isShowRefundConfirm:true
});
}
let items = [];
let actions = [];
if(item.status==this.data.orderStatus.created){
items = ['联系用户','取消订单'];
actions = [makePhoneCallAction,refundAction]
}else if(item.status==this.data.orderStatus.received){
items = ['包裹加价','联系用户','取消订单'];
actions = [markupAction,makePhoneCallAction,refundAction]
}else if(item.status==this.data.orderStatus.delivering){
items = ['联系用户'];
actions = [makePhoneCallAction]
}
if(items.length!=actions.length){
throw new Error('sb')
}
wx.showActionSheet({
itemList: items,
success:(res)=>{
console.log(res);
actions[res.tapIndex]();
}
})
},
refund(event){
const item = this.currentOrder;
if(item.receiving)return;
const index = this.data.list.findIndex((item)=>item.orderid==this.currentOrder.orderid);
if(index>-1){
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)=>{
this.setData({
tempImgs:this.data.tempImgs.concat(res.tempFiles)
});
wx.nextTick(()=>{
console.log('nextTick');
this.uploadImages();
})
}
});
},
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 index = event.currentTarget.dataset.index;
const item = event.currentTarget.dataset.item;
const hasUnReceive = !!item.packages.find((p)=>!p.receivedAll);
if(hasUnReceive){
this.setData({
[`list[${index}].receiving`]:false
})
wx.showToast({
icon:'none',
title: '请先取件',
})
return;
}
orderApi.pickup(item.orderid).then((data)=>{
wx.showToast({
icon:'success',
title: '取货成功',
})
this.refreshList();
wx.removeStorage({
key: item.orderid,
})
}).catch(()=>{
this.setData({
[`list[${index}].receiving`]:false
})
})
},
//完成配送 选择图片
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((data)=>{
let urls = [];
this.data.tempImgs.map((item)=>{
urls.push(item.serverUrl);
})
//需求改成选择了就上图图片,所以传完就设置了 false这里要发请求再次改成true
this.setData({
completing:true
})
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){
this.setData({
completing:false
})
return;
}
this.setData({
completing:true
})
let onProgress = (res)=>{
//进度
this.setData({
[`tempImgs[${imgIndex}].progress`]:res.progress
})
}
//无奈之举不大范围改动代码的同时我需要获取到上传任务task来中断上传操作不然要出问题task在上传时被附加到了onProgress
this.data.tempImgs[imgIndex].onProgress = onProgress;
let uploadResult = {};
try {
uploadResult = await userApi.uploadImg(file,onProgress);
} catch (error) {
// await this.uploadImages();
// return;
}
if(uploadResult.url){
this.setData({
[`tempImgs[${imgIndex}].uploaded`]:true,
[`tempImgs[${imgIndex}].serverUrl`]:uploadResult.url
})
await this.uploadImages();
}else{
//上传失败
this.setData({
completing:false
})
wx.showToast({
icon:'error',
title: '上传失败',
})
throw 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
});
},
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:510
})
}
},
logout(){
app.logout();
},
concatUser(event){
const item = event.currentTarget.dataset.item;
wx.makePhoneCall({
phoneNumber: item.address.phone,
})
},
emptyFun(){},
onShow(){
console.log('onshow');
if(this.data.statusDetailKey==this.data.statusDetail.received.key.toLowerCase()){
let needRefreshData = {}
this.data.list.map((item,index)=>{
item.packages.map((pItem,pIndex)=>{
let reveivedAll = this.getPackageReceiveStatus(item.orderid,pItem);
needRefreshData[`list[${index}].packages[${pIndex}].receivedAll`] = reveivedAll;
})
})
this.setData(needRefreshData);
}
if(this.haveNewOrder){
this.data.statusDetailKey = this.data.statusDetail.created.key.toLowerCase();
this.data.buildingIndex = 0;
this.refreshList();
this.haveNewOrder = false;
}
},
getPackageReceiveStatus(orderId,pkg){
let receivedInfo = wx.getStorageSync(orderId);
let codes = receivedInfo[pkg.id];
let tempVar = {};
pkg.pickup_codes.map((code)=>{
tempVar[code] = 1;
})
if(codes&&Object.keys(tempVar).length==codes.length){
return true;
}
return false;
},
loopOrderChange(event){
this.setData({
isStartLoopOrder:event.detail.value
})
},
switchOnTap(){
this.setData({
isShowConfirmDelivering:true
})
},
setDelivering(){
userApi.setDelivering(!this.data.userInfo.is_delivering).then(()=>{
return app.forceGetUserInfo();
}).then((data)=>{
this.setData({
userInfo:data
})
});
},
navToAgreement(){
wx.navigateTo({
url: `/pages/browser/index?url=${this.data.appConfig.url_user_agreement}`,
})
}
})