dm-wechat-mini/pages/help/address/edit/index.js
2025-03-25 20:24:03 +08:00

263 lines
6.3 KiB
JavaScript

import commonApi from '../../../../api/common';
const app = getApp();
Page({
/**
* 页面的初始数据
*/
data: {
buildingList:[],
buildingIndex:0,
communityId:null,
communityName:'',
editType:'add',
addressType:'',
addressDetail:{},
name:'',
gender:'MALE',
phone:'',
community_building_id:'',
address_detail:'',
isShowConfirm:false,
choosedAddress:{}
},
validator:{
name:{required:true,message:'请输入姓名',shake:true,autoFocus:true},
phone:{type:'phone',message:'请输入正确的手机号',shake:true,autoFocus:true},
buildingIndex:{required:true,message:'请选择楼栋',shake:true,autoFocus:true},
address_detail:{required:true,message:'请输入详细地址',shake:true,autoFocus:true}
},
validatorCommon:{
communityName:{required:true,message:'请选择收货地址',shake:true},
name:{required:true,message:'请输入收货人',shake:true,autoFocus:true},
phone:{type:'phone',message:'请输入正确的手机号',shake:true,autoFocus:true},
address_detail:{required:true,message:'请输入楼号/门牌号',shake:true,autoFocus:true}
},
showModal(){
this.setData({
isShowConfirm:true
})
},
deleteAddress(){
commonApi.address.delete(this.data.addressDetail.id).then(()=>{
this.updateAddressIndex();
wx.navigateBack({
success(){
wx.showToast({
title: '删除成功',
icon:'success'
});
}
});
});
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
const communityId = options.community_id||0;
const communityName = options.community_name||'';
const addressId = options.address_id;
this.setData({
editType:addressId?'edit':'add',
addressType:options.type||''
});
//修改
if(addressId){
commonApi.address.detail(addressId).then((data)=>{
this.setData({
communityId:data.community_id,
communityName:data.community_name,
addressDetail:data,
addressType:data.address_type.toLowerCase(),
communityName:data.community_name,
name:data.name,
gender:data.gender,
phone:data.phone,
community_building_id:data.community_building_id,
address_detail:data.address_detail
});
this.data.choosedAddress = {
name:data.community_name,
latitude:data.latitude,
longitude:data.longitude
}
if(this.data.addressType=='pickup'){
this.getBuildingList();
}
});
}else{
//新增
this.setData({
communityId,communityName
})
if(this.data.addressType=='pickup'){
this.getBuildingList();
}
}
},
save(){
const valid = app.validateForm(this.data.addressType=='pickup'?this.validator:this.validatorCommon,this);
console.log(valid);
if(valid.length!=0){
return;
}
let data = {
address_detail:this.data.address_detail,
name:this.data.name,
gender:this.data.gender,
phone:this.data.phone,
address_type:this.data.addressType.toUpperCase()
}
if(this.data.addressType=='common'){
data.longitude = this.data.choosedAddress.longitude;
data.latitude = this.data.choosedAddress.latitude;
data.community_name = this.data.communityName;
}else{
data.community_id = this.data.communityId;
data.community_building_id = this.data.buildingList[this.data.buildingIndex].id;
}
if(this.data.editType=='edit'){
//编辑
data.id = this.data.addressDetail.id;
commonApi.address.update(data).then((data)=>{
this.updateAddressIndex();
wx.navigateBack({
success(){
wx.showToast({
title: '修改成功',
icon:'success'
});
}
});
});
}else if(this.data.editType=='add'){
//新增
commonApi.address.add(data).then((data)=>{
this.updateAddressIndex();
wx.navigateBack({
delta:2,
success(){
wx.showToast({
title: '新增成功',
icon:'success'
});
}
});
});
}
},
updateAddressIndex(){
const pages = getCurrentPages();
const prePage = pages[pages.length-3];
if(prePage){
//首页进来的 让他自己通过onshow刷新
prePage.data.manuallyChangedCommunity = false;
//商品详情进来的,需要刷新详情,因为涉及删除和修改地址,避免额外代码并带来额外错误的可能性
if(prePage.getDetail){
prePage.getDetail();
}
}
},
getBuildingList(){
commonApi.building.list(this.data.communityId).then((data)=>{
let buildingIndex = 0;
data.items.map((item,index)=>{
item.displayText = `${item.community_name} ${item.building_name}`;
if(item.id==this.data.addressDetail.community_building_id){
buildingIndex = index;
}
});
this.setData({
buildingList:data.items,
buildingIndex
})
});
},
buildingChange(event){
console.log(this.data.buildingIndex);
},
genderChange(event){
this.setData({
gender:event.detail.value
})
},
openMap(){
app.getLocation().then((data)=>{
// wx.openLocation({
// latitude: data.latitude,
// longitude: data.longitude,
// })
wx.chooseLocation({
latitude: data.latitude,
longitude: data.longitude,
success:(res)=>{
this.setData({
choosedAddress:res,
communityName:res.name,
communityNameMessage:''
})
},
fail(res){
console.log('error',res);
}
});
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
}
})