choose_address.html 11.3 KB
<!doctype html>
<html lang="en">
<head>
		<meta charset="UTF-8">
		<meta name="viewport"
		      content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
		<meta http-equiv="X-UA-Compatible" content="ie=edge">
		<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
		<title></title>
		<script src="../../assets/js/fontsize.js"></script>
		<link rel="stylesheet" href="../../assets/css/api.css"/>
		<link rel="stylesheet" href="../../assets/icon/iconfont.css">
		<style type="text/css">
				body {
						padding-bottom: 1rem;
				}

				.city_top {
						position: fixed;
						left: 0;
						top: 0;
						width: 100%;
						display: flex;
						align-items: center;
						justify-content: space-between;
						padding: 0.25rem;
						font-size: 0.3rem;
						color: #333;
						border: 1px solid #f5f5f5;
						background-color: #fff;
				}

				.city_name_box {
						display: flex;
						align-items: center;
						flex: 1;
				}

				.city_name_box .iconfont {
						margin: 0 0.1rem 0 0;
				}

				.city_input {
						flex: 3;
						margin: 0 0.1rem 0 0.1rem;
						background-color: #f2f2f2;
						padding: 0 0 0 0.2rem;
						border-radius: 0.1rem;
				}

				.city_input input {
						width: 100%;
						padding: 0.2rem 0.25rem;
						color: #333;
						font-size: 0.26rem;
				}

				.city_input input::-webkit-input-placeholder {
						color: #aaa;
				}

				.city_item {
						padding: 0.3rem 0.25rem;
						color: #999;
						font-size: 0.26rem;
						display: flex;
						justify-content: space-between;
				}

				.choose_box {
						display: flex;
						align-items: center;
						justify-content: space-between;
						font-size: 0.24rem;
						color: #333;
						padding: 0.2rem 0.25rem;
						border-bottom: 1px solid #f2f2f2;
						margin-top: 1.3rem;
				}

				.position {
						display: flex;
						align-items: center;

				}

				.position span {
						margin-left: 0.2rem;
				}

				.city_name {
						color: #333;
						font-size: 0.3rem;
				}

				.city_address {
						font-size: 0.26rem;
				}

				.my_address {
						margin: 0.25rem;
				}

				.my_address .iconfont {
						margin: 0 0.1rem 0 0;
						color: #ccc;
				}

				.address_title {
						font-size: 14px;
						color: #999;
				}

				.choose_city {
						font-size: 18px;
				}

				.address_item {
						color: #424242;
						font-size: 16px;
						padding: 0.26rem 0.5rem 0.26rem 0.2rem;
						border-bottom: 1px solid #f2f2f2;
				}
		</style>
</head>
<body>
<div id="loadStart"></div>
<div id="app" v-cloak>
		<div class="city_top">
				<div class="city_name_box" @click="choose_city_fun">
						<span class="iconfont icon-dizhi"></span>
						<div class="clamp1">{{cityname}}</div>
				</div>
				<div class="city_input"><input type="search" placeholder="请输入你的服务地址" v-model="keyword" @input="getCity"></div>
				<div class="main_color" @click="search_word">搜索</div>
		</div>
		<div class="choose_box" @click="choose_now">
				<span class="choose_city">{{street}}</span>
				<div class="main_color position"><span class="iconfont icon-zhongxindingwei"></span><span>重新定位</span></div>
		</div>
		<div v-if="address_show">
				<div class="my_address">
						<div class="address_title"><span class="iconfont icon-shouye"></span><span> 我的服务地址</span></div>
						<div class="address_item nodata" v-if="user_address_list.length==0">暂无服务地址</div>
						<div class="address_item" v-for="(item,index) in user_address_list"
						     @click="choose_address(item.address,item.address_info,item.lon,item.lat)" v-else>
								<span>{{item.address}}{{item.address_info}}</span>
						</div>
				</div>
				<div class="my_address">
						<div class="address_title"><span class="iconfont icon-dizhi"></span><span>我的附近地址</span></div>
						<div class="address_item" v-for="(item,index) in nearList"
						     @click="choose_address(item.address,'',item.lon,item.lat)">
								<span>{{item.address}}</span>
						</div>
				</div>
		</div>
		<div v-for="(item,index) in keywords" class="city_item"
		     @click="choose_address(item.address,item.name,item.lon,item.lat)" v-else>
				<div>
						<div class="city_name">{{item.name}}</div>
						<div class="city_address">{{item.address}}</div>
				</div>
		</div>
</div>
</body>
</html>
<script type="text/javascript" src="../../assets/js/api.js"></script>
<script type="text/javascript" src="../../assets/js/public.js"></script>
<script type="text/javascript" src="../../assets/js/fastclick.js"></script>
<script>
    new FastClick(document.body);
