WebApp【公共组件库】@前端(For Git Submodule)
Tevin
2021-04-08 f969b32736ab60b141d076f7aeaafdbd9dc1c546
调整地址三级联动api,在小程序中将地址数据分离为外置的静态文件
1 files added
2 files modified
169 ■■■■■ changed files
forms/chinaArea/CChinaArea.vue 9 ●●●● patch | view | raw | blame | history
forms/chinaArea/ChinaLocations.js 8 ●●●● patch | view | raw | blame | history
forms/chinaArea/ChinaLocations.weapp.js 152 ●●●●● patch | view | raw | blame | history
forms/chinaArea/CChinaArea.vue
@@ -35,7 +35,7 @@
import ChinaLocations from './ChinaLocations';
import './cChinaArea.scss';
const { locationTree, getRegionNames } = ChinaLocations;
const { onReady, getLocationTree, getRegionNames } = ChinaLocations;
export default {
    name: 'CChinaArea',
@@ -79,6 +79,7 @@
                    return;
                }
            }
            const locationTree = getLocationTree();
            const curVal = this.itemRes.formData[this.itemRes.name];
            const range = [
                locationTree,
@@ -123,6 +124,7 @@
            this.current = current;
        },
        updateColumns(roll) {
            const locationTree = getLocationTree();
            // 第一列滚动
            if (roll.column === 0) {
                const cities = locationTree[roll.value].children;
@@ -141,6 +143,7 @@
            }
        },
        handleChange(detail) {
            const locationTree = getLocationTree();
            const codes = [];
            const provice = locationTree[detail[0]];
            codes[0] = provice.value;
@@ -153,7 +156,9 @@
    },
    mounted() {
        if (process.env.TARO_ENV === 'weapp') {
            this.handleOpen();
            onReady(() => {
                this.handleOpen();
            });
        }
    },
};
forms/chinaArea/ChinaLocations.js
@@ -6,6 +6,7 @@
import ChinaLocationData from './ChinaLocationData.json';
const locationTree = [];
Object.keys(ChinaLocationData).forEach((code1) => {
    const province = {
        label: ChinaLocationData[code1].name,
@@ -34,7 +35,12 @@
});
export default {
    locationTree,
    onReady(callback) {
        callback();
    },
    getLocationTree() {
        return locationTree;
    },
    // 获取省市区拼合文本
    getRegionText(regions) {
        if (regions.length === 0) {
forms/chinaArea/ChinaLocations.weapp.js
New file
@@ -0,0 +1,152 @@
/**
 * ChinaLocations
 * @author Tevin
 */
import Taro from '@tarojs/taro';
import { Fetcher } from '@components/bases/Fetcher';
import project from '@project';
const locationTree = [];
let ChinaLocationData = {};
let readyCallback = () => { };
Taro.request({
    url: Fetcher.host + project.host.assetsPath + '/datas/ChinaLocation.json',
    header: {
        'X-Requested-With': 'XMLHttpRequest',
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
        'Ax-Rq-Type': 'separation',
    },
    credentials: 'same-origin',
    dataType: 'json',
    timeout: 30 * 1000,
    method: 'GET',
    success: response => {
        ChinaLocationData = response.data;
        Object.keys(ChinaLocationData).forEach((code1) => {
            const province = {
                label: ChinaLocationData[code1].name,
                value: code1,
                children: [],
            };
            const children1 = ChinaLocationData[code1].children;
            Object.keys(children1).forEach((code2) => {
                const city = {
                    label: children1[code2].name,
                    value: code2,
                    children: [],
                };
                if (typeof children1[code2].children !== 'undefined') {
                    const children2 = children1[code2].children;
                    Object.keys(children2).forEach((code3) => {
                        city.children.push({
                            label: children2[code3],
                            value: code3,
                        });
                    });
                }
                province.children.push(city);
            });
            locationTree.push(province);
        });
        readyCallback();
    }
});
export default {
    onReady(callback) {
        if (ChinaLocationData.length > 0) {
            callback();
        } else {
            readyCallback = callback;
        }
    },
    getLocationTree() {
        return locationTree;
    },
    // 获取省市区拼合文本
    getRegionText(regions, callback) {
        if (regions.length === 0) {
            return '';
        }
        let address = '';
        let tempLocationData = ChinaLocationData;
        regions.forEach((code) => {
            if (typeof tempLocationData[code].name === 'string') {
                address += tempLocationData[code].name;
            } else {
                address += tempLocationData[code];
            }
            tempLocationData = tempLocationData[code].children;
        });
        return address;
    },
    // 省市区名称
    getRegionNames(regions, callback) {
        if (typeof regions === 'string') {
            regions = regions.split(',');
        }
        if (!regions || regions.length === 0 || !regions[0]) {
            return [];
        }
        let address = [];
        let tempLocationData = ChinaLocationData;
        regions.forEach((code) => {
            if (!tempLocationData[code]) {
                return;
            }
            if (typeof tempLocationData[code].name === 'string') {
                address.push(tempLocationData[code].name);
            } else {
                address.push(tempLocationData[code]);
            }
            tempLocationData = tempLocationData[code].children;
        });
        return address;
    },
    // 省市区文本转code
    getRegionCodes(regions, callback) {
        if (typeof regions === 'string') {
            regions = regions.split(',');
        }
        if (!regions || regions.length === 0 || !regions[0]) {
            return '';
        }
        const codes = [];
        // 省
        for (let provinceCode in ChinaLocationData) {
            if (ChinaLocationData.hasOwnProperty(provinceCode)) {
                if (ChinaLocationData[provinceCode].name === regions[0]) {
                    codes[0] = provinceCode;
                    // 市
                    const provinceChildren = ChinaLocationData[provinceCode].children;
                    for (let cityCode in provinceChildren) {
                        if (provinceChildren.hasOwnProperty(cityCode)) {
                            if (provinceChildren[cityCode].name === regions[1]) {
                                codes[1] = cityCode;
                                // 区
                                const areaChildren = provinceChildren[cityCode].children;
                                for (let areaCode in areaChildren) {
                                    if (areaChildren.hasOwnProperty(areaCode)) {
                                        if (areaChildren[areaCode] === regions[2]) {
                                            codes[2] = areaCode;
                                            break;
                                        }
                                    }
                                }
                                break;
                            }
                        }
                    }
                    break;
                }
            }
        }
        return codes;
    },
};