375 lines
8.8 KiB
JavaScript
375 lines
8.8 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:'',
|
||
stationCount:0,
|
||
count:0
|
||
},
|
||
|
||
isShowOrderConfirm:false,
|
||
preOrder:{},
|
||
preOrderUnPayId:'',
|
||
|
||
manuallyChangedCommunity:false,
|
||
|
||
preOrdering:false,
|
||
ordering:false,
|
||
|
||
genderKV:userApi.genderKV,
|
||
navBarHeight:0,
|
||
isShowPayModal:false,
|
||
|
||
appConfig:{}
|
||
},
|
||
|
||
/**
|
||
* 生命周期函数--监听页面加载
|
||
*/
|
||
onLoad(options) {
|
||
let height = this.selectComponent("#navBar").getHeight();
|
||
this.setData({
|
||
navBarHeight:height
|
||
})
|
||
// this.getAddress();
|
||
app.getAppConfig().then((data)=>{
|
||
this.setData({
|
||
appConfig:data
|
||
})
|
||
})
|
||
},
|
||
navToHowToUse(){
|
||
if(this.data.appConfig&&this.data.appConfig.url_how_to_use){
|
||
wx.navigateTo({
|
||
url: `/pages/browser/index?url=${encodeURIComponent(this.data.appConfig.url_how_to_use)}`,
|
||
})
|
||
}
|
||
},
|
||
/**
|
||
* 生命周期函数--监听页面初次渲染完成
|
||
*/
|
||
onReady() {
|
||
},
|
||
/**
|
||
* 生命周期函数--监听页面显示
|
||
*/
|
||
onShow() {
|
||
wx.getStorage({
|
||
key:'pre-order',
|
||
success:(res)=>{
|
||
const name = [];
|
||
let count = 0;
|
||
res.data.price_request.packages.map((item)=>{
|
||
name.push(item.name);
|
||
count+=item.pickup_codes.length;
|
||
});
|
||
this.setData({
|
||
package:{
|
||
name:name.join('|'),
|
||
stationCount:name.length,
|
||
count:count
|
||
}
|
||
})
|
||
},
|
||
fail:(res)=>{
|
||
this.setData({
|
||
package:{
|
||
name:'',
|
||
stationCount:0,
|
||
count:0
|
||
}
|
||
})
|
||
}
|
||
});
|
||
|
||
this.setData({
|
||
isLogin:!!app.globalData.accessToken
|
||
})
|
||
//手动更改了社区,就不要再次更新默认地址
|
||
if(!this.data.manuallyChangedCommunity){
|
||
|
||
if(!app.globalData.accessToken){
|
||
return;
|
||
}
|
||
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({
|
||
community_id:communityId,
|
||
address_type:'PICKUP'
|
||
}).then((data)=>{
|
||
let currentAddress = data[0]||{};
|
||
data.map((item,index)=>{
|
||
if(item.is_default){
|
||
currentAddress = item;
|
||
}
|
||
})
|
||
this.setData({
|
||
currentAddress,
|
||
});
|
||
});
|
||
},
|
||
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});
|
||
let params = {
|
||
community_id:this.data.currentCommunity.id,
|
||
delivery_date:res.data.delivery_date
|
||
};
|
||
if(res.data.price_request.packages&&res.data.price_request.packages.length>0){
|
||
let realItem = [];
|
||
res.data.price_request.packages.map((item)=>{
|
||
let p = {
|
||
pickup_codes:item.pickup_codes.join(','),
|
||
station_name:item.name
|
||
}
|
||
if(item.id){
|
||
p.station_id = item.id
|
||
}
|
||
realItem.push(p)
|
||
})
|
||
params.packages = realItem;
|
||
}
|
||
if(res.data.price_request.pickup_images){
|
||
params.pickup_images_count = res.data.price_request.pickup_images_count;
|
||
params.pickup_images = res.data.price_request.pickup_images;
|
||
}
|
||
userApi.order.pre(params).then((data)=>{
|
||
wx.hideTabBar({
|
||
success:()=>{
|
||
this.setData({
|
||
isShowOrderConfirm:true,
|
||
preOrdering:false,
|
||
preOrder:data
|
||
});
|
||
}
|
||
});
|
||
}).catch((data)=>{
|
||
let _setData = {
|
||
preOrdering:false
|
||
}
|
||
if(data.code==400&&data.data&&data.data.orderid){
|
||
_setData.isShowPayModal = true;
|
||
_setData.preOrderUnPayId = data.data.orderid;
|
||
}else{
|
||
wx.showToast({
|
||
icon:'error',
|
||
title: data.message,
|
||
});
|
||
}
|
||
this.setData(_setData);
|
||
})
|
||
},
|
||
fail(){
|
||
wx.showToast({
|
||
icon:'none',
|
||
duration:1000,
|
||
title: '选择驿站取件'
|
||
})
|
||
}
|
||
})
|
||
},
|
||
navToUnPayOrder(){
|
||
wx.navigateTo({
|
||
url: `/pages/order/detail/index?id=${this.data.preOrderUnPayId}`,
|
||
})
|
||
},
|
||
getOrder(){
|
||
if(this.data.ordering)return;
|
||
this.setData({ordering:true});
|
||
wx.getStorage({
|
||
key:'pre-order',
|
||
success:(res)=>{
|
||
res.data.addressid = this.data.currentAddress.id;
|
||
res.data.price_request.community_id = this.data.currentCommunity.id;
|
||
|
||
if(res.data.price_request.packages&&res.data.price_request.packages.length>0){
|
||
let realItem = [];
|
||
res.data.price_request.packages.map((item)=>{
|
||
let p = {
|
||
pickup_codes:item.pickup_codes.join(','),
|
||
station_name:item.name
|
||
}
|
||
if(item.id){
|
||
p.station_id = item.id
|
||
}
|
||
realItem.push(p)
|
||
})
|
||
res.data.price_request.packages = realItem;
|
||
}
|
||
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.delivery_time}`,
|
||
})
|
||
}
|
||
});
|
||
}).catch((e)=>{
|
||
this.setData({
|
||
ordering:false
|
||
});
|
||
});
|
||
}
|
||
});
|
||
},
|
||
enterPageContainer(){
|
||
// wx.hideTabBar();
|
||
},
|
||
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?type=pickup&community_id=${this.data.currentCommunity.id}&community_name=${this.data.currentCommunity.name}`,
|
||
})
|
||
}else{
|
||
wx.showToast({
|
||
icon:'error',
|
||
title: '请先选择小区',
|
||
})
|
||
}
|
||
}else{
|
||
app.navToLogin();
|
||
}
|
||
},
|
||
navToWXGroup(){
|
||
if(this.data.currentCommunity&&this.data.currentCommunity.id){
|
||
wx.navigateTo({
|
||
url: `/pages/my/promation/wx-group/index?communityId=${this.data.currentCommunity.id}`,
|
||
})
|
||
}else{
|
||
wx.showToast({
|
||
icon:'error',
|
||
title: '请先选择小区',
|
||
})
|
||
}
|
||
}
|
||
}) |