123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- 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);
- }
- }
- });
|