</script>
<script type="text/javascript" src="../../assets/js/vue.min.js"></script>
<script type="text/javascript" src="../../assets/js/axios.min.js"></script>
<script type="text/javascript" src="../../assets/icon/iconfont.js"></script>
<script>
    var app = new Vue({
        el: '#app',
        data: {
            cityname: '',
            lat: '',
            lng: '',
            keyword: '',
            keywords: [],
            cities: [],
            street: '',
            nearList: [],
            user_address_list: [],
            address_show: true,
            latitude: '',
            longitude: ''
        },
        created: function () {
            apiready = function () {
                api.setFrameAttr({
                    name: 'choose_address',
                    bounces: true
                });
                app.getNearBy();
                app.cityname = getcity() ? getcity() : api.pageParam.cityname;
                api.addEventListener({
                    name: 'city'
                }, function (ret, err) {
                    // alert(JSON.stringify(ret));
                    app.cityname = ret.value.city;
                });
                app.getUsedAddress();
            }
        },
        methods: {
            // 获取我的服务地址
            getUsedAddress: function () {
                var header = {
                    "XX-Device-Type": getDevice(),
                    'XX-Token': getToken()
                };
                getRequest('post', 'portal/Goods/getUsedAddress', null, header).then(function (res) {
                    // alert(JSON.stringify(res));
                    if (res.data.code == 1) {
                        app.user_address_list = res.data.data;
                    } else {
                        toastMsg(res.data.msg)
                    }
                })
            },
			// 重新获取当前定位信息
            choose_now: function () {
                api.showProgress({
                    title: '正在重新定位',
                    text: '定位中...',
                    modal: true
                });
                app.getNearBy();
                setTimeout(function() {
                    api.hideProgress();
                }, 2000)
            },
            // 获取附近地址
            getNearBy: function () {
                var aMap = api.require('aMap');
                aMap.open({
                    rect: {
                        x: 0,
                        y: 0,
                        w: 0,
                        h: 0
                    },
                }, function (ret, err) {
                    if (ret.status) {
                        aMap.getLocation(function (ret_loc, err_loc) {
                            if (ret_loc.status) {
                                aMap.getNameFromCoords({
                                    lon: ret_loc.lon,
                                    lat: ret_loc.lat
                                }, function (ret_city, err_city) {
                                    if (ret_city.status) {
                                        app.getName(ret_loc.lon, ret_loc.lat);
                                        app.street = ret_city.street;
                                        // app.cityname=ret.
                                        aMap.searchNearby({
                                            keyword: ret_city.street,
                                            lon: ret_loc.lon,
                                            lat: ret_loc.lat,
                                            radius: 1000,
                                            offset: 20,
                                            page: 1,
                                            sortrule: 0
                                        }, function (ret_near, err_near) {
                                            if (ret_near.status) {
                                                app.nearList = ret_near.pois.slice(0, 10);
                                            }
                                            loadEnd();
                                        });

                                    } else {
                                        toastMsg(JSON.stringify(err_city));
                                    }
                                });

                            } else {
                                toastMsg(JSON.stringify(err_loc));
                            }
                        });
                    } else {
                        toastMsg(JSON.stringify(err));
                    }
                });
            },
            // 获取搜索地址
            getCity: function () {
                if (app.keyword == '') {
                    app.address_show = true;
                } else {
                    app.address_show = false;
                }
                var aMap = api.require('aMap');
                aMap.searchInCity({
                    city: app.cityname,
                    keyword: app.keyword,
                    offset: 20,
                    page: 1,
                    sortrule: 0
                }, function (ret) {
                    if (ret.status) {
                        app.keywords = ret.pois;
                        // alert(JSON.stringify(app.keywords))
                    }
                });
            },
            // 搜索地址
            search_word: function () {
                if (app.keyword == '') {
                    toastMsg('请输入搜索内容')
                } else {
                    app.getCity();
                }
            },
            // 选择地址
            choose_address: function (address, name, longitude, latitude) {
                api.sendEvent({
                    name: 'choose_city',
                    extra: {
                        address: address,
                        name: name,
                        longitude: longitude,
                        latitude: latitude,
                        city: app.cityname
                    }
                });
                closeWindow();
            },
            // 切换城市
            choose_city_fun: function () {
                api.openWin({
                    url: '../index/city_win.html',
                    name: 'city_win'
                });
                $api.rmStorage('city')
            },
            getName: function (lon, lat) {
                var aMap = api.require('aMap');
                aMap.getNameFromCoords({
                    lon: lon,
                    lat: lat,
                }, function (ret, err) {
                    if (ret.status) {
                        // alert(JSON.stringify(ret))
                        app.cityname = ret.city
                    } else {
                        // toastMsg(err.code);
                    }
                });
            }
        }
    })
</script>