dm-wechat-mini/components/address/index.js
2025-02-13 19:11:45 +08:00

148 lines
3.7 KiB
JavaScript

import commonApi from '../../api/common';
const app = getApp();
Component({
/**
* 组件的属性列表
*/
properties: {
saveText:{
type:String,
value:"保存"
},
deleteText:{
type:String,
value:''
},
communityId:{
type:Number,
value:null
}
},
/**
* 组件的初始数据
*/
data: {
buildingList:[],
buildingIndex:0,
communityList:[],
communityIndex:0,
currentEditAddress:{},
name:'',
gender:'',
phone:'',
community_building_id:'',
address_detail:''
},
lifetimes:{
attached(){
if(app.globalData.tempAddress){
this.setData({
currentEditAddress:app.globalData.tempAddress,
name:app.globalData.tempAddress.name,
gender:app.globalData.tempAddress.gender,
phone:app.globalData.tempAddress.phone,
community_building_id:app.globalData.tempAddress.community_building_id,
address_detail:app.globalData.tempAddress.address_detail
})
}
commonApi.community.list().then((data)=>{
let communityIndex;
this.setData({
communityList:data.items
});
data.items.map((item,index)=>{
if(item.id==this.data.currentEditAddress.id){
communityIndex = index;
}
})
//没有找到 id 就是新增,就去找默认的社区进行填充
if(communityIndex){
this.getBuildingList();
}else{
for(let [index,item] of data.items.entries()){
if(item.id==this.properties.communityId){
this.setData({
communityIndex:index
});
}
}
this.getBuildingList();
}
});
// console.log(app.globalData.tempAddress);
}
},
/**
* 组件的方法列表
*/
methods: {
save(){
this.triggerEvent('save');
let data = {
community_id:this.data.communityList[this.data.communityIndex].id,
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
}
console.log(data);
if(this.data.currentEditAddress&&this.data.currentEditAddress.id){
//编辑
data.id = this.data.currentEditAddress.id;
commonApi.address.update(data);
}else{
//新增
data.is_default = true;
commonApi.address.add(data).then((data)=>{
const pages = getCurrentPages();
const prePage = pages[pages.length-2];
prePage.changeAddress(data);
wx.navigateBack({
success(){
wx.showToast({
title: '新增成功',
icon:'success'
});
}
});
});
}
},
getBuildingList(){
const current = this.data.communityList[this.data.communityIndex];
commonApi.building.list(current.id).then((data)=>{
let buildingIndex = 0;
data.items.map((item,index)=>{
item.displayText = `${item.community_name} ${item.building_name}`;
if(item.id==this.data.currentEditAddress.id){
buildingIndex = index;
}
});
this.setData({
buildingList:data.items,
buildingIndex
})
});
},
del(){
this.triggerEvent('delete')
},
chooseCommonity(event){
this.setData({
communityIndex:event.detail.value
})
},
buildingChange(event){
const buildingIndex = event.detail.value;
this.setData({buildingIndex});
}
}
})