const { getProvince, getCity, getArea, getStreet } = require("../../utils/api/customField"); // components/area-picker/index.js Component({ options: { lifetimes: true }, /** * 组件的属性列表 */ properties: { content: { type: String, default: null }, hasStreet: { type: Boolean, default: false } }, /** * 组件的初始数据 */ data: { addressList: [], valueIndex: [], result: [], result_show: null }, /** * 组件的方法列表 */ methods: { //获取省市区街道列表并赋值 async getAddressList(column = -1, index = 0, value) { let { addressList, valueIndex, result, result_show } = this.data; if (column <= -1) { await getProvince().then(provinceList => { addressList[0] = provinceList.data; if (value) { index = provinceList.data.findIndex(item => { return item.name == value[0]; }); index = index > -1 ? index : 0; } else { index = 0; } }); } if (column <= 0) { valueIndex[0] = index; result[0] = addressList[0][index].name; await getCity(addressList[0][index || 0].areaCodeId).then( cityList => { addressList[1] = cityList.data; if (value) { index = cityList.data.findIndex(item => { return item.name == value[1]; }); index = index > -1 ? index : 0; } else { index = 0; } } ); } if (column <= 1) { valueIndex[1] = index; result[1] = addressList[1][index].name; await getArea(addressList[1][index || 0].areaCodeId).then( areaList => { addressList[2] = areaList.data; if (value) { index = areaList.data.findIndex(item => { return item.name == value[2]; }); index = index > -1 ? index : 0; } else { index = 0; } } ); } if (column <= 2) { valueIndex[2] = index; result[2] = addressList[2][index].name; if (this.data.hasStreet) { await getStreet(addressList[2][index || 0].areaCodeId).then( streetList => { addressList[3] = streetList.data; if (value) { index = streetList.data.findIndex(item => { return item.name == value[3]; }); index = index > -1 ? index : 0; } else { index = 0; } } ); } } if (column <= 3 && this.data.hasStreet) { valueIndex[3] = index; result[3] = addressList[3].length > 0 ? addressList[3][index].name : "暂无街道"; } if (column <= -1 && !value) { result = []; result_show = null; } result_show = result.join("-") || null; this.setData({ addressList, valueIndex, result_show }); this.triggerEvent("value", result_show); }, //改变列时 changeColumn(e) { let column = e.detail.column; let index = e.detail.value; this.getAddressList(column, index); }, // 点击确定时 changeValue(e) { let { valueIndex, result, result_show } = this.data; valueIndex = e.detail.value; valueIndex.forEach((item, index) => { if (this.data.addressList[index][item]) { result[index] = this.data.addressList[index][item].name; } }); result_show = result.join("-"); this.setData({ valueIndex, result_show }); this.triggerEvent("value", result_show); } }, lifetimes: { // 组件初始化时 attached: async function() { let value = this.data.content; value = value ? value.split("-") : null; this.getAddressList(-1, 0, value); } } });