city-casereport/pages/chooseArea/chooseArea.js
2023-12-06 14:22:42 +08:00

264 lines
5.0 KiB
JavaScript

// pages/chooseArea/chooseArea.js
const app = getApp();
Page({
/**
* 页面的初始数据
*/
data: {
token: '',
keys: {
value: 'id',
label: 'name'
},
showPicker: false,
dicList: [],
currentDicType: -1,
area1: null,
area2: null,
area3: null,
area4: null,
area5: null,
isDefault: false
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
var _self = this;
wx.getStorage({
key: 'token',
success: function (res) {
_self.setData({
token: res.data
})
}
})
var isDefault = options.isDefault;
_self.setData({
isDefault: isDefault
})
if (isDefault) {
_self.setData({
area1: {
name: '内蒙古自治区',
id: '99537',
areaCode: '150000000000',
},
area2: {
name: '包头市',
id: '100904',
areaCode: '150200000000',
},
area3: {
name: '稀土高新技术产业开发区',
id: '752234',
areaCode: '150271000000',
}
})
}
},
onShowArea1() {
this.getAreaList(1, '');
},
onShowArea2() {
if (this.data.area1 == null) {
wx.showToast({
title: '请选择省份',
icon: 'error'
})
} else {
this.getAreaList(2, this.data.area1.id)
}
},
onShowArea3() {
if (this.data.area2 == null) {
wx.showToast({
title: '请选择市',
icon: 'error'
})
} else {
this.getAreaList(3, this.data.area2.id)
}
},
onShowArea4() {
if (this.data.area3 == null) {
wx.showToast({
title: '请选择旗县区',
icon: 'error'
})
} else {
this.getAreaList(4, this.data.area3.id)
}
},
onShowArea5() {
if (this.data.area4 == null) {
wx.showToast({
title: '请选择乡镇街道',
icon: 'error'
})
} else {
this.getAreaList(5, this.data.area4.id)
}
},
//确定选择
doConfirm() {
var _self = this;
var isLegal = this.checkParams();
if (isLegal) {
var temp = {};
//名字拼接
temp.id = _self.data.area3.id;
temp.code = _self.data.area3.areaCode;
var name = _self.data.area1.name + "/" + _self.data.area2.name + "/" + _self.data.area3.name;
temp.name = name;
if (_self.data.area4 != null) {
temp.name += "/" + _self.data.area4.name;
temp.id = _self.data.area4.id;
temp.code = _self.data.area4.areaCode;
}
if (_self.data.area5 != null) {
temp.name += "/" + _self.data.area5.name;
temp.id = _self.data.area5.id;
temp.code = _self.data.area5.areaCode;
}
if (_self.data.isDefault) {
const eventChannel = _self.getOpenerEventChannel();
eventChannel.emit('getDeformityLiveAddress', {
backData: temp
});
} else {
const eventChannel = _self.getOpenerEventChannel();
eventChannel.emit('getBackData', {
backData: temp
});
}
wx.navigateBack()
}
},
//校验参数
checkParams() {
var _self = this;
if (_self.data.area1 == null) {
wx.showToast({
title: '请选择省份',
icon: 'none'
})
return false;
}
if (_self.data.area2 == null) {
wx.showToast({
title: '请选择市',
icon: 'none'
})
return false;
}
if (_self.data.area3 == null) {
wx.showToast({
title: '请选择乡镇街道',
icon: 'none'
})
return false;
}
return true;
},
onDicPickerChange(e) {
console.log(e)
var _self = this;
var type = e.target.dataset.type;
var index = e.detail.columns[0].index;
switch (type) {
case 1:
_self.setData({
area1: _self.data.dicList[index],
area2: null,
area3: null,
area4: null,
area5: null,
showPicker: false,
currentDicType: -1
})
break;
case 2:
_self.setData({
area2: _self.data.dicList[index],
area3: null,
area4: null,
area5: null,
showPicker: false,
currentDicType: -1
})
break;
case 3:
_self.setData({
area3: _self.data.dicList[index],
area4: null,
area5: null,
showPicker: false,
currentDicType: -1
})
break;
case 4:
_self.setData({
area4: _self.data.dicList[index],
area5: null,
showPicker: false,
currentDicType: -1
})
break;
case 5:
_self.setData({
area5: _self.data.dicList[index],
showPicker: false,
currentDicType: -1
})
break;
}
},
//获取地区数据
getAreaList(type, id) {
var _self = this;
wx.showLoading({
title: '加载中...',
})
_self.setData({
dicList: []
})
app.restAjax.get(app.restAjax.path('{reqesutUrl}app/areatree/list', [app.personUrl]), { id: id }, {
headers: {
token: _self.data.token
}
}, function (code, data) {
wx.hideLoading();
if (data && data.length > 0) {
_self.setData({
dicList: data,
showPicker: true,
currentDicType: type
})
} else {
wx.showToast({
title: '暂无数据',
icon: 'error'
})
}
}, function (code, error) {
wx.hideLoading();
wx.showToast({
title: '加载失败',
icon: 'error'
})
});
},
onDicPickerCancel() {
this.setData({
showPicker: false,
})
},
onCardTypePick(e) {
// console.log(e);
},
})