dm-wechat-mini/pages/help/address/edit/index.js
2025-02-15 23:33:33 +08:00

192 lines
3.9 KiB
JavaScript

import commonApi from '../../../../api/common';
Page({
/**
* 页面的初始数据
*/
data: {
buildingList:[],
buildingIndex:0,
communityId:null,
communityName:'',
editType:'add',
addressDetail:{},
name:'',
gender:'MALE',
phone:'',
community_building_id:'',
address_detail:''
},
deleteAddress(){
wx.showModal({
title: '确定删除此地址吗',
content: '',
complete: (res) => {
if (res.confirm) {
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;
const communityName = options.community_name;
const addressId = options.address_id;
this.setData({
editType:addressId?'edit':'add'
});
//修改
if(addressId){
commonApi.address.detail(addressId).then((data)=>{
this.setData({
communityId:data.community_id,
communityName:data.community_name,
addressDetail:data,
name:data.name,
gender:data.gender,
phone:data.phone,
community_building_id:data.community_building_id,
address_detail:data.address_detail
});
this.getBuildingList();
});
}else{
//新增
this.setData({
communityId,communityName
})
this.getBuildingList();
}
},
save(){
let data = {
community_id:this.data.communityId,
community_building_id:this.data.buildingList[this.data.buildingIndex].id,
address_detail:this.data.address_detail,
name:this.data.name,
gender:this.data.gender,
phone:this.data.phone
}
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];
prePage.data.manuallyChangedCommunity = false;
},
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);
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
}
})