jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/** common.js By Beginner Emain:zheng_jinfan@126.com HomePage:http://www.zhengjinfan.cn */
layui.define(['jquery'], function(exports) {
    "use strict";
    var jQuery = layui.jquery;
    /**
     * jquery.citys.js 1.0
     * http://jquerywidget.com
     */
    ;
    (function($) {
        $.support.cors = true;
        $.fn.citys = function(parameter, getApi) {
            if(typeof parameter == 'function') { //重载
                getApi = parameter;
                parameter = {};
            } else {
                parameter = parameter || {};
                getApi = getApi || function() {};
            }
            var defaults = {
                dataUrl: 'http://passer-by.com/data_location/list.json', //数据库地址
                dataType: 'json', //数据库类型:'json'或'jsonp'
                provinceField: 'province', //省份字段名
                cityField: 'city', //城市字段名
                areaField: 'area', //地区字段名
                code: 0, //地区编码
                province: 0, //省份,可以为地区编码或者名称
                city: 0, //城市,可以为地区编码或者名称
                area: 0, //地区,可以为地区编码或者名称
                required: true, //是否必须选一个
                nodata: 'hidden', //当无数据时的表现形式:'hidden'隐藏,'disabled'禁用,为空不做任何处理
                onChange: function() {} //地区切换时触发,回调函数传入地区数据
            };
            var options = $.extend({}, defaults, parameter);
            return this.each(function() {
                //对象定义
                var _api = {};
                var $this = $(this);
                var $province = $this.find('select[name="' + options.provinceField + '"]'),
                    $city = $this.find('select[name="' + options.cityField + '"]'),
                    $area = $this.find('select[name="' + options.areaField + '"]');
                $.ajax({
                    url: options.dataUrl,
                    type: 'GET',
                    crossDomain: true,
                    dataType: options.dataType,
                    jsonpCallback: 'jsonp_location',
                    success: function(data) {
                        var province, city, area, hasCity;
                        if(options.code) { //如果设置地区编码,则忽略单独设置的信息
                            var c = options.code - options.code % 1e4;
                            if(data[c]) {
                                options.province = c;
                            }
                            c = options.code - (options.code % 1e4 ? options.code % 1e2 : options.code);
                            if(data[c]) {
                                options.city = c;
                            }
                            c = options.code % 1e2 ? options.code : 0;
                            if(data[c]) {
                                options.area = c;
                            }
                        }
                        var updateData = function() {
                            province = {}, city = {}, area = {};
                            hasCity = false; //判断是非有地级城市
                            for(code in data) {
                                if(!(code % 1e4)) { //获取所有的省级行政单位
                                    province[code] = data[code];
                                    if(options.required && !options.province) {
                                        if(options.city && !(options.city % 1e4)) { //省未填,并判断为直辖市
                                            options.province = options.city;
                                        } else {
                                            options.province = code;
                                        }
                                    } else if(data[code].indexOf(options.province) > -1) {
                                        options.province = isNaN(options.province) ? code : options.province;
                                    }
                                } else {
                                    var p = code - options.province;
                                    if(options.province && p > 0 && p < 1e4) { //同省的城市或地区
                                        if(!(code % 100)) {
                                            hasCity = true;
                                            city[code] = data[code];
                                            if(options.required && !options.city) {
                                                options.city = code;
                                            } else if(data[code].indexOf(options.city) > -1) {
                                                options.city = isNaN(options.city) ? code : options.city;
                                            }
                                        } else if(p > 9000) { //省直辖县级行政单位
                                            city[code] = data[code];
                                        } else if(hasCity) { //非直辖市
                                            var c = code - options.city;
                                            if(options.city && c > 0 && c < 100) { //同个城市的地区
                                                area[code] = data[code];
                                                if(options.required && !options.area) {
                                                    options.area = code;
                                                } else if(data[code].indexOf(options.area) > -1) {
                                                    options.area = isNaN(options.area) ? code : options.area;
                                                }
                                            }
                                        } else {
                                            city[code] = data[code]; //直辖市
                                            if(options.area) {
                                                options.city = options.area;
                                                options.area = '';
                                            }
                                            if(options.required && !options.city) {
                                                options.city = code;
                                            } else if(data[code].indexOf(options.city) > -1) {
                                                options.city = isNaN(options.city) ? code : options.city;
                                            }
                                        }
                                    }
                                }
                            }
                        };
                        var format = {
                            province: function() {
                                $province.empty();
                                if(!options.required) {
                                    $province.append('<option value=""> - 请选择 - </option>');
                                }
                                for(i in province) {
                                    $province.append('<option value="' + i + '">' + province[i] + '</option>');
                                }
                                if(options.province) {
                                    $province.val(options.province);
                                }
                                this.city();
                            },
                            city: function() {
                                $city.empty();
                                if(!options.required) {
                                    $city.append('<option value=""> - 请选择 - </option>');
                                }
                                if(options.nodata == 'disabled') {
                                    $city.prop('disabled', $.isEmptyObject(city));
                                } else if(options.nodata == 'hidden') {
                                    $city.css('display', $.isEmptyObject(city) ? 'none' : '');
                                }
                                for(i in city) {
                                    $city.append('<option value="' + i + '">' + city[i] + '</option>');
                                }
                                if(options.city) {
                                    $city.val(options.city);
                                }
                                this.area();
                            },
                            area: function() {
                                $area.empty();
                                if(!hasCity) {
                                    $area.css('display', 'none');
                                } else {
                                    $area.css('display', '');
                                    if(!options.required) {
                                        $area.append('<option value=""> - 请选择 - </option>');
                                    }
                                    if(options.nodata == 'disabled') {
                                        $area.prop('disabled', $.isEmptyObject(area));
                                    } else if(options.nodata == 'hidden') {
                                        $area.css('display', $.isEmptyObject(area) ? 'none' : '');
                                    }
                                    for(i in area) {
                                        $area.append('<option value="' + i + '">' + area[i] + '</option>');
                                    }
                                    if(options.area) {
                                        $area.val(options.area);
                                    }
                                }
                            }
                        };
                        //获取当前地理信息
                        _api.getInfo = function() {
                            var status = {
                                direct: !hasCity,
                                province: data[options.province] || '',
                                city: data[options.city] || '',
                                area: data[options.area] || '',
                                code: options.area || options.city || options.province
                            };
                            return status;
                        };
                        //事件绑定
                        $province.on('change', function() {
                            options.province = $(this).val();
                            options.city = 0;
                            options.area = 0;
                            updateData();
                            format.city();
                            options.onChange(_api.getInfo());
                        });
                        $city.on('change', function() {
                            options.city = $(this).val();
                            options.area = 0;
                            updateData();
                            format.area();
                            options.onChange(_api.getInfo());
                        });
                        $area.on('change', function() {
                            options.area = $(this).val();
                            options.onChange(_api.getInfo());
                        });
                        //初始化
                        updateData();
                        format.province();
                        getApi(_api);
                    }
                });
            });
        };
    })(jQuery);
    exports('city', null);
});