beefast-mini-deliveryman/pages/order-detail/index.js
2025-03-17 19:12:00 +08:00

265 lines
6.6 KiB
JavaScript
Raw Permalink 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';
const app = getApp();
import {hidePhoneNumber} from '../../utils/util';
Page({
orderId:'',
/**
* 页面的初始数据
*/
data: {
orderDetail:{},
refreshTrigger:false,
orderStatus:orderApi.status,
orderStatusKV:orderApi.statusKV,
deliverStatusKV:orderApi.deliverStatusKV,
genderKV:userApi.genderKV,
orderStep:0,
receiving:false,
isShowRefundConfirm:false
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
this.orderId = options.id;
this.getOrderDetail();
},
getOrderDetail(){
orderApi.detail(this.orderId).then((data)=>{
let orderStep = 0;
if(data.status==this.data.orderStatus.received){
orderStep = 1;
}else if(data.status==this.data.orderStatus.delivering){
orderStep = 2;
}else if(data.status==this.data.orderStatus.unpaid||data.status==this.data.orderStatus.completed){
orderStep = 3;
}
data.complete_time = this.formatTime(data.complete_time);
data.pickup_time = this.formatTime(data.pickup_time);
data.received_time = this.formatTime(data.received_time);
const receivedStatusCode = wx.getStorageSync(data.orderid);
data.packages.map((item,index)=>{
//formattedCode 预防有重复 code造成选中问题,如果code全部显示出来可能跑腿的在取了快递之后后面遇到重复的这个会找不到包裹因为已经取了
item.formattedCode = {};
item.pickup_codes.map((code)=>{
if(!item.formattedCode[code]){
item.formattedCode[code] = {
sameCount:item.pickup_codes.filter((c)=>c==code).length
}
}
})
if(receivedStatusCode){
if(Object.keys(item.formattedCode).length==(receivedStatusCode[item.id]||[]).length){
item.receivedAll = true;
}
(receivedStatusCode[item.id]||[]).map((code)=>{
item.formattedCode[code].checked = true;
})
}
});
console.log(data.packages);
if(data.status==this.data.orderStatus.created||data.status==this.data.orderStatus.completed||data.status==this.data.orderStatus.unpaid){
data.address_phone = hidePhoneNumber(data.address_phone);
}
this.setData({
orderDetail:data,
refreshTrigger:false,
receiving:false,
orderStep
})
}).catch((res)=>{
this.setData({
refreshTrigger:false
})
})
},
copyOrderId(){
wx.setClipboardData({
data: this.data.orderDetail.orderid,
})
},
formatTime(time){
return (time||'').substr(5,11);
},
preview(event){
const current = event.currentTarget.dataset.url;
wx.previewImage({
current:current,
urls: this.data.orderDetail.complete_images,
})
},
previewPackageImage(event){
const current = event.currentTarget.dataset.item;
wx.previewImage({
current:current,
urls: this.data.orderDetail.pickup_images,
})
},
selectPackageCode(event){
const index = event.currentTarget.dataset.index;
const item = this.data.orderDetail.packages[index];
let receviedStatus = wx.getStorageSync(item.orderid)||{};
const checkedCode = event.detail.value;
for(let key in item.formattedCode){
item.formattedCode[key].checked = !!checkedCode.find((code)=>code==key);
}
let receivedAll = Object.keys(item.formattedCode).length==checkedCode.length;
item.receivedAll = receivedAll;
this.setData({
[`orderDetail.packages[${index}]`]:item
});
receviedStatus[item.id] = checkedCode;
wx.setStorage({
key:item.orderid,
data:receviedStatus
})
},
showMoreAS(event){
const item = this.data.orderDetail;
const markupAction = ()=>{
const markupView = this.selectComponent('#markupView');
markupView.show(item)
}
const makePhoneCallAction = ()=>{
wx.makePhoneCall({
phoneNumber: item.address_phone,
});
}
const refundAction = ()=>{
console.log(item.receiving);
if(item.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)=>{
actions[res.tapIndex]();
}
})
},
markupSuccess(){
app.globalData.needRefreshOrderList = true;
this.getOrderDetail();
},
refund(event){
const item = this.data.orderDetail;
if(item.receiving)return;
this.setData({
receiving:true
})
orderApi.cancel(item.orderid,event.detail).then((data)=>{
wx.showToast({
title: '取消成功',
icon:'success'
});
app.globalData.needRefreshOrderList = true;
this.getOrderDetail();
})
},
receivedOrder(){
const item = this.data.orderDetail;
const hasUnReceive = !!item.packages.find((p)=>!p.receivedAll);
if(hasUnReceive){
this.setData({
receiving:false
})
wx.showToast({
icon:'none',
title: '请先取件',
})
return;
}
orderApi.pickup(item.orderid).then((data)=>{
wx.showToast({
icon:'success',
title: '取货成功',
})
app.globalData.needRefreshOrderList = true;
this.getOrderDetail();
wx.removeStorage({
key: item.orderid,
})
}).catch(()=>{
this.setData({
receiving:false
})
})
},
emptyFun(){},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
}
})