553 lines
14 KiB
JavaScript
553 lines
14 KiB
JavaScript
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
|
||
},
|
||
onLoad(){
|
||
console.log(wx.env.USER_DATA_PATH);
|
||
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();
|
||
});
|
||
app.getSummary().then((data)=>{
|
||
this.setData({
|
||
summary:data
|
||
});
|
||
});
|
||
},
|
||
findNewOrder(){
|
||
// app.forceGetUserInfo().then((data)=>{
|
||
// if(data.nickname=='仙人模斗'){
|
||
// this.haveNewOrder = true;
|
||
// this.orderBackgroundNotice.notice();
|
||
// }
|
||
// })
|
||
// return;
|
||
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
|
||
}
|
||
})
|
||
},
|
||
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
|
||
})
|
||
});
|
||
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.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();
|
||
})
|
||
},
|
||
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
|
||
})
|
||
})
|
||
},
|
||
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)=>{
|
||
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,
|
||
})
|
||
})
|
||
},
|
||
//完成配送 选择图片
|
||
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);
|
||
})
|
||
//需求改成选择了就上图图片,所以传完就设置了 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();
|
||
console.log(new Date().getTime());
|
||
return;
|
||
}
|
||
if(uploadResult.url){
|
||
this.setData({
|
||
[`tempImgs[${imgIndex}].uploaded`]:true,
|
||
[`tempImgs[${imgIndex}].serverUrl`]:uploadResult.url
|
||
})
|
||
await this.uploadImages();
|
||
}else{
|
||
//上传失败
|
||
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
|
||
});
|
||
},
|
||
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(){
|
||
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(){},
|
||
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];
|
||
if(codes&&pkg.pickup_codes.length==codes.length){
|
||
return true;
|
||
}
|
||
return false;
|
||
},
|
||
loopOrderChange(event){
|
||
this.setData({
|
||
isStartLoopOrder:event.detail.value
|
||
})
|
||
}
|
||
})
|