Commit 92aaf098 by 匡雅琴

省市组件

1 parent 31a64223
......@@ -4,6 +4,11 @@
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"@amap/amap-jsapi-loader": {
"version": "1.0.1",
"resolved": "https://registry.npmmirror.com/@amap/amap-jsapi-loader/-/amap-jsapi-loader-1.0.1.tgz",
"integrity": "sha512-nPyLKt7Ow/ThHLkSvn2etQlUzqxmTVgK7bIgwdBRTg2HK5668oN7xVxkaiRe3YZEzGzfV2XgH5Jmu2T73ljejw=="
},
"@babel/code-frame": {
"version": "7.14.5",
"resolved": "https://registry.nlark.com/@babel/code-frame/download/@babel/code-frame-7.14.5.tgz?cache=0&sync_timestamp=1623280394200&other_urls=https%3A%2F%2Fregistry.nlark.com%2F%40babel%2Fcode-frame%2Fdownload%2F%40babel%2Fcode-frame-7.14.5.tgz",
......@@ -6992,7 +6997,7 @@
},
"gulp": {
"version": "4.0.2",
"resolved": "https://registry.npmjs.org/gulp/-/gulp-4.0.2.tgz",
"resolved": "https://registry.npmmirror.com/gulp/-/gulp-4.0.2.tgz",
"integrity": "sha512-dvEs27SCZt2ibF29xYgmnwwCYZxdxhQ/+LFWlbAW8y7jt68L/65402Lz3+CKy0Ov4rOs+NERmDq7YlZaDqUIfA==",
"requires": {
"glob-watcher": "^5.0.3",
......@@ -14778,6 +14783,21 @@
"integrity": "sha1-9rRQHC7EzdJrp4vnIilh3ndiFZg=",
"dev": true
},
"uppercamelcase": {
"version": "1.1.0",
"resolved": "https://registry.npmmirror.com/uppercamelcase/-/uppercamelcase-1.1.0.tgz",
"integrity": "sha512-C7YEMvhgrvTEKEEVqA7LXNID/1TvvIwYZqNIKLquS6y/MGSkRQAav9LnTTILlC1RqUM8eTVBOe1U/fnB652PRA==",
"requires": {
"camelcase": "^1.2.1"
},
"dependencies": {
"camelcase": {
"version": "1.2.1",
"resolved": "https://registry.npmmirror.com/camelcase/-/camelcase-1.2.1.tgz",
"integrity": "sha512-wzLkDa4K/mzI1OSITC+DUyjgIl/ETNHE9QvYgy6J6Jvqyyz4C0Xfd+lQhb19sX2jMpZV4IssUn0VDVmglV+s4g=="
}
}
},
"uri-js": {
"version": "4.4.1",
"resolved": "https://registry.npm.taobao.org/uri-js/download/uri-js-4.4.1.tgz?cache=0&sync_timestamp=1610237624359&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Furi-js%2Fdownload%2Furi-js-4.4.1.tgz",
......@@ -15045,6 +15065,14 @@
"resolved": "https://registry.nlark.com/vue/download/vue-2.6.14.tgz?cache=0&sync_timestamp=1631890705744&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fvue%2Fdownload%2Fvue-2.6.14.tgz",
"integrity": "sha1-5RqlJQJQ1Wmj+606ilpofWA24jU="
},
"vue-amap": {
"version": "0.5.10",
"resolved": "https://registry.npmmirror.com/vue-amap/-/vue-amap-0.5.10.tgz",
"integrity": "sha512-9ViNCev1vx32+zZ5RvF/TmUZNbwL9QrdA2/OnD2GlXMfQBkJy7D08Vb7379t6guqnopDPtWJ8K6gg72h9+4GUg==",
"requires": {
"uppercamelcase": "^1.1.0"
}
},
"vue-hot-reload-api": {
"version": "2.3.4",
"resolved": "https://registry.nlark.com/vue-hot-reload-api/download/vue-hot-reload-api-2.3.4.tgz",
......@@ -15073,7 +15101,7 @@
}
},
"vue-loader-v16": {
"version": "npm:vue-loader@16.8.3",
"version": "npm:vue-loader-v16@16.8.3",
"resolved": "https://registry.npmmirror.com/vue-loader/-/vue-loader-16.8.3.tgz",
"integrity": "sha512-7vKN45IxsKxe5GcVCbc2qFU5aWzyiLrYJyUuMz4BQLKctCj/fmCa0w6fGiiQ2cLFetNcek1ppGJQDCup0c1hpA==",
"dev": true,
......
......@@ -15,11 +15,13 @@
"document": "concurrently \"npm run gulp\" \"npm run live\""
},
"dependencies": {
"@amap/amap-jsapi-loader": "^1.0.1",
"core-js": "^3.6.5",
"element-ui": "^2.15.6",
"glsx-common": "^1.3.2",
"gulp": "^4.0.2",
"vue": "^2.6.11"
"vue": "^2.6.11",
"vue-amap": "^0.5.10"
},
"devDependencies": {
"@vue/cli-plugin-babel": "~4.5.0",
......
......@@ -117,7 +117,7 @@
<GLFooter></GLFooter>
<!-- 测试 GLProvince -->
<GLProvince ref="provinceForm2" :subdistrict="subdistrict" @selectProvince="selectProvince" @selectCity="selectCity" @selectTown="selectTown"></GLProvince>
<GLProvince ref="provinceForm" :subdistrict="subdistrict" @selectProvince="selectProvince" @selectCity="selectCity" @selectTown="selectTown"></GLProvince>
</div>
</template>
<script>
......@@ -896,9 +896,20 @@ export default {
// kiveStatus4:'123',
// }
// ]
}, 3000)
}, 3000),
this.setAddress()
},
methods: {
setAddress() {
this.$refs.provinceForm.init({
province: '河南省',
city: '洛阳市',
townName: '洛龙区',
fullAddress: '河南省洛阳市洛龙区开元路街道洛阳市机关事务管理局洛阳市党政办公大楼',
lng: 112.453,
lat: 34.62
})
},
subscribe(child) {
child.call('push', { src: this.src })
child.on('route-to', params => {
......
......@@ -6,6 +6,7 @@ import Vue from 'vue'
import App from './App.vue'
// 一次性注册所有的过滤器 本地测试
import filters from '@/filters'
import VueAMap from 'vue-amap'
// 一次性注册所有的自定义指令
// import directives from '@/directives'
// 注册全局过滤器(一次性注册)
......@@ -30,6 +31,14 @@ import './index'
Vue.config.productionTip = false
Vue.use(VueAMap)
// 初始化vue-amap
VueAMap.initAMapApiLoader({
key: 'bd290dc27d0945959280753215ddda71', // 这里写你申请的高德地图的key
plugin: ['AMap.PlaceSearch', 'AMap.Geocoder', 'AMap.Geolocation'],
v: '1.4.15'
})
new Vue({
render: h => h(App)
}).$mount('#app')
<template>
<div v-if="provinceView">
<el-form ref="provinceForm" :model="provinceForm" :inline="true" class="province">
<el-select size="small" v-model="provinceForm.provinceName" clearable placeholder="选择省份" :style="{ width: defaultWidth }" @clear="selectProvince" class="select-padding">
<el-option v-for="(item, index) in AllData" :key="'province' + index" :label="item.name" :value="item.name" @click.native="selectProvince(item)"></el-option>
</el-select>
<el-select size="small" v-model="provinceForm.cityName" clearable placeholder="选择市" :style="{ width: defaultWidth }" @clear="selectCity" class="select-padding">
<el-option v-for="(item, index) in cityList" :key="'city' + index" :label="item.name" :value="item.name" @click.native="selectCity(item)"></el-option>
</el-select>
<el-select v-if="subdistrict == 3" size="small" v-model="provinceForm.townName" clearable placeholder="选择区" :style="{ width: defaultWidth }" @clear="selectTown" class="select-padding">
<el-option v-for="(item, index) in townList" :key="'town' + index" :label="item.name" :value="item.name" @click.native="selectTown(item)"></el-option>
</el-select>
<el-form ref="provinceForm" :model="provinceForm" :inline="inline" class="province" label-width="154px">
<el-form-item :label="provinceLabel">
<el-select size="small" v-model="provinceForm.provinceName" clearable placeholder="选择省份" @clear="selectProvince" class="select-padding">
<el-option v-for="(item, index) in AllData" :key="'province' + index" :label="item.name" :value="item.name" @click.native="selectProvince(item)"></el-option>
</el-select>
<el-select size="small" v-model="provinceForm.cityName" clearable placeholder="选择市" @clear="selectCity" class="select-padding">
<el-option v-for="(item, index) in cityList" :key="'city' + index" :label="item.name" :value="item.name" @click.native="selectCity(item)"></el-option>
</el-select>
<el-select v-if="subdistrict == 3" size="small" v-model="provinceForm.townName" clearable placeholder="选择区" @clear="selectTown" class="select-padding">
<el-option v-for="(item, index) in townList" :key="'town' + index" :label="item.name" :value="item.name" @click.native="selectTown(item)"></el-option>
</el-select>
</el-form-item>
<el-form-item :label="detailAddressLabel">
<el-input size="small" v-if="mapView" readonly placeholder="请选择详细地址" v-model="provinceForm.fullAddress">
<el-button size="small" slot="append" @click="hanleSelectMap">
<img src="@/assets/map.png" alt="" class="map-icon" />
</el-button>
</el-input>
</el-form-item>
</el-form>
<el-dialog :visible.sync="dialogMapVisible" width="60%">
<map-view ref="mapForm" :mapHeight="mapHeight"></map-view>
<span slot="footer" class="dialog-footer">
<el-button size="small" @click="dialogMapVisible = false">取 消</el-button>
<el-button size="small" type="primary" @click="getAddress">确 定</el-button>
</span>
</el-dialog>
</div>
</template>
<script>
import mapView from './mapView.vue'
export default {
name: 'GLProvince',
components: {
mapView
},
props: {
// 是否一行展示
inline: {
type: Boolean,
default: false
},
// 省市级别,2--只显示省市,3--显示省市区
subdistrict: {
type: Number,
// `2-只显示省市`
default: 2
},
// 省市下拉组件宽度
defaultWidth: {
type: String,
default: '150px'
},
// 组件是否展示,默认展示
provinceView: {
type: Boolean,
default: true
},
// 地图组件是否展示,默认展示
// 如果需要展示地图组件,需要下载vue-amap包,并且在main.js中引入
// import VueAMap from "vue-amap"
// Vue.use(VueAMap)
// 初始化vue-amap
// VueAMap.initAMapApiLoader({
// key: "bd290dc27d0945959280753215ddda71", // 这里写你申请的高德地图的key
// plugin: ["AMap.PlaceSearch", "AMap.Geocoder", "AMap.Geolocation"],
// v: "1.4.15"
// });
mapView: {
type: Boolean,
default: true
},
// 省份标签值
provinceLabel: {
type: String,
default: '省市地址:'
},
// 详细地址标签值
detailAddressLabel: {
type: String,
default: '详细地址:'
}
},
data() {
......@@ -55,8 +99,16 @@ export default {
// 城市代码
cityCode: '',
// 地区代码
townCode: ''
}
townCode: '',
lat: '',
lng: '',
// 全部地址
fullAddress: ''
},
mapHeight: 380,
dialogMapVisible: false,
// 暂存地址表单
addressData: {}
}
},
mounted() {
......@@ -93,7 +145,6 @@ export default {
provinceList() {
var params = {}
params.subdistrict = this.subdistrict
// params ? (params.subdistrict ? '' : (params.subdistrict = this.subdistrict)) : (params.subdistrict = this.subdistrict)
let data = ''
for (let key in params) {
data += '&' + key + '=' + params[key]
......@@ -111,12 +162,14 @@ export default {
return pro
},
// 省市回显
init(province, city, town) {
this.provinceForm = {
provinceName: province,
cityName: city,
townName: town
}
init(provinceData) {
const { province, city, townName, fullAddress, lng, lat } = provinceData
this.provinceForm.provinceName = province
this.provinceForm.cityName = city
this.provinceForm.townName = townName
this.provinceForm.fullAddress = fullAddress
this.provinceForm.lng = lng
this.provinceForm.lat = lat
if (province) {
this.AllData.map(item => {
if (item.name.indexOf(province) != -1) {
......@@ -145,6 +198,8 @@ export default {
}
},
selectProvince(val) {
this.provinceForm.provinceName = val.name
this.provinceForm.fullAddress = ''
this.provinceForm.cityName = ''
this.provinceForm.townName = ''
if (val) {
......@@ -157,6 +212,8 @@ export default {
},
selectCity(val) {
this.provinceForm.cityName = val.name
this.provinceForm.fullAddress = ''
this.provinceForm.townName = ''
if (val) {
this.townList = val.districts
......@@ -168,18 +225,109 @@ export default {
},
selectTown(val) {
this.provinceForm.townName = val.name
this.provinceForm.fullAddress = ''
// 选择区
this.$emit('selectTown', val)
},
// 打开地图弹框
hanleSelectMap() {
console.log(this.provinceForm)
this.dialogMapVisible = true
this.$nextTick(() => {
if (this.provinceForm.fullAddress && !this.$refs.mapForm.address) {
this.$refs.mapForm.address = this.provinceForm.fullAddress
this.$refs.mapForm.addMarker([this.provinceForm.lng, this.provinceForm.lat])
}
})
},
// 选择地图地址
getAddress() {
if (!this.$refs.mapForm.address) {
this.$message.success('请选择地址')
return
}
this.provinceForm.fullAddress = this.$refs.mapForm.address
let lnglat = this.$refs.mapForm.lnglat
this.provinceForm.lat = lnglat.lat
this.provinceForm.lng = lnglat.lng
var mapData = this.$refs.mapForm.geocoderAddress
var fullAddress = ''
var provincecode = ''
if (mapData.addressComponent) {
fullAddress = mapData.addressComponent
provincecode = mapData.adcode
this.addressData.fullAddress = fullAddress
this.addressData.provincecode = provincecode
} else {
fullAddress = this.addressData.fullAddress
provincecode = this.addressData.provincecode
}
// 获取省市
var province = ''
var city = ''
var town = ''
province = fullAddress.province
city = fullAddress.city ? fullAddress.city : fullAddress.district
town = fullAddress.district
// 直辖市需额外判断
const municipalities = ['北京市', '天津市', '上海市', '重庆市']
var municipality = municipalities.find(item => item == province)
if (municipality) {
city = city.slice(0, 2) + '城区'
if (municipality === '重庆市') {
provincecode = provincecode.slice(0, 4)
provincecode == 5001 ? (city = city.slice(0, 2) + '城区') : (city = city.slice(0, 2) + '郊县')
}
}
// 判断省市是否匹配
// 如果只展示省市
if (this.provinceForm.provinceName && !this.provinceForm.townName) {
if (this.provinceForm.provinceName !== province || this.provinceForm.cityName !== city) {
this.$message.error('您选择的地址错误,请重新选择')
return
}
}
// 如果展示省市区
if (this.provinceForm.provinceName && this.provinceForm.townName) {
if (this.provinceForm.provinceName !== province || this.provinceForm.cityName !== city || this.provinceForm.townName !== town) {
this.$message.error('您选择的地址错误,请重新选择')
return
}
}
this.dialogMapVisible = false
this.subdistrict == 2 ? this.init(province, city) : this.init(province, city, town)
// console.log(this.provinceForm.lat, 'provinceForm')
// 通过地图选择省市区
this.$emit('selectAddressByMap', this.provinceForm)
}
}
}
</script>
<style scoped lang="scss">
.province {
display: inline-flex;
}
// .province {
// display: inline-flex;
// }
.select-padding {
margin-right: 10px;
&:nth-child(1) {
margin-left: 0;
}
margin-left: 10px;
}
.map-icon {
vertical-align: middle;
}
/deep/ {
.el-input-group__append {
background-color: #fff;
padding: 0 11px;
}
.el-form-item__content {
width: 474px;
display: flex;
}
}
</style>
<template>
<div class="map-box" :style="{ height: mapHeight + 'px' }">
<div id="container" style="width: 100%; height: 100%"></div>
<div class="map-address">{{ address }}</div>
<div id="myPageTop" style="position: absolute; top: 10px; width: 50%; left: 25%">
<el-input placeholder="请输入关键字" v-model="input" class="input-with-select">
<el-button slot="append" icon="el-icon-search" @click="searchAddress"></el-button>
</el-input>
</div>
</div>
</template>
<script>
let map, marker, placeSearch
export default {
name: 'map',
props: {
mapHeight: {
type: Number,
default: 300
}
},
data() {
return {
input: '',
address: '',
lnglat: [],
geocoderAddress: {}
}
},
methods: {
mapInit() {
map = new AMap.Map('container', {
resizeEnable: true,
zoom: 20
})
AMap.plugin(['AMap.PlaceSearch'], function() {
//构造地点查询类
placeSearch = new AMap.PlaceSearch({
pageSize: 8, // 单页显示结果条数
pageIndex: 1, // 页码
citylimit: false, //是否强制限制在设置的城市内搜索
// panel: "panel", // 结果列表将在此容器中进行展示。
autoFitView: true, // 是否自动调整地图视野使绘制的 Marker点都处于视口的可见范围
renderStyle: 'default'
})
})
// 添加列表点选监听事件
map.on('click', this.getAddress)
},
searchAddress() {
let _this = this
placeSearch.search(this.input, function(status, result) {
if (status === 'complete' && result.poiList.pois.length) {
let lnglat = result.poiList.pois[0].location
_this.getAddress(lnglat)
}
})
},
addMarker(lnglat) {
let position
if (lnglat.lng) {
position = [lnglat.lng, lnglat.lat]
} else {
position = lnglat
}
marker = new AMap.Marker({
icon: new AMap.Icon({
image: '//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png',
size: new AMap.Size(27, 34), //图标大小
imageSize: new AMap.Size(27, 34)
}),
position: position,
offset: new AMap.Pixel(-10, -10)
})
map.add(marker)
map.setFitView()
},
getAddress(e) {
let self = this
let lnglat
if (e.type == 'click') {
lnglat = e.lnglat
} else {
let { lng, lat } = e
lnglat = [lng, lat]
}
if (marker) {
marker.setMap(null)
marker = null
}
self.addMarker(lnglat)
AMap.plugin('AMap.Geocoder', () => {
const geocoder = new AMap.Geocoder()
geocoder.getAddress(lnglat, (status, result) => {
self.lnglat = lnglat
self.address = result.regeocode.formattedAddress
geocoder.getLocation(self.address, (status, result) => {
if (status === 'complete' && result.info === 'OK') {
this.geocoderAddress = result.geocodes[0]
}
})
})
})
},
selectAddress(e) {
//这里获得点选地点的经纬度
this.getAddress()
}
},
mounted() {
this.mapInit()
}
}
</script>
<style scoped lang="scss">
.map-box {
position: relative;
width: 100%;
}
#container {
position: absolute;
height: 100%;
}
#panel {
position: absolute;
max-height: 90%;
overflow-y: auto;
top: 80px;
right: 10px;
width: 280px;
}
.map-address {
position: absolute;
line-height: 20px;
bottom: -30px;
}
</style>
......@@ -3,7 +3,7 @@
*
*/
import Vue from 'vue'
import { Table, TableColumn, Popover, Button, ButtonGroup, Loading, Input, Form, FormItem, Pagination, Tooltip, Icon, Select, Option, DatePicker, Dropdown, DropdownItem,DropdownMenu } from 'element-ui'
import { Table, TableColumn, Popover, Button, ButtonGroup, Loading, Input, Form, FormItem, Pagination, Tooltip, Icon, Select, Option, DatePicker, Dropdown, DropdownItem, DropdownMenu, Dialog, Message, MessageBox } from 'element-ui'
// import 'element-ui/lib/theme-chalk/index.css' //todo 按需引入插件样式
import 'element-ui/lib/theme-chalk/table.css'
import 'element-ui/lib/theme-chalk/table-column.css'
......@@ -23,6 +23,7 @@ import 'element-ui/lib/theme-chalk/date-picker.css'
import 'element-ui/lib/theme-chalk/dropdown.css'
import 'element-ui/lib/theme-chalk/dropdown-item.css'
import 'element-ui/lib/theme-chalk/dropdown-menu.css'
import 'element-ui/lib/theme-chalk/dialog.css'
// 解决 ElTable 自动宽度高度导致的「ResizeObserver loop limit exceeded」问题
const fixElTableErr = table => {
......@@ -51,6 +52,8 @@ Vue.use(DatePicker)
Vue.use(Dropdown)
Vue.use(DropdownItem)
Vue.use(DropdownMenu)
Vue.use(Dialog)
Vue.prototype.$message = Message
//打包后的组件dist 测试组件可能需要的
export { Table, TableColumn, Popover, Button, ButtonGroup, Loading, Input, Form, FormItem, Pagination, Tooltip, Icon, Select, Option, DatePicker, Dropdown, DropdownItem,DropdownMenu }
export { Table, TableColumn, Popover, Button, ButtonGroup, Loading, Input, Form, FormItem, Pagination, Tooltip, Icon, Select, Option, DatePicker, Dropdown, DropdownItem, DropdownMenu, Dialog, Message }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!