296 lines
6.6 KiB
JavaScript
296 lines
6.6 KiB
JavaScript
const app = getApp();
|
||
import commonApi from '../../../api/common';
|
||
import userApi from '../../../api/user';
|
||
|
||
Page({
|
||
|
||
/**
|
||
* 页面的初始数据
|
||
*/
|
||
data: {
|
||
isLogin:!!app.globalData.accessToken,
|
||
currentAddress:null,
|
||
currentCommunity:{
|
||
id:null,
|
||
name:''
|
||
},
|
||
package:{
|
||
name:'',
|
||
count:0
|
||
},
|
||
|
||
isShowOrderConfirm:false,
|
||
preOrder:{},
|
||
|
||
manuallyChangedCommunity:false,
|
||
|
||
preOrdering:false,
|
||
ordering:false,
|
||
|
||
genderKV:userApi.genderKV,
|
||
navBarHeight:0
|
||
},
|
||
|
||
/**
|
||
* 生命周期函数--监听页面加载
|
||
*/
|
||
onLoad(options) {
|
||
let height = this.selectComponent("#navBar").getHeight();
|
||
this.setData({
|
||
navBarHeight:height
|
||
})
|
||
this.setData({
|
||
isLogin:!!app.globalData.accessToken
|
||
})
|
||
// this.getAddress();
|
||
},
|
||
|
||
/**
|
||
* 生命周期函数--监听页面初次渲染完成
|
||
*/
|
||
onReady() {
|
||
|
||
},
|
||
/**
|
||
* 生命周期函数--监听页面显示
|
||
*/
|
||
onShow() {
|
||
wx.getStorage({
|
||
key:'pre-order',
|
||
success:(res)=>{
|
||
const name = [];
|
||
let count = 0;
|
||
res.data.price_request.packages.map((item)=>{
|
||
name.push(item.station_name);
|
||
count+=item.pickup_codes.split(',').length;
|
||
});
|
||
this.setData({
|
||
package:{
|
||
name:name.join('|'),
|
||
count:count
|
||
}
|
||
})
|
||
},
|
||
fail:(res)=>{
|
||
this.setData({
|
||
package:{
|
||
name:'',
|
||
count:0
|
||
}
|
||
})
|
||
}
|
||
});
|
||
console.log('manuallyChangedCommunity',this.data.manuallyChangedCommunity);
|
||
if(!this.data.manuallyChangedCommunity){
|
||
app.forceGetUserInfo().then((data)=>{
|
||
if(data.default_address){
|
||
this.setData({
|
||
currentCommunity:{
|
||
id:data.default_address.community_id,
|
||
name:data.default_address.community_name
|
||
},
|
||
currentAddress:data.default_address
|
||
})
|
||
}else{
|
||
this.setData({
|
||
currentAddress:null
|
||
})
|
||
}
|
||
})
|
||
}
|
||
},
|
||
getAddress(communityId){
|
||
commonApi.address.list(communityId).then((data)=>{
|
||
let currentAddress = data[0]||{};
|
||
data.map((item,index)=>{
|
||
if(item.is_default){
|
||
currentAddress = item;
|
||
}
|
||
})
|
||
this.setData({
|
||
currentAddress,
|
||
});
|
||
});
|
||
},
|
||
changeCommunity(community){
|
||
console.log('changeCommunity',community);
|
||
//手动设置社区之后 做个标记
|
||
this.setData({
|
||
currentCommunity:{
|
||
id:community.id,
|
||
name:community.name
|
||
},
|
||
manuallyChangedCommunity:true
|
||
});
|
||
this.getAddress(community.id);
|
||
wx.removeStorage({
|
||
key: 'pre-order',
|
||
})
|
||
},
|
||
changeAddress(address){
|
||
//手动设置地址之后,由于同时设置了默认地址,所以标记改为 false 便于找当前显示的地址
|
||
this.setData({
|
||
currentAddress:address,
|
||
manuallyChangedCommunity:false
|
||
})
|
||
},
|
||
preOrder(event){
|
||
let msg = '';
|
||
if(!(this.data.currentCommunity&&this.data.currentCommunity.id)){
|
||
msg = '选择小区';
|
||
}
|
||
if(!(this.data.currentAddress&&this.data.currentAddress.id)){
|
||
msg = '选择送达地址';
|
||
}
|
||
if(msg){
|
||
wx.showToast({
|
||
title:msg,
|
||
duration:1000,
|
||
icon:'none'
|
||
});
|
||
return;
|
||
}
|
||
if(this.data.preOrdering)return;
|
||
wx.getStorage({
|
||
key:'pre-order',
|
||
success:(res)=>{
|
||
this.setData({preOrdering:true});
|
||
userApi.order.pre({
|
||
packages:res.data.price_request.packages
|
||
}).then((data)=>{
|
||
this.setData({
|
||
isShowOrderConfirm:true,
|
||
preOrdering:false,
|
||
preOrder:data
|
||
});
|
||
}).catch((data)=>{
|
||
if(data.code==400&&data.data&&data.data.orderid){
|
||
wx.showModal({
|
||
title: '你有订单未支付',
|
||
content: '',
|
||
confirmText:'去支付',
|
||
complete: (res) => {
|
||
if (res.confirm) {
|
||
wx.navigateTo({
|
||
url: `/pages/order/detail/index?id=${data.data.orderid}`,
|
||
})
|
||
}
|
||
}
|
||
})
|
||
}
|
||
this.setData({
|
||
preOrdering:false
|
||
});
|
||
})
|
||
},
|
||
fail(){
|
||
wx.showToast({
|
||
icon:'none',
|
||
duration:1000,
|
||
title: '选择取件驿站'
|
||
})
|
||
}
|
||
})
|
||
},
|
||
getOrder(){
|
||
if(this.data.ordering)return;
|
||
this.setData({ordering:true});
|
||
wx.getStorage({
|
||
key:'pre-order',
|
||
success:(res)=>{
|
||
res.data.addressid = this.data.currentAddress.id;
|
||
userApi.order.real(res.data).then((data)=>{
|
||
this.setData({
|
||
isShowOrderConfirm:false,
|
||
ordering:false
|
||
});
|
||
wx.removeStorage({
|
||
key: 'pre-order',
|
||
});
|
||
wx.showTabBar({
|
||
success(){
|
||
wx.navigateTo({
|
||
url: `/pages/help/success/index?id=${data.order.orderid}&success_text=${data.success_text}`,
|
||
})
|
||
}
|
||
});
|
||
});
|
||
}
|
||
});
|
||
},
|
||
enterPageContainer(){
|
||
console.log('enterPageContainer');
|
||
wx.hideTabBar();
|
||
},
|
||
leavePageContainer(){
|
||
console.log('leavePageContainer');
|
||
wx.showTabBar();
|
||
},
|
||
/**
|
||
* 生命周期函数--监听页面隐藏
|
||
*/
|
||
onHide() {
|
||
|
||
},
|
||
|
||
/**
|
||
* 生命周期函数--监听页面卸载
|
||
*/
|
||
onUnload() {
|
||
|
||
},
|
||
|
||
/**
|
||
* 页面相关事件处理函数--监听用户下拉动作
|
||
*/
|
||
onPullDownRefresh() {
|
||
|
||
},
|
||
|
||
/**
|
||
* 页面上拉触底事件的处理函数
|
||
*/
|
||
onReachBottom() {
|
||
|
||
},
|
||
|
||
/**
|
||
* 用户点击右上角分享
|
||
*/
|
||
onShareAppMessage() {
|
||
|
||
},
|
||
goToAddPackage(){
|
||
if(this.data.currentCommunity&&this.data.currentCommunity.id){
|
||
wx.navigateTo({
|
||
url: `/pages/help/package/index?communityId=${this.data.currentCommunity.id}`,
|
||
})
|
||
}else{
|
||
wx.showToast({
|
||
icon:'error',
|
||
title: '请先选择小区',
|
||
})
|
||
}
|
||
},
|
||
goToCommunity(){
|
||
wx.navigateTo({
|
||
url: `/pages/help/community/index?communityId=${this.data.currentCommunity.id}`,
|
||
})
|
||
},
|
||
goToAddress(){
|
||
if(app.globalData.accessToken){
|
||
if(this.data.currentCommunity&&this.data.currentCommunity.id){
|
||
wx.navigateTo({
|
||
url: `/pages/help/address/index/index?community_id=${this.data.currentCommunity.id}&community_name=${this.data.currentCommunity.name}`,
|
||
})
|
||
}else{
|
||
wx.showToast({
|
||
icon:'error',
|
||
title: '请先选择小区',
|
||
})
|
||
}
|
||
}else{
|
||
app.navToLogin();
|
||
}
|
||
}
|
||
}) |